Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,6 +21,22 @@ st.set_page_config(
|
|
21 |
|
22 |
ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg']
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# ======================
|
25 |
# UTILITY FUNCTIONS
|
26 |
# ======================
|
@@ -50,7 +66,6 @@ def process_image(uploaded_file):
|
|
50 |
try:
|
51 |
image = Image.open(uploaded_file)
|
52 |
buffer = io.BytesIO()
|
53 |
-
# Ensure image.format is valid; default to PNG if not available.
|
54 |
fmt = image.format if image.format else "PNG"
|
55 |
image.save(buffer, format=fmt)
|
56 |
return base64.b64encode(buffer.getvalue()).decode('utf-8'), fmt
|
@@ -72,7 +87,6 @@ def generate_pdf(report_text, logo_b64):
|
|
72 |
try:
|
73 |
logo_data = base64.b64decode(logo_b64)
|
74 |
logo_image = Image.open(io.BytesIO(logo_data))
|
75 |
-
# Resize the logo (adjust max width as needed)
|
76 |
logo_width, logo_height = logo_image.size
|
77 |
logo_aspect = logo_height / logo_width
|
78 |
max_logo_width = 150
|
@@ -161,10 +175,8 @@ def display_main_interface(logo_b64):
|
|
161 |
st.markdown("---")
|
162 |
|
163 |
if st.session_state.get('analysis_result'):
|
164 |
-
# Create two columns: one for download and one for the clear button
|
165 |
col1, col2 = st.columns(2)
|
166 |
|
167 |
-
# Left column for the Download button
|
168 |
with col1:
|
169 |
pdf_report = generate_pdf(st.session_state.analysis_result, logo_b64)
|
170 |
st.download_button("π Download Nutrition Report",
|
@@ -172,11 +184,10 @@ def display_main_interface(logo_b64):
|
|
172 |
file_name="nutrition_report.pdf",
|
173 |
mime="application/pdf")
|
174 |
|
175 |
-
# Right column for the Clear button
|
176 |
with col2:
|
177 |
if st.button("Clear Analysis ποΈ"):
|
178 |
st.session_state.pop('analysis_result')
|
179 |
-
|
180 |
|
181 |
if st.session_state.get('analysis_result'):
|
182 |
st.markdown("### π― Nutrition Analysis Report")
|
@@ -201,7 +212,7 @@ def render_sidebar(client, logo_b64):
|
|
201 |
report = generate_analysis(uploaded_file, client)
|
202 |
if report:
|
203 |
st.session_state.analysis_result = report
|
204 |
-
|
205 |
else:
|
206 |
st.error("Failed to generate analysis.")
|
207 |
|
|
|
21 |
|
22 |
ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg']
|
23 |
|
24 |
+
# ======================
|
25 |
+
# RERUN HELPER FUNCTION
|
26 |
+
# ======================
|
27 |
+
def rerun():
|
28 |
+
"""
|
29 |
+
Helper function to rerun the app.
|
30 |
+
Tries to use st.experimental_rerun if available; otherwise, does nothing.
|
31 |
+
"""
|
32 |
+
if hasattr(st, "experimental_rerun"):
|
33 |
+
st.experimental_rerun()
|
34 |
+
else:
|
35 |
+
# Fallback: you might consider raising an exception to force a rerun.
|
36 |
+
# However, this is not recommended for production. Instead, you might simply
|
37 |
+
# notify the user to manually refresh the page.
|
38 |
+
st.warning("Please refresh the page to update the results.")
|
39 |
+
|
40 |
# ======================
|
41 |
# UTILITY FUNCTIONS
|
42 |
# ======================
|
|
|
66 |
try:
|
67 |
image = Image.open(uploaded_file)
|
68 |
buffer = io.BytesIO()
|
|
|
69 |
fmt = image.format if image.format else "PNG"
|
70 |
image.save(buffer, format=fmt)
|
71 |
return base64.b64encode(buffer.getvalue()).decode('utf-8'), fmt
|
|
|
87 |
try:
|
88 |
logo_data = base64.b64decode(logo_b64)
|
89 |
logo_image = Image.open(io.BytesIO(logo_data))
|
|
|
90 |
logo_width, logo_height = logo_image.size
|
91 |
logo_aspect = logo_height / logo_width
|
92 |
max_logo_width = 150
|
|
|
175 |
st.markdown("---")
|
176 |
|
177 |
if st.session_state.get('analysis_result'):
|
|
|
178 |
col1, col2 = st.columns(2)
|
179 |
|
|
|
180 |
with col1:
|
181 |
pdf_report = generate_pdf(st.session_state.analysis_result, logo_b64)
|
182 |
st.download_button("π Download Nutrition Report",
|
|
|
184 |
file_name="nutrition_report.pdf",
|
185 |
mime="application/pdf")
|
186 |
|
|
|
187 |
with col2:
|
188 |
if st.button("Clear Analysis ποΈ"):
|
189 |
st.session_state.pop('analysis_result')
|
190 |
+
rerun()
|
191 |
|
192 |
if st.session_state.get('analysis_result'):
|
193 |
st.markdown("### π― Nutrition Analysis Report")
|
|
|
212 |
report = generate_analysis(uploaded_file, client)
|
213 |
if report:
|
214 |
st.session_state.analysis_result = report
|
215 |
+
rerun()
|
216 |
else:
|
217 |
st.error("Failed to generate analysis.")
|
218 |
|