Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os # Import the os module
|
3 |
import gradio as gr
|
4 |
import cv2
|
@@ -160,8 +159,8 @@ def save_results_to_pdf(test_results, filename):
|
|
160 |
except Exception as e:
|
161 |
return f"Error saving PDF: {str(e)}", None
|
162 |
|
163 |
-
#
|
164 |
-
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""
|
165 |
from datetime import datetime
|
166 |
current_date = datetime.now().strftime("%B %d, %Y")
|
167 |
|
@@ -204,13 +203,20 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
204 |
</div>
|
205 |
|
206 |
<div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
|
207 |
-
<
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
212 |
</div>
|
213 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
"""
|
215 |
return html
|
216 |
|
@@ -293,6 +299,7 @@ def analyze_face(input_data):
|
|
293 |
build_table("❤️ Vitals",
|
294 |
[("SpO2", spo2, (95, 100)),
|
295 |
("Heart Rate", heart_rate, (60, 100)),
|
|
|
296 |
("Temperature", test_values["Temperature"], (97, 99)),
|
297 |
("BP Systolic", test_values["BP Systolic"], (90, 120)),
|
298 |
("BP Diastolic", test_values["BP Diastolic"], (60, 80))])
|
@@ -303,13 +310,6 @@ def analyze_face(input_data):
|
|
303 |
_, buffer = cv2.imencode('.png', frame_rgb)
|
304 |
profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
|
305 |
|
306 |
-
# Create a temporary file path for the PDF
|
307 |
-
pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
308 |
-
pdf_filepath = f"/tmp/{pdf_filename}"
|
309 |
-
|
310 |
-
# Save the PDF
|
311 |
-
pdf_result, _ = save_results_to_pdf(test_results, pdf_filepath)
|
312 |
-
|
313 |
# Use global patient details
|
314 |
global current_patient_details
|
315 |
health_card_html = build_health_card(
|
@@ -319,11 +319,39 @@ def analyze_face(input_data):
|
|
319 |
current_patient_details['name'],
|
320 |
current_patient_details['age'],
|
321 |
current_patient_details['gender'],
|
322 |
-
current_patient_details['id']
|
323 |
-
pdf_filepath=pdf_filepath
|
324 |
)
|
325 |
|
326 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
|
328 |
|
329 |
# Create Gradio interface
|
@@ -354,4 +382,4 @@ with gr.Blocks() as demo:
|
|
354 |
outputs=[result_html, result_pdf])
|
355 |
|
356 |
# Launch Gradio for Replit
|
357 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
1 |
import os # Import the os module
|
2 |
import gradio as gr
|
3 |
import cv2
|
|
|
159 |
except Exception as e:
|
160 |
return f"Error saving PDF: {str(e)}", None
|
161 |
|
162 |
+
# Build health card layout
|
163 |
+
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
|
164 |
from datetime import datetime
|
165 |
current_date = datetime.now().strftime("%B %d, %Y")
|
166 |
|
|
|
203 |
</div>
|
204 |
|
205 |
<div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
|
206 |
+
<button onclick="window.print()" style="padding: 12px 24px; background: linear-gradient(135deg, #4caf50, #45a049); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3); transition: all 0.3s;">
|
207 |
+
📥 Download Report
|
208 |
+
</button>
|
209 |
+
<button style="padding: 12px 24px; background: linear-gradient(135deg, #2196f3, #1976d2); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);">
|
210 |
+
📞 Find Labs Near Me
|
211 |
+
</button>
|
212 |
</div>
|
213 |
</div>
|
214 |
+
<style>
|
215 |
+
@media print {{
|
216 |
+
/* Hide input sections during print */
|
217 |
+
.gradio-container {{ display: none; }}
|
218 |
+
}}
|
219 |
+
</style>
|
220 |
"""
|
221 |
return html
|
222 |
|
|
|
299 |
build_table("❤️ Vitals",
|
300 |
[("SpO2", spo2, (95, 100)),
|
301 |
("Heart Rate", heart_rate, (60, 100)),
|
302 |
+
("Respiratory Rate", rr, (12, 20)),
|
303 |
("Temperature", test_values["Temperature"], (97, 99)),
|
304 |
("BP Systolic", test_values["BP Systolic"], (90, 120)),
|
305 |
("BP Diastolic", test_values["BP Diastolic"], (60, 80))])
|
|
|
310 |
_, buffer = cv2.imencode('.png', frame_rgb)
|
311 |
profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
|
312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
# Use global patient details
|
314 |
global current_patient_details
|
315 |
health_card_html = build_health_card(
|
|
|
319 |
current_patient_details['name'],
|
320 |
current_patient_details['age'],
|
321 |
current_patient_details['gender'],
|
322 |
+
current_patient_details['id']
|
|
|
323 |
)
|
324 |
|
325 |
+
# Generate PDF and return for download
|
326 |
+
pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
327 |
+
pdf_result, pdf_filepath = save_results_to_pdf(test_results, pdf_filename)
|
328 |
+
|
329 |
+
if pdf_filepath:
|
330 |
+
# Copy the PDF to a temporary directory for Gradio to serve it
|
331 |
+
temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)
|
332 |
+
shutil.copy(pdf_filepath, temp_pdf_path)
|
333 |
+
|
334 |
+
return health_card_html, temp_pdf_path
|
335 |
+
|
336 |
+
|
337 |
+
# Modified route_inputs function
|
338 |
+
def route_inputs(mode, image, video, patient_name, patient_age, patient_gender, patient_id):
|
339 |
+
if mode == "Image" and image is None:
|
340 |
+
return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
|
341 |
+
if mode == "Video" and video is None:
|
342 |
+
return "<div style='color:red;'>⚠️ Error: No video provided.</div>", None
|
343 |
+
|
344 |
+
# Store patient details globally for use in analyze_face
|
345 |
+
global current_patient_details
|
346 |
+
current_patient_details = {
|
347 |
+
'name': patient_name,
|
348 |
+
'age': patient_age,
|
349 |
+
'gender': patient_gender,
|
350 |
+
'id': patient_id
|
351 |
+
}
|
352 |
+
|
353 |
+
health_card_html, pdf_file_path = analyze_face(image if mode == "Image" else video)
|
354 |
+
return health_card_html, pdf_file_path
|
355 |
|
356 |
|
357 |
# Create Gradio interface
|
|
|
382 |
outputs=[result_html, result_pdf])
|
383 |
|
384 |
# Launch Gradio for Replit
|
385 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|