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