ibrahim yıldız commited on
Commit
2037190
·
verified ·
1 Parent(s): 182d8f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -86
app.py CHANGED
@@ -1,96 +1,114 @@
1
  import streamlit as st
2
- import requests
 
 
3
  from PIL import Image
4
 
5
- # API configurations
6
- GEMINI_API_URL = "https://api.gemini.google.com/v1/recognize"
7
- GEMINI_API_KEY = "AIzaSyB6z5crVaKSj1ct_gVsq6nrHtSGme_JmoY" # Replace with your actual Gemini API key
8
-
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
 
14
- # Streamlit app setup
15
- st.title("AI-Powered Food Nutrition Analyzer with Dobby")
16
- st.write("Upload an image of your meal, and Dobby will analyze it and provide feedback!")
17
 
18
- # File upload
19
- uploaded_file = st.file_uploader("Upload your food image", type=["jpg", "jpeg", "png"])
 
20
 
21
- if uploaded_file is not None:
22
- # Display uploaded image
23
  image = Image.open(uploaded_file)
24
  st.image(image, caption="Uploaded Image", use_column_width=True)
25
 
26
- # Save the image temporarily
27
- with open("temp_image.jpg", "wb") as temp_file:
28
- temp_file.write(uploaded_file.getbuffer())
29
-
30
- # Prepare the image for Gemini API request
31
- with open("temp_image.jpg", "rb") as img_file:
32
- files = {"file": img_file}
33
- headers = {"Authorization": f"Bearer {GEMINI_API_KEY}"}
34
-
35
- # Send request to Gemini API
36
- response = requests.post(GEMINI_API_URL, headers=headers, files=files)
37
-
38
- # Process the Gemini API response
39
- if response.status_code == 200:
40
- data = response.json()
41
- st.write("**Detected Foods and Their Nutritional Information:**")
42
- food_log = []
43
- for item in data.get("ingredients", []):
44
- food_name = item["name"]
45
- calories = item.get("calories", "N/A")
46
- protein = item.get("protein", "N/A")
47
- carbs = item.get("carbohydrates", "N/A")
48
- fat = item.get("fat", "N/A")
49
-
50
- food_log.append(
51
- f"{food_name}: {calories} kcal, Protein: {protein} g, Carbs: {carbs} g, Fat: {fat} g"
52
- )
53
-
54
- st.write(f"**{food_name}**")
55
- st.write(f"- Calories: {calories} kcal")
56
- st.write(f"- Protein: {protein} g")
57
- st.write(f"- Carbohydrates: {carbs} g")
58
- st.write(f"- Fat: {fat} g")
59
-
60
- # Prepare Dobby's feedback
61
- tone = st.radio("Choose Dobby's tone:", ["Motivational (Leashed)", "Sarcastic (Unhinged)"])
62
- selected_model = DOBBY_LEASHED_MODEL if tone == "Motivational (Leashed)" else DOBBY_UNHINGED_MODEL
63
-
64
- dobby_prompt = f"""
65
- Here is the meal log:
66
- {'; '.join(food_log)}.
67
- Provide feedback on this meal in a {tone.lower()} tone.
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(f"Error in processing the image: {response.status_code}")
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.")