Muhammadidrees commited on
Commit
19ebc29
Β·
verified Β·
1 Parent(s): abd829c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -98
app.py CHANGED
@@ -1,99 +1,95 @@
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
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
-
38
- Lab Values:
39
- - Albumin: {albumin} g/dL
40
- - Creatinine: {creatinine} mg/dL
41
- - Glucose: {glucose} mg/dL
42
- - C-Reactive Protein: {crp} mg/L
43
- - Mean Cell Volume: {mcv} fL
44
- - Red Cell Distribution Width: {rdw} %
45
- - Alkaline Phosphatase: {alp} U/L
46
- - White Blood Cell Count: {wbc} K/uL
47
- - Lymphocyte Percentage: {lymph} %
48
- """
49
-
50
- prompt = system_prompt + "\n" + patient_input
51
-
52
- # Call LLM
53
- result = pipe(prompt, max_new_tokens=400, do_sample=True, temperature=0.6)
54
- return result[0]["generated_text"]
55
-
56
-
57
- # Build Gradio UI
58
- with gr.Blocks() as demo:
59
- gr.Markdown("## πŸ§ͺ Medical Insights AI β€” Enter Patient Data")
60
-
61
- with gr.Row():
62
- albumin = gr.Number(label="Albumin (g/dL)")
63
- wbc = gr.Number(label="White Blood Cell Count (K/uL)")
64
-
65
- with gr.Row():
66
- creatinine = gr.Number(label="Creatinine (mg/dL)")
67
- lymph = gr.Number(label="Lymphocyte Percentage (%)")
68
-
69
- with gr.Row():
70
- glucose = gr.Number(label="Glucose (mg/dL)")
71
- age = gr.Number(label="Age (years)")
72
-
73
- with gr.Row():
74
- crp = gr.Number(label="C-Reactive Protein (mg/L)")
75
- gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
76
-
77
- with gr.Row():
78
- mcv = gr.Number(label="Mean Cell Volume (fL)")
79
- height = gr.Number(label="Height (cm)")
80
-
81
- with gr.Row():
82
- rdw = gr.Number(label="Red Cell Distribution Width (%)")
83
- weight = gr.Number(label="Weight (kg)")
84
-
85
- with gr.Row():
86
- alp = gr.Number(label="Alkaline Phosphatase (U/L)")
87
-
88
-
89
- analyze_btn = gr.Button("πŸ”Ž Analyze")
90
- output = gr.Textbox(label="AI Medical Assessment", lines=12)
91
-
92
- analyze_btn.click(
93
- fn=analyze,
94
- inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,
95
- wbc, lymph, age, gender, height, weight],
96
- outputs=output
97
- )
98
-
99
  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()