aneesqumar commited on
Commit
b296d93
Β·
verified Β·
1 Parent(s): 0d3ec65

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +160 -0
  2. requirements.txt +6 -0
  3. typhoidguard_model_reduced.pkl +3 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
6
+ from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
7
+ from reportlab.lib import colors
8
+ from reportlab.lib.units import inch
9
+ from reportlab.platypus import Table
10
+ from reportlab.platypus import TableStyle
11
+
12
+ # Load model
13
+ model = joblib.load("typhoidguard_model_reduced.pkl")
14
+
15
+ # Risk & Agent Logic
16
+ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
17
+
18
+ data = pd.DataFrame([{
19
+ "Platelet Count": platelets,
20
+ "Age": age,
21
+ "Hemoglobin (g/dL)": hb,
22
+ "Treatment Duration": duration,
23
+ "Calcium (mg/dL)": calcium,
24
+ "Potassium (mmol/L)": potassium,
25
+ "Gender": gender
26
+ }])
27
+
28
+ risk = model.predict_proba(data)[0][1] * 100
29
+ risk = round(risk, 2)
30
+
31
+ # Agent Reasoning
32
+ factors = []
33
+ if platelets < 150000:
34
+ factors.append("Low Platelet Count")
35
+ if hb < 10:
36
+ factors.append("Low Hemoglobin")
37
+ if duration > 7:
38
+ factors.append("Prolonged Treatment Duration")
39
+
40
+ if risk > 65:
41
+ status = "πŸ”΄ HIGH RISK"
42
+ recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
43
+ color = "#d62828"
44
+ elif 35 <= risk <= 65:
45
+ status = "🟑 MODERATE RISK"
46
+ recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
47
+ color = "#f4a261"
48
+ else:
49
+ status = "🟒 LOW RISK"
50
+ recommendation = "Continue standard treatment protocol."
51
+ color = "#2a9d8f"
52
+
53
+ explanation = f"""
54
+ <div style="font-family:Arial; padding:15px;">
55
+ <h2 style="color:{color};">Patient: {name}</h2>
56
+ <h3>Risk of Treatment Failure: {risk}%</h3>
57
+ <h3>Status: {status}</h3>
58
+ <h4>Clinical Risk Factors:</h4>
59
+ <ul>
60
+ {''.join([f"<li>{f}</li>" for f in factors])}
61
+ </ul>
62
+ <h4>Agent Recommendation:</h4>
63
+ <p>{recommendation}</p>
64
+ </div>
65
+ """
66
+
67
+ return explanation
68
+
69
+
70
+ # PDF Generation
71
+ def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
72
+
73
+ file_path = f"{name}_Typhoid_Report.pdf"
74
+ doc = SimpleDocTemplate(file_path)
75
+ elements = []
76
+ styles = getSampleStyleSheet()
77
+
78
+ elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
79
+ elements.append(Spacer(1, 0.5 * inch))
80
+
81
+ data = [
82
+ ["Patient Name", name],
83
+ ["Age", age],
84
+ ["Gender", gender],
85
+ ["Platelet Count", platelets],
86
+ ["Hemoglobin", hb],
87
+ ["Treatment Duration", duration],
88
+ ["Calcium", calcium],
89
+ ["Potassium", potassium],
90
+ ]
91
+
92
+ table = Table(data)
93
+ table.setStyle(TableStyle([
94
+ ('BACKGROUND', (0,0), (-1,0), colors.lightblue),
95
+ ('GRID', (0,0), (-1,-1), 1, colors.grey)
96
+ ]))
97
+
98
+ elements.append(table)
99
+ elements.append(Spacer(1, 0.5 * inch))
100
+ elements.append(Paragraph("Generated under HEC Generative AI Training Hackathon", styles["Normal"]))
101
+ elements.append(Paragraph("Team Kohsar AI - AJK", styles["Normal"]))
102
+
103
+ doc.build(elements)
104
+ return file_path
105
+
106
+
107
+ # Interface
108
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
109
+
110
+ gr.Markdown("""
111
+ # πŸ₯ TyphoidGuard AI
112
+ ### HEC Generative AI Training Hackathon – Pakistan
113
+ #### Team Kohsar AI (AJK)
114
+ """)
115
+
116
+ with gr.Row():
117
+ name = gr.Textbox(label="Patient Name")
118
+ age = gr.Number(label="Age")
119
+ gender = gr.Radio(["Male", "Female"], label="Gender")
120
+
121
+ with gr.Row():
122
+ platelets = gr.Number(label="Platelet Count")
123
+ hb = gr.Number(label="Hemoglobin (g/dL)")
124
+ duration = gr.Number(label="Treatment Duration (Days)")
125
+
126
+ with gr.Row():
127
+ calcium = gr.Number(label="Calcium (mg/dL)")
128
+ potassium = gr.Number(label="Potassium (mmol/L)")
129
+
130
+ output = gr.HTML()
131
+
132
+ predict_btn = gr.Button("πŸ” Analyze Patient")
133
+ pdf_btn = gr.Button("πŸ“„ Generate PDF Report")
134
+
135
+ predict_btn.click(
136
+ predict_and_generate,
137
+ inputs=[name, age, gender, platelets, hb, duration, calcium, potassium],
138
+ outputs=output
139
+ )
140
+
141
+ pdf_btn.click(
142
+ generate_pdf,
143
+ inputs=[name, age, gender, platelets, hb, duration, calcium, potassium],
144
+ outputs=gr.File()
145
+ )
146
+
147
+ gr.Markdown("""
148
+ ---
149
+ ### πŸ‘₯ Team Members:
150
+ - Anees Qumar Abbasi (Team Lead)
151
+ - Sharafat Hussain
152
+ - Salma Asghar
153
+ - Kaleem Hussain
154
+ - Munazza Zahra
155
+ - Kokub Khurishid
156
+
157
+ Β© 2026 Team Kohsar AI – All Rights Reserved
158
+ """)
159
+
160
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ scikit-learn
3
+ pandas
4
+ numpy
5
+ reportlab
6
+ joblib
typhoidguard_model_reduced.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3da4eb8f010a4d05b23d554b738eb6756acdafe2eb78766c85f10829e719c85
3
+ size 124971