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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -9,31 +9,27 @@ client1 = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET"))
9
 
10
  # 1. Function to identify dish from image
11
  def identify_dish(image_bytes):
12
- try
13
- encoded_image = base64.b64encode(image_bytes).decode("utf-8")
14
- dish_name = ""
15
- except Exception:
16
- st.error("Invalid image format. Please upload a valid JPEG, PNG, or GIF image.")
17
- return None
18
-
19
- for message in client.chat_completion(
20
- model="meta-llama/Llama-3.2-11B-Vision-Instruct",
21
- messages=[
22
- {
23
- "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'.",
24
- "content": [
25
- {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}" }},
26
- {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
27
- ],
28
  }
29
- ],
30
- max_tokens=70,
31
- stream=True,
32
- ):
33
- if message.choices[0].delta.content:
34
- dish_name += message.choices[0].delta.content
35
 
36
- return dish_name.strip()
 
 
 
 
 
37
 
38
  # 2. Function to get user inputs and calculate daily caloric needs
39
  def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
@@ -92,15 +88,14 @@ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
92
 
93
  Provide the response in the following format:
94
  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").
95
- 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.
96
  """
97
- response = client1.chat_completion(
98
  model="meta-llama/Meta-Llama-3-8B-Instruct",
99
- messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
100
- max_tokens=700
101
  )
102
 
103
- return response.choices[0].message.content
104
 
105
  # Streamlit App Title
106
  st.title("AI Diet Planner")
@@ -172,7 +167,7 @@ elif menu == "User Guide":
172
  3. **Submit**: Click the submit button to receive the results.
173
 
174
  ### Features:
175
- - **Dish Identification**: The app identifies dishes based on the uploaded image.
176
- - **Metric Calculation**: Calculate BMI, BMR, TDEE, and daily caloric needs based on your personal information.
177
- - **Personalized Diet Plan**: Get a customized diet plan based on the dish identified and your weight goal.
178
  """)
 
9
 
10
  # 1. Function to identify dish from image
11
  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
 
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")
 
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
  """)