SuriRaja commited on
Commit
1ccaea2
·
verified ·
1 Parent(s): 9ff4823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -78
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # Face Detection-Based AI Automation of Lab Tests
2
- # Gradio App - Stable Deployment Version for Hugging Face
3
 
4
  import gradio as gr
5
  import cv2
@@ -28,51 +28,20 @@ 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"
32
  elif value > high:
33
- return "🔺 HIGH"
34
  else:
35
- return "✅ Normal"
36
-
37
- def generate_flags_extended(params):
38
- hb, wbc, platelets, iron, ferritin, tibc, bilirubin, creatinine, tsh, cortisol, fbs, hba1c = params
39
- flags = []
40
- if hb < 13.5:
41
- flags.append("Hemoglobin Low - Possible Anemia")
42
- if wbc < 4.0 or wbc > 11.0:
43
- flags.append("Abnormal WBC Count - Possible Infection")
44
- if platelets < 150:
45
- flags.append("Platelet Drop Risk - Bruising Possible")
46
- if iron < 60:
47
- flags.append("Iron Deficiency Detected")
48
- if ferritin < 30:
49
- flags.append("Low Ferritin - Iron Store Low")
50
- if tibc > 400:
51
- flags.append("High TIBC - Iron Absorption Issue")
52
- if bilirubin > 1.2:
53
- flags.append("Jaundice Detected - Elevated Bilirubin")
54
- if creatinine > 1.2:
55
- flags.append("Kidney Function Concern - High Creatinine")
56
- if tsh < 0.4 or tsh > 4.0:
57
- flags.append("Thyroid Imbalance - Check TSH")
58
- if cortisol < 5 or cortisol > 25:
59
- flags.append("Stress Hormone Abnormality - Cortisol")
60
- if fbs > 110:
61
- flags.append("High Fasting Blood Sugar")
62
- if hba1c > 5.7:
63
- flags.append("Elevated HbA1c - Diabetes Risk")
64
- flags.append("Mood / Stress analysis requires separate behavioral model")
65
- return flags
66
 
67
  def analyze_face(image):
68
  if image is None:
69
- return "⚠️ Error: No image provided.", None
70
 
71
  frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
72
  result = face_mesh.process(frame_rgb)
73
-
74
  if not result.multi_face_landmarks:
75
- return "⚠️ Error: Face not detected.", None
76
 
77
  landmarks = result.multi_face_landmarks[0].landmark
78
  heart_rate = estimate_heart_rate(frame_rgb, landmarks)
@@ -84,46 +53,71 @@ def analyze_face(image):
84
  tsh, cortisol = 2.5, 18
85
  fbs, hba1c = 120, 6.2
86
 
