EAV123 commited on
Commit
7d4a989
1 Parent(s): df68180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -47
app.py CHANGED
@@ -1,67 +1,102 @@
1
-
2
- import streamlit as st
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
- ## Function to load Google Gemini Pro Vision API And get response
 
 
 
 
10
 
11
- def get_gemini_repsonse(input,image,prompt):
12
- model=genai.GenerativeModel('gemini-pro-vision')
13
- response=model.generate_content([input,image[0],prompt])
 
14
  return response.text
15
 
16
- def input_image_setup(uploaded_file):
17
- # Check if a file has been uploaded
 
18
  if uploaded_file is not None:
19
- # Read the file into bytes, image into numbers
20
  bytes_data = uploaded_file.getvalue()
21
 
22
- image_parts = [ #In dic format
 
23
  {
24
- "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
25
  "data": bytes_data
26
  }
27
  ]
28
  return image_parts
29
  else:
30
- raise FileNotFoundError("No file uploaded")
31
 
32
- ##initialize our streamlit app
33
-
34
- st.set_page_config(page_title="Nutrition and Calories Manager App")
35
-
36
- st.header("Nutrition and Calories Manager App")
37
- input=st.text_input("Input Prompt: ",key="input")
38
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
39
- image=""
40
- if uploaded_file is not None:
41
- image = Image.open(uploaded_file)
42
- st.image(image, caption="Uploaded Image.", use_column_width=True)
43
-
44
-
45
- submit=st.button("CHECK")
46
-
47
- input_prompt="""
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 item with calories intake
50
- is below format
51
- 1. Item 1 -no of calories
52
- 2. Item 2 -no of calories
53
- ----
54
- ----
55
- Total Calories
56
- Also mention about the important nutrients like Protein , Carbohydrates and Fats in each item
57
- Put the output in a tabular format with each item in a separate row and details in columns
58
- """
59
-
60
- ## If submit button is clicked
61
-
62
- if submit:
63
- image_data=input_image_setup(uploaded_file)
64
- response=get_gemini_repsonse(input_prompt,image_data,input)
65
- st.subheader("The Response is")
66
- st.write(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)