Rammohan0504 commited on
Commit
04a7bf8
·
verified ·
1 Parent(s): cfa3c70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -98,7 +98,6 @@ def get_risk_color(value, normal_range):
98
 
99
  # Function to build table for test results
100
  def build_table(title, rows):
101
- # Define color mapping
102
  color_map = {
103
  "Normal": "#28a745",
104
  "High": "#dc3545",
@@ -129,7 +128,7 @@ def build_table(title, rows):
129
  return html
130
 
131
  # Function to save the health report to PDF
132
- def save_results_to_pdf(test_results, filename):
133
  try:
134
  doc = SimpleDocTemplate(filename, pagesize=letter)
135
  styles = getSampleStyleSheet()
@@ -153,7 +152,8 @@ def save_results_to_pdf(test_results, filename):
153
 
154
  flowables = [Paragraph("Health Report", title_style), Spacer(1, 12)]
155
 
156
- test_values = {
 
157
  "Hemoglobin": (13.5, 17.5),
158
  "WBC Count": (4.0, 11.0),
159
  "Platelet Count": (150, 450),
@@ -172,14 +172,15 @@ def save_results_to_pdf(test_results, filename):
172
  "BP Systolic": (90, 120),
173
  "BP Diastolic": (60, 80)
174
  }
 
175
  for section_name, html in test_results.items():
176
  flowables.append(Paragraph(section_name, styles['Heading2']))
177
  table_data = [["Test", "Result", "Range", "Level"]]
178
- for label, value in test_values.items():
179
  if any(label in html for section_html in test_results.values()):
180
- simulated_value = test_values[label][0] + random.uniform(-1, 1)
181
- level, _, _ = get_risk_color(simulated_value, value)
182
- table_data.append([label, f"{simulated_value:.2f}", f"{value[0]} - {value[1]}", level])
183
  table = Table(table_data)
184
  table.setStyle(TableStyle([
185
  ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
@@ -206,13 +207,6 @@ def build_health_card(profile_image, test_results, summary, pdf_filepath, patien
206
 
207
  pdf_filename = os.path.basename(pdf_filepath) if pdf_filepath else "health_report.pdf"
208
 
209
- # Define color mapping for consistency
210
- color_map = {
211
- "Normal": "#28a745",
212
- "High": "#dc3545",
213
- "Low": "#ffc107"
214
- }
215
-
216
  html = f"""
217
  <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;">
218
 
@@ -252,9 +246,10 @@ def build_health_card(profile_image, test_results, summary, pdf_filepath, patien
252
  </div>
253
 
254
  <div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
255
- <a href="/file=/tmp/{pdf_filename}" download="{pdf_filename}" 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; text-decoration: none;">
 
256
  📥 Download Report
257
- </a>
258
  <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);">
259
  📞 Find Labs Near Me
260
  </button>
@@ -319,6 +314,12 @@ def analyze_face(input_data):
319
  spo2 = spo2_model.predict([spo2_features])[0]
320
  rr = int(12 + abs(heart_rate % 5 - 2))
321
 
 
 
 
 
 
 
322
  test_results = {
323
  "Hematology": build_table("🩸 Hematology", [("Hemoglobin", test_values["Hemoglobin"], (13.5, 17.5)),
324
  ("WBC Count", test_values["WBC Count"], (4.0, 11.0)),
@@ -331,9 +332,9 @@ def analyze_face(input_data):
331
  ("Urea", test_values["Urea"], (7, 20))]),
332
  "Electrolytes": build_table("🧪 Electrolytes", [("Sodium", test_values["Sodium"], (135, 145)),
333
  ("Potassium", test_values["Potassium"], (3.5, 5.1))]),
334
- "Vitals": build_table("❤️ Vitals", [("SpO2", spo2, (95, 100)),
335
- ("Heart Rate", heart_rate, (60, 100)),
336
- ("Respiratory Rate", rr, (12, 20)),
337
  ("Temperature", test_values["Temperature"], (97, 99)),
338
  ("BP Systolic", test_values["BP Systolic"], (90, 120)),
339
  ("BP Diastolic", test_values["BP Diastolic"], (60, 80))])
@@ -345,7 +346,7 @@ def analyze_face(input_data):
345
  profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
346
 
347
  pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
348
- pdf_result, pdf_filepath = save_results_to_pdf(test_results, pdf_filename)
349
 
350
  if pdf_filepath:
351
  temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)
 
98
 
99
  # Function to build table for test results
100
  def build_table(title, rows):
 
101
  color_map = {
102
  "Normal": "#28a745",
103
  "High": "#dc3545",
 
128
  return html
129
 
130
  # Function to save the health report to PDF
131
+ def save_results_to_pdf(test_results, test_values, filename):
132
  try:
133
  doc = SimpleDocTemplate(filename, pagesize=letter)
134
  styles = getSampleStyleSheet()
 
152
 
153
  flowables = [Paragraph("Health Report", title_style), Spacer(1, 12)]
154
 
155
+ # Define ranges for all tests
156
+ test_ranges = {
157
  "Hemoglobin": (13.5, 17.5),
158
  "WBC Count": (4.0, 11.0),
159
  "Platelet Count": (150, 450),
 
172
  "BP Systolic": (90, 120),
173
  "BP Diastolic": (60, 80)
174
  }
175
+
176
  for section_name, html in test_results.items():
177
  flowables.append(Paragraph(section_name, styles['Heading2']))
178
  table_data = [["Test", "Result", "Range", "Level"]]
179
+ for label in test_ranges:
180
  if any(label in html for section_html in test_results.values()):
181
+ value = test_values.get(label, test_ranges[label][0] + random.uniform(-1, 1)) # Use actual value or fallback
182
+ level, _, _ = get_risk_color(value, test_ranges[label])
183
+ table_data.append([label, f"{value:.2f}" if label not in ["WBC Count", "Platelet Count"] else f"{value:.0f}", f"{test_ranges[label][0]} - {test_ranges[label][1]}", level])
184
  table = Table(table_data)
185
  table.setStyle(TableStyle([
186
  ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
 
207
 
208
  pdf_filename = os.path.basename(pdf_filepath) if pdf_filepath else "health_report.pdf"
209
 
 
 
 
 
 
 
 
210
  html = f"""
211
  <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;">
212
 
 
246
  </div>
247
 
248
  <div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
249
+ <!-- Disabled the direct download link to rely on Gradio File component -->
250
+ <button disabled style="padding: 12px 24px; background: #ccc; color: white; border: none; border-radius: 8px; cursor: not-allowed; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);">
251
  📥 Download Report
252
+ </button>
253
  <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);">
254
  📞 Find Labs Near Me
255
  </button>
 
314
  spo2 = spo2_model.predict([spo2_features])[0]
315
  rr = int(12 + abs(heart_rate % 5 - 2))
316
 
317
+ test_values.update({
318
+ "SpO2": spo2,
319
+ "Heart Rate": heart_rate,
320
+ "Respiratory Rate": rr
321
+ })
322
+
323
  test_results = {
324
  "Hematology": build_table("🩸 Hematology", [("Hemoglobin", test_values["Hemoglobin"], (13.5, 17.5)),
325
  ("WBC Count", test_values["WBC Count"], (4.0, 11.0)),
 
332
  ("Urea", test_values["Urea"], (7, 20))]),
333
  "Electrolytes": build_table("🧪 Electrolytes", [("Sodium", test_values["Sodium"], (135, 145)),
334
  ("Potassium", test_values["Potassium"], (3.5, 5.1))]),
335
+ "Vitals": build_table("❤️ Vitals", [("SpO2", test_values["SpO2"], (95, 100)),
336
+ ("Heart Rate", test_values["Heart Rate"], (60, 100)),
337
+ ("Respiratory Rate", test_values["Respiratory Rate"], (12, 20)),
338
  ("Temperature", test_values["Temperature"], (97, 99)),
339
  ("BP Systolic", test_values["BP Systolic"], (90, 120)),
340
  ("BP Diastolic", test_values["BP Diastolic"], (60, 80))])
 
346
  profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
347
 
348
  pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
349
+ pdf_result, pdf_filepath = save_results_to_pdf(test_results, test_values, pdf_filename)
350
 
351
  if pdf_filepath:
352
  temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)