Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,13 +148,222 @@ class BiomarkerRequest(BaseModel):
|
|
| 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()
|
|
|
|
| 148 |
|
| 149 |
# ---------------- Gemini Report ----------------
|
| 150 |
def generate_report(data: BiomarkerRequest) -> str:
|
| 151 |
+
|
| 152 |
user_message = f"""
|
| 153 |
Patient Info:
|
| 154 |
- Age: {data.age}, Gender: {data.gender}, Height: {data.height}, Weight: {data.weight}
|
| 155 |
Biomarkers: {data.dict()}
|
| 156 |
"""
|
| 157 |
+
prompt = """
|
| 158 |
+
You are an advanced **Medical Insight Generation AI** trained to analyze **biomarkers and lab results**.
|
| 159 |
+
|
| 160 |
+
β οΈ IMPORTANT β OUTPUT FORMAT INSTRUCTIONS:
|
| 161 |
+
Return your report in this strict markdown structure.
|
| 162 |
+
|
| 163 |
+
------------------------------
|
| 164 |
+
### Executive Summary
|
| 165 |
+
**Top 3 Health Priorities:**
|
| 166 |
+
1. ...
|
| 167 |
+
2. ...
|
| 168 |
+
3. ...
|
| 169 |
+
make it more detailed
|
| 170 |
+
|
| 171 |
+
**Key Strengths:**
|
| 172 |
+
- ...
|
| 173 |
+
- ...
|
| 174 |
+
make it detailed
|
| 175 |
+
------------------------------
|
| 176 |
+
### System-Specific Analysis
|
| 177 |
+
|
| 178 |
+
**Cardiovascular System**
|
| 179 |
+
Status: Normal. Explanation: Lipid profile including Total Cholesterol, LDL, HDL, Triglycerides, Apo A-1, Apo B, Apo Ratio, and Cholesterol/HDL Ratio are within reference ranges, indicating low risk of atherosclerosis, coronary artery disease, and other cardiovascular disorders. hs-CRP, CK, CK-MB, and Homocysteine levels are normal, reflecting minimal systemic inflammation and proper myocardial health.
|
| 180 |
+
|
| 181 |
+
**Metabolic & Glycemic Control**
|
| 182 |
+
Status: Normal. Explanation: Fasting Blood Sugar, HbA1c, Insulin, C-Peptide, and HOMA-IR are within healthy ranges, suggesting effective glucose metabolism, insulin sensitivity, and low risk of prediabetes or diabetes.
|
| 183 |
+
|
| 184 |
+
**Liver Function**
|
| 185 |
+
Status: Normal. Explanation: ALT, AST, ALP, GGT, LDH, Total Bilirubin, Direct and Indirect Bilirubin, Albumin, Globulin, Albumin/Globulin Ratio, Total Protein, Ammonia, and Magnesium are within reference ranges, reflecting normal hepatocellular integrity, protein synthesis, and biliary excretion. Abnormalities could indicate hepatic injury, cholestasis, or metabolic liver disorders.
|
| 186 |
+
|
| 187 |
+
**Renal Function**
|
| 188 |
+
Status: Normal. Explanation: Urea, Creatinine, eGFR, Uric Acid, Sodium, Potassium, Chloride, Phosphorus, Calcium, Ionized Calcium, Bicarbonate, Serum Osmolality, Amylase, and Lipase are within expected ranges, suggesting proper kidney filtration, electrolyte balance, and pancreatic enzyme activity. Deviations may indicate renal impairment, electrolyte disorders, or pancreatitis risk.
|
| 189 |
+
|
| 190 |
+
**Thyroid Function**
|
| 191 |
+
Status: Normal. Explanation: TSH, Free T3, Free T4, Total T3, Total T4, Reverse T3, TPO Ab, and TG Ab are within reference limits, showing normal thyroid hormone production, peripheral conversion, and autoimmune status. Abnormal levels may indicate hypothyroidism, hyperthyroidism, or thyroid autoimmunity.
|
| 192 |
+
|
| 193 |
+
**Adrenal & Stress Hormones**
|
| 194 |
+
Status: Normal. Explanation: Cortisol, ACTH, DHEA-S, IGF-1, Leptin, and Adiponectin are within normal ranges, reflecting healthy adrenal function, stress response, metabolic regulation, and energy homeostasis. Abnormalities could indicate adrenal insufficiency, Cushingβs syndrome, metabolic disorders, or leptin/adiponectin imbalance.
|
| 195 |
+
|
| 196 |
+
**Sex Hormones & Reproductive Health**
|
| 197 |
+
Status: Normal. Explanation: Total Testosterone, Free Testosterone, SHBG, Estrogen, Progesterone, LH, and FSH are within expected ranges based on gender and menstrual cycle, indicating balanced gonadal function, fertility potential, and hormonal homeostasis. Deviations may impact reproductive function, libido, or secondary sexual characteristics.
|
| 198 |
+
|
| 199 |
+
**Vitamins & Minerals**
|
| 200 |
+
Status: Normal. Explanation: Vitamin D, Vitamin B12, Iron, TIBC, Transferrin, Zinc, Copper, Selenium, and Magnesium are within reference ranges, supporting optimal hematologic function, enzymatic reactions, immune defense, and bone health. Deficiencies may lead to anemia, metabolic disturbances, or immune dysfunction.
|
| 201 |
+
|
| 202 |
+
**Hematology & Immune Function**
|
| 203 |
+
Status: Normal. Explanation: Hemoglobin, MCV, RDW, WBC, Lymphocytes, Albumin, Globulin, ANA, IgE, IgG, Anti-CCP, dsDNA, SSA/SSB, RNP, Sm Antibodies, ANCA, Anti-ENA, IL-6, and Allergy Panel are within normal limits, indicating proper oxygen transport, red blood cell morphology, and immune competence. Deviations could indicate anemia, infection, inflammation, or autoimmune conditions.
|
| 204 |
+
|
| 205 |
+
**Cancer Markers**
|
| 206 |
+
Status: Normal. Explanation: CA125, CA15-3, CA19-9, PSA, CEA, AFP, Calcitonin, and TNF are within reference ranges, suggesting low risk for malignancy or tumor activity. Elevated values may require further imaging or diagnostic evaluation.
|
| 207 |
+
|
| 208 |
+
**Inflammatory Markers**
|
| 209 |
+
Status: Normal. Explanation: hs-CRP, IL-6, and Homocysteine are within recommended ranges, reflecting low systemic inflammation and minimal cardiovascular or metabolic risk. Elevations may indicate chronic inflammation, autoimmune activity, or thrombotic risk.
|
| 210 |
+
|
| 211 |
+
------------------------------
|
| 212 |
+
### Personalized Action Plan
|
| 213 |
+
**Nutrition:** ...
|
| 214 |
+
make it detailed
|
| 215 |
+
**Lifestyle:** ...
|
| 216 |
+
make it detailed
|
| 217 |
+
**Testing:** ...
|
| 218 |
+
make it detailed
|
| 219 |
+
**Medical Consultation:** ...
|
| 220 |
+
make it detailed
|
| 221 |
+
------------------------------
|
| 222 |
+
### Interaction Alerts
|
| 223 |
+
- ...
|
| 224 |
+
- ...
|
| 225 |
+
make it detailed
|
| 226 |
+
------------------------------
|
| 227 |
+
### Normal Ranges
|
| 228 |
+
# Kidney Function
|
| 229 |
+
- Urea (S): 17β43 mg/dL
|
| 230 |
+
- Creatinine (Men): 0.74β1.35 mg/dL
|
| 231 |
+
- Creatinine (Women): 0.59β1.04 mg/dL
|
| 232 |
+
- Uric Acid (Men): 3.4β7.0 mg/dL
|
| 233 |
+
- Uric Acid (Women): 2.4β6.0 mg/dL
|
| 234 |
+
- Calcium (S): 8.5β10.5 mg/dL
|
| 235 |
+
- Phosphorus (S): 2.5β4.5 mg/dL
|
| 236 |
+
- Sodium (S): 135β145 mEq/L
|
| 237 |
+
- Potassium (S): 3.5β5.1 mEq/L
|
| 238 |
+
- Chloride (S): 98β107 mEq/L
|
| 239 |
+
- Bicarbonate (S): 22β28 mEq/L
|
| 240 |
+
- eGFR: β₯90 mL/min/1.73mΒ²
|
| 241 |
+
- Serum Osmolality: 275β295 mOsm/kg
|
| 242 |
+
- Ionized Calcium: 1.12β1.32 mmol/L
|
| 243 |
+
- Amylase (S): 23β85 U/L
|
| 244 |
+
- Lipase (S): 0β160 U/L
|
| 245 |
+
|
| 246 |
+
# Basic Checkup
|
| 247 |
+
- WBC: 4β10 Γ10^3/ΞΌL
|
| 248 |
+
- Hemoglobin: 13β17 g/dL
|
| 249 |
+
- MCV: 80β100 fL
|
| 250 |
+
- RDW: 11.5β14.5 %
|
| 251 |
+
- Lymphocytes: 20β40 %
|
| 252 |
+
|
| 253 |
+
# Diabetic Profile
|
| 254 |
+
- Fasting Blood Sugar: 70β99 mg/dL
|
| 255 |
+
- HbA1c: <5.7 %
|
| 256 |
+
- Insulin: 2β20 Β΅IU/mL
|
| 257 |
+
- C-Peptide: 0.5β2.0 ng/mL
|
| 258 |
+
- HOMA-IR: <1 Optimal, 1β2 Normal, >2 Insulin Resistance
|
| 259 |
+
|
| 260 |
+
# Lipid Profile
|
| 261 |
+
- Total Cholesterol: <200 mg/dL
|
| 262 |
+
- LDL: <100 mg/dL
|
| 263 |
+
- HDL (Men): β₯40 mg/dL
|
| 264 |
+
- HDL (Women): β₯50 mg/dL
|
| 265 |
+
- Triglycerides: <150 mg/dL
|
| 266 |
+
- Apo A-1: 120β160 mg/dL
|
| 267 |
+
- Apo B: <90 mg/dL
|
| 268 |
+
- Apo B/A1 ratio: 0.3β0.7
|
| 269 |
+
- Cholesterol/HDL Ratio: <3.5 Optimal
|
| 270 |
+
|
| 271 |
+
# Liver Function
|
| 272 |
+
- Albumin: 3.5β5.0 g/dL
|
| 273 |
+
- Total Protein: 6.0β8.3 g/dL
|
| 274 |
+
- ALT: 10β40 U/L
|
| 275 |
+
- AST: 10β40 U/L
|
| 276 |
+
- ALP: 44β147 U/L
|
| 277 |
+
- GGT: 8β61 U/L
|
| 278 |
+
- LDH: 140β280 U/L
|
| 279 |
+
- Globulin: 2.0β3.5 g/dL
|
| 280 |
+
- Albumin/Globulin Ratio: 1.1β2.5
|
| 281 |
+
- Magnesium: 1.7β2.2 mg/dL
|
| 282 |
+
- Total Bilirubin: 0.1β1.2 mg/dL
|
| 283 |
+
- Direct Bilirubin: 0.0β0.3 mg/dL
|
| 284 |
+
- Indirect Bilirubin: 0.2β0.9 mg/dL
|
| 285 |
+
- Ammonia: 15β45 Β΅mol/L
|
| 286 |
+
|
| 287 |
+
# Cardiac Profile
|
| 288 |
+
- hs-CRP: 1β3 mg/L
|
| 289 |
+
- CK: 40β200 U/L
|
| 290 |
+
- CK-MB: 0β25 U/L
|
| 291 |
+
- Homocysteine: 5β15 Β΅mol/L
|
| 292 |
+
|
| 293 |
+
# Minerals & Heavy Metals
|
| 294 |
+
- Zinc: 70β120 Β΅g/dL
|
| 295 |
+
- Copper: 70β140 Β΅g/dL
|
| 296 |
+
- Selenium: 70β150 Β΅g/L
|
| 297 |
+
|
| 298 |
+
# Iron Profile
|
| 299 |
+
- Iron (Men): 60β170 Β΅g/dL
|
| 300 |
+
- Iron (Women): 50β170 Β΅g/dL
|
| 301 |
+
- TIBC: 250β450 Β΅g/dL
|
| 302 |
+
- Transferrin: 200β360 mg/dL
|
| 303 |
+
|
| 304 |
+
# Vitamins
|
| 305 |
+
- Vitamin D: 30β60 ng/mL
|
| 306 |
+
- Vitamin B12: 200β900 pg/mL
|
| 307 |
+
|
| 308 |
+
# Hormones
|
| 309 |
+
- Total Testosterone (Men): 300β1000 ng/dL
|
| 310 |
+
- Total Testosterone (Women): 15β70 ng/dL
|
| 311 |
+
- Free Testosterone (Men): 5β21 pg/mL
|
| 312 |
+
- Free Testosterone (Women): 0.5β4.2 pg/mL
|
| 313 |
+
- Estrogen (Men): 10β40 pg/mL
|
| 314 |
+
- Estrogen (Women Follicular): 30β120 pg/mL
|
| 315 |
+
- Estrogen (Women Ovulation): 130β370 pg/mL
|
| 316 |
+
- Estrogen (Women Luteal): 70β250 pg/mL
|
| 317 |
+
- Estrogen (Women Postmenopause): <20β30 pg/mL
|
| 318 |
+
- Progesterone: 0.2β1.4 ng/mL
|
| 319 |
+
- SHBG (Men): 10β57 nmol/L
|
| 320 |
+
- SHBG (Women): 18β144 nmol/L
|
| 321 |
+
- LH: 1.7β8.6 IU/L
|
| 322 |
+
- FSH: 1.5β12.4 IU/L
|
| 323 |
+
- DHEA-S (Men): 280β640 Β΅g/dL
|
| 324 |
+
- DHEA-S (Women): 65β380 Β΅g/dL
|
| 325 |
+
- Cortisol (AM): 6β23 Β΅g/dL
|
| 326 |
+
- Cortisol (PM): 2β14 Β΅g/dL
|
| 327 |
+
- IGF-1: 100β300 ng/mL
|
| 328 |
+
- Leptin (Men): 0.5β8 ng/mL
|
| 329 |
+
- Leptin (Women): 5β25 ng/mL
|
| 330 |
+
- Adiponectin: 5β30 Β΅g/mL
|
| 331 |
+
|
| 332 |
+
# Thyroid
|
| 333 |
+
- TSH: 0.4β4.0 Β΅IU/mL
|
| 334 |
+
- Free T3: 2.0β4.4 pg/mL
|
| 335 |
+
- Free T4: 0.8β1.8 ng/dL
|
| 336 |
+
- Total T3: 80β180 ng/dL
|
| 337 |
+
- Total T4: 4.5β12 Β΅g/dL
|
| 338 |
+
- Reverse T3: 9β24 ng/dL
|
| 339 |
+
- TPO Ab: <35 IU/mL
|
| 340 |
+
- TG Ab: <40 IU/mL
|
| 341 |
+
|
| 342 |
+
# Cancer Markers
|
| 343 |
+
- CA125: <35 U/mL
|
| 344 |
+
- CA15-3: <30 U/mL
|
| 345 |
+
- CA19-9: <37 U/mL
|
| 346 |
+
- PSA: <4 ng/mL
|
| 347 |
+
- CEA: <5 ng/mL
|
| 348 |
+
- Calcitonin: <10 pg/mL
|
| 349 |
+
- AFP: <10 ng/mL
|
| 350 |
+
- TNF: <8 pg/m
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
------------------------------
|
| 355 |
+
### Tabular Mapping
|
| 356 |
+
| Biomarker | Value | Status | Insight | Reference Range |
|
| 357 |
+
| Albumin | X | Normal | ... | 3.5β5.0 g/dL |
|
| 358 |
+
| Creatinine | X | High | ... | 0.7β1.3 mg/dL |
|
| 359 |
+
| Glucose | X | ... | ... | 70β100 mg/dL |
|
| 360 |
+
------------------------------
|
| 361 |
+
"""
|
| 362 |
+
|
| 363 |
+
|
| 364 |
model = genai.GenerativeModel(MODEL_ID)
|
| 365 |
+
response = model.generate_content(f"{prompt}\n\n{user_message}")
|
| 366 |
+
## response = model.generate_content(user_message)
|
| 367 |
if not response or not getattr(response, "text", None):
|
| 368 |
return "β οΈ Gemini returned empty response."
|
| 369 |
return response.text.strip()
|