Spaces:
Sleeping
Sleeping
File size: 8,506 Bytes
05360e4 7d1bfcb 05360e4 7d1bfcb 9084b77 2fda0aa 7d1bfcb 05360e4 9084b77 70e1d58 e906233 70e1d58 05360e4 7d1bfcb 70e1d58 05360e4 e9341e2 05360e4 e9341e2 05360e4 e906233 05360e4 2fda0aa 05360e4 7d1bfcb e906233 7d1bfcb e906233 7d1bfcb 70e1d58 05360e4 70e1d58 e9341e2 70e1d58 e9341e2 70e1d58 e9341e2 70e1d58 7d1bfcb 9084b77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import streamlit as st
import base64
from huggingface_hub import InferenceClient
import os
import requests
# Initialize Hugging Face Inference client using tokens from environment variables
client = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DISH"))
client1 = InferenceClient(api_key=os.getenv("HF_API_TOKEN_DIET"))
groq_api_key = os.getenv("API_Groq")
# 1. Function to identify dish from image
def identify_dish(image_bytes):
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 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'.",
"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()
# 2. Function to 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}
"""
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=500
)
return response.choices[0].message.content
# 4. Function to generate a recipe using Groq API
def generate_recipe(recipe_name):
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {groq_api_key}",
"Content-Type": "application/json"
}
data = {
"model": "llama-3.1-70b-versatile",
"messages": [
{"role": "system", "content": "You are a skilled chef with 10 years of experience."},
{"role": "user", "content": f"Create a detailed recipe for {recipe_name}"}
]
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
generated_recipe = result['choices'][0]['message']['content']
return generated_recipe
else:
return f"Error: {response.status_code}, {response.text}"
# Streamlit App Title
st.title("AI Diet Planner")
# Sidebar for user input
st.sidebar.title("User Input")
image_file = st.sidebar.file_uploader("Upload an image of the dish", type=["jpeg", "png"])
age = st.sidebar.number_input("Enter your age", min_value=1)
gender = st.sidebar.selectbox("Select your gender", ["male", "female"])
height_cm = st.sidebar.number_input("Enter your height (cm)", min_value=1.0)
weight_kg = st.sidebar.number_input("Enter your weight (kg)", min_value=1.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:</b> {metrics['BMR']:.2f} calories</p>
<p><b>Your TDEE:</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)
# Step 4: Ask for recipe generation
st.markdown("<hr>", unsafe_allow_html=True)
recipe_option = st.radio("Would you like a recipe for a dish?", ["No", "Yes"])
if recipe_option == "Yes":
recipe_name = st.text_input("Enter the dish name for the recipe:")
if st.button("Generate Recipe"):
recipe = generate_recipe(recipe_name)
st.write("### Generated Recipe:")
st.markdown(f"<div style='background-color: #fff3cd; color: #856404; padding: 10px; border-radius: 10px;'>{recipe}</div>", unsafe_allow_html=True)
else:
st.error("Please upload a valid image in JPEG or PNG format.")
# CSS styling
st.markdown("""
<style>
.stButton button { background-color: #4CAF50; color: white; }
.stContainer { border: 1px solid #ddd; padding: 20px; margin-bottom: 20px; }
hr { border: 1px solid #e9ecef; margin: 20px 0; }
</style>
""", unsafe_allow_html=True)
|