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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
NON-INVASIVE ANEMIA SCREENING - Palpebral & Buccal
|
| 3 |
+
===================================================
|
| 4 |
+
Palpebral conjunctiva (required) + Buccal mucosa (optional)
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import pickle
|
| 11 |
+
import cv2
|
| 12 |
+
import uuid
|
| 13 |
+
from datetime import datetime
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
# ============================================================
|
| 17 |
+
# CONFIG
|
| 18 |
+
# ============================================================
|
| 19 |
+
MODEL_PATH = "model.pkl"
|
| 20 |
+
YOLO_PALP = "best.pt"
|
| 21 |
+
YOLO_BUCC = "buccal_best.pt"
|
| 22 |
+
DATA_LOG = "collected_data.csv"
|
| 23 |
+
|
| 24 |
+
# ============================================================
|
| 25 |
+
# LOAD MODELS
|
| 26 |
+
# ============================================================
|
| 27 |
+
def load_models():
|
| 28 |
+
global ml_model, feature_names, yolo_palp, yolo_bucc
|
| 29 |
+
|
| 30 |
+
with open(MODEL_PATH, 'rb') as f:
|
| 31 |
+
data = pickle.load(f)
|
| 32 |
+
ml_model = data['model']
|
| 33 |
+
feature_names = data['features']
|
| 34 |
+
print(f"ML model: {data.get('name', 'Unknown')}, {len(feature_names)} features")
|
| 35 |
+
|
| 36 |
+
from ultralytics import YOLO
|
| 37 |
+
yolo_palp = YOLO(YOLO_PALP)
|
| 38 |
+
print("Palpebral YOLO loaded")
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
yolo_bucc = YOLO(YOLO_BUCC)
|
| 42 |
+
print("Buccal YOLO loaded")
|
| 43 |
+
except:
|
| 44 |
+
yolo_bucc = None
|
| 45 |
+
print("Buccal YOLO not found")
|
| 46 |
+
|
| 47 |
+
# ============================================================
|
| 48 |
+
# IMAGE PROCESSING
|
| 49 |
+
# ============================================================
|
| 50 |
+
def calibrate_image(img):
|
| 51 |
+
try:
|
| 52 |
+
if img is None:
|
| 53 |
+
return None
|
| 54 |
+
if len(img.shape) == 2:
|
| 55 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
| 56 |
+
elif img.shape[2] == 4:
|
| 57 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
|
| 58 |
+
else:
|
| 59 |
+
img_rgb = img.copy()
|
| 60 |
+
|
| 61 |
+
gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
|
| 62 |
+
_, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)
|
| 63 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 64 |
+
|
| 65 |
+
if not contours:
|
| 66 |
+
return img_rgb
|
| 67 |
+
|
| 68 |
+
c = max(contours, key=cv2.contourArea)
|
| 69 |
+
x, y, w, h = cv2.boundingRect(c)
|
| 70 |
+
if w * h < 1000:
|
| 71 |
+
return img_rgb
|
| 72 |
+
|
| 73 |
+
patch = img_rgb[y+h//4:y+3*h//4, x+w//4:x+3*w//4]
|
| 74 |
+
if patch.size == 0:
|
| 75 |
+
return img_rgb
|
| 76 |
+
|
| 77 |
+
avg = np.mean(patch, axis=(0, 1))
|
| 78 |
+
if any(v < 10 for v in avg):
|
| 79 |
+
return img_rgb
|
| 80 |
+
|
| 81 |
+
scale = np.array([128, 128, 128]) / avg
|
| 82 |
+
return np.clip(img_rgb * scale, 0, 255).astype(np.uint8)
|
| 83 |
+
except:
|
| 84 |
+
return img
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def extract_features(img, yolo, prefix):
|
| 88 |
+
if img is None or yolo is None:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
img_cal = calibrate_image(img)
|
| 93 |
+
if img_cal is None:
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
results = yolo(img_cal, verbose=False)
|
| 97 |
+
|
| 98 |
+
if len(results) > 0 and results[0].masks is not None:
|
| 99 |
+
masks = results[0].masks.data.cpu().numpy()
|
| 100 |
+
confs = results[0].boxes.conf.cpu().numpy()
|
| 101 |
+
if len(confs) > 0:
|
| 102 |
+
m = masks[np.argmax(confs)]
|
| 103 |
+
m = cv2.resize(m, (img_cal.shape[1], img_cal.shape[0]))
|
| 104 |
+
mask = (m > 0.5).astype(np.uint8) * 255
|
| 105 |
+
else:
|
| 106 |
+
h, w = img_cal.shape[:2]
|
| 107 |
+
mask = np.zeros((h, w), dtype=np.uint8)
|
| 108 |
+
mask[h//4:3*h//4, w//4:3*w//4] = 255
|
| 109 |
+
else:
|
| 110 |
+
h, w = img_cal.shape[:2]
|
| 111 |
+
mask = np.zeros((h, w), dtype=np.uint8)
|
| 112 |
+
mask[h//4:3*h//4, w//4:3*w//4] = 255
|
| 113 |
+
|
| 114 |
+
px = img_cal[mask > 0]
|
| 115 |
+
if len(px) < 100:
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
R, G, B = [np.mean(px[:, i]) for i in range(3)]
|
| 119 |
+
hsv = cv2.cvtColor(img_cal, cv2.COLOR_RGB2HSV)
|
| 120 |
+
H, S, V = [np.mean(hsv[mask > 0][:, i]) for i in range(3)]
|
| 121 |
+
lab = cv2.cvtColor(img_cal, cv2.COLOR_RGB2LAB)
|
| 122 |
+
L, A, Bc = [np.mean(lab[mask > 0][:, i]) for i in range(3)]
|
| 123 |
+
|
| 124 |
+
return {
|
| 125 |
+
f'{prefix}_R': R, f'{prefix}_G': G, f'{prefix}_B': B,
|
| 126 |
+
f'{prefix}_H': H, f'{prefix}_S': S, f'{prefix}_V': V,
|
| 127 |
+
f'{prefix}_L_color': L, f'{prefix}_A_color': A, f'{prefix}_B_color': Bc
|
| 128 |
+
}
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Error {prefix}: {e}")
|
| 131 |
+
return None
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# ============================================================
|
| 135 |
+
# FORM TO FEATURES
|
| 136 |
+
# ============================================================
|
| 137 |
+
def form_to_features(age, income_range, family_size, diet,
|
| 138 |
+
food_grains, food_leafy, food_cereals, food_pulses,
|
| 139 |
+
food_fish, food_eggs, food_meat, food_jaggery, food_nuts,
|
| 140 |
+
fruit_freq, veg_freq, tea_coffee,
|
| 141 |
+
comorb_dm, comorb_htn, comorb_thyroid,
|
| 142 |
+
prev_anemia, family_anemia, hookworm, bleeding_disorder, deworming,
|
| 143 |
+
menstrual, pads_per_day,
|
| 144 |
+
gravida, para, live_births, abortions, stillbirths,
|
| 145 |
+
family_dm, family_htn):
|
| 146 |
+
|
| 147 |
+
income_map = {"Below ₹5,000": 2500, "₹5,000 - ₹10,000": 7500, "₹10,000 - ₹15,000": 12500,
|
| 148 |
+
"₹15,000 - ₹25,000": 20000, "₹25,000 - ₹50,000": 37500, "Above ₹50,000": 75000}
|
| 149 |
+
freq_map = {"Daily": 4, "Twice a week": 3, "Once a week": 2, "Rarely": 1, "Never": 0}
|
| 150 |
+
pads_map = {"1-2": 1.5, "2-3": 2.5, "3-4": 3.5, "4-5": 4.5, "More than 5": 6, "Not applicable": 2.5}
|
| 151 |
+
|
| 152 |
+
income = income_map.get(income_range, 20000)
|
| 153 |
+
|
| 154 |
+
return {
|
| 155 |
+
'age': age, 'income': income, 'family_size': family_size,
|
| 156 |
+
'income_per_capita': income / max(family_size, 1),
|
| 157 |
+
'diet_veg': 1 if diet == "Vegetarian" else 0,
|
| 158 |
+
'diet_nonveg': 1 if diet == "Non-vegetarian" else 0,
|
| 159 |
+
'diet_mixed': 1 if diet == "Mixed" else 0,
|
| 160 |
+
'food_whole_grains': int(food_grains), 'food_green_leafy_vegetables': int(food_leafy),
|
| 161 |
+
'food_cereals': int(food_cereals), 'food_pulses': int(food_pulses),
|
| 162 |
+
'food_fish': int(food_fish), 'food_eggs': int(food_eggs),
|
| 163 |
+
'food_meat': int(food_meat), 'food_jaggery': int(food_jaggery), 'food_nuts': int(food_nuts),
|
| 164 |
+
'food_iron_count': sum([food_grains, food_leafy, food_cereals, food_pulses, food_fish, food_eggs, food_meat, food_jaggery, food_nuts]),
|
| 165 |
+
'fruit_freq': freq_map.get(fruit_freq, 2), 'veg_freq': freq_map.get(veg_freq, 2),
|
| 166 |
+
'tea_coffee_daily': 1 if tea_coffee == "Yes" else 0, 'tea_coffee_daily_missing': 0,
|
| 167 |
+
'comorb_dm': int(comorb_dm), 'comorb_htn': int(comorb_htn), 'comorb_thyroid': int(comorb_thyroid),
|
| 168 |
+
'comorb_none': 1 if not any([comorb_dm, comorb_htn, comorb_thyroid]) else 0,
|
| 169 |
+
'comorb_count': sum([int(comorb_dm), int(comorb_htn), int(comorb_thyroid)]),
|
| 170 |
+
'prev_anemia': 1 if prev_anemia == "Yes" else 0, 'prev_anemia_missing': 0,
|
| 171 |
+
'family_anemia': 1 if family_anemia == "Yes" else 0, 'family_anemia_missing': 0,
|
| 172 |
+
'hookworm_history': 1 if hookworm == "Yes" else 0, 'hookworm_history_missing': 0,
|
| 173 |
+
'bleeding_disorder': 1 if bleeding_disorder == "Yes" else 0, 'bleeding_disorder_missing': 0,
|
| 174 |
+
'deworming_done': 1 if deworming == "Yes" else 0, 'deworming_done_missing': 0,
|
| 175 |
+
'menstrual_regular': 1 if menstrual == "Regular" else 0,
|
| 176 |
+
'menstrual_irregular': 1 if menstrual == "Irregular" else 0,
|
| 177 |
+
'pads_per_day': pads_map.get(pads_per_day, 2.5), 'pads_missing': 0,
|
| 178 |
+
'gravida': gravida, 'para': para, 'live_births': live_births,
|
| 179 |
+
'abortions': abortions, 'stillbirths': stillbirths,
|
| 180 |
+
'has_obstetric_history': 1 if gravida > 0 else 0,
|
| 181 |
+
'pregnancy_loss': abortions + stillbirths,
|
| 182 |
+
'parity_ratio': para / gravida if gravida > 0 else 0,
|
| 183 |
+
'family_dm': 1 if family_dm == "Yes" else 0, 'family_dm_missing': 0,
|
| 184 |
+
'family_htn': 1 if family_htn == "Yes" else 0, 'family_htn_missing': 0,
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# ============================================================
|
| 189 |
+
# PREDICTION
|
| 190 |
+
# ============================================================
|
| 191 |
+
def predict_hb(img_palp, img_bucc,
|
| 192 |
+
age, income_range, family_size, diet,
|
| 193 |
+
food_grains, food_leafy, food_cereals, food_pulses,
|
| 194 |
+
food_fish, food_eggs, food_meat, food_jaggery, food_nuts,
|
| 195 |
+
fruit_freq, veg_freq, tea_coffee,
|
| 196 |
+
comorb_dm, comorb_htn, comorb_thyroid,
|
| 197 |
+
prev_anemia, family_anemia, hookworm, bleeding_disorder, deworming,
|
| 198 |
+
menstrual, pads_per_day,
|
| 199 |
+
gravida, para, live_births, abortions, stillbirths,
|
| 200 |
+
family_dm, family_htn, actual_hb):
|
| 201 |
+
|
| 202 |
+
session_id = str(uuid.uuid4())[:8]
|
| 203 |
+
sites = []
|
| 204 |
+
|
| 205 |
+
if img_palp is None:
|
| 206 |
+
return "❌ Palpebral image required.", "", ""
|
| 207 |
+
|
| 208 |
+
all_features = {}
|
| 209 |
+
|
| 210 |
+
# Palpebral (required)
|
| 211 |
+
palp_feat = extract_features(img_palp, yolo_palp, 'palpebral')
|
| 212 |
+
if palp_feat is None:
|
| 213 |
+
return "❌ Could not process palpebral image.", "", ""
|
| 214 |
+
all_features.update(palp_feat)
|
| 215 |
+
sites.append('palpebral')
|
| 216 |
+
|
| 217 |
+
# Buccal (optional)
|
| 218 |
+
if img_bucc is not None and yolo_bucc:
|
| 219 |
+
bucc_feat = extract_features(img_bucc, yolo_bucc, 'buccal')
|
| 220 |
+
if bucc_feat:
|
| 221 |
+
all_features.update(bucc_feat)
|
| 222 |
+
sites.append('buccal')
|
| 223 |
+
|
| 224 |
+
# Tabular
|
| 225 |
+
tab_feat = form_to_features(age, income_range, family_size, diet,
|
| 226 |
+
food_grains, food_leafy, food_cereals, food_pulses,
|
| 227 |
+
food_fish, food_eggs, food_meat, food_jaggery, food_nuts,
|
| 228 |
+
fruit_freq, veg_freq, tea_coffee,
|
| 229 |
+
comorb_dm, comorb_htn, comorb_thyroid,
|
| 230 |
+
prev_anemia, family_anemia, hookworm, bleeding_disorder, deworming,
|
| 231 |
+
menstrual, pads_per_day,
|
| 232 |
+
gravida, para, live_births, abortions, stillbirths,
|
| 233 |
+
family_dm, family_htn)
|
| 234 |
+
all_features.update(tab_feat)
|
| 235 |
+
|
| 236 |
+
# Default values for missing image features
|
| 237 |
+
defaults = {
|
| 238 |
+
'palpebral_R': 150, 'palpebral_G': 100, 'palpebral_B': 100,
|
| 239 |
+
'palpebral_H': 10, 'palpebral_S': 100, 'palpebral_V': 150,
|
| 240 |
+
'palpebral_L_color': 130, 'palpebral_A_color': 150, 'palpebral_B_color': 140,
|
| 241 |
+
'buccal_R': 160, 'buccal_G': 110, 'buccal_B': 110,
|
| 242 |
+
'buccal_H': 8, 'buccal_S': 90, 'buccal_V': 160,
|
| 243 |
+
'buccal_L_color': 140, 'buccal_A_color': 145, 'buccal_B_color': 135,
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
for f in feature_names:
|
| 247 |
+
if f not in all_features:
|
| 248 |
+
all_features[f] = defaults.get(f, 0)
|
| 249 |
+
|
| 250 |
+
X = pd.DataFrame([all_features])[feature_names]
|
| 251 |
+
hb = round(float(ml_model.predict(X)[0]), 1)
|
| 252 |
+
|
| 253 |
+
if hb >= 12:
|
| 254 |
+
sev, col, rec = "Normal", "🟢", "No anemia detected."
|
| 255 |
+
elif hb >= 11:
|
| 256 |
+
sev, col, rec = "Mild Anemia", "🟡", "Consider iron-rich foods."
|
| 257 |
+
elif hb >= 8:
|
| 258 |
+
sev, col, rec = "Moderate Anemia", "🟠", "Consult healthcare provider."
|
| 259 |
+
else:
|
| 260 |
+
sev, col, rec = "Severe Anemia", "🔴", "Seek immediate medical attention."
|
| 261 |
+
|
| 262 |
+
# Log
|
| 263 |
+
try:
|
| 264 |
+
record = {'session_id': session_id, 'timestamp': datetime.now().isoformat(),
|
| 265 |
+
'predicted_hb': hb, 'actual_hb': actual_hb if actual_hb > 0 else None,
|
| 266 |
+
'sites': ','.join(sites), **all_features}
|
| 267 |
+
df = pd.DataFrame([record])
|
| 268 |
+
if Path(DATA_LOG).exists():
|
| 269 |
+
df.to_csv(DATA_LOG, mode='a', header=False, index=False)
|
| 270 |
+
else:
|
| 271 |
+
df.to_csv(DATA_LOG, index=False)
|
| 272 |
+
except:
|
| 273 |
+
pass
|
| 274 |
+
|
| 275 |
+
result = f"## {col} Estimated Hb: {hb} g/dL\n### {sev}\n{rec}\n\n---\n**Sites:** {', '.join(sites)} | **Session:** {session_id}"
|
| 276 |
+
return result, session_id, f"{hb} g/dL"
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
# ============================================================
|
| 280 |
+
# INTERFACE
|
| 281 |
+
# ============================================================
|
| 282 |
+
def create_app():
|
| 283 |
+
with gr.Blocks(title="Anemia Screening", theme=gr.themes.Soft()) as app:
|
| 284 |
+
gr.Markdown("# 🩺 Non-Invasive Anemia Screening\n**Palpebral conjunctiva required. Buccal mucosa optional.**")
|
| 285 |
+
|
| 286 |
+
with gr.Row():
|
| 287 |
+
with gr.Column(scale=1):
|
| 288 |
+
img_palp = gr.Image(label="Palpebral Conjunctiva (Required)", type="numpy")
|
| 289 |
+
img_bucc = gr.Image(label="Buccal Mucosa (Optional)", type="numpy")
|
| 290 |
+
|
| 291 |
+
with gr.Column(scale=2):
|
| 292 |
+
with gr.Accordion("Demographics", open=True):
|
| 293 |
+
with gr.Row():
|
| 294 |
+
age = gr.Slider(10, 80, 25, step=1, label="Age")
|
| 295 |
+
family_size = gr.Slider(1, 15, 4, step=1, label="Family Size")
|
| 296 |
+
income_range = gr.Dropdown(["Below ₹5,000", "₹5,000 - ₹10,000", "₹10,000 - ₹15,000", "₹15,000 - ₹25,000", "₹25,000 - ₹50,000", "Above ₹50,000"], value="₹10,000 - ₹15,000", label="Income")
|
| 297 |
+
|
| 298 |
+
with gr.Accordion("Diet", open=True):
|
| 299 |
+
diet = gr.Radio(["Vegetarian", "Non-vegetarian", "Mixed"], value="Mixed", label="Diet")
|
| 300 |
+
with gr.Row():
|
| 301 |
+
food_grains = gr.Checkbox(label="Whole Grains", value=True)
|
| 302 |
+
food_leafy = gr.Checkbox(label="Leafy Veg", value=True)
|
| 303 |
+
food_cereals = gr.Checkbox(label="Cereals", value=True)
|
| 304 |
+
with gr.Row():
|
| 305 |
+
food_pulses = gr.Checkbox(label="Pulses", value=True)
|
| 306 |
+
food_fish = gr.Checkbox(label="Fish")
|
| 307 |
+
food_eggs = gr.Checkbox(label="Eggs")
|
| 308 |
+
with gr.Row():
|
| 309 |
+
food_meat = gr.Checkbox(label="Meat")
|
| 310 |
+
food_jaggery = gr.Checkbox(label="Jaggery")
|
| 311 |
+
food_nuts = gr.Checkbox(label="Nuts")
|
| 312 |
+
with gr.Row():
|
| 313 |
+
fruit_freq = gr.Dropdown(["Daily", "Twice a week", "Once a week", "Rarely", "Never"], value="Twice a week", label="Fruits")
|
| 314 |
+
veg_freq = gr.Dropdown(["Daily", "Twice a week", "Once a week", "Rarely", "Never"], value="Daily", label="Vegetables")
|
| 315 |
+
tea_coffee = gr.Radio(["Yes", "No"], value="Yes", label="Daily Tea/Coffee")
|
| 316 |
+
|
| 317 |
+
with gr.Accordion("Medical History", open=False):
|
| 318 |
+
with gr.Row():
|
| 319 |
+
comorb_dm = gr.Checkbox(label="Diabetes")
|
| 320 |
+
comorb_htn = gr.Checkbox(label="Hypertension")
|
| 321 |
+
comorb_thyroid = gr.Checkbox(label="Thyroid")
|
| 322 |
+
with gr.Row():
|
| 323 |
+
prev_anemia = gr.Radio(["Yes", "No"], value="No", label="Anemia (past 12mo)")
|
| 324 |
+
family_anemia = gr.Radio(["Yes", "No"], value="No", label="Family Anemia")
|
| 325 |
+
with gr.Row():
|
| 326 |
+
hookworm = gr.Radio(["Yes", "No"], value="No", label="Hookworm")
|
| 327 |
+
bleeding_disorder = gr.Radio(["Yes", "No"], value="No", label="Bleeding Disorder")
|
| 328 |
+
deworming = gr.Radio(["Yes", "No"], value="Yes", label="Deworming Done")
|
| 329 |
+
with gr.Row():
|
| 330 |
+
family_dm = gr.Radio(["Yes", "No"], value="No", label="Family Diabetes")
|
| 331 |
+
family_htn = gr.Radio(["Yes", "No"], value="No", label="Family Hypertension")
|
| 332 |
+
|
| 333 |
+
with gr.Accordion("Menstrual/Obstetric", open=False):
|
| 334 |
+
with gr.Row():
|
| 335 |
+
menstrual = gr.Radio(["Regular", "Irregular", "Not applicable"], value="Regular", label="Menstrual")
|
| 336 |
+
pads_per_day = gr.Dropdown(["1-2", "2-3", "3-4", "4-5", "More than 5", "Not applicable"], value="2-3", label="Pads/day")
|
| 337 |
+
with gr.Row():
|
| 338 |
+
gravida = gr.Slider(0, 10, 0, step=1, label="Gravida")
|
| 339 |
+
para = gr.Slider(0, 10, 0, step=1, label="Para")
|
| 340 |
+
live_births = gr.Slider(0, 10, 0, step=1, label="Live Births")
|
| 341 |
+
with gr.Row():
|
| 342 |
+
abortions = gr.Slider(0, 5, 0, step=1, label="Abortions")
|
| 343 |
+
stillbirths = gr.Slider(0, 5, 0, step=1, label="Stillbirths")
|
| 344 |
+
|
| 345 |
+
with gr.Row():
|
| 346 |
+
actual_hb = gr.Number(label="Actual Hb (optional)", value=0, minimum=0, maximum=25)
|
| 347 |
+
submit = gr.Button("🔬 Estimate Hb", variant="primary", size="lg")
|
| 348 |
+
|
| 349 |
+
result = gr.Markdown()
|
| 350 |
+
with gr.Row():
|
| 351 |
+
session = gr.Textbox(label="Session ID", interactive=False)
|
| 352 |
+
hb_out = gr.Textbox(label="Estimated Hb", interactive=False)
|
| 353 |
+
|
| 354 |
+
gr.Markdown("---\n🔒 Anonymous data collection. No PII stored.")
|
| 355 |
+
|
| 356 |
+
submit.click(predict_hb,
|
| 357 |
+
[img_palp, img_bucc, age, income_range, family_size, diet,
|
| 358 |
+
food_grains, food_leafy, food_cereals, food_pulses, food_fish, food_eggs, food_meat, food_jaggery, food_nuts,
|
| 359 |
+
fruit_freq, veg_freq, tea_coffee, comorb_dm, comorb_htn, comorb_thyroid,
|
| 360 |
+
prev_anemia, family_anemia, hookworm, bleeding_disorder, deworming,
|
| 361 |
+
menstrual, pads_per_day, gravida, para, live_births, abortions, stillbirths,
|
| 362 |
+
family_dm, family_htn, actual_hb],
|
| 363 |
+
[result, session, hb_out])
|
| 364 |
+
|
| 365 |
+
return app
|
| 366 |
+
|
| 367 |
+
if __name__ == "__main__":
|
| 368 |
+
load_models()
|
| 369 |
+
create_app().launch()
|