Muhammadidrees commited on
Commit
72fc912
·
verified ·
1 Parent(s): 72b5dee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -93
app.py CHANGED
@@ -1,94 +1,94 @@
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
- # 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
-
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
-
84
- analyze_btn = gr.Button("🔎 Analyze")
85
- output = gr.Textbox(label="AI Medical Assessment", lines=12)
86
-
87
- analyze_btn.click(
88
- fn=analyze,
89
- inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
90
- wbc, lymph, age, gender, height, weight, bmi],
91
- outputs=output
92
- )
93
-
94
  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
+ # 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
+
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
+
84
+ analyze_btn = gr.Button("🔎 Analyze")
85
+ output = gr.Textbox(label="AI Medical Assessment", lines=12)
86
+
87
+ analyze_btn.click(
88
+ fn=analyze,
89
+ inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
90
+ wbc, lymph, age, gender, height, weight, bmi],
91
+ outputs=output
92
+ )
93
+ ##Launch
94
  demo.launch()