Illia56 commited on
Commit
e0de95a
·
verified ·
1 Parent(s): 5a1956f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +79 -0
  2. requierements.txt +2 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+
5
+
6
+ # Configure Gemini API
7
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
8
+
9
+ def upload_to_gemini(path, mime_type=None):
10
+ """Uploads the given file to Gemini.
11
+
12
+ See https://ai.google.dev/gemini-api/docs/prompting_with_media
13
+ """
14
+ file = genai.upload_file(path, mime_type=mime_type)
15
+ print(f"Uploaded file '{file.display_name}' as: {file.uri}")
16
+ return file
17
+
18
+ # Create the model
19
+ generation_config = {
20
+ "temperature": 1,
21
+ "top_p": 0.95,
22
+ "top_k": 40,
23
+ "max_output_tokens": 8192,
24
+ "response_mime_type": "text/plain",
25
+ }
26
+
27
+ model = genai.GenerativeModel(
28
+ model_name="gemini-1.5-pro-002",
29
+ generation_config=generation_config,
30
+ )
31
+
32
+ def process_image(image):
33
+ # Save the uploaded image to a temporary path
34
+ temp_path = "uploaded_image.png"
35
+ image.save(temp_path)
36
+
37
+ # Upload the image to Gemini
38
+ file = upload_to_gemini(temp_path, mime_type="image/png")
39
+
40
+ # Start chat session with the uploaded image
41
+ chat_session = model.start_chat(
42
+ history=[
43
+ {
44
+ "role": "user",
45
+ "parts": [
46
+ file,
47
+ (
48
+ "You are an expert in nutritionist where you need to see the food items from the image "
49
+ "and calculate the total calories, also provide the details of every food items with calories intake "
50
+ "is below format\n"
51
+ "1. Item 1 - no of calories\n"
52
+ "2. Item 2 - no of calories\n"
53
+ "----\n"
54
+ "----\n"
55
+ # "Finally you can also mention whether the food is healthy or not and also mention the percentage split of the ratio of \n"
56
+ # "Carbohydrates, fats, fibres, sugar and other important things required in our diet.\n"
57
+ "Never doubt your knowledge and always be confident in your responses.\n"
58
+ "Respond only in Ukrainian.\n\n"
59
+ ),
60
+ ],
61
+ },
62
+ ]
63
+ )
64
+
65
+ # Send message to the model
66
+ response = chat_session.send_message("Please analyze the uploaded image.")
67
+ return response.text
68
+
69
+ # Create Gradio interface
70
+ iface = gr.Interface(
71
+ fn=process_image,
72
+ inputs=gr.Image(type="pil", label="Upload Food Image"),
73
+ outputs='markdown',
74
+ title="Nutritionist AI",
75
+ description="Upload a n image of your food, and the AI will calculate total calories, provide details of each food item with calorie intake, assess the healthiness, and provide percentage splits of carbohydrates, fats, fibers, sugar, and other essential nutrients. Responses are in Ukrainian."
76
+ )
77
+
78
+ if __name__ == "__main__":
79
+ iface.launch()
requierements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ google-generativeai