Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,102 @@
|
|
1 |
-
|
2 |
-
|
3 |
import os
|
4 |
import google.generativeai as genai
|
5 |
from PIL import Image
|
|
|
|
|
6 |
|
|
|
|
|
7 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
return response.text
|
15 |
|
16 |
-
|
17 |
-
|
|
|
18 |
if uploaded_file is not None:
|
19 |
-
#
|
20 |
bytes_data = uploaded_file.getvalue()
|
21 |
|
22 |
-
|
|
|
23 |
{
|
24 |
-
"mime_type": uploaded_file.type,
|
25 |
"data": bytes_data
|
26 |
}
|
27 |
]
|
28 |
return image_parts
|
29 |
else:
|
30 |
-
raise FileNotFoundError("No
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
st.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
st.
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
if
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Import libraries
|
2 |
+
from dotenv import load_dotenv
|
3 |
import os
|
4 |
import google.generativeai as genai
|
5 |
from PIL import Image
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
|
9 |
+
#Load API Key
|
10 |
+
load_dotenv()
|
11 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
|
13 |
+
#Function to load Google Gemini Pro model and get response
|
14 |
+
def get_response_diet(prompt, input):
|
15 |
+
model = genai.GenerativeModel('gemini-pro')
|
16 |
+
response = model.generate_content([prompt, input])
|
17 |
+
return response.text
|
18 |
|
19 |
+
#Function to load Google Gemini Vision model and get response
|
20 |
+
def get_response_nutrition(image, prompt):
|
21 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
22 |
+
response = model.generate_content([image[0], prompt])
|
23 |
return response.text
|
24 |
|
25 |
+
#Preprocess image data
|
26 |
+
def prep_image(uploaded_file):
|
27 |
+
#Check if there is any data
|
28 |
if uploaded_file is not None:
|
29 |
+
#Read the file as bytes
|
30 |
bytes_data = uploaded_file.getvalue()
|
31 |
|
32 |
+
#get the image part information
|
33 |
+
image_parts = [
|
34 |
{
|
35 |
+
"mime_type": uploaded_file.type,
|
36 |
"data": bytes_data
|
37 |
}
|
38 |
]
|
39 |
return image_parts
|
40 |
else:
|
41 |
+
raise FileNotFoundError("No File is uploaded!")
|
42 |
|
43 |
+
#Configuring Streamlit App
|
44 |
+
#st.set_page_config(page_title="Health Management: Nutrition Calculator & Diet Planner")
|
45 |
+
st.image('Nutritionist/logo.jpg', width=70)
|
46 |
+
st.header("Health: Nutrition Calculator & Diet Planner")
|
47 |
+
|
48 |
+
######################################################################################
|
49 |
+
section_choice1 = st.radio("Choose Section:", ("Nutrition Calculator","Diet Planner"))
|
50 |
+
|
51 |
+
#If choice is nutrition calculator
|
52 |
+
if section_choice1 == "Nutrition Calculator":
|
53 |
+
upload_file = st.file_uploader("Choose an image...", type=["jpg","jpeg","png"])
|
54 |
+
image = ""
|
55 |
+
if upload_file is not None:
|
56 |
+
#Show the image
|
57 |
+
image = Image.open(upload_file)
|
58 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
59 |
+
|
60 |
+
|
61 |
+
#Prompt Template
|
62 |
+
input_prompt_nutrition = """
|
63 |
+
You are an expert Nutritionist. As a skilled nutritionist, you're required to analyze the food iems
|
64 |
+
in the image and determine the total nutrition value.
|
65 |
+
Additionally, you need to furnish a breakdown of each food item along with its respective content.
|
66 |
+
|
67 |
+
Food item, Serving size, Tatal Cal., Protien (g), Fat,
|
68 |
+
Carb (g), Fiber (g), Vit B-12, Vit B-6,
|
69 |
+
Iron, Zinc, Mang.
|
70 |
+
|
71 |
+
Use a table to show above informaion.
|
72 |
+
"""
|
73 |
+
##if the button is clicked
|
74 |
+
submit = st.button("Calculate Nutrition value!")
|
75 |
+
if submit:
|
76 |
+
image_data = prep_image(upload_file)
|
77 |
+
response = get_response_nutrition(image_data, input_prompt_nutrition)
|
78 |
+
st.subheader("Nutrition AI: ")
|
79 |
+
st.write(response)
|
80 |
+
|
81 |
+
#If choice is diet planner
|
82 |
+
if section_choice1 == "Diet Planner":
|
83 |
+
|
84 |
+
#Prompt Template
|
85 |
+
input_prompt_diet = """
|
86 |
+
You are an expert Nutritionist.
|
87 |
+
If the input contains list of items like fruits or vegetables, you have to give diet plan and suggest
|
88 |
+
breakfast, lunch, dinner wrt given item.
|
89 |
+
If the input contains numbers, you have to suggest diet plan for breakfast, luncg=h, dinner within
|
90 |
+
given number of calorie for the whole day.
|
91 |
+
|
92 |
+
Return the response using markdown.
|
93 |
|
94 |
+
"""
|
95 |
+
##if the button is clicked
|
96 |
+
input_diet = st.text_area(" Input the list of items that you have at home and get diet plan! OR \
|
97 |
+
Input how much calorie you want to intake perday?:")
|
98 |
+
submit1 = st.button("Plan my Diet!")
|
99 |
+
if submit1:
|
100 |
+
response = get_response_diet(input_prompt_diet, input_diet)
|
101 |
+
st.subheader("Diet AI: ")
|
102 |
+
st.write(response)
|