Muhammadidrees commited on
Commit
59852e7
·
verified ·
1 Parent(s): 1f638e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -102
app.py CHANGED
@@ -1,102 +1,102 @@
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
16
- ):
17
- # Calculate BMI (hidden from user, only passed to LLM)
18
- try:
19
- height_m = height / 100 # cm → m
20
- bmi = round(weight / (height_m ** 2), 2)
21
- except Exception:
22
- bmi = "N/A"
23
-
24
- # System-style instruction
25
- system_prompt = (
26
- "You are an advanced AI medical assistant. "
27
- "Analyze the patient’s biomarkers and demographics. "
28
- "Provide a structured assessment including: "
29
- "patient_profile, lab_results, risk_assessment, clinical_impression, recommendations. "
30
- )
31
-
32
- # Construct patient profile input
33
- patient_input = f"""
34
- Patient Profile:
35
- - Age: {age}
36
- - Gender: {gender}
37
- - Height: {height} cm
38
- - Weight: {weight} kg
39
- - BMI: {bmi}
40
-
41
- Lab Values:
42
- - Albumin: {albumin} g/dL
43
- - Creatinine: {creatinine} mg/dL
44
- - Glucose: {glucose} mg/dL
45
- - C-Reactive Protein: {crp} mg/L
46
- - Mean Cell Volume: {mcv} fL
47
- - Red Cell Distribution Width: {rdw} %
48
- - Alkaline Phosphatase: {alp} U/L
49
- - White Blood Cell Count: {wbc} K/uL
50
- - Lymphocyte Percentage: {lymph} %
51
- """
52
-
53
- prompt = system_prompt + "\n" + patient_input
54
-
55
- # Call LLM
56
- result = pipe(prompt, max_new_tokens=1000, do_sample=True, temperature=0.6)
57
- return result[0]["generated_text"]
58
-
59
-
60
- # Build Gradio UI
61
- with gr.Blocks() as demo:
62
- gr.Markdown("## 🧪 Medical Insights AI — Enter Patient Data")
63
-
64
- with gr.Row():
65
- albumin = gr.Number(label="Albumin (g/dL)")
66
- wbc = gr.Number(label="White Blood Cell Count (K/uL)")
67
-
68
- with gr.Row():
69
- creatinine = gr.Number(label="Creatinine (mg/dL)")
70
- lymph = gr.Number(label="Lymphocyte Percentage (%)")
71
-
72
- with gr.Row():
73
- glucose = gr.Number(label="Glucose (mg/dL)")
74
- age = gr.Number(label="Age (years)")
75
-
76
- with gr.Row():
77
- crp = gr.Number(label="C-Reactive Protein (mg/L)")
78
- gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
79
-
80
- with gr.Row():
81
- mcv = gr.Number(label="Mean Cell Volume (fL)")
82
- height = gr.Number(label="Height (cm)")
83
-
84
- with gr.Row():
85
- rdw = gr.Number(label="Red Cell Distribution Width (%)")
86
- weight = gr.Number(label="Weight (kg)")
87
-
88
- with gr.Row():
89
- alp = gr.Number(label="Alkaline Phosphatase (U/L)")
90
-
91
- analyze_btn = gr.Button("🔎 Analyze")
92
- output = gr.Textbox(label="AI Medical Assessment", lines=12)
93
-
94
- # Run analysis
95
- analyze_btn.click(
96
- fn=analyze,
97
- inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
98
- wbc, lymph, age, gender, height, weight],
99
- outputs=output
100
- )
101
-
102
- 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
16
+ ):
17
+ # Calculate BMI (hidden from user, only passed to LLM)
18
+ try:
19
+ height_m = height / 100 # cm → m
20
+ bmi = round(weight / (height_m ** 2), 2)
21
+ except Exception:
22
+ bmi = "N/A"
23
+
24
+ # System-style instruction
25
+ system_prompt = (
26
+ "You are an advanced AI medical assistant. "
27
+ "Analyze the patient’s biomarkers and demographics. "
28
+ "Provide a structured assessment including: "
29
+ "patient_profile, lab_results, risk_assessment, clinical_impression, recommendations. "
30
+ )
31
+
32
+ # Construct patient profile input
33
+ patient_input = f"""
34
+ Patient Profile:
35
+ - Age: {age}
36
+ - Gender: {gender}
37
+ - Height: {height} cm
38
+ - Weight: {weight} kg
39
+ - BMI: {bmi}
40
+
41
+ Lab Values:
42
+ - Albumin: {albumin} g/dL
43
+ - Creatinine: {creatinine} mg/dL
44
+ - Glucose: {glucose} mg/dL
45
+ - C-Reactive Protein: {crp} mg/L
46
+ - Mean Cell Volume: {mcv} fL
47
+ - Red Cell Distribution Width: {rdw} %
48
+ - Alkaline Phosphatase: {alp} U/L
49
+ - White Blood Cell Count: {wbc} K/uL
50
+ - Lymphocyte Percentage: {lymph} %
51
+ """
52
+
53
+ prompt = system_prompt + "\n" + patient_input
54
+
55
+ # Call LLM
56
+ result = pipe(prompt, max_new_tokens=1800, do_sample=True, temperature=0.6)
57
+ return result[0]["generated_text"]
58
+
59
+
60
+ # Build Gradio UI
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("## 🧪 Medical Insights AI — Enter Patient Data")
63
+
64
+ with gr.Row():
65
+ albumin = gr.Number(label="Albumin (g/dL)")
66
+ wbc = gr.Number(label="White Blood Cell Count (K/uL)")
67
+
68
+ with gr.Row():
69
+ creatinine = gr.Number(label="Creatinine (mg/dL)")
70
+ lymph = gr.Number(label="Lymphocyte Percentage (%)")
71
+
72
+ with gr.Row():
73
+ glucose = gr.Number(label="Glucose (mg/dL)")
74
+ age = gr.Number(label="Age (years)")
75
+
76
+ with gr.Row():
77
+ crp = gr.Number(label="C-Reactive Protein (mg/L)")
78
+ gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
79
+
80
+ with gr.Row():
81
+ mcv = gr.Number(label="Mean Cell Volume (fL)")
82
+ height = gr.Number(label="Height (cm)")
83
+
84
+ with gr.Row():
85
+ rdw = gr.Number(label="Red Cell Distribution Width (%)")
86
+ weight = gr.Number(label="Weight (kg)")
87
+
88
+ with gr.Row():
89
+ alp = gr.Number(label="Alkaline Phosphatase (U/L)")
90
+
91
+ analyze_btn = gr.Button("🔎 Analyze")
92
+ output = gr.Textbox(label="AI Medical Assessment", lines=12)
93
+
94
+ # Run analysis
95
+ analyze_btn.click(
96
+ fn=analyze,
97
+ inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
98
+ wbc, lymph, age, gender, height, weight],
99
+ outputs=output
100
+ )
101
+
102
+ demo.launch()