Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import base64
|
@@ -44,30 +44,15 @@ def encode_image(image_path):
|
|
44 |
return ""
|
45 |
|
46 |
|
47 |
-
def resize_image(image):
|
48 |
-
"""Resize the image to reduce size before encoding it to base64"""
|
49 |
-
max_width = 800 # Max width in pixels
|
50 |
-
max_height = 800 # Max height in pixels
|
51 |
-
image.thumbnail((max_width, max_height))
|
52 |
-
return image
|
53 |
-
|
54 |
-
|
55 |
-
def compress_image(image):
|
56 |
-
"""Compress the image to JPEG format with a lower quality to reduce size"""
|
57 |
-
buffer = io.BytesIO()
|
58 |
-
image.save(buffer, format="JPEG", quality=70) # Reduce quality to 70%
|
59 |
-
return base64.b64encode(buffer.getvalue()).decode('utf-8')
|
60 |
-
|
61 |
-
|
62 |
def process_image(uploaded_file):
|
63 |
-
"""Convert
|
64 |
try:
|
65 |
image = Image.open(uploaded_file)
|
66 |
-
|
67 |
-
|
68 |
-
return
|
69 |
except Exception as e:
|
70 |
-
st.error(f"
|
71 |
return None, None
|
72 |
|
73 |
|
@@ -106,10 +91,9 @@ def generate_pdf(report_text, logo_b64):
|
|
106 |
|
107 |
|
108 |
def generate_analysis(uploaded_file, client):
|
109 |
-
"""Generate
|
110 |
base64_image, img_format = process_image(uploaded_file)
|
111 |
if not base64_image:
|
112 |
-
st.error("Failed to process image. Cannot generate analysis.")
|
113 |
return None
|
114 |
|
115 |
image_url = f"data:image/{img_format.lower()};base64,{base64_image}"
|
@@ -117,35 +101,34 @@ def generate_analysis(uploaded_file, client):
|
|
117 |
try:
|
118 |
response = client.chat.completions.create(
|
119 |
model="llama-3.2-11b-vision-preview",
|
120 |
-
messages=[
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
}],
|
142 |
temperature=0.2,
|
143 |
max_tokens=400,
|
144 |
top_p=0.5
|
145 |
)
|
146 |
return response.choices[0].message.content
|
147 |
except Exception as e:
|
148 |
-
st.error(f"
|
149 |
return None
|
150 |
|
151 |
# ======================
|
@@ -212,4 +195,4 @@ def main():
|
|
212 |
render_sidebar(client)
|
213 |
|
214 |
if __name__ == "__main__":
|
215 |
-
main()
|
|
|
1 |
+
streamlit as st
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import base64
|
|
|
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 |
|
|
|
91 |
|
92 |
|
93 |
def generate_analysis(uploaded_file, client):
|
94 |
+
"""Generate AI-powered food analysis"""
|
95 |
base64_image, img_format = process_image(uploaded_file)
|
96 |
if not base64_image:
|
|
|
97 |
return None
|
98 |
|
99 |
image_url = f"data:image/{img_format.lower()};base64,{base64_image}"
|
|
|
101 |
try:
|
102 |
response = client.chat.completions.create(
|
103 |
model="llama-3.2-11b-vision-preview",
|
104 |
+
messages=[
|
105 |
+
{
|
106 |
+
"type": "text",
|
107 |
+
"text": """
|
108 |
+
You are an expert nutritionist with advanced image analysis capabilities.
|
109 |
+
Your task is to analyze the provided image, identify all visible food items, and estimate their calorie content with high accuracy.
|
110 |
+
**Instructions:**
|
111 |
+
- Identify and list each food item visible in the image.
|
112 |
+
- For each item, estimate the calorie content based on standard nutritional data, considering portion size, cooking method, and food density.
|
113 |
+
- Clearly mark any calorie estimate as "approximate" if based on assumptions due to unclear details.
|
114 |
+
- Calculate and provide the total estimated calories for the entire meal.
|
115 |
+
**Output Format:**
|
116 |
+
- Food Item 1: [Name] – Estimated Calories: [value] kcal
|
117 |
+
- Food Item 2: [Name] – Estimated Calories: [value] kcal
|
118 |
+
- ...
|
119 |
+
- **Total Estimated Calories:** [value] kcal
|
120 |
+
If the image lacks sufficient detail or is unclear, specify the limitations and include your confidence level in the estimate as a percentage.
|
121 |
+
"""
|
122 |
+
}
|
123 |
+
|
124 |
+
],
|
|
|
125 |
temperature=0.2,
|
126 |
max_tokens=400,
|
127 |
top_p=0.5
|
128 |
)
|
129 |
return response.choices[0].message.content
|
130 |
except Exception as e:
|
131 |
+
st.error(f"API communication error: {e}")
|
132 |
return None
|
133 |
|
134 |
# ======================
|
|
|
195 |
render_sidebar(client)
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
+
main()
|