Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,182 +1,197 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
import os
|
4 |
-
import base64
|
5 |
-
import io
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
from groq import Groq
|
8 |
-
from reportlab.lib.pagesizes import letter
|
9 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
10 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
11 |
-
|
12 |
-
# ======================
|
13 |
-
# CONFIGURATION SETTINGS
|
14 |
-
# ======================
|
15 |
-
st.set_page_config(
|
16 |
-
page_title="Smart Diet Analyzer",
|
17 |
-
page_icon="π",
|
18 |
-
layout="wide",
|
19 |
-
initial_sidebar_state="expanded"
|
20 |
-
)
|
21 |
-
|
22 |
-
ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg']
|
23 |
-
|
24 |
-
# ======================
|
25 |
-
# UTILITY FUNCTIONS
|
26 |
-
# ======================
|
27 |
-
|
28 |
-
def initialize_api_client():
|
29 |
-
"""Initialize Groq API client"""
|
30 |
-
load_dotenv()
|
31 |
-
api_key = os.getenv("GROQ_API_KEY")
|
32 |
-
if not api_key:
|
33 |
-
st.error("API key not found. Please verify .env configuration.")
|
34 |
-
st.stop()
|
35 |
-
return Groq(api_key=api_key)
|
36 |
-
|
37 |
-
|
38 |
-
def encode_image(image_path):
|
39 |
-
"""Encode an image to base64"""
|
40 |
-
try:
|
41 |
-
with open(image_path, "rb") as img_file:
|
42 |
-
return base64.b64encode(img_file.read()).decode("utf-8")
|
43 |
-
except FileNotFoundError:
|
44 |
-
return ""
|
45 |
-
|
46 |
-
|
47 |
-
def process_image(uploaded_file):
|
48 |
-
"""Convert image to base64 string"""
|
49 |
-
try:
|
50 |
-
image = Image.open(uploaded_file)
|
51 |
-
buffer = io.BytesIO()
|
52 |
-
image.save(buffer, format=image.format)
|
53 |
-
return base64.b64encode(buffer.getvalue()).decode('utf-8'), image.format
|
54 |
-
except Exception as e:
|
55 |
-
st.error(f"Image processing error: {e}")
|
56 |
-
return None, None
|
57 |
-
|
58 |
-
|
59 |
-
def generate_pdf(report_text):
|
60 |
-
"""Generate a PDF report"""
|
61 |
-
buffer = io.BytesIO()
|
62 |
-
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
63 |
-
styles = getSampleStyleSheet()
|
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 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
if st.session_state.get('analysis_result'):
|
153 |
-
|
154 |
-
st.
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
import io
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from groq import Groq
|
8 |
+
from reportlab.lib.pagesizes import letter
|
9 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
10 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
11 |
+
|
12 |
+
# ======================
|
13 |
+
# CONFIGURATION SETTINGS
|
14 |
+
# ======================
|
15 |
+
st.set_page_config(
|
16 |
+
page_title="Smart Diet Analyzer",
|
17 |
+
page_icon="π",
|
18 |
+
layout="wide",
|
19 |
+
initial_sidebar_state="expanded"
|
20 |
+
)
|
21 |
+
|
22 |
+
ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg']
|
23 |
+
|
24 |
+
# ======================
|
25 |
+
# UTILITY FUNCTIONS
|
26 |
+
# ======================
|
27 |
+
|
28 |
+
def initialize_api_client():
|
29 |
+
"""Initialize Groq API client"""
|
30 |
+
load_dotenv()
|
31 |
+
api_key = os.getenv("GROQ_API_KEY")
|
32 |
+
if not api_key:
|
33 |
+
st.error("API key not found. Please verify .env configuration.")
|
34 |
+
st.stop()
|
35 |
+
return Groq(api_key=api_key)
|
36 |
+
|
37 |
+
|
38 |
+
def encode_image(image_path):
|
39 |
+
"""Encode an image to base64"""
|
40 |
+
try:
|
41 |
+
with open(image_path, "rb") as img_file:
|
42 |
+
return base64.b64encode(img_file.read()).decode("utf-8")
|
43 |
+
except FileNotFoundError:
|
44 |
+
return ""
|
45 |
+
|
46 |
+
|
47 |
+
def process_image(uploaded_file):
|
48 |
+
"""Convert image to base64 string"""
|
49 |
+
try:
|
50 |
+
image = Image.open(uploaded_file)
|
51 |
+
buffer = io.BytesIO()
|
52 |
+
image.save(buffer, format=image.format)
|
53 |
+
return base64.b64encode(buffer.getvalue()).decode('utf-8'), image.format
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Image processing error: {e}")
|
56 |
+
return None, None
|
57 |
+
|
58 |
+
|
59 |
+
def generate_pdf(report_text, logo_b64):
|
60 |
+
"""Generate a PDF report"""
|
61 |
+
buffer = io.BytesIO()
|
62 |
+
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
63 |
+
styles = getSampleStyleSheet()
|
64 |
+
|
65 |
+
# Include logo at the beginning of the report
|
66 |
+
logo_image = Image.open(io.BytesIO(base64.b64decode(logo_b64)))
|
67 |
+
logo_width, logo_height = logo_image.size
|
68 |
+
logo_aspect = logo_height / logo_width
|
69 |
+
max_logo_width = 150 # Adjust as needed
|
70 |
+
logo_width = min(logo_width, max_logo_width)
|
71 |
+
logo_height = int(logo_width * logo_aspect)
|
72 |
+
|
73 |
+
story = [
|
74 |
+
Paragraph(f'<img src="data:image/png;base64,{logo_b64}" width="{logo_width}" height="{logo_height}">', styles['Title']),
|
75 |
+
Spacer(1, 12),
|
76 |
+
Paragraph("<b>Nutrition Analysis Report</b>", styles['Title']),
|
77 |
+
Spacer(1, 12),
|
78 |
+
Paragraph(report_text.replace('\n', '<br/>'), styles['BodyText'])
|
79 |
+
]
|
80 |
+
|
81 |
+
doc.build(story)
|
82 |
+
buffer.seek(0)
|
83 |
+
return buffer
|
84 |
+
|
85 |
+
|
86 |
+
def generate_analysis(uploaded_file, client):
|
87 |
+
"""Generate AI-powered food analysis"""
|
88 |
+
base64_image, img_format = process_image(uploaded_file)
|
89 |
+
if not base64_image:
|
90 |
+
return None
|
91 |
+
|
92 |
+
image_url = f"data:image/{img_format.lower()};base64,{base64_image}"
|
93 |
+
|
94 |
+
try:
|
95 |
+
response = client.chat.completions.create(
|
96 |
+
model="llama-3.2-11b-vision-preview",
|
97 |
+
messages=[
|
98 |
+
{
|
99 |
+
"role": "user",
|
100 |
+
"content": [
|
101 |
+
{"type": "text", "text": """
|
102 |
+
You are an expert nutritionist with advanced image analysis capabilities.
|
103 |
+
Your task is to analyze the provided image, identify all visible food items, and estimate their calorie content as accurately as possible.
|
104 |
+
|
105 |
+
**Instructions:**
|
106 |
+
- List each identified food item separately.
|
107 |
+
- Use known nutritional data to provide accurate calorie estimates.
|
108 |
+
- Consider portion size, cooking method, and density of food.
|
109 |
+
- Clearly specify if an item's calorie count is an estimate due to ambiguity.
|
110 |
+
- Provide the total estimated calorie count for the entire meal.
|
111 |
+
|
112 |
+
**Output Format:**
|
113 |
+
- Food Item 1: [Name] β Estimated Calories: [value] kcal
|
114 |
+
- Food Item 2: [Name] β Estimated Calories: [value] kcal
|
115 |
+
- ...
|
116 |
+
- **Total Estimated Calories:** [value] kcal
|
117 |
+
|
118 |
+
If the image is unclear or lacks enough details, state the limitations and provide a confidence percentage for the estimation.
|
119 |
+
"""},
|
120 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
121 |
+
]
|
122 |
+
}
|
123 |
+
],
|
124 |
+
temperature=0.2,
|
125 |
+
max_tokens=400,
|
126 |
+
top_p=0.5
|
127 |
+
)
|
128 |
+
return response.choices[0].message.content
|
129 |
+
except Exception as e:
|
130 |
+
st.error(f"API communication error: {e}")
|
131 |
+
return None
|
132 |
+
|
133 |
+
# ======================
|
134 |
+
# UI COMPONENTS
|
135 |
+
# ======================
|
136 |
+
|
137 |
+
def display_main_interface():
|
138 |
+
"""Render primary application interface"""
|
139 |
+
logo_b64 = encode_image("src/logo.png")
|
140 |
+
|
141 |
+
# HTML with inline styles to change text colors
|
142 |
+
st.markdown(f"""
|
143 |
+
<div style="text-align: center;">
|
144 |
+
<img src="data:image/png;base64,{logo_b64}" width="100">
|
145 |
+
<h2 style="color: #4CAF50;">Smart Diet Analyzer</h2>
|
146 |
+
<p style="color: #FF6347;">AI-Powered Food & Nutrition Analysis</p>
|
147 |
+
</div>
|
148 |
+
""", unsafe_allow_html=True)
|
149 |
+
|
150 |
+
st.markdown("---")
|
151 |
+
|
152 |
+
if st.session_state.get('analysis_result'):
|
153 |
+
# Create two columns: one for download and one for clear button
|
154 |
+
col1, col2 = st.columns([1, 1])
|
155 |
+
|
156 |
+
# Left column for the Download button
|
157 |
+
with col1:
|
158 |
+
pdf_report = generate_pdf(st.session_state.analysis_result, logo_b64)
|
159 |
+
st.download_button("π Download Nutrition Report", data=pdf_report, file_name="nutrition_report.pdf", mime="application/pdf")
|
160 |
+
|
161 |
+
# Right column for the Clear button
|
162 |
+
with col2:
|
163 |
+
if st.button("Clear Analysis ποΈ"):
|
164 |
+
st.session_state.pop('analysis_result')
|
165 |
+
st.rerun()
|
166 |
+
|
167 |
+
if st.session_state.get('analysis_result'):
|
168 |
+
st.markdown("### π― Nutrition Analysis Report")
|
169 |
+
st.info(st.session_state.analysis_result)
|
170 |
+
|
171 |
+
|
172 |
+
def render_sidebar(client):
|
173 |
+
"""Create sidebar UI elements"""
|
174 |
+
with st.sidebar:
|
175 |
+
st.subheader("Image Upload")
|
176 |
+
uploaded_file = st.file_uploader("Upload Food Image", type=ALLOWED_FILE_TYPES)
|
177 |
+
|
178 |
+
if uploaded_file:
|
179 |
+
st.image(Image.open(uploaded_file), caption="Uploaded Food Image")
|
180 |
+
if st.button("Analyze Meal π½οΈ"):
|
181 |
+
with st.spinner("Analyzing image..."):
|
182 |
+
report = generate_analysis(uploaded_file, client)
|
183 |
+
st.session_state.analysis_result = report
|
184 |
+
st.rerun()
|
185 |
+
|
186 |
+
# ======================
|
187 |
+
# APPLICATION ENTRYPOINT
|
188 |
+
# ======================
|
189 |
+
|
190 |
+
def main():
|
191 |
+
"""Primary application controller"""
|
192 |
+
client = initialize_api_client()
|
193 |
+
display_main_interface()
|
194 |
+
render_sidebar(client)
|
195 |
+
|
196 |
+
if __name__ == "__main__":
|
197 |
+
main()
|