Spaces:
Sleeping
Sleeping
ibrahim yıldız commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,15 +12,15 @@ 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 and user info
|
| 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 |
if "user_info" not in st.session_state:
|
| 21 |
st.session_state.user_info = {}
|
| 22 |
|
| 23 |
-
|
| 24 |
st.markdown("""
|
| 25 |
<style>
|
| 26 |
.stApp {
|
|
@@ -48,18 +48,9 @@ st.markdown("""
|
|
| 48 |
color: #FFFFFF; /* White text for headers */
|
| 49 |
margin-bottom: 10px;
|
| 50 |
}
|
| 51 |
-
.button {
|
| 52 |
-
background-color: #4CAF50; /* Green button */
|
| 53 |
-
color: white;
|
| 54 |
-
font-size: 16px;
|
| 55 |
-
padding: 10px 20px;
|
| 56 |
-
border-radius: 5px;
|
| 57 |
-
}
|
| 58 |
</style>
|
| 59 |
""", unsafe_allow_html=True)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
st.markdown('<div class="title">Calorie Bro</div>', unsafe_allow_html=True)
|
| 64 |
st.markdown('<div class="subtitle">Your AI image calorie tracker, powered by Dobby. Dare to get feedback!</div>', unsafe_allow_html=True)
|
| 65 |
|
|
@@ -92,13 +83,8 @@ if st.session_state.user_info:
|
|
| 92 |
bytes_data = uploaded_file.getvalue()
|
| 93 |
return [{"mime_type": uploaded_file.type, "data": bytes_data}]
|
| 94 |
|
| 95 |
-
def get_dobby_comment(
|
| 96 |
selected_model = DOBBY_UNHINGED_MODEL # Default tone is sarcastic
|
| 97 |
-
dobby_prompt = f"""
|
| 98 |
-
The user had {meal}: {food_name}.
|
| 99 |
-
Nutritional data: {total_calories} kcal, {total_protein} g protein, {total_carbs} g carbs, {total_fat} g fat.
|
| 100 |
-
The user's goal is to {goal.lower()}. Provide feedback for this meal.
|
| 101 |
-
"""
|
| 102 |
response = requests.post(
|
| 103 |
DOBBY_API_URL,
|
| 104 |
headers={
|
|
@@ -107,7 +93,7 @@ if st.session_state.user_info:
|
|
| 107 |
},
|
| 108 |
json={
|
| 109 |
"model": selected_model,
|
| 110 |
-
"messages": [{"role": "user", "content":
|
| 111 |
},
|
| 112 |
)
|
| 113 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
|
@@ -155,43 +141,58 @@ if st.session_state.user_info:
|
|
| 155 |
st.session_state.daily_totals["Carbs"] += total_carbs
|
| 156 |
st.session_state.daily_totals["Fat"] += total_fat
|
| 157 |
|
| 158 |
-
# Get Dobby's feedback
|
| 159 |
-
dobby_comment = get_dobby_comment(
|
| 160 |
-
food_name, total_calories, total_protein, total_carbs, total_fat, meal,
|
| 161 |
-
st.session_state.user_info["Goal"], st.session_state.user_info["Tone"]
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
# Display results in the desired format
|
| 165 |
st.write(
|
| 166 |
f"**{food_name}: {total_calories} kcal, {total_protein}g of Protein, {total_carbs}g of Carbs, {total_fat}g of Fat**"
|
| 167 |
)
|
| 168 |
-
st.write(dobby_comment)
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
st.error(f"Error processing the image: {e}")
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
st.subheader("Daily Summary")
|
| 176 |
-
st.write(f"**Total Calories:** {st.session_state.daily_totals['Calories']} kcal")
|
| 177 |
st.write(f"**Total Protein:** {st.session_state.daily_totals['Protein']} g")
|
| 178 |
st.write(f"**Total Carbs:** {st.session_state.daily_totals['Carbs']} g")
|
| 179 |
st.write(f"**Total Fat:** {st.session_state.daily_totals['Fat']} g")
|
|
|
|
| 180 |
|
| 181 |
# Final Dobby comment
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
Total daily intake: {st.session_state.daily_totals['Calories']} kcal,
|
| 188 |
-
{st.session_state.daily_totals['Protein']} g protein, {st.session_state.daily_totals['Carbs']} g carbs,
|
| 189 |
-
{st.session_state.daily_totals['Fat']} g fat.
|
| 190 |
-
The user's goal is to {st.session_state.user_info['Goal'].lower()}. Provide a summary and feedback for the day.
|
| 191 |
"""
|
| 192 |
-
final_comment = get_dobby_comment(
|
| 193 |
-
|
| 194 |
-
st.session_state.daily_totals["Carbs"], st.session_state.daily_totals["Fat"], "Daily Summary",
|
| 195 |
-
st.session_state.user_info["Goal"], st.session_state.user_info["Tone"]
|
| 196 |
-
)
|
| 197 |
-
st.write(final_comment)
|
|
|
|
| 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, workouts, and user info
|
| 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, "Workout Calories Burnt": 0}
|
| 20 |
if "user_info" not in st.session_state:
|
| 21 |
st.session_state.user_info = {}
|
| 22 |
|
| 23 |
+
# Custom CSS for modern look
|
| 24 |
st.markdown("""
|
| 25 |
<style>
|
| 26 |
.stApp {
|
|
|
|
| 48 |
color: #FFFFFF; /* White text for headers */
|
| 49 |
margin-bottom: 10px;
|
| 50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
</style>
|
| 52 |
""", unsafe_allow_html=True)
|
| 53 |
|
|
|
|
|
|
|
| 54 |
st.markdown('<div class="title">Calorie Bro</div>', unsafe_allow_html=True)
|
| 55 |
st.markdown('<div class="subtitle">Your AI image calorie tracker, powered by Dobby. Dare to get feedback!</div>', unsafe_allow_html=True)
|
| 56 |
|
|
|
|
| 83 |
bytes_data = uploaded_file.getvalue()
|
| 84 |
return [{"mime_type": uploaded_file.type, "data": bytes_data}]
|
| 85 |
|
| 86 |
+
def get_dobby_comment(prompt):
|
| 87 |
selected_model = DOBBY_UNHINGED_MODEL # Default tone is sarcastic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
response = requests.post(
|
| 89 |
DOBBY_API_URL,
|
| 90 |
headers={
|
|
|
|
| 93 |
},
|
| 94 |
json={
|
| 95 |
"model": selected_model,
|
| 96 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 97 |
},
|
| 98 |
)
|
| 99 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
|
|
|
| 141 |
st.session_state.daily_totals["Carbs"] += total_carbs
|
| 142 |
st.session_state.daily_totals["Fat"] += total_fat
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# Display results in the desired format
|
| 145 |
st.write(
|
| 146 |
f"**{food_name}: {total_calories} kcal, {total_protein}g of Protein, {total_carbs}g of Carbs, {total_fat}g of Fat**"
|
| 147 |
)
|
|
|
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
st.error(f"Error processing the image: {e}")
|
| 151 |
|
| 152 |
+
# Step 3: Workout tracking
|
| 153 |
+
if all(st.session_state.meal_data[meal] for meal in ["Breakfast", "Lunch", "Dinner"]):
|
| 154 |
+
st.markdown('<div class="header">Step 3: Log Your Workout</div>', unsafe_allow_html=True)
|
| 155 |
+
workout_input = st.text_area("Describe your workout for the day:", help="E.g., 'I walked for 30 minutes and biked for 1 hour.'")
|
| 156 |
+
if st.button("Analyze Workout"):
|
| 157 |
+
with st.spinner("Calculating calories burnt..."):
|
| 158 |
+
workout_prompt = f"""
|
| 159 |
+
The user provided this workout description: "{workout_input}".
|
| 160 |
+
Estimate the calories burned and summarize the activities. Provide the output in this format:
|
| 161 |
+
Calories Burned: X kcal
|
| 162 |
+
Activities: [List of activities]
|
| 163 |
+
"""
|
| 164 |
+
try:
|
| 165 |
+
workout_analysis = analyze_food_with_gemini(workout_prompt, [""])
|
| 166 |
+
lines = workout_analysis.split("\n")
|
| 167 |
+
calories_burned = int(lines[0].replace("Calories Burned: ", "").replace("kcal", "").strip())
|
| 168 |
+
activities = lines[1].replace("Activities: ", "").strip()
|
| 169 |
+
|
| 170 |
+
# Update daily totals
|
| 171 |
+
st.session_state.daily_totals["Workout Calories Burnt"] = calories_burned
|
| 172 |
+
|
| 173 |
+
# Dobby's comment on workout
|
| 174 |
+
dobby_workout_comment = get_dobby_comment(f"The user did this workout: {workout_input}. Provide feedback.")
|
| 175 |
+
st.write(f"**Calories Burned:** {calories_burned} kcal")
|
| 176 |
+
st.write(f"**Activities:** {activities}")
|
| 177 |
+
st.write(f"**Dobby's Comment:** {dobby_workout_comment}")
|
| 178 |
+
except Exception as e:
|
| 179 |
+
st.error(f"Error processing workout: {e}")
|
| 180 |
+
|
| 181 |
+
# Step 4: Daily summary
|
| 182 |
+
if st.session_state.daily_totals["Workout Calories Burnt"]:
|
| 183 |
st.subheader("Daily Summary")
|
| 184 |
+
st.write(f"**Total Calories Consumed:** {st.session_state.daily_totals['Calories']} kcal")
|
| 185 |
st.write(f"**Total Protein:** {st.session_state.daily_totals['Protein']} g")
|
| 186 |
st.write(f"**Total Carbs:** {st.session_state.daily_totals['Carbs']} g")
|
| 187 |
st.write(f"**Total Fat:** {st.session_state.daily_totals['Fat']} g")
|
| 188 |
+
st.write(f"**Calories Burned from Workout:** {st.session_state.daily_totals['Workout Calories Burnt']} kcal")
|
| 189 |
|
| 190 |
# Final Dobby comment
|
| 191 |
+
total_calorie_balance = st.session_state.daily_totals['Calories'] - st.session_state.daily_totals['Workout Calories Burnt']
|
| 192 |
+
final_summary_prompt = f"""
|
| 193 |
+
The user consumed {st.session_state.daily_totals['Calories']} kcal and burned {st.session_state.daily_totals['Workout Calories Burnt']} kcal through workouts.
|
| 194 |
+
Total calorie balance: {total_calorie_balance} kcal.
|
| 195 |
+
Provide a summary and feedback for the day.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
"""
|
| 197 |
+
final_comment = get_dobby_comment(final_summary_prompt)
|
| 198 |
+
st.write(f"**Dobby's Daily Summary:** {final_comment}")
|
|
|
|
|
|
|
|
|
|
|
|