Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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.
|
96 |
"""
|
97 |
-
response = client1.
|
98 |
model="meta-llama/Meta-Llama-3-8B-Instruct",
|
99 |
-
|
100 |
-
max_tokens=700
|
101 |
)
|
102 |
|
103 |
-
return response.
|
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**:
|
176 |
-
- **
|
177 |
-
- **
|
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 |
""")
|