Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
import base64
|
|
@@ -9,9 +9,7 @@ from reportlab.lib.pagesizes import letter
|
|
| 9 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as ReportLabImage
|
| 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="π",
|
|
@@ -21,10 +19,7 @@ st.set_page_config(
|
|
| 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()
|
|
@@ -103,24 +98,23 @@ def generate_analysis(uploaded_file, client):
|
|
| 103 |
model="llama-3.2-11b-vision-preview",
|
| 104 |
messages=[
|
| 105 |
{
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
],
|
| 125 |
temperature=0.2,
|
| 126 |
max_tokens=400,
|
|
@@ -131,10 +125,7 @@ def generate_analysis(uploaded_file, client):
|
|
| 131 |
st.error(f"API communication error: {e}")
|
| 132 |
return None
|
| 133 |
|
| 134 |
-
# ======================
|
| 135 |
# UI COMPONENTS
|
| 136 |
-
# ======================
|
| 137 |
-
|
| 138 |
def display_main_interface():
|
| 139 |
"""Render primary application interface"""
|
| 140 |
logo_b64 = encode_image("src/logo.png")
|
|
@@ -184,10 +175,7 @@ def render_sidebar(client):
|
|
| 184 |
st.session_state.analysis_result = report
|
| 185 |
st.rerun()
|
| 186 |
|
| 187 |
-
# ======================
|
| 188 |
# APPLICATION ENTRYPOINT
|
| 189 |
-
# ======================
|
| 190 |
-
|
| 191 |
def main():
|
| 192 |
"""Primary application controller"""
|
| 193 |
client = initialize_api_client()
|
|
@@ -195,4 +183,4 @@ def main():
|
|
| 195 |
render_sidebar(client)
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import os
|
| 4 |
import base64
|
|
|
|
| 9 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as ReportLabImage
|
| 10 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 11 |
|
|
|
|
| 12 |
# CONFIGURATION SETTINGS
|
|
|
|
| 13 |
st.set_page_config(
|
| 14 |
page_title="Smart Diet Analyzer",
|
| 15 |
page_icon="π",
|
|
|
|
| 19 |
|
| 20 |
ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg']
|
| 21 |
|
|
|
|
| 22 |
# UTILITY FUNCTIONS
|
|
|
|
|
|
|
| 23 |
def initialize_api_client():
|
| 24 |
"""Initialize Groq API client"""
|
| 25 |
load_dotenv()
|
|
|
|
| 98 |
model="llama-3.2-11b-vision-preview",
|
| 99 |
messages=[
|
| 100 |
{
|
| 101 |
+
"type": "text",
|
| 102 |
+
"text": """
|
| 103 |
+
You are an expert nutritionist with advanced image analysis capabilities.
|
| 104 |
+
Your task is to analyze the provided image, identify all visible food items, and estimate their calorie content with high accuracy.
|
| 105 |
+
**Instructions:**
|
| 106 |
+
- Identify and list each food item visible in the image.
|
| 107 |
+
- For each item, estimate the calorie content based on standard nutritional data, considering portion size, cooking method, and food density.
|
| 108 |
+
- Clearly mark any calorie estimate as "approximate" if based on assumptions due to unclear details.
|
| 109 |
+
- Calculate and provide the total estimated calories for the entire meal.
|
| 110 |
+
**Output Format:**
|
| 111 |
+
- Food Item 1: [Name] β Estimated Calories: [value] kcal
|
| 112 |
+
- Food Item 2: [Name] β Estimated Calories: [value] kcal
|
| 113 |
+
- ...
|
| 114 |
+
- **Total Estimated Calories:** [value] kcal
|
| 115 |
+
If the image lacks sufficient detail or is unclear, specify the limitations and include your confidence level in the estimate as a percentage.
|
| 116 |
+
"""
|
| 117 |
+
}
|
|
|
|
| 118 |
],
|
| 119 |
temperature=0.2,
|
| 120 |
max_tokens=400,
|
|
|
|
| 125 |
st.error(f"API communication error: {e}")
|
| 126 |
return None
|
| 127 |
|
|
|
|
| 128 |
# UI COMPONENTS
|
|
|
|
|
|
|
| 129 |
def display_main_interface():
|
| 130 |
"""Render primary application interface"""
|
| 131 |
logo_b64 = encode_image("src/logo.png")
|
|
|
|
| 175 |
st.session_state.analysis_result = report
|
| 176 |
st.rerun()
|
| 177 |
|
|
|
|
| 178 |
# APPLICATION ENTRYPOINT
|
|
|
|
|
|
|
| 179 |
def main():
|
| 180 |
"""Primary application controller"""
|
| 181 |
client = initialize_api_client()
|
|
|
|
| 183 |
render_sidebar(client)
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
+
main()
|