Spaces:
Sleeping
Sleeping
ibrahim yıldız commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
import json
|
| 4 |
-
from PIL import Image
|
| 5 |
import google.generativeai as genai
|
|
|
|
| 6 |
|
| 7 |
-
# API
|
| 8 |
-
|
| 9 |
-
genai.configure(api_key=GENAI_API_KEY)
|
| 10 |
|
|
|
|
| 11 |
DOBBY_API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 12 |
-
DOBBY_API_KEY = "fw_3ZjtsywUGddwa1wGY4VvB3eW" # Replace with your Dobby API key
|
| 13 |
DOBBY_LEASHED_MODEL = "accounts/sentientfoundation/models/dobby-mini-leashed-llama-3-1-8b#accounts/sentientfoundation/deployments/22e7b3fd"
|
| 14 |
DOBBY_UNHINGED_MODEL = "accounts/sentientfoundation/models/dobby-mini-unhinged-llama-3-1-8b#accounts/sentientfoundation/deployments/81e155fc"
|
| 15 |
|
|
@@ -22,28 +20,21 @@ uploaded_file = st.file_uploader("Upload your food image", type=["jpg", "jpeg",
|
|
| 22 |
tone = st.radio("Select Dobby's tone:", ["Motivational (Leashed)", "Sarcastic (Unhinged)"])
|
| 23 |
|
| 24 |
# Function to analyze food image using Gemini API
|
| 25 |
-
def
|
| 26 |
-
prompt = """
|
| 27 |
-
You are an expert nutritionist analyzing the food items in the image.
|
| 28 |
-
Start by naming the meal based on the image, identify all ingredients, and estimate calories for each ingredient.
|
| 29 |
-
Summarize the total calories, protein, carbs, and fat. Provide data in JSON format:
|
| 30 |
-
{
|
| 31 |
-
"meal_name": "Meal Name",
|
| 32 |
-
"total_calories": X,
|
| 33 |
-
"total_protein": X,
|
| 34 |
-
"total_carbs": X,
|
| 35 |
-
"total_fat": X
|
| 36 |
-
}
|
| 37 |
-
"""
|
| 38 |
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
| 39 |
-
response = model.generate_content([
|
| 40 |
return response.text
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Function to get Dobby's comment
|
| 43 |
def get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, tone):
|
| 44 |
selected_model = DOBBY_LEASHED_MODEL if tone == "Motivational (Leashed)" else DOBBY_UNHINGED_MODEL
|
| 45 |
dobby_prompt = f"""
|
| 46 |
-
The user had a meal: {food_name}.
|
| 47 |
Nutritional data: {total_calories} kcal, {total_protein} g protein, {total_carbs} g carbs, {total_fat} g fat.
|
| 48 |
Comment on this meal in a {tone.lower()} tone.
|
| 49 |
"""
|
|
@@ -62,39 +53,40 @@ def get_dobby_comment(food_name, total_calories, total_protein, total_carbs, tot
|
|
| 62 |
|
| 63 |
# Main app logic
|
| 64 |
if uploaded_file:
|
| 65 |
-
st.image(uploaded_file, caption="Uploaded Image",
|
| 66 |
with st.spinner("Analyzing the image..."):
|
| 67 |
try:
|
| 68 |
-
# Prepare image data
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
# Extract
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
total_protein = analysis_data["total_protein"]
|
| 79 |
-
total_carbs = analysis_data["total_carbs"]
|
| 80 |
-
total_fat = analysis_data["total_fat"]
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
# Display results
|
| 88 |
-
st.success("Analysis Complete!")
|
| 89 |
-
st.subheader("Results:")
|
| 90 |
-
st.write(f"**Food Name:** {food_name}")
|
| 91 |
-
st.write(f"**Total Calories:** {total_calories} kcal")
|
| 92 |
-
st.write(f"**Protein:** {total_protein} g")
|
| 93 |
-
st.write(f"**Carbohydrates:** {total_carbs} g")
|
| 94 |
-
st.write(f"**Fat:** {total_fat} g")
|
| 95 |
st.subheader("Dobby's Comment:")
|
| 96 |
st.write(dobby_comment)
|
| 97 |
-
except json.JSONDecodeError:
|
| 98 |
-
st.error("Error parsing Gemini API response. Ensure the response is valid JSON.")
|
| 99 |
except Exception as e:
|
| 100 |
st.error(f"Error processing the image: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
from PIL import Image
|
| 4 |
|
| 5 |
+
# Configure Google Generative AI library with an API key
|
| 6 |
+
genai.configure(api_key="AIzaSyB6z5crVaKSj1ct_gVsq6nrHtSGme_JmoY")
|
|
|
|
| 7 |
|
| 8 |
+
# API configurations for Dobby
|
| 9 |
DOBBY_API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 10 |
+
DOBBY_API_KEY = "fw_3ZjtsywUGddwa1wGY4VvB3eW" # Replace with your valid Dobby API key
|
| 11 |
DOBBY_LEASHED_MODEL = "accounts/sentientfoundation/models/dobby-mini-leashed-llama-3-1-8b#accounts/sentientfoundation/deployments/22e7b3fd"
|
| 12 |
DOBBY_UNHINGED_MODEL = "accounts/sentientfoundation/models/dobby-mini-unhinged-llama-3-1-8b#accounts/sentientfoundation/deployments/81e155fc"
|
| 13 |
|
|
|
|
| 20 |
tone = st.radio("Select Dobby's tone:", ["Motivational (Leashed)", "Sarcastic (Unhinged)"])
|
| 21 |
|
| 22 |
# Function to analyze food image using Gemini API
|
| 23 |
+
def analyze_food_with_gemini(input_prompt, image_data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
| 25 |
+
response = model.generate_content([input_prompt, image_data[0]])
|
| 26 |
return response.text
|
| 27 |
|
| 28 |
+
# Function to prepare image data for Gemini API
|
| 29 |
+
def prepare_image_data(uploaded_file):
|
| 30 |
+
bytes_data = uploaded_file.getvalue()
|
| 31 |
+
return [{"mime_type": uploaded_file.type, "data": bytes_data}]
|
| 32 |
+
|
| 33 |
# Function to get Dobby's comment
|
| 34 |
def get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, tone):
|
| 35 |
selected_model = DOBBY_LEASHED_MODEL if tone == "Motivational (Leashed)" else DOBBY_UNHINGED_MODEL
|
| 36 |
dobby_prompt = f"""
|
| 37 |
+
The user had a meal: {food_name}.
|
| 38 |
Nutritional data: {total_calories} kcal, {total_protein} g protein, {total_carbs} g carbs, {total_fat} g fat.
|
| 39 |
Comment on this meal in a {tone.lower()} tone.
|
| 40 |
"""
|
|
|
|
| 53 |
|
| 54 |
# Main app logic
|
| 55 |
if uploaded_file:
|
| 56 |
+
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True) # Display uploaded image
|
| 57 |
with st.spinner("Analyzing the image..."):
|
| 58 |
try:
|
| 59 |
+
# Prepare image data for Gemini API
|
| 60 |
+
image_data = prepare_image_data(uploaded_file)
|
| 61 |
+
input_prompt = """
|
| 62 |
+
You are an expert nutritionist analyzing the food items in the image.
|
| 63 |
+
Start by naming the meal based on the image, identify all ingredients, and estimate calories for each ingredient.
|
| 64 |
+
Summarize the total calories, protein, carbs, and fat. Provide data in this format:
|
| 65 |
+
Meal Name: [Name of the meal]
|
| 66 |
+
Total Calories: X kcal
|
| 67 |
+
Protein: X g
|
| 68 |
+
Carbs: X g
|
| 69 |
+
Fat: X g
|
| 70 |
+
"""
|
| 71 |
+
# Get food analysis from Gemini API
|
| 72 |
+
analysis = analyze_food_with_gemini(input_prompt, image_data)
|
| 73 |
|
| 74 |
+
# Extract analysis results from Gemini response
|
| 75 |
+
st.subheader("Results:")
|
| 76 |
+
st.write(analysis)
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# Parse relevant data for Dobby
|
| 79 |
+
lines = analysis.split("\n")
|
| 80 |
+
food_name = lines[0].replace("Meal Name: ", "").strip()
|
| 81 |
+
total_calories = lines[1].replace("Total Calories: ", "").replace("kcal", "").strip()
|
| 82 |
+
total_protein = lines[2].replace("Protein: ", "").replace("g", "").strip()
|
| 83 |
+
total_carbs = lines[3].replace("Carbs: ", "").replace("g", "").strip()
|
| 84 |
+
total_fat = lines[4].replace("Fat: ", "").replace("g", "").strip()
|
| 85 |
+
|
| 86 |
+
# Get Dobby's feedback
|
| 87 |
+
dobby_comment = get_dobby_comment(food_name, total_calories, total_protein, total_carbs, total_fat, tone)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
st.subheader("Dobby's Comment:")
|
| 90 |
st.write(dobby_comment)
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
st.error(f"Error processing the image: {e}")
|