Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,168 +1,169 @@
|
|
| 1 |
-
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
-
|
| 4 |
-
import re
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
| 7 |
-
# ----------------
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 10 |
MODEL_ID = "gemini-2.5-flash"
|
| 11 |
|
| 12 |
-
# ----------------
|
| 13 |
class BiomarkerRequest(BaseModel):
|
| 14 |
# Demographics
|
| 15 |
age: int = Field(default=52)
|
| 16 |
gender: str = Field(default="female")
|
| 17 |
-
height: float = Field(default=165
|
| 18 |
-
weight: float = Field(default=70
|
| 19 |
|
| 20 |
# Kidney / Electrolytes
|
| 21 |
-
urea: float = 25.0
|
| 22 |
-
creatinine: float = 1.0
|
| 23 |
-
uric_acid: float = 5.0
|
| 24 |
-
calcium: float = 9.5
|
| 25 |
-
phosphorus: float = 3.5
|
| 26 |
-
sodium: float = 140.0
|
| 27 |
-
potassium: float = 4.2
|
| 28 |
-
chloride: float = 100.0
|
| 29 |
-
bicarbonate: float = 24.0
|
| 30 |
-
egfr: float = 100.0
|
| 31 |
-
serum_osmolality: float = 285.0
|
| 32 |
-
ionized_calcium: float = 1.2
|
| 33 |
-
amylase: float = 50.0
|
| 34 |
-
lipase: float = 50.0
|
| 35 |
|
| 36 |
# Hematology
|
| 37 |
-
wbc: float = 6.0
|
| 38 |
-
hemoglobin: float = 14.0
|
| 39 |
-
mcv: float = 90.0
|
| 40 |
-
rdw: float = 13.0
|
| 41 |
-
lymphocytes: float = 30.0
|
| 42 |
|
| 43 |
# Glycemic / Metabolic
|
| 44 |
-
fasting_blood_sugar: float = 85.0
|
| 45 |
-
hb1ac: float = 5.4
|
| 46 |
-
insulin: float = 10.0
|
| 47 |
-
c_peptide: float = 1.2
|
| 48 |
-
homa_ir: float = 1.2
|
| 49 |
|
| 50 |
# Lipid Profile
|
| 51 |
-
total_cholesterol: float = 180.0
|
| 52 |
-
ldl: float = 90.0
|
| 53 |
-
hdl: float = 50.0
|
| 54 |
-
cholesterol_hdl_ratio: float = 3.0
|
| 55 |
-
triglycerides: float = 120.0
|
| 56 |
-
apo_a1: float = 150.0
|
| 57 |
-
apo_b: float = 70.0
|
| 58 |
-
apo_ratio: float = 0.5
|
| 59 |
|
| 60 |
# Liver
|
| 61 |
-
albumin: float = 4.5
|
| 62 |
-
total_protein: float = 7.0
|
| 63 |
-
alt: float = 25.0
|
| 64 |
-
ast: float = 24.0
|
| 65 |
-
alp: float = 80.0
|
| 66 |
-
ggt: float = 20.0
|
| 67 |
-
ld: float = 180.0
|
| 68 |
-
globulin: float = 2.5
|
| 69 |
-
albumin_globulin_ratio: float = 1.8
|
| 70 |
-
magnesium: float = 2.0
|
| 71 |
-
total_bilirubin: float = 0.7
|
| 72 |
-
direct_bilirubin: float = 0.2
|
| 73 |
-
indirect_bilirubin: float = 0.5
|
| 74 |
-
ammonia: float = 30.0
|
| 75 |
|
| 76 |
# Cardiac / Inflammation
|
| 77 |
-
hs_crp: float = 1.0
|
| 78 |
-
ck: float = 100.0
|
| 79 |
-
ck_mb: float = 10.0
|
| 80 |
-
homocysteine: float = 10.0
|
| 81 |
-
zinc: float = 90.0
|
| 82 |
-
copper: float = 100.0
|
| 83 |
-
selenium: float = 100.0
|
| 84 |
|
| 85 |
# Iron / Minerals
|
| 86 |
-
iron: float = 100.0
|
| 87 |
-
tibc: float = 350.0
|
| 88 |
-
transferrin: float = 250.0
|
| 89 |
|
| 90 |
# Vitamins
|
| 91 |
-
vitamin_d: float = 35.0
|
| 92 |
-
vitamin_b12: float = 500.0
|
| 93 |
|
| 94 |
# Sex Hormones
|
| 95 |
-
total_testosterone: float = 450.0
|
| 96 |
-
free_testosterone: float = 15.0
|
| 97 |
-
estrogen: float = 60.0
|
| 98 |
-
progesterone: float = 1.0
|
| 99 |
-
dhea_s: float = 250.0
|
| 100 |
-
shbg: float = 40.0
|
| 101 |
-
lh: float = 5.0
|
| 102 |
-
fsh: float = 6.0
|
| 103 |
|
| 104 |
# Thyroid
|
| 105 |
-
tsh: float = 2.0
|
| 106 |
-
free_t3: float = 3.2
|
| 107 |
-
free_t4: float = 1.2
|
| 108 |
-
total_t3: float = 120.0
|
| 109 |
-
total_t4: float = 8.0
|
| 110 |
-
reverse_t3: float = 15.0
|
| 111 |
-
tpo_ab: float = 20.0
|
| 112 |
-
tg_ab: float = 20.0
|
| 113 |
|
| 114 |
# Adrenal / Stress
|
| 115 |
-
cortisol: float = 12.0
|
| 116 |
-
acth: float = 30.0
|
| 117 |
-
igf1: float = 200.0
|
| 118 |
-
leptin: float = 15.0
|
| 119 |
-
adiponectin: float = 15.0
|
| 120 |
|
| 121 |
# Cancer markers
|
| 122 |
-
ca125: float = 20.0
|
| 123 |
-
ca15_3: float = 25.0
|
| 124 |
-
ca19_9: float = 20.0
|
| 125 |
-
psa: float = 2.0
|
| 126 |
-
cea: float = 2.0
|
| 127 |
-
calcitonin: float = 5.0
|
| 128 |
-
afp: float = 5.0
|
| 129 |
-
tnf: float = 5.0
|
| 130 |
|
| 131 |
# Immune / Autoimmune
|
| 132 |
-
ana: float = 0.0
|
| 133 |
-
ige: float = 50.0
|
| 134 |
-
igg: float = 1000.0
|
| 135 |
-
anti_ccp: float = 10.0
|
| 136 |
-
dsdna: float = 5.0
|
| 137 |
-
ssa_ssb: float = 0.0
|
| 138 |
-
rnp: float = 0.0
|
| 139 |
-
sm_antibodies: float = 0.0
|
| 140 |
-
anca: float = 0.0
|
| 141 |
-
anti_ena: float = 0.0
|
| 142 |
-
il6: float = 5.0
|
| 143 |
-
allergy_panel: float = 1.0
|
| 144 |
-
|
| 145 |
-
# ----------------
|
| 146 |
-
def
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def parse_medical_report(text: str):
|
| 158 |
-
# Basic parser for demonstration
|
| 159 |
-
return {"report": text}
|
| 160 |
|
| 161 |
# ---------------- Gradio Function ----------------
|
| 162 |
-
def
|
| 163 |
age, gender, height, weight,
|
| 164 |
-
urea, creatinine, uric_acid, calcium, phosphorus, sodium, potassium, chloride,
|
| 165 |
-
amylase, lipase,
|
| 166 |
wbc, hemoglobin, mcv, rdw, lymphocytes,
|
| 167 |
fasting_blood_sugar, hb1ac, insulin, c_peptide, homa_ir,
|
| 168 |
total_cholesterol, ldl, hdl, cholesterol_hdl_ratio, triglycerides, apo_a1, apo_b, apo_ratio,
|
|
@@ -171,196 +172,123 @@ def run_biomarker_analysis(
|
|
| 171 |
hs_crp, ck, ck_mb, homocysteine, zinc, copper, selenium,
|
| 172 |
iron, tibc, transferrin,
|
| 173 |
vitamin_d, vitamin_b12,
|
| 174 |
-
total_testosterone, free_testosterone, estrogen, progesterone, dhea_s, shbg,
|
| 175 |
-
lh, fsh,
|
| 176 |
tsh, free_t3, free_t4, total_t3, total_t4, reverse_t3, tpo_ab, tg_ab,
|
| 177 |
cortisol, acth, igf1, leptin, adiponectin,
|
| 178 |
-
ca125, ca15_3, ca19_9, psa, cea, calcitonin, afp,
|
| 179 |
-
|
| 180 |
-
il6, allergy_panel
|
| 181 |
):
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
#
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
gr.
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
gr.Number(label="homocysteine", value=10.0),
|
| 297 |
-
gr.Number(label="zinc", value=90.0),
|
| 298 |
-
gr.Number(label="copper", value=100.0),
|
| 299 |
-
gr.Number(label="selenium", value=100.0),
|
| 300 |
-
|
| 301 |
-
# Iron / Minerals
|
| 302 |
-
gr.Number(label="iron", value=100.0),
|
| 303 |
-
gr.Number(label="tibc", value=350.0),
|
| 304 |
-
gr.Number(label="transferrin", value=250.0),
|
| 305 |
-
|
| 306 |
-
# Vitamins
|
| 307 |
-
gr.Number(label="vitamin_d", value=35.0),
|
| 308 |
-
gr.Number(label="vitamin_b12", value=500.0),
|
| 309 |
-
|
| 310 |
-
# Sex Hormones
|
| 311 |
-
gr.Number(label="total_testosterone", value=450.0),
|
| 312 |
-
gr.Number(label="free_testosterone", value=15.0),
|
| 313 |
-
gr.Number(label="estrogen", value=60.0),
|
| 314 |
-
gr.Number(label="progesterone", value=1.0),
|
| 315 |
-
gr.Number(label="dhea_s", value=250.0),
|
| 316 |
-
gr.Number(label="shbg", value=40.0),
|
| 317 |
-
gr.Number(label="lh", value=5.0),
|
| 318 |
-
gr.Number(label="fsh", value=6.0),
|
| 319 |
-
|
| 320 |
-
# Thyroid
|
| 321 |
-
gr.Number(label="tsh", value=2.0),
|
| 322 |
-
gr.Number(label="free_t3", value=3.2),
|
| 323 |
-
gr.Number(label="free_t4", value=1.2),
|
| 324 |
-
gr.Number(label="total_t3", value=120.0),
|
| 325 |
-
gr.Number(label="total_t4", value=8.0),
|
| 326 |
-
gr.Number(label="reverse_t3", value=15.0),
|
| 327 |
-
gr.Number(label="tpo_ab", value=20.0),
|
| 328 |
-
gr.Number(label="tg_ab", value=20.0),
|
| 329 |
-
|
| 330 |
-
# Adrenal / Stress
|
| 331 |
-
gr.Number(label="cortisol", value=12.0),
|
| 332 |
-
gr.Number(label="acth", value=30.0),
|
| 333 |
-
gr.Number(label="igf1", value=200.0),
|
| 334 |
-
gr.Number(label="leptin", value=15.0),
|
| 335 |
-
gr.Number(label="adiponectin", value=15.0),
|
| 336 |
-
|
| 337 |
-
# Cancer markers
|
| 338 |
-
gr.Number(label="ca125", value=20.0),
|
| 339 |
-
gr.Number(label="ca15_3", value=25.0),
|
| 340 |
-
gr.Number(label="ca19_9", value=20.0),
|
| 341 |
-
gr.Number(label="psa", value=2.0),
|
| 342 |
-
gr.Number(label="cea", value=2.0),
|
| 343 |
-
gr.Number(label="calcitonin", value=5.0),
|
| 344 |
-
gr.Number(label="afp", value=5.0),
|
| 345 |
-
gr.Number(label="tnf", value=5.0),
|
| 346 |
-
|
| 347 |
-
# Immune / Autoimmune
|
| 348 |
-
gr.Number(label="ana", value=0.0),
|
| 349 |
-
gr.Number(label="ige", value=50.0),
|
| 350 |
-
gr.Number(label="igg", value=1000.0),
|
| 351 |
-
gr.Number(label="anti_ccp", value=10.0),
|
| 352 |
-
gr.Number(label="dsdna", value=5.0),
|
| 353 |
-
gr.Number(label="ssa_ssb", value=0.0),
|
| 354 |
-
gr.Number(label="rnp", value=0.0),
|
| 355 |
-
gr.Number(label="sm_antibodies", value=0.0),
|
| 356 |
-
gr.Number(label="anca", value=0.0),
|
| 357 |
-
gr.Number(label="anti_ena", value=0.0),
|
| 358 |
-
gr.Number(label="il6", value=5.0),
|
| 359 |
-
gr.Number(label="allergy_panel", value=1.0),
|
| 360 |
-
]
|
| 361 |
-
|
| 362 |
-
output = gr.JSON(label="Medical Insights")
|
| 363 |
-
submit = gr.Button("Analyze")
|
| 364 |
-
submit.click(fn=run_biomarker_analysis, inputs=inputs, outputs=output)
|
| 365 |
-
|
| 366 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
+
import gradio as gr
|
|
|
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
# ---------------- Initialize ----------------
|
| 7 |
+
app = FastAPI(title="LLM Model API + Gradio UI", version="4.0")
|
| 8 |
+
|
| 9 |
+
GEMINI_API_KEY='AIzaSyAtUi_DukKWIFVoze9cvlGnlP60eW0NzOg'
|
| 10 |
+
if not GEMINI_API_KEY:
|
| 11 |
+
raise ValueError("❌ GEMINI_API_KEY not found.")
|
| 12 |
+
|
| 13 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
MODEL_ID = "gemini-2.5-flash"
|
| 15 |
|
| 16 |
+
# ---------------- Schema ----------------
|
| 17 |
class BiomarkerRequest(BaseModel):
|
| 18 |
# Demographics
|
| 19 |
age: int = Field(default=52)
|
| 20 |
gender: str = Field(default="female")
|
| 21 |
+
height: float = Field(default=165)
|
| 22 |
+
weight: float = Field(default=70)
|
| 23 |
|
| 24 |
# Kidney / Electrolytes
|
| 25 |
+
urea: float = Field(default=25.0)
|
| 26 |
+
creatinine: float = Field(default=1.0)
|
| 27 |
+
uric_acid: float = Field(default=5.0)
|
| 28 |
+
calcium: float = Field(default=9.5)
|
| 29 |
+
phosphorus: float = Field(default=3.5)
|
| 30 |
+
sodium: float = Field(default=140.0)
|
| 31 |
+
potassium: float = Field(default=4.2)
|
| 32 |
+
chloride: float = Field(default=100.0)
|
| 33 |
+
bicarbonate: float = Field(default=24.0)
|
| 34 |
+
egfr: float = Field(default=100.0)
|
| 35 |
+
serum_osmolality: float = Field(default=285.0)
|
| 36 |
+
ionized_calcium: float = Field(default=1.2)
|
| 37 |
+
amylase: float = Field(default=50.0)
|
| 38 |
+
lipase: float = Field(default=50.0)
|
| 39 |
|
| 40 |
# Hematology
|
| 41 |
+
wbc: float = Field(default=6.0)
|
| 42 |
+
hemoglobin: float = Field(default=14.0)
|
| 43 |
+
mcv: float = Field(default=90.0)
|
| 44 |
+
rdw: float = Field(default=13.0)
|
| 45 |
+
lymphocytes: float = Field(default=30.0)
|
| 46 |
|
| 47 |
# Glycemic / Metabolic
|
| 48 |
+
fasting_blood_sugar: float = Field(default=85.0)
|
| 49 |
+
hb1ac: float = Field(default=5.4)
|
| 50 |
+
insulin: float = Field(default=10.0)
|
| 51 |
+
c_peptide: float = Field(default=1.2)
|
| 52 |
+
homa_ir: float = Field(default=1.2)
|
| 53 |
|
| 54 |
# Lipid Profile
|
| 55 |
+
total_cholesterol: float = Field(default=180.0)
|
| 56 |
+
ldl: float = Field(default=90.0)
|
| 57 |
+
hdl: float = Field(default=50.0)
|
| 58 |
+
cholesterol_hdl_ratio: float = Field(default=3.0)
|
| 59 |
+
triglycerides: float = Field(default=120.0)
|
| 60 |
+
apo_a1: float = Field(default=150.0)
|
| 61 |
+
apo_b: float = Field(default=70.0)
|
| 62 |
+
apo_ratio: float = Field(default=0.5)
|
| 63 |
|
| 64 |
# Liver
|
| 65 |
+
albumin: float = Field(default=4.5)
|
| 66 |
+
total_protein: float = Field(default=7.0)
|
| 67 |
+
alt: float = Field(default=25.0)
|
| 68 |
+
ast: float = Field(default=24.0)
|
| 69 |
+
alp: float = Field(default=80.0)
|
| 70 |
+
ggt: float = Field(default=20.0)
|
| 71 |
+
ld: float = Field(default=180.0)
|
| 72 |
+
globulin: float = Field(default=2.5)
|
| 73 |
+
albumin_globulin_ratio: float = Field(default=1.8)
|
| 74 |
+
magnesium: float = Field(default=2.0)
|
| 75 |
+
total_bilirubin: float = Field(default=0.7)
|
| 76 |
+
direct_bilirubin: float = Field(default=0.2)
|
| 77 |
+
indirect_bilirubin: float = Field(default=0.5)
|
| 78 |
+
ammonia: float = Field(default=30.0)
|
| 79 |
|
| 80 |
# Cardiac / Inflammation
|
| 81 |
+
hs_crp: float = Field(default=1.0)
|
| 82 |
+
ck: float = Field(default=100.0)
|
| 83 |
+
ck_mb: float = Field(default=10.0)
|
| 84 |
+
homocysteine: float = Field(default=10.0)
|
| 85 |
+
zinc: float = Field(default=90.0)
|
| 86 |
+
copper: float = Field(default=100.0)
|
| 87 |
+
selenium: float = Field(default=100.0)
|
| 88 |
|
| 89 |
# Iron / Minerals
|
| 90 |
+
iron: float = Field(default=100.0)
|
| 91 |
+
tibc: float = Field(default=350.0)
|
| 92 |
+
transferrin: float = Field(default=250.0)
|
| 93 |
|
| 94 |
# Vitamins
|
| 95 |
+
vitamin_d: float = Field(default=35.0)
|
| 96 |
+
vitamin_b12: float = Field(default=500.0)
|
| 97 |
|
| 98 |
# Sex Hormones
|
| 99 |
+
total_testosterone: float = Field(default=450.0)
|
| 100 |
+
free_testosterone: float = Field(default=15.0)
|
| 101 |
+
estrogen: float = Field(default=60.0)
|
| 102 |
+
progesterone: float = Field(default=1.0)
|
| 103 |
+
dhea_s: float = Field(default=250.0)
|
| 104 |
+
shbg: float = Field(default=40.0)
|
| 105 |
+
lh: float = Field(default=5.0)
|
| 106 |
+
fsh: float = Field(default=6.0)
|
| 107 |
|
| 108 |
# Thyroid
|
| 109 |
+
tsh: float = Field(default=2.0)
|
| 110 |
+
free_t3: float = Field(default=3.2)
|
| 111 |
+
free_t4: float = Field(default=1.2)
|
| 112 |
+
total_t3: float = Field(default=120.0)
|
| 113 |
+
total_t4: float = Field(default=8.0)
|
| 114 |
+
reverse_t3: float = Field(default=15.0)
|
| 115 |
+
tpo_ab: float = Field(default=20.0)
|
| 116 |
+
tg_ab: float = Field(default=20.0)
|
| 117 |
|
| 118 |
# Adrenal / Stress
|
| 119 |
+
cortisol: float = Field(default=12.0)
|
| 120 |
+
acth: float = Field(default=30.0)
|
| 121 |
+
igf1: float = Field(default=200.0)
|
| 122 |
+
leptin: float = Field(default=15.0)
|
| 123 |
+
adiponectin: float = Field(default=15.0)
|
| 124 |
|
| 125 |
# Cancer markers
|
| 126 |
+
ca125: float = Field(default=20.0)
|
| 127 |
+
ca15_3: float = Field(default=25.0)
|
| 128 |
+
ca19_9: float = Field(default=20.0)
|
| 129 |
+
psa: float = Field(default=2.0)
|
| 130 |
+
cea: float = Field(default=2.0)
|
| 131 |
+
calcitonin: float = Field(default=5.0)
|
| 132 |
+
afp: float = Field(default=5.0)
|
| 133 |
+
tnf: float = Field(default=5.0)
|
| 134 |
|
| 135 |
# Immune / Autoimmune
|
| 136 |
+
ana: float = Field(default=0.0)
|
| 137 |
+
ige: float = Field(default=50.0)
|
| 138 |
+
igg: float = Field(default=1000.0)
|
| 139 |
+
anti_ccp: float = Field(default=10.0)
|
| 140 |
+
dsdna: float = Field(default=5.0)
|
| 141 |
+
ssa_ssb: float = Field(default=0.0)
|
| 142 |
+
rnp: float = Field(default=0.0)
|
| 143 |
+
sm_antibodies: float = Field(default=0.0)
|
| 144 |
+
anca: float = Field(default=0.0)
|
| 145 |
+
anti_ena: float = Field(default=0.0)
|
| 146 |
+
il6: float = Field(default=5.0)
|
| 147 |
+
allergy_panel: float = Field(default=1.0)
|
| 148 |
+
|
| 149 |
+
# ---------------- Gemini Report ----------------
|
| 150 |
+
def generate_report(data: BiomarkerRequest) -> str:
|
| 151 |
+
user_message = f"""
|
| 152 |
+
Patient Info:
|
| 153 |
+
- Age: {data.age}, Gender: {data.gender}, Height: {data.height}, Weight: {data.weight}
|
| 154 |
+
Biomarkers: {data.dict()}
|
| 155 |
+
"""
|
| 156 |
+
model = genai.GenerativeModel(MODEL_ID)
|
| 157 |
+
response = model.generate_content(user_message)
|
| 158 |
+
if not response or not getattr(response, "text", None):
|
| 159 |
+
return "⚠️ Gemini returned empty response."
|
| 160 |
+
return response.text.strip()
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
# ---------------- Gradio Function ----------------
|
| 163 |
+
def gradio_interface(
|
| 164 |
age, gender, height, weight,
|
| 165 |
+
urea, creatinine, uric_acid, calcium, phosphorus, sodium, potassium, chloride, bicarbonate, egfr, serum_osmolality, ionized_calcium,
|
| 166 |
+
amylase, lipase,
|
| 167 |
wbc, hemoglobin, mcv, rdw, lymphocytes,
|
| 168 |
fasting_blood_sugar, hb1ac, insulin, c_peptide, homa_ir,
|
| 169 |
total_cholesterol, ldl, hdl, cholesterol_hdl_ratio, triglycerides, apo_a1, apo_b, apo_ratio,
|
|
|
|
| 172 |
hs_crp, ck, ck_mb, homocysteine, zinc, copper, selenium,
|
| 173 |
iron, tibc, transferrin,
|
| 174 |
vitamin_d, vitamin_b12,
|
| 175 |
+
total_testosterone, free_testosterone, estrogen, progesterone, dhea_s, shbg, lh, fsh,
|
|
|
|
| 176 |
tsh, free_t3, free_t4, total_t3, total_t4, reverse_t3, tpo_ab, tg_ab,
|
| 177 |
cortisol, acth, igf1, leptin, adiponectin,
|
| 178 |
+
ca125, ca15_3, ca19_9, psa, cea, calcitonin, afp, tnf,
|
| 179 |
+
ana, ige, igg, anti_ccp, dsdna, ssa_ssb, rnp, sm_antibodies, anca, anti_ena, il6, allergy_panel
|
|
|
|
| 180 |
):
|
| 181 |
+
req = BiomarkerRequest(
|
| 182 |
+
age=age, gender=gender, height=height, weight=weight,
|
| 183 |
+
urea=urea, creatinine=creatinine, uric_acid=uric_acid, calcium=calcium, phosphorus=phosphorus,
|
| 184 |
+
sodium=sodium, potassium=potassium, chloride=chloride, bicarbonate=bicarbonate, egfr=egfr,
|
| 185 |
+
serum_osmolality=serum_osmolality, ionized_calcium=ionized_calcium, amylase=amylase, lipase=lipase,
|
| 186 |
+
wbc=wbc, hemoglobin=hemoglobin, mcv=mcv, rdw=rdw, lymphocytes=lymphocytes,
|
| 187 |
+
fasting_blood_sugar=fasting_blood_sugar, hb1ac=hb1ac, insulin=insulin, c_peptide=c_peptide, homa_ir=homa_ir,
|
| 188 |
+
total_cholesterol=total_cholesterol, ldl=ldl, hdl=hdl, cholesterol_hdl_ratio=cholesterol_hdl_ratio, triglycerides=triglycerides,
|
| 189 |
+
apo_a1=apo_a1, apo_b=apo_b, apo_ratio=apo_ratio,
|
| 190 |
+
albumin=albumin, total_protein=total_protein, alt=alt, ast=ast, alp=alp, ggt=ggt, ld=ld, globulin=globulin, albumin_globulin_ratio=albumin_globulin_ratio,
|
| 191 |
+
magnesium=magnesium, total_bilirubin=total_bilirubin, direct_bilirubin=direct_bilirubin, indirect_bilirubin=indirect_bilirubin, ammonia=ammonia,
|
| 192 |
+
hs_crp=hs_crp, ck=ck, ck_mb=ck_mb, homocysteine=homocysteine, zinc=zinc, copper=copper, selenium=selenium,
|
| 193 |
+
iron=iron, tibc=tibc, transferrin=transferrin,
|
| 194 |
+
vitamin_d=vitamin_d, vitamin_b12=vitamin_b12,
|
| 195 |
+
total_testosterone=total_testosterone, free_testosterone=free_testosterone, estrogen=estrogen, progesterone=progesterone, dhea_s=dhea_s, shbg=shbg, lh=lh, fsh=fsh,
|
| 196 |
+
tsh=tsh, free_t3=free_t3, free_t4=free_t4, total_t3=total_t3, total_t4=total_t4, reverse_t3=reverse_t3, tpo_ab=tpo_ab, tg_ab=tg_ab,
|
| 197 |
+
cortisol=cortisol, acth=acth, igf1=igf1, leptin=leptin, adiponectin=adiponectin,
|
| 198 |
+
ca125=ca125, ca15_3=ca15_3, ca19_9=ca19_9, psa=psa, cea=cea, calcitonin=calcitonin, afp=afp, tnf=tnf,
|
| 199 |
+
ana=ana, ige=ige, igg=igg, anti_ccp=anti_ccp, dsdna=dsdna, ssa_ssb=ssa_ssb, rnp=rnp, sm_antibodies=sm_antibodies, anca=anca, anti_ena=anti_ena, il6=il6, allergy_panel=allergy_panel
|
| 200 |
+
)
|
| 201 |
+
return generate_report(req)
|
| 202 |
+
|
| 203 |
+
# ---------------- Gradio UI ----------------
|
| 204 |
+
with gr.Blocks(theme="soft", title="LLM Biomarker Analyzer") as iface:
|
| 205 |
+
gr.Markdown("## 🧬 LLM Biomarker Analyzer")
|
| 206 |
+
gr.Markdown("Enter your biomarker and demographic data below to generate a **Gemini-powered medical insight report**:")
|
| 207 |
+
|
| 208 |
+
# Individual inputs
|
| 209 |
+
age = gr.Number(label="Age (years)", value=52)
|
| 210 |
+
gender = gr.Radio(["male", "female"], label="Gender", value="female")
|
| 211 |
+
height = gr.Number(label="Height (cm)", value=165)
|
| 212 |
+
weight = gr.Number(label="Weight (kg)", value=70)
|
| 213 |
+
|
| 214 |
+
biomarker_inputs = [
|
| 215 |
+
# Kidney / Electrolytes
|
| 216 |
+
gr.Number(label="Urea", value=25.0), gr.Number(label="Creatinine", value=1.0),
|
| 217 |
+
gr.Number(label="Uric Acid", value=5.0), gr.Number(label="Calcium", value=9.5),
|
| 218 |
+
gr.Number(label="Phosphorus", value=3.5), gr.Number(label="Sodium", value=140.0),
|
| 219 |
+
gr.Number(label="Potassium", value=4.2), gr.Number(label="Chloride", value=100.0),
|
| 220 |
+
gr.Number(label="Bicarbonate", value=24.0), gr.Number(label="eGFR", value=100.0),
|
| 221 |
+
gr.Number(label="Serum Osmolality", value=285.0), gr.Number(label="Ionized Calcium", value=1.2),
|
| 222 |
+
gr.Number(label="Amylase", value=50.0), gr.Number(label="Lipase", value=50.0),
|
| 223 |
+
|
| 224 |
+
# Hematology
|
| 225 |
+
gr.Number(label="WBC", value=6.0), gr.Number(label="Hemoglobin", value=14.0),
|
| 226 |
+
gr.Number(label="MCV", value=90.0), gr.Number(label="RDW", value=13.0),
|
| 227 |
+
gr.Number(label="Lymphocytes", value=30.0),
|
| 228 |
+
|
| 229 |
+
# Glycemic / Metabolic
|
| 230 |
+
gr.Number(label="Fasting Blood Sugar", value=85.0), gr.Number(label="HbA1c", value=5.4),
|
| 231 |
+
gr.Number(label="Insulin", value=10.0), gr.Number(label="C-Peptide", value=1.2),
|
| 232 |
+
gr.Number(label="HOMA-IR", value=1.2),
|
| 233 |
+
|
| 234 |
+
# Lipid Profile
|
| 235 |
+
gr.Number(label="Total Cholesterol", value=180.0), gr.Number(label="LDL", value=90.0),
|
| 236 |
+
gr.Number(label="HDL", value=50.0), gr.Number(label="Cholesterol/HDL Ratio", value=3.0),
|
| 237 |
+
gr.Number(label="Triglycerides", value=120.0), gr.Number(label="Apo A1", value=150.0),
|
| 238 |
+
gr.Number(label="Apo B", value=70.0), gr.Number(label="Apo Ratio", value=0.5),
|
| 239 |
+
|
| 240 |
+
# Liver
|
| 241 |
+
gr.Number(label="Albumin", value=4.5), gr.Number(label="Total Protein", value=7.0),
|
| 242 |
+
gr.Number(label="ALT", value=25.0), gr.Number(label="AST", value=24.0),
|
| 243 |
+
gr.Number(label="ALP", value=80.0), gr.Number(label="GGT", value=20.0),
|
| 244 |
+
gr.Number(label="LD", value=180.0), gr.Number(label="Globulin", value=2.5),
|
| 245 |
+
gr.Number(label="Albumin/Globulin Ratio", value=1.8), gr.Number(label="Magnesium", value=2.0),
|
| 246 |
+
gr.Number(label="Total Bilirubin", value=0.7), gr.Number(label="Direct Bilirubin", value=0.2),
|
| 247 |
+
gr.Number(label="Indirect Bilirubin", value=0.5), gr.Number(label="Ammonia", value=30.0),
|
| 248 |
+
|
| 249 |
+
# Cardiac / Inflammation
|
| 250 |
+
gr.Number(label="hs-CRP", value=1.0), gr.Number(label="CK", value=100.0),
|
| 251 |
+
gr.Number(label="CK-MB", value=10.0), gr.Number(label="Homocysteine", value=10.0),
|
| 252 |
+
gr.Number(label="Zinc", value=90.0), gr.Number(label="Copper", value=100.0), gr.Number(label="Selenium", value=100.0),
|
| 253 |
+
|
| 254 |
+
# Iron / Minerals
|
| 255 |
+
gr.Number(label="Iron", value=100.0), gr.Number(label="TIBC", value=350.0), gr.Number(label="Transferrin", value=250.0),
|
| 256 |
+
|
| 257 |
+
# Vitamins
|
| 258 |
+
gr.Number(label="Vitamin D", value=35.0), gr.Number(label="Vitamin B12", value=500.0),
|
| 259 |
+
|
| 260 |
+
# Sex Hormones
|
| 261 |
+
gr.Number(label="Total Testosterone", value=450.0), gr.Number(label="Free Testosterone", value=15.0),
|
| 262 |
+
gr.Number(label="Estrogen", value=60.0), gr.Number(label="Progesterone", value=1.0),
|
| 263 |
+
gr.Number(label="DHEA-S", value=250.0), gr.Number(label="SHBG", value=40.0),
|
| 264 |
+
gr.Number(label="LH", value=5.0), gr.Number(label="FSH", value=6.0),
|
| 265 |
+
|
| 266 |
+
# Thyroid
|
| 267 |
+
gr.Number(label="TSH", value=2.0), gr.Number(label="Free T3", value=3.2), gr.Number(label="Free T4", value=1.2),
|
| 268 |
+
gr.Number(label="Total T3", value=120.0), gr.Number(label="Total T4", value=8.0), gr.Number(label="Reverse T3", value=15.0),
|
| 269 |
+
gr.Number(label="TPO-Ab", value=20.0), gr.Number(label="TG-Ab", value=20.0),
|
| 270 |
+
|
| 271 |
+
# Adrenal / Stress
|
| 272 |
+
gr.Number(label="Cortisol", value=12.0), gr.Number(label="ACTH", value=30.0),
|
| 273 |
+
gr.Number(label="IGF-1", value=200.0), gr.Number(label="Leptin", value=15.0), gr.Number(label="Adiponectin", value=15.0),
|
| 274 |
+
|
| 275 |
+
# Cancer markers
|
| 276 |
+
gr.Number(label="CA125", value=20.0), gr.Number(label="CA15-3", value=25.0), gr.Number(label="CA19-9", value=20.0),
|
| 277 |
+
gr.Number(label="PSA", value=2.0), gr.Number(label="CEA", value=2.0), gr.Number(label="Calcitonin", value=5.0),
|
| 278 |
+
gr.Number(label="AFP", value=5.0), gr.Number(label="TNF", value=5.0),
|
| 279 |
+
|
| 280 |
+
# Immune / Autoimmune
|
| 281 |
+
gr.Number(label="ANA", value=0.0), gr.Number(label="IgE", value=50.0), gr.Number(label="IgG", value=1000.0),
|
| 282 |
+
gr.Number(label="Anti-CCP", value=10.0), gr.Number(label="dsDNA", value=5.0), gr.Number(label="SSA/SSB", value=0.0),
|
| 283 |
+
gr.Number(label="RNP", value=0.0), gr.Number(label="Sm Antibodies", value=0.0), gr.Number(label="ANCA", value=0.0),
|
| 284 |
+
gr.Number(label="Anti-ENA", value=0.0), gr.Number(label="IL6", value=5.0), gr.Number(label="Allergy Panel", value=1.0)
|
| 285 |
+
]
|
| 286 |
+
|
| 287 |
+
submit_btn = gr.Button("🧠 Generate Medical Report", variant="primary")
|
| 288 |
+
output_md = gr.Markdown(label="AI-Generated Medical Report")
|
| 289 |
+
|
| 290 |
+
submit_btn.click(fn=gradio_interface, inputs=[age, gender, height, weight] + biomarker_inputs, outputs=output_md)
|
| 291 |
+
|
| 292 |
+
# ---------------- Launch ----------------
|
| 293 |
+
if __name__ == "__main__":
|
| 294 |
+
iface.launch(server_name="0.0.0.0", server_port=7861, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|