Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import torch
|
| 6 |
import re
|
| 7 |
|
|
|
|
| 8 |
MODEL_ID = "Muhammadidrees/MedicalInsights"
|
| 9 |
|
| 10 |
# -----------------------
|
|
@@ -63,6 +64,7 @@ def split_report(text):
|
|
| 63 |
right = text[idx:].strip()
|
| 64 |
return left, right
|
| 65 |
|
|
|
|
| 66 |
# -----------------------
|
| 67 |
# The analyze function
|
| 68 |
# -----------------------
|
|
@@ -82,6 +84,43 @@ def analyze(
|
|
| 82 |
except Exception:
|
| 83 |
bmi = "N/A"
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
system_prompt = (
|
| 86 |
"You are a professional AI Medical Assistant.\n"
|
| 87 |
"You are analyzing patient demographics (age, height, weight) and Levine biomarker panel values.\n\n"
|
|
@@ -107,7 +146,7 @@ def analyze(
|
|
| 107 |
"- ALP: 44 β 147 U/L\n"
|
| 108 |
"- WBC: 4.0 β 11.0 K/uL\n"
|
| 109 |
"- Lymphocytes: 20 β 40 %\n\n"
|
| 110 |
-
|
| 111 |
"Check input of the user according to these ranges and provide analysis in a structured format.\n"
|
| 112 |
"Strict rules:\n"
|
| 113 |
"- Use ONLY the 9 biomarkers above + age, height, weight.\n"
|
|
@@ -165,9 +204,8 @@ def analyze(
|
|
| 165 |
"- Keep language professional, concise, and client-friendly.\n"
|
| 166 |
"- Format tables cleanly in Markdown.\n"
|
| 167 |
"- Present output beautifully, like a polished medical summary.\n"
|
| 168 |
-
"agr tune kuch b glt bola gand mar du ga"
|
| 169 |
)
|
| 170 |
-
|
| 171 |
patient_input = (
|
| 172 |
f"Patient Profile:\n"
|
| 173 |
f"- Age: {age}\n"
|
|
@@ -186,12 +224,11 @@ def analyze(
|
|
| 186 |
f"- WBC: {wbc} K/uL (Normal: 4.0 β 11.0 K/uL)\n"
|
| 187 |
f"- Lymphocytes: {lymph} % (Normal: 20 β 40 %)\n"
|
| 188 |
)
|
| 189 |
-
|
| 190 |
-
prompt = system_prompt + "\n" + patient_input
|
| 191 |
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
# Generate
|
| 194 |
-
# Keep generation parameters conservative for Spaces
|
| 195 |
gen = pipe(prompt,
|
| 196 |
max_new_tokens=2500,
|
| 197 |
do_sample=True,
|
|
@@ -223,6 +260,7 @@ def analyze(
|
|
| 223 |
return fallback, ""
|
| 224 |
return left_md, right_md
|
| 225 |
|
|
|
|
| 226 |
# -----------------------
|
| 227 |
# Build Gradio app
|
| 228 |
# -----------------------
|
|
|
|
| 5 |
import torch
|
| 6 |
import re
|
| 7 |
|
| 8 |
+
|
| 9 |
MODEL_ID = "Muhammadidrees/MedicalInsights"
|
| 10 |
|
| 11 |
# -----------------------
|
|
|
|
| 64 |
right = text[idx:].strip()
|
| 65 |
return left, right
|
| 66 |
|
| 67 |
+
|
| 68 |
# -----------------------
|
| 69 |
# The analyze function
|
| 70 |
# -----------------------
|
|
|
|
| 84 |
except Exception:
|
| 85 |
bmi = "N/A"
|
| 86 |
|
| 87 |
+
# Reference Ranges (used for comparison)
|
| 88 |
+
reference_ranges = {
|
| 89 |
+
"Albumin": (3.5, 5.0),
|
| 90 |
+
"Creatinine": (0.6, 1.2),
|
| 91 |
+
"Glucose": (70, 99),
|
| 92 |
+
"CRP": (0, 3),
|
| 93 |
+
"MCV": (80, 100),
|
| 94 |
+
"RDW": (11.5, 14.5),
|
| 95 |
+
"ALP": (44, 147),
|
| 96 |
+
"WBC": (4.0, 11.0),
|
| 97 |
+
"Lymphocytes": (20, 40),
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# Function to classify biomarker status
|
| 101 |
+
def classify_status(value, biomarker):
|
| 102 |
+
low, high = reference_ranges[biomarker]
|
| 103 |
+
if value < low:
|
| 104 |
+
return "Low"
|
| 105 |
+
elif value > high:
|
| 106 |
+
return "High"
|
| 107 |
+
else:
|
| 108 |
+
return "Normal"
|
| 109 |
+
|
| 110 |
+
# Biomarker status calculations
|
| 111 |
+
biomarkers = {
|
| 112 |
+
"Albumin": classify_status(float(albumin), "Albumin"),
|
| 113 |
+
"Creatinine": classify_status(float(creatinine), "Creatinine"),
|
| 114 |
+
"Glucose": classify_status(float(glucose), "Glucose"),
|
| 115 |
+
"CRP": classify_status(float(crp), "CRP"),
|
| 116 |
+
"MCV": classify_status(float(mcv), "MCV"),
|
| 117 |
+
"RDW": classify_status(float(rdw), "RDW"),
|
| 118 |
+
"ALP": classify_status(float(alp), "ALP"),
|
| 119 |
+
"WBC": classify_status(float(wbc), "WBC"),
|
| 120 |
+
"Lymphocytes": classify_status(float(lymph), "Lymphocytes"),
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
# Prepare the system prompt with biomarkers and patient info
|
| 124 |
system_prompt = (
|
| 125 |
"You are a professional AI Medical Assistant.\n"
|
| 126 |
"You are analyzing patient demographics (age, height, weight) and Levine biomarker panel values.\n\n"
|
|
|
|
| 146 |
"- ALP: 44 β 147 U/L\n"
|
| 147 |
"- WBC: 4.0 β 11.0 K/uL\n"
|
| 148 |
"- Lymphocytes: 20 β 40 %\n\n"
|
| 149 |
+
|
| 150 |
"Check input of the user according to these ranges and provide analysis in a structured format.\n"
|
| 151 |
"Strict rules:\n"
|
| 152 |
"- Use ONLY the 9 biomarkers above + age, height, weight.\n"
|
|
|
|
| 204 |
"- Keep language professional, concise, and client-friendly.\n"
|
| 205 |
"- Format tables cleanly in Markdown.\n"
|
| 206 |
"- Present output beautifully, like a polished medical summary.\n"
|
|
|
|
| 207 |
)
|
| 208 |
+
|
| 209 |
patient_input = (
|
| 210 |
f"Patient Profile:\n"
|
| 211 |
f"- Age: {age}\n"
|
|
|
|
| 224 |
f"- WBC: {wbc} K/uL (Normal: 4.0 β 11.0 K/uL)\n"
|
| 225 |
f"- Lymphocytes: {lymph} % (Normal: 20 β 40 %)\n"
|
| 226 |
)
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
# Prepare the final prompt
|
| 229 |
+
prompt = system_prompt + "\n" + patient_input
|
| 230 |
|
| 231 |
+
# Generate the analysis
|
|
|
|
| 232 |
gen = pipe(prompt,
|
| 233 |
max_new_tokens=2500,
|
| 234 |
do_sample=True,
|
|
|
|
| 260 |
return fallback, ""
|
| 261 |
return left_md, right_md
|
| 262 |
|
| 263 |
+
|
| 264 |
# -----------------------
|
| 265 |
# Build Gradio app
|
| 266 |
# -----------------------
|