Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from sklearn.linear_model import LinearRegression
|
|
6 |
import random
|
7 |
import base64
|
8 |
import joblib
|
|
|
9 |
from reportlab.lib.pagesizes import letter
|
10 |
from reportlab.pdfgen import canvas
|
11 |
from io import BytesIO
|
@@ -17,7 +18,6 @@ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True,
|
|
17 |
refine_landmarks=True,
|
18 |
min_detection_confidence=0.5)
|
19 |
|
20 |
-
|
21 |
# Functions for feature extraction
|
22 |
def extract_features(image, landmarks):
|
23 |
red_channel = image[:, :, 2]
|
@@ -117,7 +117,87 @@ def build_table(title, rows):
|
|
117 |
return html
|
118 |
|
119 |
|
120 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
def generate_pdf(html_content):
|
122 |
buffer = BytesIO()
|
123 |
c = canvas.Canvas(buffer, pagesize=letter)
|
@@ -160,12 +240,16 @@ def analyze_face(input_data):
|
|
160 |
landmarks = result.multi_face_landmarks[
|
161 |
0].landmark # Fixed: Use integer index
|
162 |
features = extract_features(frame_rgb, landmarks)
|
|
|
|
|
|
|
|
|
163 |
test_values = {}
|
164 |
r2_scores = {}
|
165 |
|
166 |
for label in models:
|
167 |
if label == "Hemoglobin":
|
168 |
-
prediction = models[label].predict(
|
169 |
test_values[label] = prediction
|
170 |
r2_scores[label] = 0.385
|
171 |
else:
|
|
|
6 |
import random
|
7 |
import base64
|
8 |
import joblib
|
9 |
+
import pandas as pd
|
10 |
from reportlab.lib.pagesizes import letter
|
11 |
from reportlab.pdfgen import canvas
|
12 |
from io import BytesIO
|
|
|
18 |
refine_landmarks=True,
|
19 |
min_detection_confidence=0.5)
|
20 |
|
|
|
21 |
# Functions for feature extraction
|
22 |
def extract_features(image, landmarks):
|
23 |
red_channel = image[:, :, 2]
|
|
|
117 |
return html
|
118 |
|
119 |
|
120 |
+
# Build health card layout
|
121 |
+
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
|
122 |
+
from datetime import datetime
|
123 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
124 |
+
|
125 |
+
html = f"""
|
126 |
+
<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;">
|
127 |
+
|
128 |
+
<div style="background-color: rgba(255, 255, 255, 0.9); border-radius: 12px; padding: 20px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
|
129 |
+
<div style="display: flex; align-items: center; margin-bottom: 15px;">
|
130 |
+
<div style="background: linear-gradient(135deg, #64b5f6, #42a5f5); padding: 8px 16px; border-radius: 8px; margin-right: 20px;">
|
131 |
+
<h3 style="margin: 0; font-size: 16px; color: white; font-weight: 600;">HEALTH CARD</h3>
|
132 |
+
</div>
|
133 |
+
<div style="margin-left: auto; text-align: right; color: #666; font-size: 12px;">
|
134 |
+
<div>Report Date: {current_date}</div>
|
135 |
+
{f'<div>Patient ID: {patient_id}</div>' if patient_id else ''}
|
136 |
+
</div>
|
137 |
+
</div>
|
138 |
+
<div style="display: flex; align-items: center;">
|
139 |
+
<img src="data:image/png;base64,{profile_image}" alt="Profile" style="width: 90px; height: 90px; border-radius: 50%; margin-right: 20px; border: 3px solid #fff; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">
|
140 |
+
<div>
|
141 |
+
<h2 style="margin: 0; font-size: 28px; color: #2c3e50; font-weight: 700;">{patient_name if patient_name else "Lab Test Results"}</h2>
|
142 |
+
<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>
|
143 |
+
<p style="margin: 4px 0 0 0; color: #888; font-size: 12px;">Face-Based Health Analysis Report</p>
|
144 |
+
</div>
|
145 |
+
</div>
|
146 |
+
</div>
|
147 |
+
|
148 |
+
<div style="background-color: rgba(255, 255, 255, 0.95); border-radius: 12px; padding: 25px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
|
149 |
+
{test_results['Hematology']}
|
150 |
+
{test_results['Iron Panel']}
|
151 |
+
{test_results['Liver & Kidney']}
|
152 |
+
{test_results['Electrolytes']}
|
153 |
+
{test_results['Vitals']}
|
154 |
+
</div>
|
155 |
+
|
156 |
+
<div style="background-color: rgba(255, 255, 255, 0.95); padding: 20px; border-radius: 12px; border: 1px solid #e0e0e0; margin-bottom: 25px;">
|
157 |
+
<h4 style="margin: 0 0 15px 0; color: #2c3e50; font-size: 18px; font-weight: 600;">📝 Summary & Recommendations</h4>
|
158 |
+
<div style="color: #444; line-height: 1.6;">
|
159 |
+
{summary}
|
160 |
+
</div>
|
161 |
+
</div>
|
162 |
+
|
163 |
+
<div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
|
164 |
+
<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;">
|
165 |
+
📥 Download Report
|
166 |
+
</button>
|
167 |
+
<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);">
|
168 |
+
📞 Find Labs Near Me
|
169 |
+
</button>
|
170 |
+
</div>
|
171 |
+
</div>
|
172 |
+
|
173 |
+
<style>
|
174 |
+
@media print {{
|
175 |
+
body * {{
|
176 |
+
visibility: hidden;
|
177 |
+
}}
|
178 |
+
#health-card, #health-card * {{
|
179 |
+
visibility: visible;
|
180 |
+
}}
|
181 |
+
#health-card {{
|
182 |
+
position: absolute;
|
183 |
+
left: 0;
|
184 |
+
top: 0;
|
185 |
+
width: 100% !important;
|
186 |
+
max-width: none !important;
|
187 |
+
margin: 0 !important;
|
188 |
+
box-shadow: none !important;
|
189 |
+
border: none !important;
|
190 |
+
}}
|
191 |
+
button {{
|
192 |
+
display: none !important;
|
193 |
+
}}
|
194 |
+
}}
|
195 |
+
</style>
|
196 |
+
"""
|
197 |
+
return html
|
198 |
+
|
199 |
+
|
200 |
+
# Function to generate PDF from HTML content using reportlab
|
201 |
def generate_pdf(html_content):
|
202 |
buffer = BytesIO()
|
203 |
c = canvas.Canvas(buffer, pagesize=letter)
|
|
|
240 |
landmarks = result.multi_face_landmarks[
|
241 |
0].landmark # Fixed: Use integer index
|
242 |
features = extract_features(frame_rgb, landmarks)
|
243 |
+
|
244 |
+
# Convert features to pandas DataFrame if the model was trained with column names
|
245 |
+
features_df = pd.DataFrame([features], columns=["feature1", "feature2", "feature3"])
|
246 |
+
|
247 |
test_values = {}
|
248 |
r2_scores = {}
|
249 |
|
250 |
for label in models:
|
251 |
if label == "Hemoglobin":
|
252 |
+
prediction = models[label].predict(features_df)[0]
|
253 |
test_values[label] = prediction
|
254 |
r2_scores[label] = 0.385
|
255 |
else:
|