87
- flags = generate_flags_extended([hb, wbc, platelets, iron, ferritin, tibc, bilirubin, creatinine, tsh, cortisol, fbs, hba1c])
88
-
89
- report_lines = [
90
- "### 🩸 Hematology",
91
- f"- Hemoglobin (Hb): {hb} g/dL - {get_risk_color(hb, (13.5, 17.5))}",
92
- f"- WBC Count: {wbc} x10^3/uL - {get_risk_color(wbc, (4.0, 11.0))}",
93
- f"- Platelet Count: {platelets} x10^3/uL - {get_risk_color(platelets, (150, 450))}",
94
-
95
- "### 🧬 Iron & Liver Panel",
96
- f"- Iron: {iron} µg/dL - {get_risk_color(iron, (60, 170))}",
97
- f"- Ferritin: {ferritin} ng/mL - {get_risk_color(ferritin, (30, 300))}",
98
- f"- TIBC: {tibc} µg/dL - {get_risk_color(tibc, (250, 400))}",
99
- f"- Bilirubin: {bilirubin} mg/dL - {get_risk_color(bilirubin, (0.3, 1.2))}",
100
-
101
- "### 🧪 Kidney, Thyroid & Stress",
102
- f"- Creatinine: {creatinine} mg/dL - {get_risk_color(creatinine, (0.6, 1.2))}",
103
- f"- TSH: {tsh} µIU/mL - {get_risk_color(tsh, (0.4, 4.0))}",
104
- f"- Cortisol: {cortisol} µg/dL - {get_risk_color(cortisol, (5, 25))}",
105
-
106
- "### 🧁 Metabolic Panel",
107
- f"- Fasting Blood Sugar: {fbs} mg/dL - {get_risk_color(fbs, (70, 110))}",
108
- f"- HbA1c: {hba1c}% - {get_risk_color(hba1c, (4.0, 5.7))}",
109
-
110
- "### ❤️ Vital Signs",
111
- f"- SpO2: {spo2}% - {get_risk_color(spo2, (95, 100))}",
112
- f"- Heart Rate: {heart_rate} bpm - {get_risk_color(heart_rate, (60, 100))}",
113
- f"- Respiratory Rate: {rr} breaths/min - {get_risk_color(rr, (12, 20))}",
114
- "- Blood Pressure: Low (simulated)",
115
-
116
- "### ⚠️ Risk Flags"
117
- ] + [f"- {flag}" for flag in flags]
118
-
119
- return "\n".join(report_lines), frame_rgb
120
-
121
- demo = gr.Interface(
122
- fn=analyze_face,
123
- inputs=gr.Image(type="numpy", label="📸 Upload Face Image"),
124
- outputs=[gr.Markdown(label="🧪 Diagnostic Report"), gr.Image(label="🧍 Annotated Face")],
125
- title="Face-Based AI Lab Test Inference",
126
- description="Upload a clear face image to get lab test estimates and vital signs via facial analysis."
127
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  demo.launch()
 
1
  # Face Detection-Based AI Automation of Lab Tests
2
+ # Redesigned UI using Gradio Blocks + HTML Cards
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") # Red background
32
  elif value > high:
33
+ return ("🔺 HIGH", "#FFE680") # Yellow background
34
  else:
35
+ return ("✅ Normal", "#CCFFCC") # Green background
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def analyze_face(image):
38
  if image is None:
39
+ return [], None
40
 
41
  frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
42
  result = face_mesh.process(frame_rgb)
 
43
  if not result.multi_face_landmarks:
44
+ return [["Face not detected", "#FFDDDD"]], None
45
 
46
  landmarks = result.multi_face_landmarks[0].landmark
47
  heart_rate = estimate_heart_rate(frame_rgb, landmarks)
 
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
+ cards = [
66
+ section("🩸 Hematology", [
67
+ ("Hemoglobin", hb, (13.5, 17.5)),
68
+ ("WBC Count", wbc, (4.0, 11.0)),
69
+ ("Platelets", platelets, (150, 450))
70
+ ]),
71
+ section("🧬 Iron & Liver Panel", [
72
+ ("Iron", iron, (60, 170)),
73
+ ("Ferritin", ferritin, (30, 300)),
74
+ ("TIBC", tibc, (250, 400)),
75
+ ("Bilirubin", bilirubin, (0.3, 1.2))
76
+ ]),
77
+ section("🧪 Kidney, Thyroid & Stress", [
78
+ ("Creatinine", creatinine, (0.6, 1.2)),
79
+ ("TSH", tsh, (0.4, 4.0)),
80
+ ("Cortisol", cortisol, (5, 25))
81
+ ]),
82
+ section("🧁 Metabolic Panel", [
83
+ ("Fasting Blood Sugar", fbs, (70, 110)),
84
+ ("HbA1c", hba1c, (4.0, 5.7))
85
+ ]),
86
+ section("❤️ Vital Signs", [
87
+ ("SpO2", spo2, (95, 100)),
88
+ ("Heart Rate", heart_rate, (60, 100)),
89
+ ("Respiratory Rate", rr, (12, 20))
90
+ ])
91
+ ]
92
+ return cards, frame_rgb
93
+
94
+ # Gradio App Layout (Custom UI with Cards)
95
+ demo = gr.Blocks()
96
+ with demo:
97
+ gr.Markdown("""
98
+ # 🧠 Face-Based Lab Test AI Report
99
+ Upload a face photo to infer health diagnostics with AI-based visual markers.
100
+ """)
101
+
102
+ with gr.Row():
103
+ with gr.Column(scale=1):
104
+ image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
105
+ submit_btn = gr.Button("🔍 Analyze")
106
+ with gr.Column(scale=2):
107
+ result_html = gr.HTML(label="🧪 Visual Diagnostic Cards")
108
+ result_image = gr.Image(label="📷 Face Scan Annotated")
109
+
110
+ def format_html(cards):
111
+ return "".join(cards)
112
+
113
+ submit_btn.click(
114
+ fn=analyze_face,
115
+ inputs=image_input,
116
+ outputs=[result_html, result_image]
117
+ ).then(
118
+ fn=format_html,
119
+ inputs=None,
120
+ outputs=result_html
121
+ )
122
 
123
  demo.launch()