SuriRaja commited on
Commit
accfefd
Β·
verified Β·
1 Parent(s): b0b5310

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # Face Detection-Based AI Automation of Lab Tests
2
- # Redesigned UI using Gradio Blocks + HTML Cards (Fixed .then() error)
3
 
4
  import gradio as gr
5
  import cv2
@@ -28,11 +28,22 @@ def estimate_spo2_rr(heart_rate):
28
  def get_risk_color(value, normal_range):
29
  low, high = normal_range
30
  if value < low:
31
- return ("πŸ”» LOW", "#FFCCCC")
32
  elif value > high:
33
- return ("πŸ”Ί HIGH", "#FFE680")
34
  else:
35
- return ("βœ… Normal", "#CCFFCC")
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def analyze_face(image):
38
  if image is None:
@@ -53,28 +64,18 @@ def analyze_face(image):
53
  tsh, cortisol = 2.5, 18
54
  fbs, hba1c = 120, 6.2
55
 
56
- def section(title, items):
57
- html = f'<div style="padding:10px;border:1px solid #ccc;border-radius:8px;margin-bottom:10px;background:#f8f9fa;">'
58
- html += f'<h4 style="margin:0 0 10px 0">{title}</h4>'
59
- for label, val, rng in items:
60
- status, bgcolor = get_risk_color(val, rng)
61
- html += f'<div style="padding:6px;margin-bottom:4px;background:{bgcolor};border-radius:4px;">{label}: {val} - {status}</div>'
62
- html += '</div>'
63
- return html
64
-
65
- report = "".join([
66
- section("🩸 Hematology", [("Hemoglobin", hb, (13.5, 17.5)), ("WBC Count", wbc, (4.0, 11.0)), ("Platelets", platelets, (150, 450))]),
67
- section("🧬 Iron & Liver Panel", [("Iron", iron, (60, 170)), ("Ferritin", ferritin, (30, 300)), ("TIBC", tibc, (250, 400)), ("Bilirubin", bilirubin, (0.3, 1.2))]),
68
- section("πŸ§ͺ Kidney, Thyroid & Stress", [("Creatinine", creatinine, (0.6, 1.2)), ("TSH", tsh, (0.4, 4.0)), ("Cortisol", cortisol, (5, 25))]),
69
- section("🧁 Metabolic Panel", [("Fasting Blood Sugar", fbs, (70, 110)), ("HbA1c", hba1c, (4.0, 5.7))]),
70
- section("❀️ Vital Signs", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20))])
71
  ])
72
 
73
- return report, frame_rgb
74
 
75
- # Gradio App Layout (Fixed HTML output handling)
76
- demo = gr.Blocks()
77
- with demo:
78
  gr.Markdown("""
79
  # 🧠 Face-Based Lab Test AI Report
80
  Upload a face photo to infer health diagnostics with AI-based visual markers.
@@ -85,13 +86,14 @@ with demo:
85
  image_input = gr.Image(type="numpy", label="πŸ“Έ Upload Face Image")
86
  submit_btn = gr.Button("πŸ” Analyze")
87
  with gr.Column(scale=2):
88
- result_html = gr.HTML(label="πŸ§ͺ Visual Diagnostic Cards")
89
  result_image = gr.Image(label="πŸ“· Face Scan Annotated")
90
 
91
- submit_btn.click(
92
- fn=analyze_face,
93
- inputs=image_input,
94
- outputs=[result_html, result_image]
95
- )
 
96
 
97
  demo.launch()
 
1
  # Face Detection-Based AI Automation of Lab Tests
2
+ # Redesigned UI using Clean Table Format for Results
3
 
4
  import gradio as gr
5
  import cv2
 
28
  def get_risk_color(value, normal_range):
29
  low, high = normal_range
30
  if value < low:
31
+ return ("Low", "πŸ”»", "#FFCCCC")
32
  elif value > high:
33
+ return ("High", "πŸ”Ί", "#FFE680")
34
  else:
35
+ return ("Normal", "βœ…", "#CCFFCC")
36
+
37
+ def build_table(title, rows):
38
+ html = f'<div style="margin-bottom: 24px;">
39
+ <h4 style="margin: 8px 0;">{title}</h4>
40
+ <table style="width:100%; border-collapse:collapse;">
41
+ <thead><tr style="background:#f0f0f0;"><th style="padding:8px;border:1px solid #ccc;">Test</th><th style="padding:8px;border:1px solid #ccc;">Result</th><th style="padding:8px;border:1px solid #ccc;">Expected Range</th><th style="padding:8px;border:1px solid #ccc;">Level</th></tr></thead><tbody>'
42
+ for label, value, ref in rows:
43
+ level, icon, bg = get_risk_color(value, ref)
44
+ html += f'<tr style="background:{bg};"><td style="padding:6px;border:1px solid #ccc;">{label}</td><td style="padding:6px;border:1px solid #ccc;">{value}</td><td style="padding:6px;border:1px solid #ccc;">{ref[0]} – {ref[1]}</td><td style="padding:6px;border:1px solid #ccc;">{icon} {level}</td></tr>'
45
+ html += '</tbody></table></div>'
46
+ return html
47
 
48
  def analyze_face(image):
49
  if image is None:
 
64
  tsh, cortisol = 2.5, 18
65
  fbs, hba1c = 120, 6.2
66
 
67
+ html_output = "".join([
68
+ build_table("🩸 Hematology", [("Hemoglobin", hb, (13.5, 17.5)), ("WBC Count", wbc, (4.0, 11.0)), ("Platelets", platelets, (150, 450))]),
69
+ build_table("🧬 Iron & Liver Panel", [("Iron", iron, (60, 170)), ("Ferritin", ferritin, (30, 300)), ("TIBC", tibc, (250, 400)), ("Bilirubin", bilirubin, (0.3, 1.2))]),
70
+ build_table("πŸ§ͺ Kidney, Thyroid & Stress", [("Creatinine", creatinine, (0.6, 1.2)), ("TSH", tsh, (0.4, 4.0)), ("Cortisol", cortisol, (5, 25))]),
71
+ build_table("🧁 Metabolic Panel", [("Fasting Blood Sugar", fbs, (70, 110)), ("HbA1c", hba1c, (4.0, 5.7))]),
72
+ build_table("❀️ Vital Signs", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20))])
 
 
 
 
 
 
 
 
 
73
  ])
74
 
75
+ return html_output, frame_rgb
76
 
77
+ # Gradio App Layout
78
+ with gr.Blocks() as demo:
 
79
  gr.Markdown("""
80
  # 🧠 Face-Based Lab Test AI Report
81
  Upload a face photo to infer health diagnostics with AI-based visual markers.
 
86
  image_input = gr.Image(type="numpy", label="πŸ“Έ Upload Face Image")
87
  submit_btn = gr.Button("πŸ” Analyze")
88
  with gr.Column(scale=2):
89
+ result_html = gr.HTML(label="πŸ§ͺ Health Report Table")
90
  result_image = gr.Image(label="πŸ“· Face Scan Annotated")
91
 
92
+ submit_btn.click(fn=analyze_face, inputs=image_input, outputs=[result_html, result_image])
93
+
94
+ gr.Markdown("""
95
+ ---
96
+ βœ… Table Format β€’ Color-coded Status β€’ Normal Range View
97
+ """)
98
 
99
  demo.launch()