Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
|
| 4 |
+
# Load your model from Hugging Face Hub
|
| 5 |
+
MODEL_ID = "Muhammadidrees/MedicalInsights"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
#model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
device_map={"": "cpu"},
|
| 12 |
+
low_cpu_mem_usage=True,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Function to build structured input and query the LLM
|
| 19 |
+
def analyze(
|
| 20 |
+
albumin, creatinine, glucose, crp, mcv, rdw, alp,
|
| 21 |
+
wbc, lymph, age, gender, height, weight, bmi
|
| 22 |
+
):
|
| 23 |
+
# System-style instruction
|
| 24 |
+
system_prompt = (
|
| 25 |
+
"You are an advanced AI medical assistant. "
|
| 26 |
+
"Analyze the patient’s biomarkers and demographics. "
|
| 27 |
+
"Provide a structured assessment including: "
|
| 28 |
+
"patient_profile, lab_results, risk_assessment, clinical_impression, recommendations. "
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Construct patient profile input
|
| 32 |
+
patient_input = f"""
|
| 33 |
+
Patient Profile:
|
| 34 |
+
- Age: {age}
|
| 35 |
+
- Gender: {gender}
|
| 36 |
+
- Height: {height} cm
|
| 37 |
+
- Weight: {weight} kg
|
| 38 |
+
- BMI: {bmi}
|
| 39 |
+
|
| 40 |
+
Lab Values:
|
| 41 |
+
- Albumin: {albumin} g/dL
|
| 42 |
+
- Creatinine: {creatinine} mg/dL
|
| 43 |
+
- Glucose: {glucose} mg/dL
|
| 44 |
+
- C-Reactive Protein: {crp} mg/L
|
| 45 |
+
- Mean Cell Volume: {mcv} fL
|
| 46 |
+
- Red Cell Distribution Width: {rdw} %
|
| 47 |
+
- Alkaline Phosphatase: {alp} U/L
|
| 48 |
+
- White Blood Cell Count: {wbc} K/uL
|
| 49 |
+
- Lymphocyte Percentage: {lymph} %
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
prompt = system_prompt + "\n" + patient_input
|
| 53 |
+
|
| 54 |
+
# Call LLM
|
| 55 |
+
result = pipe(prompt, max_new_tokens=400, do_sample=True, temperature=0.6)
|
| 56 |
+
return result[0]["generated_text"]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Build Gradio UI
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("## 🧪 Medical Insights AI — Enter Patient Data")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
albumin = gr.Number(label="Albumin (g/dL)")
|
| 65 |
+
wbc = gr.Number(label="White Blood Cell Count (K/uL)")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
creatinine = gr.Number(label="Creatinine (mg/dL)")
|
| 69 |
+
lymph = gr.Number(label="Lymphocyte Percentage (%)")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
glucose = gr.Number(label="Glucose (mg/dL)")
|
| 73 |
+
age = gr.Number(label="Age (years)")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
crp = gr.Number(label="C-Reactive Protein (mg/L)")
|
| 77 |
+
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
mcv = gr.Number(label="Mean Cell Volume (fL)")
|
| 81 |
+
height = gr.Number(label="Height (cm)")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
rdw = gr.Number(label="Red Cell Distribution Width (%)")
|
| 85 |
+
weight = gr.Number(label="Weight (kg)")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
alp = gr.Number(label="Alkaline Phosphatase (U/L)")
|
| 89 |
+
bmi = gr.Number(label="BMI")
|
| 90 |
+
|
| 91 |
+
analyze_btn = gr.Button("🔎 Analyze")
|
| 92 |
+
output = gr.Textbox(label="AI Medical Assessment", lines=12)
|
| 93 |
+
|
| 94 |
+
analyze_btn.click(
|
| 95 |
+
fn=analyze,
|
| 96 |
+
inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
|
| 97 |
+
wbc, lymph, age, gender, height, weight, bmi],
|
| 98 |
+
outputs=output
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
demo.launch()
|