Delete app11.py
Browse files
app11.py
DELETED
|
@@ -1,243 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
import os
|
| 6 |
-
from groq import Groq
|
| 7 |
-
|
| 8 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 9 |
-
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
|
| 10 |
-
from reportlab.lib import colors
|
| 11 |
-
from reportlab.lib.units import inch
|
| 12 |
-
from reportlab.platypus import Table
|
| 13 |
-
from reportlab.platypus import TableStyle
|
| 14 |
-
|
| 15 |
-
# Load model
|
| 16 |
-
model = joblib.load("typhoidguard_model_reduced.pkl")
|
| 17 |
-
|
| 18 |
-
# Groq Client (API key from HF Secrets)
|
| 19 |
-
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 20 |
-
|
| 21 |
-
# ===============================
|
| 22 |
-
# LLM REPORT FUNCTION
|
| 23 |
-
# ===============================
|
| 24 |
-
def generate_llm_report(patient_info, prob_pct, risk_label, recommendation, factors):
|
| 25 |
-
|
| 26 |
-
prompt = f"""
|
| 27 |
-
You are a responsible medical AI assistant helping rural healthcare units in Pakistan.
|
| 28 |
-
|
| 29 |
-
Patient Details:
|
| 30 |
-
{patient_info}
|
| 31 |
-
|
| 32 |
-
Predicted Risk of Treatment Failure: {prob_pct}%
|
| 33 |
-
Risk Category: {risk_label}
|
| 34 |
-
|
| 35 |
-
Clinical Risk Factors:
|
| 36 |
-
{", ".join(factors) if factors else "None"}
|
| 37 |
-
|
| 38 |
-
Agent Recommendation:
|
| 39 |
-
{recommendation}
|
| 40 |
-
|
| 41 |
-
Generate a structured detailed report including:
|
| 42 |
-
1. Clinical Interpretation
|
| 43 |
-
2. Possible Complications
|
| 44 |
-
3. Immediate Precautions
|
| 45 |
-
4. Preventative Measures
|
| 46 |
-
5. Follow-up Recommendations
|
| 47 |
-
|
| 48 |
-
Use clear bullet points.
|
| 49 |
-
End with a reminder that final decisions must be made by a qualified doctor.
|
| 50 |
-
"""
|
| 51 |
-
|
| 52 |
-
response = client.chat.completions.create(
|
| 53 |
-
model="llama-3.3-70b-versatile",
|
| 54 |
-
messages=[{"role": "user", "content": prompt}],
|
| 55 |
-
temperature=0.4,
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
return response.choices[0].message.content
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# ===============================
|
| 62 |
-
# Risk & Agentic Logic (UNCHANGED)
|
| 63 |
-
# ===============================
|
| 64 |
-
def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 65 |
-
|
| 66 |
-
data = pd.DataFrame([{
|
| 67 |
-
"Platelet Count": platelets,
|
| 68 |
-
"Age": age,
|
| 69 |
-
"Hemoglobin (g/dL)": hb,
|
| 70 |
-
"Treatment Duration": duration,
|
| 71 |
-
"Calcium (mg/dL)": calcium,
|
| 72 |
-
"Potassium (mmol/L)": potassium,
|
| 73 |
-
"Gender": gender
|
| 74 |
-
}])
|
| 75 |
-
|
| 76 |
-
X_processed = model.named_steps["preprocessor"].transform(data)
|
| 77 |
-
prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1]
|
| 78 |
-
|
| 79 |
-
low_platelets = platelets < 150000
|
| 80 |
-
low_hb = hb < 10
|
| 81 |
-
prolonged_treatment = duration > 7
|
| 82 |
-
|
| 83 |
-
if low_platelets or low_hb:
|
| 84 |
-
prob_pct = 90.0
|
| 85 |
-
else:
|
| 86 |
-
prob_pct = round(prob * 100, 2)
|
| 87 |
-
|
| 88 |
-
if prob > 0.65:
|
| 89 |
-
risk_label = "🔴 HIGH RISK"
|
| 90 |
-
color = "#d62828"
|
| 91 |
-
elif prob >= 0.35:
|
| 92 |
-
risk_label = "🟡 MODERATE RISK"
|
| 93 |
-
color = "#f4a261"
|
| 94 |
-
else:
|
| 95 |
-
risk_label = "🟢 LOW RISK"
|
| 96 |
-
color = "#2a9d8f"
|
| 97 |
-
|
| 98 |
-
if low_platelets or low_hb:
|
| 99 |
-
risk_label = "🔴 HIGH RISK"
|
| 100 |
-
color = "#d62828"
|
| 101 |
-
recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
|
| 102 |
-
elif prob >= 0.35:
|
| 103 |
-
recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
|
| 104 |
-
else:
|
| 105 |
-
recommendation = "Continue standard treatment protocol."
|
| 106 |
-
|
| 107 |
-
factors = []
|
| 108 |
-
if low_platelets:
|
| 109 |
-
factors.append("Low Platelet Count")
|
| 110 |
-
if low_hb:
|
| 111 |
-
factors.append("Low Hemoglobin")
|
| 112 |
-
if prolonged_treatment:
|
| 113 |
-
factors.append("Prolonged Treatment Duration")
|
| 114 |
-
if calcium < 8.5:
|
| 115 |
-
factors.append("Low Calcium")
|
| 116 |
-
if potassium < 3.5 or potassium > 5.0:
|
| 117 |
-
factors.append("Abnormal Potassium")
|
| 118 |
-
|
| 119 |
-
# -------- LLM CALL --------
|
| 120 |
-
patient_info = f"""
|
| 121 |
-
Age: {age}
|
| 122 |
-
Gender: {gender}
|
| 123 |
-
Platelets: {platelets}
|
| 124 |
-
Hemoglobin: {hb}
|
| 125 |
-
Treatment Duration: {duration}
|
| 126 |
-
Calcium: {calcium}
|
| 127 |
-
Potassium: {potassium}
|
| 128 |
-
"""
|
| 129 |
-
|
| 130 |
-
llm_report = generate_llm_report(
|
| 131 |
-
patient_info, prob_pct, risk_label, recommendation, factors
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# -------- HTML OUTPUT --------
|
| 135 |
-
explanation = f"""
|
| 136 |
-
<div style="font-family:Arial; padding:15px;">
|
| 137 |
-
<h2 style="color:{color};">Patient: {name}</h2>
|
| 138 |
-
<h3>Risk of Treatment Failure: {prob_pct}%</h3>
|
| 139 |
-
<h3>Status: {risk_label}</h3>
|
| 140 |
-
|
| 141 |
-
<h4>Clinical Risk Factors:</h4>
|
| 142 |
-
<ul>
|
| 143 |
-
{''.join([f"<li>{f}</li>" for f in factors]) if factors else "<li>None</li>"}
|
| 144 |
-
</ul>
|
| 145 |
-
|
| 146 |
-
<h4>Agent Recommendation:</h4>
|
| 147 |
-
<p><b>{recommendation}</b></p>
|
| 148 |
-
|
| 149 |
-
<hr>
|
| 150 |
-
|
| 151 |
-
<h3 style="color:#003366;">🧠 Detailed AI Report & Recommendations</h3>
|
| 152 |
-
<div style="background:#f8f9fa; padding:15px; border-radius:10px; line-height:1.6;">
|
| 153 |
-
{llm_report.replace("\n", "<br>")}
|
| 154 |
-
</div>
|
| 155 |
-
</div>
|
| 156 |
-
"""
|
| 157 |
-
|
| 158 |
-
return explanation
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
# ===============================
|
| 162 |
-
# PDF Generation (LLM Included)
|
| 163 |
-
# ===============================
|
| 164 |
-
def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 165 |
-
|
| 166 |
-
html_output = predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium)
|
| 167 |
-
|
| 168 |
-
file_path = f"{name}_Typhoid_Report.pdf"
|
| 169 |
-
doc = SimpleDocTemplate(file_path)
|
| 170 |
-
elements = []
|
| 171 |
-
styles = getSampleStyleSheet()
|
| 172 |
-
|
| 173 |
-
elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
|
| 174 |
-
elements.append(Spacer(1, 0.3 * inch))
|
| 175 |
-
|
| 176 |
-
elements.append(Paragraph(html_output.replace("<br>", "<br/>"), styles["Normal"]))
|
| 177 |
-
elements.append(Spacer(1, 0.3 * inch))
|
| 178 |
-
|
| 179 |
-
elements.append(Paragraph(
|
| 180 |
-
"<i>Disclaimer: This report is AI-generated. Please consult a qualified doctor before making clinical decisions.</i>",
|
| 181 |
-
styles["Normal"]
|
| 182 |
-
))
|
| 183 |
-
|
| 184 |
-
doc.build(elements)
|
| 185 |
-
return file_path
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
# ===============================
|
| 189 |
-
# Interface
|
| 190 |
-
# ===============================
|
| 191 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 192 |
-
|
| 193 |
-
gr.Markdown("""
|
| 194 |
-
# 🏥 TyphoidGuard AI
|
| 195 |
-
### HEC Generative AI Training Hackathon – Pakistan
|
| 196 |
-
#### Team Chinar AI (AJK)
|
| 197 |
-
""")
|
| 198 |
-
|
| 199 |
-
with gr.Row():
|
| 200 |
-
name = gr.Textbox(label="Patient Name")
|
| 201 |
-
age = gr.Number(label="Age")
|
| 202 |
-
gender = gr.Radio(["Male", "Female"], label="Gender")
|
| 203 |
-
|
| 204 |
-
with gr.Row():
|
| 205 |
-
platelets = gr.Number(label="Platelet Count")
|
| 206 |
-
hb = gr.Number(label="Hemoglobin (g/dL)")
|
| 207 |
-
duration = gr.Number(label="Treatment Duration (Days)")
|
| 208 |
-
|
| 209 |
-
with gr.Row():
|
| 210 |
-
calcium = gr.Number(label="Calcium (mg/dL)")
|
| 211 |
-
potassium = gr.Number(label="Potassium (mmol/L)")
|
| 212 |
-
|
| 213 |
-
output = gr.HTML()
|
| 214 |
-
|
| 215 |
-
predict_btn = gr.Button("🧠 Detailed Report and Recommendations")
|
| 216 |
-
pdf_btn = gr.Button("📄 Generate PDF Report")
|
| 217 |
-
|
| 218 |
-
predict_btn.click(
|
| 219 |
-
predict_and_generate,
|
| 220 |
-
inputs=[name, age, gender, platelets, hb, duration, calcium, potassium],
|
| 221 |
-
outputs=output
|
| 222 |
-
)
|
| 223 |
-
|
| 224 |
-
pdf_btn.click(
|
| 225 |
-
generate_pdf,
|
| 226 |
-
inputs=[name, age, gender, platelets, hb, duration, calcium, potassium],
|
| 227 |
-
outputs=gr.File()
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
gr.Markdown("""
|
| 231 |
-
---
|
| 232 |
-
### 👥 Team Members:
|
| 233 |
-
- Anees Qumar Abbasi (Team Lead)
|
| 234 |
-
- Sharafat Hussain
|
| 235 |
-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