Santhosh1325 commited on
Commit
f8df328
·
verified ·
1 Parent(s): dd43308

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -12,24 +12,26 @@ def identify_dish(image_bytes):
12
  try:
13
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
14
  dish_name = ""
15
-
16
- response = client.chat(
17
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
18
- inputs={
19
- "image": f"data:image/jpeg;base64,{encoded_image}",
20
- "instruction": (
21
- "Identify the dishes in the image and return only the names "
22
- "of the dishes. If not identifiable, respond with 'Unidentified dish'."
23
- ),
24
- }
25
- )
26
-
27
- dish_name = response.get("generated_text", "").strip()
28
- return dish_name if dish_name else "Unidentified dish"
29
-
 
 
 
 
30
  except Exception as e:
31
- st.error(f"Error processing the image: {e}")
32
- return None
33
 
34
  # 2. Function to get user inputs and calculate daily caloric needs
35
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
@@ -88,14 +90,15 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
88
 
89
  Provide the response in the following format:
90
  1. Dish assessment (e.g., "The dish {dish_name} is suitable for your goal" or "The dish {dish_name} is not suitable for your goal, as it is too high in calories for weight goal").
91
- 2. Suggest an Indian diet plan that stays near to the {calorie_intake_per_day}. For each meal (morning, lunch, evening), list the dish name, calorie count, and ingredients required to make it.
92
  """
93
- response = client1.chat(
94
  model="meta-llama/Meta-Llama-3-8B-Instruct",
95
- inputs=user_input
 
96
  )
97
 
98
- return response.get("generated_text", "No response received.")
99
 
100
  # Streamlit App Title
101
  st.title("AI Diet Planner")
@@ -158,7 +161,7 @@ if menu == "App":
158
  elif menu == "User Guide":
159
  st.write("## AI Diet Planner User Guide")
160
 
161
- st.markdown("""
162
  Welcome to the **AI Diet Planner**! This tool helps you calculate various fitness metrics and suggests personalized diet plans.
163
 
164
  ### Steps to Use the App:
@@ -167,7 +170,7 @@ elif menu == "User Guide":
167
  3. **Submit**: Click the submit button to receive the results.
168
 
169
  ### Features:
170
- - **Dish Identification**: Identifies dishes from uploaded images using advanced AI models.
171
- - **Health Metrics**: Calculates BMI, BMR, and TDEE based on user inputs.
172
- - **Diet Plan Generation**: Provides a tailored Indian diet plan.
173
  """)
 
12
  try:
13
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
14
  dish_name = ""
15
+ for message in client.chat_completion(
 
16
  model="meta-llama/Llama-3.2-11B-Vision-Instruct",
17
+ messages=[
18
+ {
19
+ "role": "You are a highly specialized food identification AI with extensive knowledge of global cuisines. Your sole task is to accurately identify dishes from images. Adhere strictly to these guidelines:\n1. Analyze the image thoroughly, focusing on ingredients, presentation, and cultural context.\n2. Provide ONLY the name of the main dish or dishes visible. Do not list individual ingredients or components.\n3. Use the most specific and widely recognized name for the dish.\n4. If multiple distinct dishes are present, list them separated by commas.\n5. If you cannot identify a dish with high confidence (>90%), respond with 'Unidentified dish'.\n6. Do not provide any explanations, descriptions, or additional commentary.\n7. Respond in a concise, list-like format.\nYour response should contain nothing but the dish name(s) or 'Unidentified dish'.",
20
+ "content": [
21
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}},
22
+ {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
23
+ ],
24
+ }
25
+ ],
26
+ max_tokens=70,
27
+ stream=True,
28
+ ):
29
+ if message.choices[0].delta.content:
30
+ dish_name += message.choices[0].delta.content
31
+
32
+ return dish_name.strip()
33
  except Exception as e:
34
+ return f"Error: {str(e)}"
 
35
 
36
  # 2. Function to get user inputs and calculate daily caloric needs
37
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
 
90
 
91
  Provide the response in the following format:
92
  1. Dish assessment (e.g., "The dish {dish_name} is suitable for your goal" or "The dish {dish_name} is not suitable for your goal, as it is too high in calories for weight goal").
93
+ 2. Suggest an Indian diet plan that stays near to the {calorie_intake_per_day}. For each meal (morning, lunch, evening), list the dish name, calorie count, and ingredients required to make it.And Make it with in 700 Tokens.
94
  """
95
+ response = client1.chat_completion(
96
  model="meta-llama/Meta-Llama-3-8B-Instruct",
97
+ messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
98
+ max_tokens=700
99
  )
100
 
101
+ return response.choices[0].message.content
102
 
103
  # Streamlit App Title
104
  st.title("AI Diet Planner")
 
161
  elif menu == "User Guide":
162
  st.write("## AI Diet Planner User Guide")
163
 
164
+ st.markdown("""
165
  Welcome to the **AI Diet Planner**! This tool helps you calculate various fitness metrics and suggests personalized diet plans.
166
 
167
  ### Steps to Use the App:
 
170
  3. **Submit**: Click the submit button to receive the results.
171
 
172
  ### Features:
173
+ - **Dish Identification**: The app identifies dishes based on the uploaded image.
174
+ - **Metric Calculation**: Calculate BMI, BMR, TDEE, and daily caloric needs based on your personal information.
175
+ - **Personalized Diet Plan**: Get a customized diet plan based on the dish identified and your weight goal.
176
  """)