SuriRaja commited on
Commit
2306344
·
verified ·
1 Parent(s): 7e2c1f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -92
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # Face Detection-Based AI Automation of Lab Tests
2
- # Gradio App with Mobile-Responsive UI and Risk-Level Coloring
3
 
4
  import gradio as gr
5
  import cv2
@@ -66,100 +66,64 @@ def generate_flags_extended(params):
66
 
67
  def analyze_face(image):
68
  if image is None:
69
- return {}, None
70
 
71
  frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
72
  result = face_mesh.process(frame_rgb)
73
 
74
- if result.multi_face_landmarks:
75
- landmarks = result.multi_face_landmarks[0].landmark
76
- heart_rate = estimate_heart_rate(frame_rgb, landmarks)
77
- spo2, rr = estimate_spo2_rr(heart_rate)
78
-
79
- hb, wbc, platelets = 12.3, 6.4, 210
80
- iron, ferritin, tibc = 55, 45, 340
81
- bilirubin, creatinine = 1.5, 1.3
82
- tsh, cortisol = 2.5, 18
83
- fbs, hba1c = 120, 6.2
84
-
85
- flags = generate_flags_extended([hb, wbc, platelets, iron, ferritin, tibc, bilirubin, creatinine, tsh, cortisol, fbs, hba1c])
86
-
87
- sections = {
88
- "🩸 Hematology": [
89
- f"Hemoglobin (Hb): {hb} g/dL - {get_risk_color(hb, (13.5, 17.5))}",
90
- f"WBC Count: {wbc} x10^3/uL - {get_risk_color(wbc, (4.0, 11.0))}",
91
- f"Platelet Count: {platelets} x10^3/uL - {get_risk_color(platelets, (150, 450))}"
92
- ],
93
- "🧬 Iron & Liver Panel": [
94
- f"Iron: {iron} µg/dL - {get_risk_color(iron, (60, 170))}",
95
- f"Ferritin: {ferritin} ng/mL - {get_risk_color(ferritin, (30, 300))}",
96
- f"TIBC: {tibc} µg/dL - {get_risk_color(tibc, (250, 400))}",
97
- f"Bilirubin: {bilirubin} mg/dL - {get_risk_color(bilirubin, (0.3, 1.2))}"
98
- ],
99
- "🧪 Kidney, Thyroid & Stress": [
100
- f"Creatinine: {creatinine} mg/dL - {get_risk_color(creatinine, (0.6, 1.2))}",
101
- f"TSH: {tsh} µIU/mL - {get_risk_color(tsh, (0.4, 4.0))}",
102
- f"Cortisol: {cortisol} µg/dL - {get_risk_color(cortisol, (5, 25))}"
103
- ],
104
- "🧁 Metabolic Panel": [
105
- f"Fasting Blood Sugar: {fbs} mg/dL - {get_risk_color(fbs, (70, 110))}",
106
- f"HbA1c: {hba1c}% - {get_risk_color(hba1c, (4.0, 5.7))}"
107
- ],
108
- "❤️ Vital Signs": [
109
- f"SpO2: {spo2}% - {get_risk_color(spo2, (95, 100))}",
110
- f"Heart Rate: {heart_rate} bpm - {get_risk_color(heart_rate, (60, 100))}",
111
- f"Respiratory Rate: {rr} breaths/min - {get_risk_color(rr, (12, 20))}",
112
- "Blood Pressure: Low (simulated)"
113
- ],
114
- "⚠️ Risk Flags": flags
115
- }
116
- return sections, frame_rgb
117
- else:
118
- return {"⚠️ Error": ["Face not detected"]}, None
119
-
120
- # Mobile-optimized UI with styled labels
121
- demo = gr.Blocks(css="""
122
- @media only screen and (max-width: 768px) {
123
- .gr-block.gr-column { width: 100% !important; }
124
- }
125
- """)
126
- with demo:
127
- gr.Markdown("""
128
- # 🧠 Face-Based AI Lab Test Inference
129
- Upload a clear face image to simulate categorized lab reports with visual grouping.
130
- """)
131
-
132
- with gr.Row():
133
- with gr.Column(scale=1):
134
- image_input = gr.Image(type="numpy", label="📸 Upload a Face Image")
135
- submit_btn = gr.Button("🔍 Analyze Now")
136
- with gr.Column(scale=2):
137
- accordion_output = gr.Accordion("📂 Diagnostic Summary", open=True)
138
- with accordion_output:
139
- result_html = gr.HighlightedText(label="📊 Grouped Report", combine_adjacent=True)
140
- result_image = gr.Image(label="🧍 Annotated Face Scan")
141
-
142
- def format_report(sections):
143
- lines = []
144
- for title, values in sections.items():
145
- lines.append((f"{title}",))
146
- for item in values:
147
- lines.append((f" - {item}",))
148
- return lines
149
-
150
- submit_btn.click(
151
- fn=analyze_face,
152
- inputs=image_input,
153
- outputs=[result_html, result_image],
154
- preprocess=False,
155
- postprocess=False,
156
- _js="(data) => [data]"
157
- ).then(
158
- fn=format_report,
159
- inputs=None,
160
- outputs=result_html
161
- )
162
-
163
- gr.Markdown("---\n✅ Optimized for Mobile · Risk Indicators: 🔻 Low, 🔺 High, ✅ Normal")
164
 
