Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel,Field | |
| from dotenv import load_dotenv | |
| import google.generativeai as genai | |
| import os | |
| import re | |
| from typing import Dict, Any, Union, List | |
| from your_fastapi_module import gradio_interface # your predict function | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import gradio as gr | |
| # ---------------- Initialize ---------------- | |
| app = FastAPI(title="LLM Model API", version="3.4") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Or your domain | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ✅ Load environment variables | |
| # load_dotenv() | |
| # ✅ Fetch Gemini API Key | |
| GEMINI_API_KEY = "AIzaSyAJ3aMnwOHsLtU1JoudCRIFdZhm6s4oNhY" | |
| if not GEMINI_API_KEY: | |
| raise ValueError("❌ GEMINI_API_KEY not found. Please set it in your .env or environment variables.") | |
| # ✅ Configure Gemini Client | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| MODEL_ID = "gemini-2.5-flash" | |
| # ---------------- Schema ---------------- | |
| class BiomarkerRequest(BaseModel): | |
| # ---------------- Patient Info ---------------- | |
| id: str = Field(default="PT01", description="ID For Patient") | |
| age: int = Field(default=52, description="Patient age in years") | |
| gender: str = Field(default="female", description="Gender of the patient") | |
| height: float = Field(default=165, description="Height in cm") | |
| weight: float = Field(default=70, description="Weight in kg") | |
| # ---------------- Kidney Function ---------------- | |
| urea: float = Field(default=30.0, description="Urea (S) in mg/dL") | |
| creatinine: float = Field(default=1.0, description="Creatinine (S) in mg/dL") | |
| uric_acid: float = Field(default=5.0, description="Uric Acid (S) in mg/dL") | |
| calcium: float = Field(default=9.5, description="Calcium (S) in mg/dL") | |
| phosphorus: float = Field(default=3.5, description="Phosphorus (S) in mg/dL") | |
| sodium: float = Field(default=140.0, description="Sodium (S) in mEq/L") | |
| potassium: float = Field(default=4.2, description="Potassium (S) in mEq/L") | |
| chloride: float = Field(default=102.0, description="Chloride (S) in mEq/L") | |
| amylase: float = Field(default=70.0, description="Amylase (S) in U/L") | |
| lipase: float = Field(default=35.0, description="Lipase (S) in U/L") | |
| bicarbonate: float = Field(default=24.0, description="Bicarbonate (S) in mEq/L") | |
| egfr: float = Field(default=100.0, description="Estimated GFR (S) in mL/min/1.73m²") | |
| serum_osmolality: float = Field(default=290.0, description="Serum Osmolality (S) in mOsm/kg") | |
| ionized_calcium: float = Field(default=1.25, description="Ionized Calcium (S) in mmol/L") | |
| # ---------------- Basic Check-up ---------------- | |
| wbc: float = Field(default=6.0, description="White Blood Cell count (×10^3/μL)") | |
| hemoglobin: float = Field(default=14.0, description="Hemoglobin (g/dL)") | |
| mcv: float = Field(default=90.0, description="Mean Corpuscular Volume (fL)") | |
| rdw: float = Field(default=13.5, description="Red Cell Distribution Width (%)") | |
| lymphocytes: float = Field(default=30.0, description="Lymphocyte percentage (%)") | |
| # ---------------- Diabetic Profile ---------------- | |
| fasting_blood_sugar: float = Field(default=85.0, description="Fasting Blood Sugar (mg/dL)") | |
| hb1ac: float = Field(default=5.4, description="HbA1c (%)") | |
| insulin: float = Field(default=10.0, description="Insulin (µIU/mL)") | |
| c_peptide: float = Field(default=1.2, description="C-Peptide (ng/mL)") | |
| homa_ir: float = Field(default=1.2, description="HOMA-IR") | |
| # ---------------- Lipid Profile ---------------- | |
| total_cholesterol: float = Field(default=180.0, description="Total Cholesterol (mg/dL)") | |
| ldl: float = Field(default=90.0, description="LDL Cholesterol (mg/dL)") | |
| hdl: float = Field(default=50.0, description="HDL Direct (mg/dL)") | |
| cholesterol_hdl_ratio: float = Field(default=3.0, description="Cholesterol/HDL Ratio") | |
| triglycerides: float = Field(default=120.0, description="Triglycerides (mg/dL)") | |
| apo_a1: float = Field(default=140.0, description="Apo A-1 (mg/dL)") | |
| apo_b: float = Field(default=70.0, description="Apo B (mg/dL)") | |
| apo_ratio: float = Field(default=0.5, description="Apo B : Apo A-1 ratio") | |
| # ---------------- Liver Function ---------------- | |
| albumin: float = Field(default=4.2, description="Albumin (g/dL)") | |
| total_protein: float = Field(default=7.0, description="Total Protein (g/dL)") | |
| alt: float = Field(default=25.0, description="ALT (U/L)") | |
| ast: float = Field(default=24.0, description="AST (U/L)") | |
| alp: float = Field(default=120.0, description="ALP (U/L)") | |
| ggt: float = Field(default=20.0, description="GGT (U/L)") | |
| ld: float = Field(default=180.0, description="LDH (U/L)") | |
| globulin: float = Field(default=3.0, description="Globulin (g/dL)") | |
| albumin_globulin_ratio: float = Field(default=1.4, description="Albumin/Globulin Ratio") | |
| magnesium: float = Field(default=2.0, description="Magnesium (mg/dL)") | |
| total_bilirubin: float = Field(default=0.7, description="Total Bilirubin (mg/dL)") | |
| direct_bilirubin: float = Field(default=0.3, description="Direct Bilirubin (mg/dL)") | |
| indirect_bilirubin: float = Field(default=0.4, description="Indirect Bilirubin (mg/dL)") | |
| ammonia: float = Field(default=35.0, description="Ammonia (NH3) (µmol/L)") | |
| # ---------------- Cardiac Profile ---------------- | |
| hs_crp: float = Field(default=1.0, description="High-Sensitivity CRP (mg/L)") | |
| ck: float = Field(default=150.0, description="Creatine Kinase (U/L)") | |
| ck_mb: float = Field(default=20.0, description="CK-MB (U/L)") | |
| homocysteine: float = Field(default=10.0, description="Homocysteine (µmol/L)") | |
| # ---------------- Mineral & Heavy Metal ---------------- | |
| zinc: float = Field(default=90.0, description="Zinc (µg/dL)") | |
| copper: float = Field(default=100.0, description="Copper (µg/dL)") | |
| selenium: float = Field(default=120.0, description="Selenium (µg/L)") | |
| # ---------------- Iron Profile ---------------- | |
| iron: float = Field(default=100.0, description="Iron (µg/dL)") | |
| tibc: float = Field(default=300.0, description="TIBC (µg/dL)") | |
| transferrin: float = Field(default=250.0, description="Transferrin (mg/dL)") | |
| # ---------------- Vitamins ---------------- | |
| vitamin_d: float = Field(default=35.0, description="Vitamin D (ng/mL)") | |
| vitamin_b12: float = Field(default=500.0, description="Vitamin B12 (pg/mL)") | |
| # ---------------- Hormone Profile ---------------- | |
| total_testosterone: float = Field(default=450.0, description="Total Testosterone (ng/dL)") | |
| free_testosterone: float = Field(default=15.0, description="Free Testosterone (pg/mL)") | |
| estrogen: float = Field(default=60.0, description="Estrogen / Estradiol (pg/mL)") | |
| progesterone: float = Field(default=1.0, description="Progesterone (ng/mL)") | |
| dhea_s: float = Field(default=250.0, description="DHEA-S (µg/dL)") | |
| shbg: float = Field(default=40.0, description="SHBG (nmol/L)") | |
| lh: float = Field(default=5.0, description="LH (IU/L)") | |
| fsh: float = Field(default=6.0, description="FSH (IU/L)") | |
| # ---------------- Thyroid Profile ---------------- | |
| tsh: float = Field(default=2.0, description="TSH (µIU/mL)") | |
| free_t3: float = Field(default=3.2, description="Free T3 (pg/mL)") | |
| free_t4: float = Field(default=1.2, description="Free T4 (ng/dL)") | |
| total_t3: float = Field(default=120.0, description="Total T3 (ng/dL)") | |
| total_t4: float = Field(default=8.0, description="Total T4 (µg/dL)") | |
| reverse_t3: float = Field(default=15.0, description="Reverse T3 (ng/dL)") | |
| tpo_ab: float = Field(default=5.0, description="Thyroid Antibodies – TPO Ab (IU/mL)") | |
| tg_ab: float = Field(default=3.0, description="Thyroid Antibodies – TG Ab (IU/mL)") | |
| # ---------------- Adrenal / Stress / Other Hormones ---------------- | |
| cortisol: float = Field(default=12.0, description="Cortisol (µg/dL)") | |
| acth: float = Field(default=25.0, description="ACTH (pg/mL)") | |
| igf1: float = Field(default=200.0, description="IGF-1 (ng/mL)") | |
| leptin: float = Field(default=10.0, description="Leptin (ng/mL)") | |
| adiponectin: float = Field(default=10.0, description="Adiponectin (µg/mL)") | |
| # ---------------- Blood Marker Cancer Profile ---------------- | |
| ca125: float = Field(default=20.0, description="CA125 (U/mL)") | |
| ca15_3: float = Field(default=25.0, description="CA15-3 (U/mL)") | |
| ca19_9: float = Field(default=30.0, description="CA19-9 (U/mL)") | |
| psa: float = Field(default=1.0, description="PSA (ng/mL)") | |
| cea: float = Field(default=2.0, description="CEA (ng/mL)") | |
| calcitonin: float = Field(default=5.0, description="Calcitonin (pg/mL)") | |
| afp: float = Field(default=5.0, description="AFP (ng/mL)") | |
| tnf: float = Field(default=2.0, description="Tumor Necrosis Factor (pg/mL)") | |
| # ---------------- Immune Profile ---------------- | |
| ana: float = Field(default=0.5, description="ANA (IU/mL)") | |
| ige: float = Field(default=100.0, description="IgE (IU/mL)") | |
| igg: float = Field(default=1200.0, description="IgG (mg/dL)") | |
| anti_ccp: float = Field(default=10.0, description="Anti-CCP (U/mL)") | |
| dsdna: float = Field(default=0.5, description="dsDNA (IU/mL)") | |
| ssa_ssb: float = Field(default=5.0, description="SSA/SSB (IU/mL)") | |
| rnp: float = Field(default=1.0, description="RNP (IU/mL)") | |
| sm_antibodies: float = Field(default=0.5, description="Sm Antibodies (IU/mL)") | |
| anca: float = Field(default=0.5, description="ANCA (IU/mL)") | |
| anti_ena: float = Field(default=0.5, description="Anti-ENA (IU/mL)") | |
| il6: float = Field(default=3.0, description="IL-6 (pg/mL)") | |
| allergy_panel: float = Field(default=10.0, description="Comprehensive Allergy Profile (IgE & Food Sensitivity IgG)") | |
| # ---------------- Cleaning Utility ---------------- | |
| def clean_json(data: Union[Dict, List, str]) -> Union[Dict, List, str]: | |
| """Recursively removes separators, extra whitespace, and artifacts from all string values.""" | |
| if isinstance(data, str): | |
| text = re.sub(r"-{3,}", "", data) | |
| text = re.sub(r"\s+", " ", text) | |
| text = text.strip(" -\n\t\r") | |
| return text | |
| elif isinstance(data, list): | |
| return [clean_json(i) for i in data if i and clean_json(i)] | |
| elif isinstance(data, dict): | |
| return {k.strip(): clean_json(v) for k, v in data.items()} | |
| return data | |
| # ---------------- Parser ---------------- | |
| def parse_medical_report(text: str): | |
| """ | |
| Parses Gemini markdown response → structured JSON. | |
| Detects section headers, **bold keys**, and table entries. | |
| """ | |
| def clean_line(line: str) -> str: | |
| return re.sub(r"[\-\*\u2022]+\s*", "", line.strip()) | |
| def parse_bold_entities(block: str) -> Dict[str, str]: | |
| """Extracts **bold** entities and maps text until next bold or section.""" | |
| entities = {} | |
| pattern = re.compile(r"\*\*(.*?)\*\*(.*?)(?=\*\*|###|$)", re.S) | |
| for match in pattern.finditer(block): | |
| key = match.group(1).strip().strip(":") | |
| val = match.group(2).strip().replace("\n", " ") | |
| val = re.sub(r"\s+", " ", val) | |
| if key: | |
| entities[key] = val | |
| return entities | |
| data = { | |
| "executive_summary": {"top_priorities": [], "key_strengths": []}, | |
| "system_analysis": {}, | |
| "personalized_action_plan": {}, | |
| "interaction_alerts": [], | |
| "normal_ranges": {}, | |
| "biomarker_table": [] | |
| } | |
| # --- Executive Summary --- | |
| exec_match = re.search(r"###\s*Executive Summary(.*?)(?=###|$)", text, re.S | re.I) | |
| if exec_match: | |
| block = exec_match.group(1) | |
| priorities = re.findall(r"\d+\.\s*(.*?)\n", block) | |
| if priorities: | |
| data["executive_summary"]["top_priorities"] = [clean_line(p) for p in priorities] | |
| strengths_match = re.search(r"\*\*Key Strengths:\*\*(.*)", block, re.S) | |
| if strengths_match: | |
| strengths_text = strengths_match.group(1) | |
| strengths = [clean_line(s) for s in strengths_text.splitlines() if clean_line(s)] | |
| data["executive_summary"]["key_strengths"] = strengths | |
| # --- System Analysis --- | |
| sys_match = re.search(r"###\s*System[- ]Specific Analysis(.*?)(?=###|$)", text, re.S | re.I) | |
| if sys_match: | |
| sys_block = sys_match.group(1) | |
| data["system_analysis"] = parse_bold_entities(sys_block) | |
| # --- Personalized Action Plan --- | |
| plan_match = re.search(r"###\s*Personalized Action Plan(.*?)(?=###|$)", text, re.S | re.I) | |
| if plan_match: | |
| plan_block = plan_match.group(1) | |
| data["personalized_action_plan"] = parse_bold_entities(plan_block) | |
| # --- Interaction Alerts --- | |
| alerts_match = re.search(r"###\s*Interaction Alerts(.*?)(?=###|$)", text, re.S | re.I) | |
| if alerts_match: | |
| alerts_block = alerts_match.group(1) | |
| alerts = [clean_line(a) for a in alerts_block.splitlines() if clean_line(a)] | |
| data["interaction_alerts"] = alerts | |
| # --- Normal Ranges --- | |
| normal_match = re.search(r"###\s*Normal Ranges(.*?)(?=###|$)", text, re.S | re.I) | |
| if normal_match: | |
| normal_block = normal_match.group(1) | |
| for match in re.findall(r"-\s*([^:]+):\s*([^\n]+)", normal_block): | |
| biomarker, rng = match | |
| data["normal_ranges"][biomarker.strip()] = rng.strip() | |
| # --- Tabular Mapping --- | |
| table_match = re.search(r"###\s*Tabular Mapping(.*)", text, re.S | re.I) | |
| if table_match: | |
| table_block = table_match.group(1) | |
| # robust row matcher: capture any table rows with 5 pipe-separated columns | |
| table_pattern = r"\|\s*([^|]+)\s*\|\s*([^|]+)\s*\|\s*([^|]+)\s*\|\s*([^|]+)\s*\|\s*([^|]+)\s*\|" | |
| for biomarker, value, status, insight, ref in re.findall(table_pattern, table_block): | |
| # normalize | |
| biomarker_s = biomarker.strip() | |
| value_s = value.strip() | |
| status_s = status.strip() | |
| insight_s = insight.strip() | |
| ref_s = ref.strip() | |
| # ---------- ONLY SKIP rows where ALL five fields are empty ---------- | |
| if not any([biomarker_s, value_s, status_s, insight_s, ref_s]): | |
| # This is the empty-row you showed: skip it and continue | |
| continue | |
| # ---------- ALSO SKIP rows that are pure separator artifacts ---------- | |
| # e.g., ":-----------" or "--------" in biomarker column (common AI artifacts) | |
| def is_separator_cell(s: str) -> bool: | |
| # treat as separator if contains no alphanumeric chars | |
| return not bool(re.search(r"[A-Za-z0-9]", s)) | |
| if all(is_separator_cell(c) for c in [biomarker_s, value_s, status_s, insight_s, ref_s]): | |
| continue | |
| # ---------- Append the cleaned/valid row ---------- | |
| data["biomarker_table"].append({ | |
| "biomarker": biomarker_s, | |
| "value": value_s, | |
| "status": status_s, | |
| "insight": insight_s, | |
| "reference_range": ref_s, | |
| }) | |
| return data | |
| # ---------------- Endpoint ---------------- | |
| def predict(data: BiomarkerRequest): | |
| """Accepts biomarker input and returns structured and complete detailed medical insights.""" | |
| try: | |
| # --- Prompt Template --- | |
| prompt = """ | |
| You are an advanced **Medical Insight Generation AI** trained to analyze **biomarkers and lab results**. | |
| ⚠️ IMPORTANT — OUTPUT FORMAT INSTRUCTIONS: | |
| Return your report in this strict markdown structure. | |
| ------------------------------ | |
| ### Executive Summary | |
| **Top 3 Health Priorities:** | |
| 1. ... | |
| 2. ... | |
| 3. ... | |
| make it more detailed | |
| **Key Strengths:** | |
| - ... | |
| - ... | |
| make it detailed | |
| ------------------------------ | |
| ### System-Specific Analysis | |
| **Cardiovascular System** | |
| 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. | |
| **Metabolic & Glycemic Control** | |
| 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. | |
| **Liver Function** | |
| 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. | |
| **Renal Function** | |
| 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. | |
| **Thyroid Function** | |
| 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. | |
| **Adrenal & Stress Hormones** | |
| 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. | |
| **Sex Hormones & Reproductive Health** | |
| 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. | |
| **Vitamins & Minerals** | |
| 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. | |
| **Hematology & Immune Function** | |
| 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. | |
| **Cancer Markers** | |
| 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. | |
| **Inflammatory Markers** | |
| 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. | |
| ------------------------------ | |
| ### Personalized Action Plan | |
| **Nutrition:** ... | |
| make it detailed | |
| **Lifestyle:** ... | |
| make it detailed | |
| **Testing:** ... | |
| make it detailed | |
| **Medical Consultation:** ... | |
| make it detailed | |
| ------------------------------ | |
| ### Interaction Alerts | |
| - ... | |
| - ... | |
| make it detailed | |
| ------------------------------ | |
| ### Normal Ranges | |
| # Kidney Function | |
| - Urea (S): 17–43 mg/dL | |
| - Creatinine (Men): 0.74–1.35 mg/dL | |
| - Creatinine (Women): 0.59–1.04 mg/dL | |
| - Uric Acid (Men): 3.4–7.0 mg/dL | |
| - Uric Acid (Women): 2.4–6.0 mg/dL | |
| - Calcium (S): 8.5–10.5 mg/dL | |
| - Phosphorus (S): 2.5–4.5 mg/dL | |
| - Sodium (S): 135–145 mEq/L | |
| - Potassium (S): 3.5–5.1 mEq/L | |
| - Chloride (S): 98–107 mEq/L | |
| - Bicarbonate (S): 22–28 mEq/L | |
| - eGFR: ≥90 mL/min/1.73m² | |
| - Serum Osmolality: 275–295 mOsm/kg | |
| - Ionized Calcium: 1.12–1.32 mmol/L | |
| - Amylase (S): 23–85 U/L | |
| - Lipase (S): 0–160 U/L | |
| # Basic Checkup | |
| - WBC: 4–10 ×10^3/μL | |
| - Hemoglobin: 13–17 g/dL | |
| - MCV: 80–100 fL | |
| - RDW: 11.5–14.5 % | |
| - Lymphocytes: 20–40 % | |
| # Diabetic Profile | |
| - Fasting Blood Sugar: 70–99 mg/dL | |
| - HbA1c: <5.7 % | |
| - Insulin: 2–20 µIU/mL | |
| - C-Peptide: 0.5–2.0 ng/mL | |
| - HOMA-IR: <1 Optimal, 1–2 Normal, >2 Insulin Resistance | |
| # Lipid Profile | |
| - Total Cholesterol: <200 mg/dL | |
| - LDL: <100 mg/dL | |
| - HDL (Men): ≥40 mg/dL | |
| - HDL (Women): ≥50 mg/dL | |
| - Triglycerides: <150 mg/dL | |
| - Apo A-1: 120–160 mg/dL | |
| - Apo B: <90 mg/dL | |
| - Apo B/A1 ratio: 0.3–0.7 | |
| - Cholesterol/HDL Ratio: <3.5 Optimal | |
| # Liver Function | |
| - Albumin: 3.5–5.0 g/dL | |
| - Total Protein: 6.0–8.3 g/dL | |
| - ALT: 10–40 U/L | |
| - AST: 10–40 U/L | |
| - ALP: 44–147 U/L | |
| - GGT: 8–61 U/L | |
| - LDH: 140–280 U/L | |
| - Globulin: 2.0–3.5 g/dL | |
| - Albumin/Globulin Ratio: 1.1–2.5 | |
| - Magnesium: 1.7–2.2 mg/dL | |
| - Total Bilirubin: 0.1–1.2 mg/dL | |
| - Direct Bilirubin: 0.0–0.3 mg/dL | |
| - Indirect Bilirubin: 0.2–0.9 mg/dL | |
| - Ammonia: 15–45 µmol/L | |
| # Cardiac Profile | |
| - hs-CRP: 1–3 mg/L | |
| - CK: 40–200 U/L | |
| - CK-MB: 0–25 U/L | |
| - Homocysteine: 5–15 µmol/L | |
| # Minerals & Heavy Metals | |
| - Zinc: 70–120 µg/dL | |
| - Copper: 70–140 µg/dL | |
| - Selenium: 70–150 µg/L | |
| # Iron Profile | |
| - Iron (Men): 60–170 µg/dL | |
| - Iron (Women): 50–170 µg/dL | |
| - TIBC: 250–450 µg/dL | |
| - Transferrin: 200–360 mg/dL | |
| # Vitamins | |
| - Vitamin D: 30–60 ng/mL | |
| - Vitamin B12: 200–900 pg/mL | |
| # Hormones | |
| - Total Testosterone (Men): 300–1000 ng/dL | |
| - Total Testosterone (Women): 15–70 ng/dL | |
| - Free Testosterone (Men): 5–21 pg/mL | |
| - Free Testosterone (Women): 0.5–4.2 pg/mL | |
| - Estrogen (Men): 10–40 pg/mL | |
| - Estrogen (Women Follicular): 30–120 pg/mL | |
| - Estrogen (Women Ovulation): 130–370 pg/mL | |
| - Estrogen (Women Luteal): 70–250 pg/mL | |
| - Estrogen (Women Postmenopause): <20–30 pg/mL | |
| - Progesterone: 0.2–1.4 ng/mL | |
| - SHBG (Men): 10–57 nmol/L | |
| - SHBG (Women): 18–144 nmol/L | |
| - LH: 1.7–8.6 IU/L | |
| - FSH: 1.5–12.4 IU/L | |
| - DHEA-S (Men): 280–640 µg/dL | |
| - DHEA-S (Women): 65–380 µg/dL | |
| - Cortisol (AM): 6–23 µg/dL | |
| - Cortisol (PM): 2–14 µg/dL | |
| - IGF-1: 100–300 ng/mL | |
| - Leptin (Men): 0.5–8 ng/mL | |
| - Leptin (Women): 5–25 ng/mL | |
| - Adiponectin: 5–30 µg/mL | |
| # Thyroid | |
| - TSH: 0.4–4.0 µIU/mL | |
| - Free T3: 2.0–4.4 pg/mL | |
| - Free T4: 0.8–1.8 ng/dL | |
| - Total T3: 80–180 ng/dL | |
| - Total T4: 4.5–12 µg/dL | |
| - Reverse T3: 9–24 ng/dL | |
| - TPO Ab: <35 IU/mL | |
| - TG Ab: <40 IU/mL | |
| # Cancer Markers | |
| - CA125: <35 U/mL | |
| - CA15-3: <30 U/mL | |
| - CA19-9: <37 U/mL | |
| - PSA: <4 ng/mL | |
| - CEA: <5 ng/mL | |
| - Calcitonin: <10 pg/mL | |
| - AFP: <10 ng/mL | |
| - TNF: <8 pg/m | |
| ------------------------------ | |
| ### Tabular Mapping | |
| | Biomarker | Value | Status | Insight | Reference Range | | |
| | Albumin | X | Normal | ... | 3.5–5.0 g/dL | | |
| | Creatinine | X | High | ... | 0.7–1.3 mg/dL | | |
| | Glucose | X | ... | ... | 70–100 mg/dL | | |
| ------------------------------ | |
| """ | |
| # --- Format User Data --- | |
| user_message = f""" | |
| **Patient Info** | |
| - Id: {data.id} | |
| - Age: {data.age} | |
| - Gender: {data.gender} | |
| - Height: {data.height} cm | |
| - Weight: {data.weight} kg | |
| **Metabolic & Glycemic Control** | |
| - Fasting Blood Sugar: {data.fasting_blood_sugar} mg/dL | |
| - HbA1c: {data.hb1ac} % | |
| - Insulin: {data.insulin} µIU/mL | |
| - C-Peptide: {data.c_peptide} ng/mL | |
| - HOMA-IR: {data.homa_ir} | |
| - Leptin: {data.leptin} ng/mL | |
| **Cardiovascular System** | |
| - Total Cholesterol: {data.total_cholesterol} mg/dL | |
| - LDL: {data.ldl} mg/dL | |
| - HDL: {data.hdl} mg/dL | |
| - Triglycerides: {data.triglycerides} mg/dL | |
| - ApoB: {data.apo_b} mg/dL | |
| - Cholesterol/HDL Ratio: {data.cholesterol_hdl_ratio} | |
| - hs-CRP: {data.hs_crp} mg/L | |
| - Homocysteine: {data.homocysteine} µmol/L | |
| **Liver Function** | |
| - ALT: {data.alt} U/L | |
| - AST: {data.ast} U/L | |
| - GGT: {data.ggt} U/L | |
| - Total Bilirubin: {data.total_bilirubin} mg/dL | |
| - Total Protein: {data.total_protein} g/dL | |
| **Renal Function** | |
| - Creatinine: {data.creatinine} mg/dL | |
| - eGFR: {data.egfr} mL/min/1.73m2 | |
| - Uric Acid: {data.uric_acid} mg/dL | |
| **Vitamins & Minerals** | |
| - Vitamin D: {data.vitamin_d} ng/mL | |
| - Vitamin B12: {data.vitamin_b12} pg/mL | |
| - Iron: {data.iron} µg/dL | |
| - Zinc: {data.zinc} µg/dL | |
| **Thyroid Function** | |
| - TSH: {data.tsh} µIU/mL | |
| - Free T3: {data.free_t3} pg/mL | |
| - Free T4: {data.free_t4} ng/dL | |
| **Sex Hormones & Reproductive Health** | |
| - Total Testosterone: {data.total_testosterone} ng/dL | |
| - Free Testosterone: {data.free_testosterone} pg/mL | |
| - Estrogen (Estradiol): {data.estrogen} pg/mL | |
| - SHBG: {data.shbg} nmol/L | |
| **Adrenal & Stress Hormones** | |
| - Cortisol: {data.cortisol} µg/dL | |
| - DHEA-S: {data.dhea_s} µg/dL | |
| **Autoimmune / Inflammatory Markers** | |
| - Anti-CCP: {data.anti_ccp} U/mL | |
| """ | |
| # --- Gemini Call --- | |
| model = genai.GenerativeModel(MODEL_ID) | |
| response = model.generate_content(f"{prompt}\n\n{user_message}") | |
| if not response or not getattr(response, "text", None): | |
| raise ValueError("Empty response from Gemini model.") | |
| report_text = response.text.strip() | |
| # --- Parse + Clean --- | |
| parsed_output = parse_medical_report(report_text) | |
| cleaned_output = clean_json(parsed_output) | |
| return cleaned_output | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}") | |
| with gr.Blocks(theme="soft", title="LLM Biomarker Analyzer") as iface: | |
| gr.Markdown("# 🧬 LLM Biomarker Analyzer") | |
| gr.Markdown("Enter your full biomarker and demographic data below to generate a **Gemini-powered comprehensive medical insight report**:") | |
| with gr.Column(): | |
| with gr.Accordion("👤 Demographics", open=True): | |
| age = gr.Number(label="Age (years)", value=52) | |
| gender = gr.Radio(["male", "female"], label="Gender", value="female") | |
| height = gr.Number(label="Height (cm)", value=165) | |
| weight = gr.Number(label="Weight (kg)", value=70) | |
| with gr.Accordion("🩸 Basic Chemistry & Kidney Function", open=False): | |
| urea = gr.Number(label="Urea (mg/dL)", value=30) | |
| creatinine = gr.Number(label="Creatinine (mg/dL)", value=1.0) | |
| uric_acid = gr.Number(label="Uric Acid (mg/dL)", value=5.0) | |
| egfr = gr.Number(label="eGFR (mL/min)", value=100) | |
| amylase = gr.Number(label="Amylase (U/L)", value=70) | |
| lipase = gr.Number(label="Lipase (U/L)", value=50) | |
| bicarbonate = gr.Number(label="Bicarbonate (mmol/L)", value=24) | |
| serum_osmolality = gr.Number(label="Serum Osmolality (mOsm/kg)", value=290) | |
| with gr.Accordion("⚡ Electrolytes & Minerals", open=False): | |
| sodium = gr.Number(label="Sodium (mEq/L)", value=140) | |
| potassium = gr.Number(label="Potassium (mEq/L)", value=4.2) | |
| chloride = gr.Number(label="Chloride (mEq/L)", value=102) | |
| calcium = gr.Number(label="Calcium (mg/dL)", value=9.5) | |
| phosphorus = gr.Number(label="Phosphorus (mg/dL)", value=3.5) | |
| magnesium = gr.Number(label="Magnesium (mg/dL)", value=2.0) | |
| ionized_calcium = gr.Number(label="Ionized Calcium (mmol/L)", value=1.25) | |
| with gr.Accordion("🥼 Liver & Proteins", open=False): | |
| albumin = gr.Number(label="Albumin (g/dL)", value=4.2) | |
| total_protein = gr.Number(label="Total Protein (g/dL)", value=7.0) | |
| globulin = gr.Number(label="Globulin (g/dL)", value=3.0) | |
| albumin_globulin_ratio = gr.Number(label="A/G Ratio", value=1.4) | |
| alt = gr.Number(label="ALT (U/L)", value=25) | |
| ast = gr.Number(label="AST (U/L)", value=24) | |
| alp = gr.Number(label="ALP (U/L)", value=120) | |
| ggt = gr.Number(label="GGT (U/L)", value=20) | |
| ld = gr.Number(label="LD (U/L)", value=180) | |
| total_bilirubin = gr.Number(label="Total Bilirubin (mg/dL)", value=1.0) | |
| direct_bilirubin = gr.Number(label="Direct Bilirubin (mg/dL)", value=0.3) | |
| indirect_bilirubin = gr.Number(label="Indirect Bilirubin (mg/dL)", value=0.7) | |
| ammonia = gr.Number(label="Ammonia (µmol/L)", value=35) | |
| with gr.Accordion("🩺 Hematology", open=False): | |
| wbc = gr.Number(label="WBC (×10³/μL)", value=6.0) | |
| hemoglobin = gr.Number(label="Hemoglobin (g/dL)", value=14.0) | |
| mcv = gr.Number(label="MCV (fL)", value=90) | |
| rdw = gr.Number(label="RDW (%)", value=13.5) | |
| lymphocytes = gr.Number(label="Lymphocytes (%)", value=30) | |
| with gr.Accordion("🍬 Diabetes & Insulin Resistance", open=False): | |
| fasting_blood_sugar = gr.Number(label="Fasting Blood Sugar (mg/dL)", value=85) | |
| hb1ac = gr.Number(label="HbA1c (%)", value=5.4) | |
| insulin = gr.Number(label="Insulin (μU/mL)", value=10) | |
| c_peptide = gr.Number(label="C-Peptide (ng/mL)", value=1.2) | |
| homa_ir = gr.Number(label="HOMA-IR", value=1.2) | |
| with gr.Accordion("❤️ Lipids", open=False): | |
| total_cholesterol = gr.Number(label="Total Cholesterol (mg/dL)", value=180) | |
| ldl = gr.Number(label="LDL (mg/dL)", value=90) | |
| hdl = gr.Number(label="HDL (mg/dL)", value=50) | |
| triglycerides = gr.Number(label="Triglycerides (mg/dL)", value=120) | |
| cholesterol_hdl_ratio = gr.Number(label="Chol/HDL Ratio", value=3.0) | |
| apo_a1 = gr.Number(label="Apo A1 (mg/dL)", value=140) | |
| apo_b = gr.Number(label="Apo B (mg/dL)", value=90) | |
| apo_ratio = gr.Number(label="Apo A1/B Ratio", value=1.55) | |
| with gr.Accordion("🔥 Inflammation & Others", open=False): | |
| hs_crp = gr.Number(label="hs-CRP (mg/L)", value=1.0) | |
| homocysteine = gr.Number(label="Homocysteine (μmol/L)", value=10) | |
| ferritin = gr.Number(label="Ferritin (ng/mL)", value=100) | |
| ck = gr.Number(label="CK (U/L)", value=150) | |
| ck_mb = gr.Number(label="CK-MB (ng/mL)", value=5) | |
| zinc = gr.Number(label="Zinc (µg/dL)", value=90) | |
| copper = gr.Number(label="Copper (µg/dL)", value=110) | |
| selenium = gr.Number(label="Selenium (µg/L)", value=120) | |
| iron = gr.Number(label="Iron (µg/dL)", value=80) | |
| tibc = gr.Number(label="TIBC (µg/dL)", value=300) | |
| transferrin = gr.Number(label="Transferrin (mg/dL)", value=250) | |
| allergy_panel = gr.Number(label="Allergy Panel Score", value=0) | |
| with gr.Accordion("🌡️ Hormones & Vitamins", open=False): | |
| tsh = gr.Number(label="TSH (μIU/mL)", value=2.0) | |
| free_t3 = gr.Number(label="Free T3 (pg/mL)", value=3.2) | |
| free_t4 = gr.Number(label="Free T4 (ng/dL)", value=1.2) | |
| total_t3 = gr.Number(label="Total T3 (ng/dL)", value=1.3) | |
| total_t4 = gr.Number(label="Total T4 (µg/dL)", value=7.8) | |
| reverse_t3 = gr.Number(label="Reverse T3 (ng/dL)", value=15) | |
| tpo_ab = gr.Number(label="TPO Ab (IU/mL)", value=10) | |
| tg_ab = gr.Number(label="TG Ab (IU/mL)", value=12) | |
| total_testosterone = gr.Number(label="Total Testosterone (ng/dL)", value=500) | |
| free_testosterone = gr.Number(label="Free Testosterone (ng/dL)", value=15) | |
| estrogen = gr.Number(label="Estrogen (pg/mL)", value=50) | |
| progesterone = gr.Number(label="Progesterone (ng/mL)", value=1.2) | |
| dhea_s = gr.Number(label="DHEA-S (µg/dL)", value=200) | |
| shbg = gr.Number(label="SHBG (nmol/L)", value=40) | |
| lh = gr.Number(label="LH (mIU/mL)", value=5) | |
| fsh = gr.Number(label="FSH (mIU/mL)", value=6) | |
| cortisol = gr.Number(label="Cortisol (µg/dL)", value=12) | |
| acth = gr.Number(label="ACTH (pg/mL)", value=25) | |
| igf1 = gr.Number(label="IGF-1 (ng/mL)", value=150) | |
| leptin = gr.Number(label="Leptin (ng/mL)", value=10) | |
| adiponectin = gr.Number(label="Adiponectin (µg/mL)", value=8) | |
| with gr.Accordion("🧬 Tumor Markers", open=False): | |
| ca125 = gr.Number(label="CA-125 (U/mL)", value=20) | |
| ca15_3 = gr.Number(label="CA 15-3 (U/mL)", value=25) | |
| ca19_9 = gr.Number(label="CA 19-9 (U/mL)", value=15) | |
| psa = gr.Number(label="PSA (ng/mL)", value=2.0) | |
| cea = gr.Number(label="CEA (ng/mL)", value=3.0) | |
| calcitonin = gr.Number(label="Calcitonin (pg/mL)", value=5) | |
| afp = gr.Number(label="AFP (ng/mL)", value=10) | |
| with gr.Accordion("🧪 Immunology", open=False): | |
| tnf = gr.Number(label="TNF-α (pg/mL)", value=10) | |
| ana = gr.Number(label="ANA (IU/mL)", value=5) | |
| ige = gr.Number(label="IgE (IU/mL)", value=100) | |
| igg = gr.Number(label="IgG (IU/mL)", value=1200) | |
| anti_ccp = gr.Number(label="Anti-CCP (U/mL)", value=5) | |
| dsdna = gr.Number(label="dsDNA (IU/mL)", value=10) | |
| ssa_ssb = gr.Number(label="SSA/SSB (U/mL)", value=10) | |
| rnp = gr.Number(label="RNP (U/mL)", value=5) | |
| sm_antibodies = gr.Number(label="Sm Antibodies (U/mL)", value=5) | |
| anca = gr.Number(label="ANCA (U/mL)", value=5) | |
| anti_ena = gr.Number(label="Anti-ENA (U/mL)", value=5) | |
| il6 = gr.Number(label="IL-6 (pg/mL)", value=5) | |
| submit_btn = gr.Button("🧠 Generate Comprehensive Medical Report", variant="primary", size="lg") | |
| output_md = gr.Markdown(label="AI-Generated Medical Report") | |
| submit_btn.click( | |
| fn=gradio_interface, | |
| inputs=[ | |
| age, gender, height, weight, | |
| urea, creatinine, uric_acid, calcium, phosphorus, sodium, potassium, chloride, | |
| amylase, lipase, bicarbonate, egfr, serum_osmolality, ionized_calcium, | |
| wbc, hemoglobin, mcv, rdw, lymphocytes, | |
| fasting_blood_sugar, hb1ac, insulin, c_peptide, homa_ir, | |
| total_cholesterol, ldl, hdl, cholesterol_hdl_ratio, triglycerides, apo_a1, apo_b, apo_ratio, | |
| albumin, total_protein, alt, ast, alp, ggt, ld, globulin, albumin_globulin_ratio, | |
| magnesium, total_bilirubin, direct_bilirubin, indirect_bilirubin, ammonia, | |
| hs_crp, ck, ck_mb, homocysteine, zinc, copper, selenium, | |
| iron, tibc, transferrin, | |
| vitamin_d, vitamin_b12, | |
| total_testosterone, free_testosterone, estrogen, progesterone, dhea_s, shbg, | |
| lh, fsh, tsh, free_t3, free_t4, total_t3, total_t4, reverse_t3, tpo_ab, tg_ab, | |
| cortisol, acth, igf1, leptin, adiponectin, | |
| ca125, ca15_3, ca19_9, psa, cea, calcitonin, afp, | |
| tnf, ana, ige, igg, anti_ccp, dsdna, ssa_ssb, rnp, sm_antibodies, anca, anti_ena, | |
| il6, allergy_panel | |
| ], | |
| outputs=output_md | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(server_name="0.0.0.0", server_port=7860) | |