Spaces:
Sleeping
Sleeping
File size: 4,108 Bytes
59852e7 9a82969 59852e7 0e437e9 9a82969 aaa2d4c 911b6a6 aaa2d4c 59852e7 aaa2d4c 59852e7 aaa2d4c 79f708f 9422dc7 79f708f a7e45d3 9422dc7 79f708f 9a82969 59852e7 aaa2d4c 59852e7 aaa2d4c 59852e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# Load your model from Hugging Face Hub
MODEL_ID = "Muhammadidrees/MedicalInsights"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
# Function to build structured input and query the LLM
def analyze(
albumin, creatinine, glucose, crp, mcv, rdw, alp,
wbc, lymph, age, gender, height, weight
):
# Calculate BMI (hidden from user, only passed to LLM)
try:
height_m = height / 100 # cm → m
bmi = round(weight / (height_m ** 2), 2)
except Exception:
bmi = "N/A"
# System-style instruction (non-medical, fixed headings)
system_prompt = (
"""Output MUST strictly follow this structured format:
1. Executive Summary
- Longevity Vitality Score (out of 100)
- Top Priority Issues
- Key Strengths
2. System-Specific Analysis
- Blood Health (MCV, RDW, Lymphocytes, WBC)
- Protein & Liver Health (Albumin, ALP)
- Kidney Health (Creatinine µmol/L)
- Metabolic Health (Glucose mmol/L, lnCRP)
- Other relevant systems
3. Personalized Action Plan
- Medical (tests/consults)
- Nutrition (diet & supplements)
- Lifestyle (hydration, exercise, sleep)
- Testing (follow-up labs: ferritin, Vitamin D, GGT)
4. Interaction Alerts
- How biomarkers interact (e.g., anemia ↔ infection cycle, ALP with bone/liver origin)
5. Longevity Metrics
- Metabolic Health Score
- Cardiovascular & Cognitive risk trajectory
. Enhanced AI Insights & Longitudinal Risk
"""
)
# Construct profile input
patient_input = f"""
Patient Profile:
- Age: {age}
- Gender: {gender}
- Height: {height} cm
- Weight: {weight} kg
- BMI: {bmi}
Lab Values:
- Albumin: {albumin} g/dL
- Creatinine: {creatinine} mg/dL
- Glucose: {glucose} mg/dL
- C-Reactive Protein: {crp} mg/L
- Mean Cell Volume: {mcv} fL
- Red Cell Distribution Width: {rdw} %
- Alkaline Phosphatase: {alp} U/L
- White Blood Cell Count: {wbc} K/uL
- Lymphocyte Percentage: {lymph} %
"""
prompt = system_prompt + "\n" + patient_input
# Call LLM
result = pipe(
prompt,
max_new_tokens=5000,
do_sample=True,
temperature=0.3,
top_p=0.9,
return_full_text=False
)
# Force output to start from "Executive Summary"
output_text = result[0]["generated_text"].strip()
if "Executive Summary" in output_text:
output_text = output_text.split("Executive Summary", 1)[-1]
output_text = "Executive Summary" + output_text
return output_text
# Build Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🧪 Wellness Insights AI — Enter Profile Data")
with gr.Row():
albumin = gr.Number(label="Albumin (g/dL)")
wbc = gr.Number(label="White Blood Cell Count (K/uL)")
with gr.Row():
creatinine = gr.Number(label="Creatinine (mg/dL)")
lymph = gr.Number(label="Lymphocyte Percentage (%)")
with gr.Row():
glucose = gr.Number(label="Glucose (mg/dL)")
age = gr.Number(label="Age (years)")
with gr.Row():
crp = gr.Number(label="C-Reactive Protein (mg/L)")
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
with gr.Row():
mcv = gr.Number(label="Mean Cell Volume (fL)")
height = gr.Number(label="Height (cm)")
with gr.Row():
rdw = gr.Number(label="Red Cell Distribution Width (%)")
weight = gr.Number(label="Weight (kg)")
with gr.Row():
alp = gr.Number(label="Alkaline Phosphatase (U/L)")
analyze_btn = gr.Button("🔎 Analyze")
output = gr.Textbox(label="AI Wellness Assessment", lines=12)
analyze_btn.click(
fn=analyze,
inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
wbc, lymph, age, gender, height, weight],
outputs=output
)
demo.launch()
|