ibrahim yıldız commited on
Commit
4cdd98c
·
verified ·
1 Parent(s): e9025bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -81
app.py CHANGED
@@ -8,87 +8,149 @@ genai.configure(api_key="AIzaSyB6z5crVaKSj1ct_gVsq6nrHtSGme_JmoY")
8
 
9
  # API configurations for Dobby
10
  DOBBY_API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
11
- DOBBY_API_KEY = "fw_3ZjtsywUGddwa1wGY4VvB3eW" # Replace with your valid Dobby API key
12
  DOBBY_LEASHED_MODEL = "accounts/sentientfoundation/models/dobby-mini-leashed-llama-3-1-8b#accounts/sentientfoundation/deployments/22e7b3fd"
13
  DOBBY_UNHINGED_MODEL = "accounts/sentientfoundation/models/dobby-mini-unhinged-llama-3-1-8b#accounts/sentientfoundation/deployments/81e155fc"
14
 
15
- # Streamlit app setup
16
- st.title("AI-Powered Nutrition Monitor with Dobby")
17
- st.write("Upload an image of your meal, and Dobby will analyze and comment on it!")
18
-
19
- # File upload
20
- uploaded_file = st.file_uploader("Upload your food image", type=["jpg", "jpeg", "png"])
21
- tone = st.radio("Select Dobby's tone:", ["Motivational (Leashed)", "Sarcastic (Unhinged)"])
22
-
23
- # Function to analyze food image using Gemini API
24
- def analyze_food_with_gemini(input_prompt, image_data):
25
- model = genai.GenerativeModel("gemini-1.5-pro-latest")
26
- response = model.generate_content([input_prompt, image_data[0]])
27
- return response.text
28
-
29
- # Function to prepare image data for Gemini API
30
- def prepare_image_data(uploaded_file):
31
- bytes_data = uploaded_file.getvalue()
32
- return [{"mime_type": uploaded_file.type, "data": bytes_data}]
33
-
34
- # Function to get Dobby's comment
35
- def get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, tone):
36
- selected_model = DOBBY_LEASHED_MODEL if tone == "Motivational (Leashed)" else DOBBY_UNHINGED_MODEL
37
- dobby_prompt = f"""
38
- The user had a meal: {food_name}.
39
- Nutritional data: {total_calories} kcal, {total_protein} g protein, {total_carbs} g carbs, {total_fat} g fat.
40
- Comment on this meal.
41
- """
42
- response = requests.post(
43
- DOBBY_API_URL,
44
- headers={
45
- "Authorization": f"Bearer {DOBBY_API_KEY}",
46
- "Content-Type": "application/json",
47
- },
48
- json={
49
- "model": selected_model,
50
- "messages": [{"role": "user", "content": dobby_prompt}],
51
- },
52
- )
53
- return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
54
-
55
- # Main app logic
56
- if uploaded_file:
57
- st.image(uploaded_file, caption="Uploaded Image", use_container_width=True) # Display uploaded image
58
- with st.spinner("Analyzing the image..."):
59
- try:
60
- # Prepare image data for Gemini API
61
- image_data = prepare_image_data(uploaded_file)
62
- input_prompt = """
63
- You are an expert nutritionist analyzing the food items in the image.
64
- Start by naming the meal based on the image, identify all ingredients, and estimate calories for each ingredient.
65
- Summarize the total calories, protein, carbs, and fat. Provide data in this format:
66
- Meal Name: [Name of the meal]
67
- Total Calories: X kcal
68
- Protein: X g
69
- Carbs: X g
70
- Fat: X g
71
- say nothing else
72
- """
73
- # Get food analysis from Gemini API
74
- analysis = analyze_food_with_gemini(input_prompt, image_data)
75
-
76
- # Extract analysis results from Gemini response
77
- #st.subheader("Results:")
78
- st.write(analysis)
79
-
80
- # Parse relevant data for Dobby
81
- lines = analysis.split("\n")
82
- food_name = lines[0].replace("Meal Name: ", "").strip()
83
- total_calories = lines[1].replace("Total Calories: ", "").replace("kcal", "").strip()
84
- total_protein = lines[2].replace("Protein: ", "").replace("g", "").strip()
85
- total_carbs = lines[3].replace("Carbs: ", "").replace("g", "").strip()
86
- total_fat = lines[4].replace("Fat: ", "").replace("g", "").strip()
87
-
88
- # Get Dobby's feedback
89
- dobby_comment = get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, tone)
90
-
91
- st.subheader("Dobby's Comment:")
92
- st.write(dobby_comment)
93
- except Exception as e:
94
- st.error(f"Error processing the image: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # API configurations for Dobby
10
  DOBBY_API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
11
+ DOBBY_API_KEY = "fw_3ZjtsywUGddwa1wGY4VvB3eW"
12
  DOBBY_LEASHED_MODEL = "accounts/sentientfoundation/models/dobby-mini-leashed-llama-3-1-8b#accounts/sentientfoundation/deployments/22e7b3fd"
13
  DOBBY_UNHINGED_MODEL = "accounts/sentientfoundation/models/dobby-mini-unhinged-llama-3-1-8b#accounts/sentientfoundation/deployments/81e155fc"
14
 
15
+ # Initialize session state for tracking meals
16
+ if "meal_data" not in st.session_state:
17
+ st.session_state.meal_data = {"Breakfast": {}, "Lunch": {}, "Dinner": {}}
18
+ if "daily_totals" not in st.session_state:
19
+ st.session_state.daily_totals = {"Calories": 0, "Protein": 0, "Carbs": 0, "Fat": 0}
20
+
21
+ # App setup
22
+ st.title("AI-Powered Nutrition Tracker with Dobby")
23
+ st.write("Track your meals and get daily feedback based on your nutrition goals!")
24
+
25
+ # Step 1: User inputs weight and goal
26
+ if "user_info" not in st.session_state:
27
+ st.subheader("Step 1: Set Your Goal")
28
+ weight = st.number_input("Enter your weight (kg):", min_value=30, max_value=300, step=1)
29
+ goal = st.selectbox("Select your goal:", ["Lose Weight", "Maintain Weight", "Gain Weight"])
30
+ if st.button("Submit"):
31
+ st.session_state.user_info = {"Weight": weight, "Goal": goal}
32
+ st.success("Your information has been saved! Proceed to meal tracking.")
33
+
34
+ # Step 2: Meal tracking
35
+ if "user_info" in st.session_state:
36
+ meal = st.selectbox("Select Meal:", ["Breakfast", "Lunch", "Dinner"])
37
+ uploaded_file = st.file_uploader(f"Upload your {meal} image", type=["jpg", "jpeg", "png"])
38
+ tone = st.radio("Select Dobby's tone:", ["Motivational (Leashed)", "Sarcastic (Unhinged)"])
39
+
40
+ # Functions for analysis and comments
41
+ def analyze_food_with_gemini(input_prompt, image_data):
42
+ model = genai.GenerativeModel("gemini-1.5-pro-latest")
43
+ response = model.generate_content([input_prompt, image_data[0]])
44
+ return response.text
45
+
46
+ def prepare_image_data(uploaded_file):
47
+ bytes_data = uploaded_file.getvalue()
48
+ return [{"mime_type": uploaded_file.type, "data": bytes_data}]
49
+
50
+ def get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, meal, goal):
51
+ selected_model = DOBBY_LEASHED_MODEL if tone == "Motivational (Leashed)" else DOBBY_UNHINGED_MODEL
52
+ dobby_prompt = f"""
53
+ The user had {meal}: {food_name}.
54
+ Nutritional data: {total_calories} kcal, {total_protein} g protein, {total_carbs} g carbs, {total_fat} g fat.
55
+ The user's goal is to {goal.lower()}. Provide feedback for this meal.
56
+ """
57
+ response = requests.post(
58
+ DOBBY_API_URL,
59
+ headers={
60
+ "Authorization": f"Bearer {DOBBY_API_KEY}",
61
+ "Content-Type": "application/json",
62
+ },
63
+ json={
64
+ "model": selected_model,
65
+ "messages": [{"role": "user", "content": dobby_prompt}],
66
+ },
67
+ )
68
+ return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
69
+
70
+ # Process uploaded meal image
71
+ if uploaded_file:
72
+ st.image(uploaded_file, caption=f"Uploaded {meal} Image", use_container_width=True)
73
+ with st.spinner("Analyzing the image..."):
74
+ try:
75
+ # Prepare image data and analyze with Gemini
76
+ image_data = prepare_image_data(uploaded_file)
77
+ input_prompt = """
78
+ You are an expert nutritionist analyzing the food items in the image.
79
+ Start by naming the meal based on the image, identify all ingredients, and estimate calories for each ingredient.
80
+ Summarize the total calories, protein, carbs, and fat. Provide data in this format:
81
+ Meal Name: [Name of the meal]
82
+ Total Calories: X kcal
83
+ Protein: X g
84
+ Carbs: X g
85
+ Fat: X g
86
+ say nothing else
87
+ """
88
+ analysis = analyze_food_with_gemini(input_prompt, image_data)
89
+
90
+ # Extract analysis data
91
+ lines = analysis.split("\n")
92
+ food_name = lines[0].replace("Meal Name: ", "").strip()
93
+ total_calories = int(lines[1].replace("Total Calories: ", "").replace("kcal", "").strip())
94
+ total_protein = int(lines[2].replace("Protein: ", "").replace("g", "").strip())
95
+ total_carbs = int(lines[3].replace("Carbs: ", "").replace("g", "").strip())
96
+ total_fat = int(lines[4].replace("Fat: ", "").replace("g", "").strip())
97
+
98
+ # Save data for this meal
99
+ st.session_state.meal_data[meal] = {
100
+ "Food Name": food_name,
101
+ "Calories": total_calories,
102
+ "Protein": total_protein,
103
+ "Carbs": total_carbs,
104
+ "Fat": total_fat,
105
+ }
106
+
107
+ # Update daily totals
108
+ st.session_state.daily_totals["Calories"] += total_calories
109
+ st.session_state.daily_totals["Protein"] += total_protein
110
+ st.session_state.daily_totals["Carbs"] += total_carbs
111
+ st.session_state.daily_totals["Fat"] += total_fat
112
+
113
+ # Get Dobby's feedback
114
+ dobby_comment = get_dobby_comment(
115
+ food_name, total_calories, total_protein, total_carbs, total_fat, meal, st.session_state.user_info["Goal"]
116
+ )
117
+
118
+ # Display results
119
+ st.success(f"{meal} Analysis Complete!")
120
+ st.write(f"**Food Name:** {food_name}")
121
+ st.write(f"**Calories:** {total_calories} kcal")
122
+ st.write(f"**Protein:** {total_protein} g")
123
+ st.write(f"**Carbs:** {total_carbs} g")
124
+ st.write(f"**Fat:** {total_fat} g")
125
+ st.subheader(f"Dobby's {meal} Comment:")
126
+ st.write(dobby_comment)
127
+
128
+ except Exception as e:
129
+ st.error(f"Error processing the image: {e}")
130
+
131
+ # Step 3: Daily Summary
132
+ if all(st.session_state.meal_data[meal] for meal in ["Breakfast", "Lunch", "Dinner"]):
133
+ st.subheader("Daily Summary")
134
+ st.write(f"**Total Calories:** {st.session_state.daily_totals['Calories']} kcal")
135
+ st.write(f"**Total Protein:** {st.session_state.daily_totals['Protein']} g")
136
+ st.write(f"**Total Carbs:** {st.session_state.daily_totals['Carbs']} g")
137
+ st.write(f"**Total Fat:** {st.session_state.daily_totals['Fat']} g")
138
+
139
+ # Final Dobby comment
140
+ final_prompt = f"""
141
+ The user had the following meals today:
142
+ Breakfast: {st.session_state.meal_data['Breakfast']['Food Name']}
143
+ Lunch: {st.session_state.meal_data['Lunch']['Food Name']}
144
+ Dinner: {st.session_state.meal_data['Dinner']['Food Name']}
145
+ Total daily intake: {st.session_state.daily_totals['Calories']} kcal,
146
+ {st.session_state.daily_totals['Protein']} g protein, {st.session_state.daily_totals['Carbs']} g carbs,
147
+ {st.session_state.daily_totals['Fat']} g fat.
148
+ The user's goal is to {st.session_state.user_info['Goal'].lower()}. Provide a summary and feedback for the day.
149
+ """
150
+ final_comment = get_dobby_comment(
151
+ "", st.session_state.daily_totals["Calories"], st.session_state.daily_totals["Protein"],
152
+ st.session_state.daily_totals["Carbs"], st.session_state.daily_totals["Fat"], "Daily Summary",
153
+ st.session_state.user_info["Goal"]
154
+ )
155
+ st.subheader("Dobby's Daily Summary Comment:")
156
+ st.write(final_comment)