Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ 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 |
|
@@ -33,7 +33,6 @@ def extract_features(image, landmarks):
|
|
33 |
|
34 |
return [red_percent, green_percent, blue_percent]
|
35 |
|
36 |
-
|
37 |
def train_model(output_range):
|
38 |
X = [[
|
39 |
random.uniform(0.2, 0.5),
|
@@ -55,8 +54,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 = {
|
@@ -81,7 +79,6 @@ models = {
|
|
81 |
"Temperature": train_model((97, 99))
|
82 |
}
|
83 |
|
84 |
-
|
85 |
# Helper function for risk level color coding
|
86 |
def get_risk_color(value, normal_range):
|
87 |
low, high = normal_range
|
@@ -92,7 +89,6 @@ def get_risk_color(value, normal_range):
|
|
92 |
else:
|
93 |
return ("Normal", "✅", "#d4edda")
|
94 |
|
95 |
-
|
96 |
# Function to build table for test results
|
97 |
def build_table(title, rows):
|
98 |
html = (
|
@@ -119,7 +115,6 @@ def build_table(title, rows):
|
|
119 |
html += '</tbody></table></div>'
|
120 |
return html
|
121 |
|
122 |
-
|
123 |
# Function to save the health report to PDF
|
124 |
def save_results_to_pdf(test_results, filename):
|
125 |
try:
|
@@ -164,7 +159,6 @@ def save_results_to_pdf(test_results, filename):
|
|
164 |
except Exception as e:
|
165 |
return f"Error saving PDF: {str(e)}", None
|
166 |
|
167 |
-
|
168 |
# Build health card layout
|
169 |
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
|
170 |
from datetime import datetime
|
@@ -220,7 +214,6 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
220 |
"""
|
221 |
return html
|
222 |
|
223 |
-
|
224 |
# Initialize global variable for patient details
|
225 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
226 |
|
@@ -245,8 +238,7 @@ def analyze_face(input_data):
|
|
245 |
result = face_mesh.process(frame_rgb)
|
246 |
if not result.multi_face_landmarks:
|
247 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
248 |
-
landmarks = result.multi_face_landmarks[
|
249 |
-
0].landmark # Fixed: Use integer index
|
250 |
features = extract_features(frame_rgb, landmarks)
|
251 |
test_values = {}
|
252 |
r2_scores = {}
|
@@ -257,8 +249,7 @@ def analyze_face(input_data):
|
|
257 |
test_values[label] = prediction
|
258 |
r2_scores[label] = 0.385
|
259 |
else:
|
260 |
-
value = models[label].predict(
|
261 |
-
[[random.uniform(0.2, 0.5) for _ in range(7)]])[0]
|
262 |
test_values[label] = value
|
263 |
r2_scores[label] = 0.0
|
264 |
|
|
|
10 |
from datetime import datetime
|
11 |
import shutil
|
12 |
from reportlab.lib.pagesizes import letter
|
13 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
14 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
15 |
from reportlab.lib import colors
|
16 |
|
|
|
33 |
|
34 |
return [red_percent, green_percent, blue_percent]
|
35 |
|
|
|
36 |
def train_model(output_range):
|
37 |
X = [[
|
38 |
random.uniform(0.2, 0.5),
|
|
|
54 |
spo2_model = joblib.load("spo2_model_simulated.pkl")
|
55 |
hr_model = joblib.load("heart_rate_model.pkl")
|
56 |
except FileNotFoundError:
|
57 |
+
print("Error: One or more .pkl model files are missing. Please upload them.")
|
|
|
58 |
exit(1)
|
59 |
|
60 |
models = {
|
|
|
79 |
"Temperature": train_model((97, 99))
|
80 |
}
|
81 |
|
|
|
82 |
# Helper function for risk level color coding
|
83 |
def get_risk_color(value, normal_range):
|
84 |
low, high = normal_range
|
|
|
89 |
else:
|
90 |
return ("Normal", "✅", "#d4edda")
|
91 |
|
|
|
92 |
# Function to build table for test results
|
93 |
def build_table(title, rows):
|
94 |
html = (
|
|
|
115 |
html += '</tbody></table></div>'
|
116 |
return html
|
117 |
|
|
|
118 |
# Function to save the health report to PDF
|
119 |
def save_results_to_pdf(test_results, filename):
|
120 |
try:
|
|
|
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
|
|
|
214 |
"""
|
215 |
return html
|
216 |
|
|
|
217 |
# Initialize global variable for patient details
|
218 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
219 |
|
|
|
238 |
result = face_mesh.process(frame_rgb)
|
239 |
if not result.multi_face_landmarks:
|
240 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
241 |
+
landmarks = result.multi_face_landmarks[0].landmark # Fixed: Use integer index
|
|
|
242 |
features = extract_features(frame_rgb, landmarks)
|
243 |
test_values = {}
|
244 |
r2_scores = {}
|
|
|
249 |
test_values[label] = prediction
|
250 |
r2_scores[label] = 0.385
|
251 |
else:
|
252 |
+
value = models[label].predict([[random.uniform(0.2, 0.5) for _ in range(7)]])[0]
|
|
|
253 |
test_values[label] = value
|
254 |
r2_scores[label] = 0.0
|
255 |
|