Spaces:
Sleeping
Sleeping
ibrahim yıldız commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,114 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
st.title("
|
| 16 |
-
st.
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
image = Image.open(uploaded_file)
|
| 24 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 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 |
-
# Send request to Dobby API
|
| 71 |
-
dobby_response = requests.post(
|
| 72 |
-
DOBBY_API_URL,
|
| 73 |
-
headers={
|
| 74 |
-
"Authorization": f"Bearer {DOBBY_API_KEY}",
|
| 75 |
-
"Content-Type": "application/json",
|
| 76 |
-
},
|
| 77 |
-
json={
|
| 78 |
-
"model": selected_model,
|
| 79 |
-
"messages": [{"role": "user", "content": dobby_prompt}],
|
| 80 |
-
},
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
# Process Dobby API response
|
| 84 |
-
if dobby_response.status_code == 200:
|
| 85 |
-
dobby_reply = dobby_response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
| 86 |
-
st.write("**Dobby says:**")
|
| 87 |
-
st.success(dobby_reply)
|
| 88 |
-
else:
|
| 89 |
-
st.error("Failed to get a response from Dobby. Please try again later.")
|
| 90 |
else:
|
| 91 |
-
st.error(
|
| 92 |
-
st.write("Response:", response.json())
|
| 93 |
-
|
| 94 |
-
# Footer
|
| 95 |
-
st.write("---")
|
| 96 |
-
st.write("Powered by [Gemini AI](https://www.google.com/ai/) and Dobby (Fireworks AI)")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv, find_dotenv
|
| 3 |
+
import os
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv(find_dotenv())
|
| 9 |
+
|
| 10 |
+
# Set API Key directly (if environment variables are not configured)
|
| 11 |
+
API_KEY = "AIzaSyB6z5crVaKSj1ct_gVsq6nrHtSGme_JmoY"
|
| 12 |
+
genai.configure(api_key=API_KEY)
|
| 13 |
+
|
| 14 |
+
# Streamlit Page Configuration
|
| 15 |
+
st.set_page_config(page_title="Nutrition Monitor", page_icon="🍎")
|
| 16 |
+
|
| 17 |
+
# Custom CSS for Streamlit App
|
| 18 |
+
st.markdown("""
|
| 19 |
+
<style>
|
| 20 |
+
.stApp {
|
| 21 |
+
background-color: #f5f5f5; /* Light grey background */
|
| 22 |
+
font-family: Arial, sans-serif; /* Clean font style */
|
| 23 |
+
}
|
| 24 |
+
.stButton>button {
|
| 25 |
+
background-color: #4CAF50; /* Green button */
|
| 26 |
+
color: white; /* White text */
|
| 27 |
+
font-size: 16px; /* Readable font size */
|
| 28 |
+
}
|
| 29 |
+
</style>
|
| 30 |
+
""", unsafe_allow_html=True)
|
| 31 |
+
|
| 32 |
+
# Define a function for Gemini API request
|
| 33 |
+
def get_gemini_response(input_prompt, image_data):
|
| 34 |
+
try:
|
| 35 |
+
# Use Gemini's generative model
|
| 36 |
+
model = genai.GenerativeModel("gemini-1.5-pro-latest")
|
| 37 |
+
# Generate response using the prompt and image
|
| 38 |
+
response = model.generate_content([input_prompt, image_data[0]])
|
| 39 |
+
return response.text
|
| 40 |
+
except Exception as e:
|
| 41 |
+
st.error(f"Error generating response: {e}")
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
# Prepare uploaded image for Gemini API
|
| 45 |
+
def prepare_image_data(uploaded_file):
|
| 46 |
+
if uploaded_file:
|
| 47 |
+
bytes_data = uploaded_file.getvalue()
|
| 48 |
+
return [
|
| 49 |
+
{
|
| 50 |
+
"mime_type": uploaded_file.type,
|
| 51 |
+
"data": bytes_data,
|
| 52 |
+
}
|
| 53 |
+
]
|
| 54 |
+
else:
|
| 55 |
+
raise FileNotFoundError("No image uploaded")
|
| 56 |
|
| 57 |
+
# Sidebar for image upload
|
| 58 |
+
st.sidebar.title("Upload Section")
|
| 59 |
+
uploaded_file = st.sidebar.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 60 |
|
| 61 |
+
# Main page header
|
| 62 |
+
st.title("AI-Powered Nutrition Monitor")
|
| 63 |
+
st.write("Upload an image of your meal, and AI will analyze its nutritional content!")
|
| 64 |
|
| 65 |
+
# Display uploaded image
|
| 66 |
+
if uploaded_file:
|
| 67 |
image = Image.open(uploaded_file)
|
| 68 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 69 |
|
| 70 |
+
# Button to analyze food
|
| 71 |
+
analyze_button = st.button("Analyze Food")
|
| 72 |
+
input_prompt = """
|
| 73 |
+
You are an expert nutritionist analyzing the food items in the image.
|
| 74 |
+
Start by determining if the image contains food items.
|
| 75 |
+
If the image does not contain any food items,
|
| 76 |
+
clearly state "No food items detected in the image."
|
| 77 |
+
and do not provide any calorie information.
|
| 78 |
+
If food items are detected,
|
| 79 |
+
start by naming the meal based on the image,
|
| 80 |
+
identify and list every ingredient you can find in the image,
|
| 81 |
+
and then estimate the total calories for each ingredient.
|
| 82 |
+
Summarize the total calories based on the identified ingredients.
|
| 83 |
+
Follow the format below:
|
| 84 |
+
|
| 85 |
+
If no food items are detected:
|
| 86 |
+
No food items detected in the image.
|
| 87 |
+
|
| 88 |
+
If food items are detected:
|
| 89 |
+
Meal Name: [Name of the meal]
|
| 90 |
+
|
| 91 |
+
1. Ingredient 1 - estimated calories
|
| 92 |
+
2. Ingredient 2 - estimated calories
|
| 93 |
+
----
|
| 94 |
+
Total estimated calories: X
|
| 95 |
+
|
| 96 |
+
Finally, mention whether the food is healthy or not,
|
| 97 |
+
and provide the percentage split of protein, carbs, and fats in the food item.
|
| 98 |
+
Also, mention the total fiber content in the food item and any other important details.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
# Process and analyze when button is clicked
|
| 102 |
+
if analyze_button:
|
| 103 |
+
if uploaded_file:
|
| 104 |
+
with st.spinner("Analyzing the image..."):
|
| 105 |
+
# Prepare image data
|
| 106 |
+
image_data = prepare_image_data(uploaded_file)
|
| 107 |
+
# Get response from Gemini API
|
| 108 |
+
response = get_gemini_response(input_prompt, image_data)
|
| 109 |
+
if response:
|
| 110 |
+
st.success("Analysis Complete!")
|
| 111 |
+
st.subheader("Food Analysis")
|
| 112 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
+
st.error("Please upload an image before clicking Analyze Food.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|