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

Update app.py

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