Muhammadidrees commited on
Commit
53b7272
Β·
verified Β·
1 Parent(s): c40c270

Update app.py

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