Spaces:
Runtime error
Runtime error
import streamlit as st | |
import base64 | |
from huggingface_hub import InferenceClient | |
import os | |
# Initialize Hugging Face Inference client using token from environment variables | |
client = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DISH")) | |
client1 = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET")) | |
# 1. Function to identify dish from image | |
def identify_dish(image_bytes): | |
try: | |
encoded_image = base64.b64encode(image_bytes).decode("utf-8") | |
dish_name = "" | |
for message in client.chat_completion( | |
model="meta-llama/Llama-3.2-11B-Vision-Instruct", | |
messages=[ | |
{ | |
"role": """You are an advanced food identification AI with in-depth knowledge of global cuisines, regional dishes, and culinary trends. Your task is to identify dishes from images with the highest accuracy possible. Follow these enhanced instructions for optimal performance: | |
Analyze the image thoroughly, considering ingredients, plating style, cultural origin, and any visible clues. | |
Always provide the most specific and widely recognized name of the dish or dishes visible. | |
If an exact match is not possible, return the closest likely match based on the visual details, even if it is a variation (e.g., "Vegetable Biryani" instead of "Mixed Veg Rice"). | |
Use synonyms or localized names if relevant to improve identification (e.g., "Aubergine" or "Eggplant"). | |
If multiple distinct dishes are present, list them separated by commas. | |
If you are uncertain about a dish but still identify it with reasonable confidence (80-90%), respond with the closest guess and indicate it as a possible match (e.g., "Possible: Paella"). | |
If you cannot identify the dish at all, respond with 'Unidentified dish'. | |
Do not include individual ingredients, explanations, or commentary. | |
Respond only with the dish name(s) or 'Unidentified dish' in a list format.""", | |
"content": [ | |
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}, | |
{"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."}, | |
], | |
} | |
], | |
max_tokens=70, | |
stream=True, | |
): | |
if message.choices[0].delta.content: | |
dish_name += message.choices[0].delta.content | |
return dish_name.strip() | |
except Exception as e: | |
return f"Error ==> Try uploading a different image. Sorry for the inconvenience caused : {str(e)}" | |
# 2. Function to get user inputs and calculate daily caloric needs | |
def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months): | |
bmi = weight_kg / ((height_cm / 100) ** 2) | |
if gender == "male": | |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age + 5 | |
else: | |
bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age - 161 | |
activity_multipliers = { | |
"sedentary": 1.2, | |
"light": 1.375, | |
"moderate": 1.55, | |
"active": 1.725, | |
"very active": 1.9 | |
} | |
tdee = bmr * activity_multipliers[activity_level] | |
if gender == "male": | |
ibw = 50 + (0.91 * (height_cm - 152.4)) | |
else: | |
ibw = 45.5 + (0.91 * (height_cm - 152.4)) | |
if weight_goal == "loss": | |
daily_caloric_needs = tdee - 500 | |
elif weight_goal == "gain": | |
daily_caloric_needs = tdee + 500 | |
else: | |
daily_caloric_needs = tdee | |
protein_calories = daily_caloric_needs * 0.2 | |
fat_calories = daily_caloric_needs * 0.25 | |
carbohydrate_calories = daily_caloric_needs * 0.55 | |
return { | |
"BMI": bmi, | |
"BMR": bmr, | |
"TDEE": tdee, | |
"IBW": ibw, | |
"Daily Caloric Needs": daily_caloric_needs, | |
"Protein Calories": protein_calories, | |
"Fat Calories": fat_calories, | |
"Carbohydrate Calories": carbohydrate_calories | |
} | |
# 3. Function to generate diet plan | |
def generate_diet_plan(dish_name, calorie_intake_per_day, goal): | |
user_input = f""" | |
You are a certified Dietitian with 20 years of experience. Based on the following input, create an Indian diet plan that fits within the calculated calorie intake and assesses if the given dish is suitable for the user's goal. | |
Input: | |
- Dish Name: {dish_name} | |
- Caloric Intake per Day: {calorie_intake_per_day} calories | |
- Goal: {goal} (e.g., weight loss, weight gain) | |
Provide the response in the following format: | |
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"). | |
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. | |
""" | |
response = client1.chat_completion( | |
model="meta-llama/Meta-Llama-3-8B-Instruct", | |
messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}], | |
max_tokens=700 | |
) | |
return response.choices[0].message.content | |
# Streamlit App Title | |
st.title("AI Diet Recommender") | |
st.write("Note : The Dish Identification model Might do some Mistakes") | |
# Sidebar navigation to switch between the app and the user guide | |
menu = st.sidebar.selectbox("Menu", ["App", "User Guide"]) | |
# If the user selects the app, show the existing app | |
if menu == "App": | |
# Sidebar for user input | |
st.sidebar.title("User Input") | |
image_file = st.sidebar.file_uploader("Upload an image of the dish", type=["jpeg", "png", "jpg", "gif"]) | |
age = st.sidebar.number_input("Enter your age", min_value=18) | |
gender = st.sidebar.selectbox("Select your gender", ["male", "female"]) | |
height_cm = st.sidebar.number_input("Enter your height (cm)", min_value=150.0) | |
weight_kg = st.sidebar.number_input("Enter your weight (kg)", min_value=50.0) | |
weight_goal = st.sidebar.selectbox("Weight goal", ["loss", "gain", "maintain"]) | |
activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"]) | |
time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1) | |
# Submit button | |
submit = st.sidebar.button("Submit") | |
# Process the image and calculate metrics upon submission | |
if submit: | |
if image_file: | |
st.write("### Results") | |
image_bytes = image_file.read() | |
# Step 1: Identify the dish | |
dish_name = identify_dish(image_bytes) | |
st.markdown("<hr>", unsafe_allow_html=True) | |
st.write("#### Dish Name Identified:") | |
st.markdown(f"<div style='background-color: #d4edda; color: #155724; padding: 10px; border-radius: 10px;'>{dish_name}</div>", unsafe_allow_html=True) | |
# Step 2: Perform Calculations | |
metrics = calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame) | |
st.markdown("<hr>", unsafe_allow_html=True) | |
st.write("#### Metrics Calculated:") | |
st.markdown(f""" | |
<div style='background-color: #f8d7da; color: #721c24; padding: 10px; border-radius: 10px;'> | |
<p><b>Your BMI:</b> {metrics['BMI']:.2f}</p> | |
<p><b>Your BMR(Basal metabolic rate):</b> {metrics['BMR']:.2f} calories</p> | |
<p><b>Your TDEE(Total Daily Energy Expenditure):</b> {metrics['TDEE']:.2f} calories</p> | |
<p><b>Ideal Body Weight (IBW):</b> {metrics['IBW']:.2f} kg</p> | |
<p><b>Daily Caloric Needs:</b> {metrics['Daily Caloric Needs']:.2f} calories</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# Step 3: Generate diet plan | |
diet_plan = generate_diet_plan(dish_name, metrics["Daily Caloric Needs"], weight_goal) | |
st.markdown("<hr>", unsafe_allow_html=True) | |
st.write("#### Diet Plan Based on Dish & Goal:") | |
st.markdown(f"<div style='background-color: #d1ecf1; color: #0c5460; padding: 10px; border-radius: 10px;'>{diet_plan}</div>", unsafe_allow_html=True) | |
else: | |
st.error("Please upload a valid image in JPEG, PNG, JPG, or GIF format.") | |
# If the user selects the User Guide, show a detailed guide about the app | |
elif menu == "User Guide": | |
st.write("## AI Diet Planner User Guide") | |
st.markdown(""" | |
Welcome to the **AI Diet Planner**! This tool helps you calculate various fitness metrics and suggests personalized diet plans. | |
### Steps to Use the App: | |
1. **Upload an image of the dish**: Use the sidebar to upload an image in `.jpeg`, `.jpg`, `.png`, or `.gif` format. | |
2. **Fill in the personal details**: Input your age, gender, height, weight, activity level, and weight goal. | |
3. **Submit**: Click the submit button to receive the results. | |
### Features: | |
- **Dish Identification**: The app identifies dishes based on the uploaded image. | |
- **Metric Calculation**: Calculate BMI, BMR, TDEE, and daily caloric needs based on your personal information. | |
- **Personalized Diet Plan**: Get a customized diet plan based on the dish identified and your weight goal. | |
""") | |