Spaces:
Sleeping
Sleeping
ibrahim yıldız commited on
Update app.py
Browse files
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"
|
| 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 |
-
#
|
| 16 |
-
|
| 17 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|