Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,16 @@
|
|
1 |
-
import os
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
import mediapipe as mp
|
6 |
-
from sklearn.linear_model import LinearRegression
|
7 |
-
import random
|
8 |
-
import base64
|
9 |
-
import joblib
|
10 |
-
from datetime import datetime
|
11 |
-
import shutil
|
12 |
from reportlab.lib.pagesizes import letter
|
13 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
14 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
15 |
from reportlab.lib import colors
|
|
|
|
|
16 |
|
17 |
# Initialize the face mesh model
|
18 |
mp_face_mesh = mp.solutions.face_mesh
|
@@ -55,8 +53,7 @@ try:
|
|
55 |
spo2_model = joblib.load("spo2_model_simulated.pkl")
|
56 |
hr_model = joblib.load("heart_rate_model.pkl")
|
57 |
except FileNotFoundError:
|
58 |
-
print(
|
59 |
-
"Error: One or more .pkl model files are missing. Please upload them.")
|
60 |
exit(1)
|
61 |
|
62 |
models = {
|
@@ -120,11 +117,10 @@ def build_table(title, rows):
|
|
120 |
return html
|
121 |
|
122 |
|
123 |
-
# Function to save
|
124 |
-
def save_results_to_pdf(test_results,
|
125 |
try:
|
126 |
-
|
127 |
-
doc = SimpleDocTemplate(filename, pagesize=letter)
|
128 |
styles = getSampleStyleSheet()
|
129 |
|
130 |
# Define custom styles
|
@@ -148,19 +144,23 @@ def save_results_to_pdf(test_results, filename):
|
|
148 |
|
149 |
# Build the PDF content
|
150 |
flowables = []
|
151 |
-
|
152 |
-
#
|
153 |
-
flowables.append(Paragraph("
|
154 |
-
|
|
|
|
|
155 |
# Add test results to the report
|
156 |
-
for
|
157 |
-
|
158 |
-
|
|
|
|
|
159 |
flowables.append(Spacer(1, 12))
|
160 |
-
|
161 |
# Build the PDF
|
162 |
doc.build(flowables)
|
163 |
-
return f"PDF saved successfully as {
|
164 |
except Exception as e:
|
165 |
return f"Error saving PDF: {str(e)}", None
|
166 |
|
@@ -172,55 +172,30 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
172 |
|
173 |
html = f"""
|
174 |
<div id="health-card" style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 700px; margin: 20px auto; border-radius: 16px; background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%); border: 2px solid #ddd; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15); padding: 30px; color: #1a1a1a;">
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
<
|
179 |
-
|
180 |
-
</div>
|
181 |
-
<div style="margin-left: auto; text-align: right; color: #666; font-size: 12px;">
|
182 |
-
<div>Report Date: {current_date}</div>
|
183 |
-
{f'<div>Patient ID: {patient_id}</div>' if patient_id else ''}
|
184 |
-
</div>
|
185 |
</div>
|
186 |
-
<div
|
187 |
-
<img src="data:image/png;base64,{profile_image}"
|
188 |
-
<div>
|
189 |
-
<h2 style="margin: 0; font-size: 28px; color: #2c3e50; font-weight: 700;">{patient_name if patient_name else "Lab Test Results"}</h2>
|
190 |
-
<p style="margin: 4px 0 0 0; color: #666; font-size: 14px;">{f"Age: {patient_age} | Gender: {patient_gender}" if patient_age and patient_gender else "AI-Generated Health Analysis"}</p>
|
191 |
-
<p style="margin: 4px 0 0 0; color: #888; font-size: 12px;">Face-Based Health Analysis Report</p>
|
192 |
-
</div>
|
193 |
</div>
|
194 |
</div>
|
195 |
|
196 |
-
<div style="
|
197 |
-
{test_results
|
198 |
-
{test_results['Iron Panel']}
|
199 |
-
{test_results['Liver & Kidney']}
|
200 |
-
{test_results['Electrolytes']}
|
201 |
-
{test_results['Vitals']}
|
202 |
</div>
|
203 |
|
204 |
-
<div style="
|
205 |
-
<h4
|
206 |
-
<div
|
207 |
-
{summary}
|
208 |
-
</div>
|
209 |
</div>
|
210 |
|
211 |
-
<div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
|
212 |
-
<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;">
|
213 |
-
📥 Download Report
|
214 |
-
</button>
|
215 |
-
<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);">
|
216 |
-
📞 Find Labs Near Me
|
217 |
-
</button>
|
218 |
-
</div>
|
219 |
</div>
|
220 |
"""
|
221 |
return html
|
222 |
|
223 |
-
|
224 |
# Initialize global variable for patient details
|
225 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
226 |
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import base64
|
4 |
import gradio as gr
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
import mediapipe as mp
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from reportlab.lib.pagesizes import letter
|
9 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
|
10 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
11 |
from reportlab.lib import colors
|
12 |
+
from datetime import datetime
|
13 |
+
import joblib
|
14 |
|
15 |
# Initialize the face mesh model
|
16 |
mp_face_mesh = mp.solutions.face_mesh
|
|
|
53 |
spo2_model = joblib.load("spo2_model_simulated.pkl")
|
54 |
hr_model = joblib.load("heart_rate_model.pkl")
|
55 |
except FileNotFoundError:
|
56 |
+
print("Error: One or more .pkl model files are missing. Please upload them.")
|
|
|
57 |
exit(1)
|
58 |
|
59 |
models = {
|
|
|
117 |
return html
|
118 |
|
119 |
|
120 |
+
# Function to save health report as PDF
|
121 |
+
def save_results_to_pdf(patient_details, test_results, pdf_filename):
|
122 |
try:
|
123 |
+
doc = SimpleDocTemplate(pdf_filename, pagesize=letter)
|
|
|
124 |
styles = getSampleStyleSheet()
|
125 |
|
126 |
# Define custom styles
|
|
|
144 |
|
145 |
# Build the PDF content
|
146 |
flowables = []
|
147 |
+
|
148 |
+
# Patient details section
|
149 |
+
flowables.append(Paragraph(f"Patient: {patient_details['name']}", title_style))
|
150 |
+
flowables.append(Paragraph(f"Age: {patient_details['age']} | Gender: {patient_details['gender']}", body_style))
|
151 |
+
flowables.append(Spacer(1, 12))
|
152 |
+
|
153 |
# Add test results to the report
|
154 |
+
for section, data in test_results.items():
|
155 |
+
flowables.append(Paragraph(f"<u>{section}</u>", title_style))
|
156 |
+
for test, result in data.items():
|
157 |
+
flowables.append(Paragraph(f"{test}: {result}", body_style))
|
158 |
+
flowables.append(Spacer(1, 6))
|
159 |
flowables.append(Spacer(1, 12))
|
160 |
+
|
161 |
# Build the PDF
|
162 |
doc.build(flowables)
|
163 |
+
return f"PDF saved successfully as {pdf_filename}", pdf_filename
|
164 |
except Exception as e:
|
165 |
return f"Error saving PDF: {str(e)}", None
|
166 |
|
|
|
172 |
|
173 |
html = f"""
|
174 |
<div id="health-card" style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 700px; margin: 20px auto; border-radius: 16px; background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%); border: 2px solid #ddd; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15); padding: 30px; color: #1a1a1a;">
|
175 |
+
<div style="display: flex; justify-content: space-between;">
|
176 |
+
<div>
|
177 |
+
<h2>{patient_name}</h2>
|
178 |
+
<p>Age: {patient_age} | Gender: {patient_gender}</p>
|
179 |
+
<p>Report Date: {current_date}</p>
|
|
|
|
|
|
|
|
|
|
|
180 |
</div>
|
181 |
+
<div>
|
182 |
+
<img src="data:image/png;base64,{profile_image}" style="width: 120px; height: 120px; border-radius: 50%;" />
|
|
|
|
|
|
|
|
|
|
|
183 |
</div>
|
184 |
</div>
|
185 |
|
186 |
+
<div style="margin-top: 20px;">
|
187 |
+
{test_results}
|
|
|
|
|
|
|
|
|
188 |
</div>
|
189 |
|
190 |
+
<div style="margin-top: 30px;">
|
191 |
+
<h4>📝 Summary & Recommendations</h4>
|
192 |
+
<div>{summary}</div>
|
|
|
|
|
193 |
</div>
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
</div>
|
196 |
"""
|
197 |
return html
|
198 |
|
|
|
199 |
# Initialize global variable for patient details
|
200 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
201 |
|