165
  demo.launch()
 
1
  # Face Detection-Based AI Automation of Lab Tests
2
+ # Gradio App - Cleaned for Hugging Face Compatibility
3
 
4
  import gradio as gr
5
  import cv2
 
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)
79
+ spo2, rr = estimate_spo2_rr(heart_rate)
80
+
81
+ hb, wbc, platelets = 12.3, 6.4, 210
82
+ iron, ferritin, tibc = 55, 45, 340
83
+ bilirubin, creatinine = 1.5, 1.3
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 = f"""
90
+ ### 🩸 Hematology
91
+ - Hemoglobin (Hb): {hb} g/dL - {get_risk_color(hb, (13.5, 17.5))}
92
+ - WBC Count: {wbc} x10^3/uL - {get_risk_color(wbc, (4.0, 11.0))}
93
+ - Platelet Count: {platelets} x10^3/uL - {get_risk_color(platelets, (150, 450))}
94
+
95
+ ### 🧬 Iron & Liver Panel
96
+ - Iron: {iron} µg/dL - {get_risk_color(iron, (60, 170))}
97
+ - Ferritin: {ferritin} ng/mL - {get_risk_color(ferritin, (30, 300))}
98
+ - TIBC: {tibc} µg/dL - {get_risk_color(tibc, (250, 400))}
99
+ - Bilirubin: {bilirubin} mg/dL - {get_risk_color(bilirubin, (0.3, 1.2))}
100
+
101
+ ### 🧪 Kidney, Thyroid & Stress
102
+ - Creatinine: {creatinine} mg/dL - {get_risk_color(creatinine, (0.6, 1.2))}
103
+ - TSH: {tsh} µIU/mL - {get_risk_color(tsh, (0.4, 4.0))}
104
+ - Cortisol: {cortisol} µg/dL - {get_risk_color(cortisol, (5, 25))}
105
+
106
+ ### 🧁 Metabolic Panel
107
+ - Fasting Blood Sugar: {fbs} mg/dL - {get_risk_color(fbs, (70, 110))}
108
+ - HbA1c: {hba1c}% - {get_risk_color(hba1c, (4.0, 5.7))}
109
+
110
+ ### ❤️ Vital Signs
111
+ - SpO2: {spo2}% - {get_risk_color(spo2, (95, 100))}
112
+ - Heart Rate: {heart_rate} bpm - {get_risk_color(heart_rate, (60, 100))}
113
+ - Respiratory Rate: {rr} breaths/min - {get_risk_color(rr, (12, 20))}
114
+ - Blood Pressure: Low (simulated)
115
+
116
+ ### ⚠️ Risk Flags
117
+ - {"\n- ".join(flags)}
118
+ """
119
+ return report, 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()