Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,124 +13,155 @@ def analyze(
|
|
| 13 |
albumin, creatinine, glucose, crp, mcv, rdw, alp,
|
| 14 |
wbc, lymph, age, gender, height, weight
|
| 15 |
):
|
| 16 |
-
# Calculate BMI
|
| 17 |
try:
|
| 18 |
height_m = height / 100 # cm → m
|
| 19 |
bmi = round(weight / (height_m ** 2), 2)
|
| 20 |
except Exception:
|
| 21 |
bmi = "N/A"
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
system_prompt =
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
1. Executive Summary
|
| 30 |
-
- Top Priority Issues
|
| 31 |
-
- Key Strengths
|
| 32 |
|
| 33 |
2. System-Specific Analysis
|
| 34 |
- Blood Health (MCV, RDW, Lymphocytes, WBC)
|
| 35 |
- Protein & Liver Health (Albumin, ALP)
|
| 36 |
-
- Kidney Health (Creatinine
|
| 37 |
-
- Metabolic Health (Glucose
|
| 38 |
-
- Other relevant systems
|
| 39 |
|
| 40 |
3. Personalized Action Plan
|
| 41 |
-
- Medical
|
| 42 |
-
- Nutrition
|
| 43 |
-
- Lifestyle
|
| 44 |
-
- Testing
|
| 45 |
|
| 46 |
4. Interaction Alerts
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
- Subclinical nutrient predictions (Iron, B12, Folate, Copper)
|
| 53 |
-
- Elevated ALP interpretation (bone vs liver origin)
|
| 54 |
-
- WBC & lymphocyte trends for immunity
|
| 55 |
-
- Predictive longevity risk profile
|
| 56 |
-
"""
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
patient_input = f"""
|
| 61 |
Patient Profile:
|
| 62 |
-
- Age: {age}
|
| 63 |
- Gender: {gender}
|
| 64 |
- Height: {height} cm
|
| 65 |
- Weight: {weight} kg
|
| 66 |
- BMI: {bmi}
|
| 67 |
|
| 68 |
-
|
| 69 |
- Albumin: {albumin} g/dL
|
| 70 |
- Creatinine: {creatinine} mg/dL
|
| 71 |
- Glucose: {glucose} mg/dL
|
| 72 |
-
- C-Reactive Protein: {crp} mg/L
|
| 73 |
-
- Mean Cell Volume: {mcv} fL
|
| 74 |
-
- Red Cell Distribution Width: {rdw} %
|
| 75 |
-
- Alkaline Phosphatase: {alp} U/L
|
| 76 |
-
- White Blood Cell Count: {wbc} K/uL
|
| 77 |
- Lymphocyte Percentage: {lymph} %
|
| 78 |
-
"""
|
| 79 |
-
|
| 80 |
-
prompt = system_prompt + "\n" + patient_input
|
| 81 |
-
|
| 82 |
-
# Call LLM
|
| 83 |
-
result = pipe(
|
| 84 |
-
prompt,
|
| 85 |
-
max_new_tokens=1000,
|
| 86 |
-
do_sample=True,
|
| 87 |
-
temperature=0.3,
|
| 88 |
-
top_p=0.9,
|
| 89 |
-
return_full_text=False
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# Force output to start from "Executive Summary"
|
| 93 |
-
output_text = result[0]["generated_text"].strip()
|
| 94 |
-
if "Executive Summary" in output_text:
|
| 95 |
-
output_text = output_text.split("Executive Summary", 1)[-1]
|
| 96 |
-
output_text = "Executive Summary" + output_text
|
| 97 |
-
|
| 98 |
-
return output_text
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# Build Gradio UI
|
| 102 |
-
with gr.Blocks() as demo:
|
| 103 |
-
gr.Markdown("## 🧪 Wellness Insights AI — Enter Profile Data")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
albumin = gr.Number(label="Albumin (g/dL)")
|
| 107 |
-
wbc = gr.Number(label="White Blood Cell Count (K/uL)")
|
| 108 |
-
|
| 109 |
-
with gr.Row():
|
| 110 |
-
creatinine = gr.Number(label="Creatinine (mg/dL)")
|
| 111 |
-
lymph = gr.Number(label="Lymphocyte Percentage (%)")
|
| 112 |
|
| 113 |
-
|
| 114 |
-
glucose = gr.Number(label="Glucose (mg/dL)")
|
| 115 |
-
age = gr.Number(label="Age (years)")
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
crp = gr.Number(label="C-Reactive Protein (mg/L)")
|
| 119 |
-
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
analyze_btn.click(
|
| 136 |
fn=analyze,
|
|
@@ -138,5 +169,11 @@ with gr.Blocks() as demo:
|
|
| 138 |
wbc, lymph, age, gender, height, weight],
|
| 139 |
outputs=output
|
| 140 |
)
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
albumin, creatinine, glucose, crp, mcv, rdw, alp,
|
| 14 |
wbc, lymph, age, gender, height, weight
|
| 15 |
):
|
| 16 |
+
# Calculate BMI
|
| 17 |
try:
|
| 18 |
height_m = height / 100 # cm → m
|
| 19 |
bmi = round(weight / (height_m ** 2), 2)
|
| 20 |
except Exception:
|
| 21 |
bmi = "N/A"
|
| 22 |
|
| 23 |
+
# Improved system prompt with clearer instructions
|
| 24 |
+
system_prompt = """You are a professional AI Medical Assistant analyzing patient biomarkers.
|
| 25 |
+
|
| 26 |
+
CRITICAL: Generate a COMPLETE report following this EXACT structure. Do not stop mid-sentence.
|
| 27 |
+
|
| 28 |
+
=== REQUIRED OUTPUT FORMAT ===
|
| 29 |
|
| 30 |
1. Executive Summary
|
| 31 |
+
- Top Priority Issues: [List 2-3 main concerns]
|
| 32 |
+
- Key Strengths: [List 2-3 positive findings]
|
| 33 |
|
| 34 |
2. System-Specific Analysis
|
| 35 |
- Blood Health (MCV, RDW, Lymphocytes, WBC)
|
| 36 |
- Protein & Liver Health (Albumin, ALP)
|
| 37 |
+
- Kidney Health (Creatinine)
|
| 38 |
+
- Metabolic Health (Glucose, CRP)
|
|
|
|
| 39 |
|
| 40 |
3. Personalized Action Plan
|
| 41 |
+
- Medical: [Recommended tests/consultations]
|
| 42 |
+
- Nutrition: [Dietary recommendations and supplements]
|
| 43 |
+
- Lifestyle: [Exercise, hydration, sleep guidance]
|
| 44 |
+
- Testing: [Follow-up labs needed]
|
| 45 |
|
| 46 |
4. Interaction Alerts
|
| 47 |
+
[Explain how biomarkers interact and influence each other]
|
| 48 |
+
|
| 49 |
+
6. Tabular Mapping
|
| 50 |
+
| Biomarker | Value | Status | AI Insight | Client Message |
|
| 51 |
+
|-----------|-------|--------|------------|----------------|
|
| 52 |
+
[Complete table for all biomarkers]
|
| 53 |
|
| 54 |
+
7. Enhanced AI Insights
|
| 55 |
+
- Subclinical Nutrient Analysis (Iron, B12, Folate status)
|
| 56 |
+
- ALP Interpretation (bone vs liver origin)
|
| 57 |
+
- Immune System Assessment (WBC & lymphocyte trends)
|
| 58 |
+
- Long-term Health Considerations
|
| 59 |
|
| 60 |
+
=== END FORMAT ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
Now analyze the following patient data and provide a COMPLETE report:"""
|
| 63 |
+
|
| 64 |
+
# Construct patient profile
|
| 65 |
patient_input = f"""
|
| 66 |
Patient Profile:
|
| 67 |
+
- Age: {age} years
|
| 68 |
- Gender: {gender}
|
| 69 |
- Height: {height} cm
|
| 70 |
- Weight: {weight} kg
|
| 71 |
- BMI: {bmi}
|
| 72 |
|
| 73 |
+
Laboratory Values:
|
| 74 |
- Albumin: {albumin} g/dL
|
| 75 |
- Creatinine: {creatinine} mg/dL
|
| 76 |
- Glucose: {glucose} mg/dL
|
| 77 |
+
- C-Reactive Protein (CRP): {crp} mg/L
|
| 78 |
+
- Mean Cell Volume (MCV): {mcv} fL
|
| 79 |
+
- Red Cell Distribution Width (RDW): {rdw} %
|
| 80 |
+
- Alkaline Phosphatase (ALP): {alp} U/L
|
| 81 |
+
- White Blood Cell Count (WBC): {wbc} K/uL
|
| 82 |
- Lymphocyte Percentage: {lymph} %
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
Generate complete analysis now:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
prompt = system_prompt + "\n" + patient_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
try:
|
| 89 |
+
# Increased max_new_tokens significantly for complete output
|
| 90 |
+
result = pipe(
|
| 91 |
+
prompt,
|
| 92 |
+
max_new_tokens=2500, # INCREASED from 1000
|
| 93 |
+
do_sample=True,
|
| 94 |
+
temperature=0.7, # INCREASED from 0.3 for better generation
|
| 95 |
+
top_p=0.92,
|
| 96 |
+
top_k=50,
|
| 97 |
+
repetition_penalty=1.1, # Prevent repetition
|
| 98 |
+
return_full_text=False,
|
| 99 |
+
pad_token_id=tokenizer.eos_token_id
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
output_text = result[0]["generated_text"].strip()
|
| 103 |
+
|
| 104 |
+
# Clean up output - remove any prompt leakage
|
| 105 |
+
if "Executive Summary" in output_text:
|
| 106 |
+
idx = output_text.find("Executive Summary")
|
| 107 |
+
output_text = output_text[idx:]
|
| 108 |
+
elif "1. Executive Summary" in output_text:
|
| 109 |
+
idx = output_text.find("1. Executive Summary")
|
| 110 |
+
output_text = output_text[idx:]
|
| 111 |
+
|
| 112 |
+
# If output seems incomplete, add a note
|
| 113 |
+
if len(output_text) < 500:
|
| 114 |
+
output_text += "\n\n⚠️ Note: Output may be incomplete. Consider re-running the analysis."
|
| 115 |
+
|
| 116 |
+
return output_text
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"Error during analysis: {str(e)}\n\nPlease check your input values and try again."
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# Build Gradio UI with improved layout
|
| 123 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
# 🧪 Wellness Insights AI
|
| 126 |
+
### Enter Patient Profile & Lab Values for Comprehensive Analysis
|
| 127 |
+
""")
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
+
with gr.Column():
|
| 131 |
+
gr.Markdown("### 👤 Demographics")
|
| 132 |
+
age = gr.Number(label="Age (years)", value=30)
|
| 133 |
+
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
|
| 134 |
+
height = gr.Number(label="Height (cm)", value=175)
|
| 135 |
+
weight = gr.Number(label="Weight (kg)", value=70)
|
| 136 |
+
|
| 137 |
+
with gr.Column():
|
| 138 |
+
gr.Markdown("### 🩸 Blood Health Markers")
|
| 139 |
+
wbc = gr.Number(label="White Blood Cell Count (K/uL)", value=7.0)
|
| 140 |
+
lymph = gr.Number(label="Lymphocyte Percentage (%)", value=30)
|
| 141 |
+
mcv = gr.Number(label="Mean Cell Volume (fL)", value=90)
|
| 142 |
+
rdw = gr.Number(label="Red Cell Distribution Width (%)", value=13)
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
+
with gr.Column():
|
| 146 |
+
gr.Markdown("### 🫀 Metabolic Markers")
|
| 147 |
+
glucose = gr.Number(label="Glucose (mg/dL)", value=95)
|
| 148 |
+
crp = gr.Number(label="C-Reactive Protein (mg/L)", value=1.5)
|
| 149 |
+
|
| 150 |
+
with gr.Column():
|
| 151 |
+
gr.Markdown("### 🧬 Organ Function Markers")
|
| 152 |
+
albumin = gr.Number(label="Albumin (g/dL)", value=4.2)
|
| 153 |
+
creatinine = gr.Number(label="Creatinine (mg/dL)", value=1.0)
|
| 154 |
+
alp = gr.Number(label="Alkaline Phosphatase (U/L)", value=70)
|
| 155 |
+
|
| 156 |
+
analyze_btn = gr.Button("🔎 Generate Comprehensive Analysis", variant="primary", size="lg")
|
| 157 |
+
|
| 158 |
+
gr.Markdown("### 📋 Analysis Report")
|
| 159 |
+
output = gr.Textbox(
|
| 160 |
+
label="AI-Generated Lab Report",
|
| 161 |
+
lines=25,
|
| 162 |
+
max_lines=50,
|
| 163 |
+
show_copy_button=True
|
| 164 |
+
)
|
| 165 |
|
| 166 |
analyze_btn.click(
|
| 167 |
fn=analyze,
|
|
|
|
| 169 |
wbc, lymph, age, gender, height, weight],
|
| 170 |
outputs=output
|
| 171 |
)
|
| 172 |
+
|
| 173 |
+
gr.Markdown("""
|
| 174 |
+
---
|
| 175 |
+
**Note:** This tool provides educational insights based on biomarker analysis.
|
| 176 |
+
Always consult healthcare professionals for medical advice.
|
| 177 |
+
""")
|
| 178 |
+
|
| 179 |
+
demo.launch(share=False)
|