Santhosh54321 commited on
Commit
05360e4
·
verified ·
1 Parent(s): 05fdd4f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import base64
3
+ from huggingface_hub import InferenceClient
4
+ import os
5
+
6
+ # Initialize Hugging Face Inference client using token from environment variables
7
+ client = InferenceClient(api_key=os.getenv("HF_API_TOKEN"))
8
+
9
+ # 1. Function to identify dish from image
10
+ def identify_dish(image_bytes):
11
+ encoded_image = base64.b64encode(image_bytes).decode("utf-8")
12
+ dish_name = ""
13
+
14
+ for message in client.chat_completion(
15
+ model="meta-llama/Llama-3.2-11B-Vision-Instruct",
16
+ messages=[
17
+ {
18
+ "role": "You are a food identification expert who identifies dishes from images. Your task is to strictly return the names of the dishes present in the image. Only return the dish names if you have high Confidence Level and without additional explanation or description.",
19
+ "content": [
20
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}" }},
21
+ {"type": "text", "text": "Identify the dishes in the image and return only the names of the dishes."},
22
+ ],
23
+ }
24
+ ],
25
+ max_tokens=70,
26
+ stream=True,
27
+ ):
28
+ if message.choices[0].delta.content:
29
+ dish_name += message.choices[0].delta.content
30
+
31
+ return dish_name.strip()
32
+
33
+ # 2. Function to get user inputs and calculate daily caloric needs
34
+ def calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame_months):
35
+ # Calculate BMI
36
+ bmi = weight_kg / ((height_cm / 100) ** 2)
37
+
38
+ # Calculate BMR using Mifflin-St Jeor formula
39
+ if gender == "male":
40
+ bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age + 5
41
+ else:
42
+ bmr = 10 * weight_kg + 6.25 * height_cm - 5 * age - 161
43
+
44
+ # Calculate TDEE (Total Daily Energy Expenditure)
45
+ activity_multipliers = {
46
+ "sedentary": 1.2,
47
+ "light": 1.375,
48
+ "moderate": 1.55,
49
+ "active": 1.725,
50
+ "very active": 1.9
51
+ }
52
+ tdee = bmr * activity_multipliers[activity_level]
53
+
54
+ # Calculate Ideal Body Weight (IBW) using Hamwi method
55
+ if gender == "male":
56
+ ibw = 50 + (0.91 * (height_cm - 152.4))
57
+ else:
58
+ ibw = 45.5 + (0.91 * (height_cm - 152.4))
59
+
60
+ # Calculate Daily Caloric Needs
61
+ if weight_goal == "loss":
62
+ daily_caloric_needs = tdee - 500 # Deficit for weight loss
63
+ elif weight_goal == "gain":
64
+ daily_caloric_needs = tdee + 500 # Surplus for weight gain
65
+ else:
66
+ daily_caloric_needs = tdee
67
+
68
+ # Calculate macronutrient calories
69
+ protein_calories = daily_caloric_needs * 0.2
70
+ fat_calories = daily_caloric_needs * 0.25
71
+ carbohydrate_calories = daily_caloric_needs * 0.55
72
+
73
+ return {
74
+ "BMI": bmi,
75
+ "BMR": bmr,
76
+ "TDEE": tdee,
77
+ "IBW": ibw,
78
+ "Daily Caloric Needs": daily_caloric_needs,
79
+ "Protein Calories": protein_calories,
80
+ "Fat Calories": fat_calories,
81
+ "Carbohydrate Calories": carbohydrate_calories
82
+ }
83
+
84
+ # 3. Function to generate diet plan
85
+ def generate_diet_plan(dish_name, calorie_intake_per_day, goal):
86
+ user_input = f"""
87
+ 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.
88
+
89
+ Input:
90
+ - Dish Name: {dish_name}
91
+ - Caloric Intake per Day: {calorie_intake_per_day} calories
92
+ - Goal: {goal}
93
+
94
+ """
95
+ # Request from the dietitian model
96
+ response = client.chat_completion(
97
+ model="meta-llama/Meta-Llama-3-8B-Instruct",
98
+ messages=[{"role": "You are a certified Dietitian with 20 years of Experience", "content": user_input}],
99
+ max_tokens=500
100
+ )
101
+
102
+ return response.choices[0].message.content
103
+
104
+ # Streamlit Sidebar for user input
105
+ st.sidebar.title("User Input")
106
+ image_file = st.sidebar.file_uploader("Upload an image of the dish", type=["jpeg", "png"])
107
+ age = st.sidebar.number_input("Enter your age", min_value=1)
108
+ gender = st.sidebar.selectbox("Select your gender", ["male", "female"])
109
+ height_cm = st.sidebar.number_input("Enter your height (cm)", min_value=1.0)
110
+ weight_kg = st.sidebar.number_input("Enter your weight (kg)", min_value=1.0)
111
+ weight_goal = st.sidebar.selectbox("Weight goal", ["loss", "gain", "maintain"])
112
+ activity_level = st.sidebar.selectbox("Activity level", ["sedentary", "light", "moderate", "active", "very active"])
113
+ time_frame = st.sidebar.number_input("Time frame to achieve goal (months)", min_value=1)
114
+
115
+ # Process the image and calculate metrics
116
+ if image_file:
117
+ st.write("### Results")
118
+ image_bytes = image_file.read()
119
+
120
+ # Step 1: Identify the dish
121
+ dish_name = identify_dish(image_bytes)
122
+ st.success(f"Identified Dish: {dish_name}")
123
+
124
+ # Step 2: Perform Calculations
125
+ metrics = calculate_metrics(age, gender, height_cm, weight_kg, weight_goal, activity_level, time_frame)
126
+ st.write(f"**Your BMI:** {metrics['BMI']:.2f}")
127
+ st.write(f"**Your BMR:** {metrics['BMR']:.2f} calories")
128
+ st.write(f"**Your TDEE:** {metrics['TDEE']:.2f} calories")
129
+ st.write(f"**Ideal Body Weight (IBW):** {metrics['IBW']:.2f} kg")
130
+ st.write(f"**Daily Caloric Needs:** {metrics['Daily Caloric Needs']:.2f} calories")
131
+
132
+ # Step 3: Generate diet plan
133
+ diet_plan = generate_diet_plan(dish_name, metrics["Daily Caloric Needs"], weight_goal)
134
+ st.write(f"### Diet Plan\n {diet_plan}")
135
+
136
+ # CSS for styling
137
+ st.markdown("""
138
+ <style>
139
+ .stButton button { background-color: #4CAF50; color: white; }
140
+ .stContainer { border: 1px solid #ddd; padding: 20px; margin-bottom: 20px; }
141
+ </style>
142
+ """, unsafe_allow_html=True)