Update app.py
Browse files
app.py
CHANGED
|
@@ -1,557 +1,557 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import joblib
|
| 5 |
-
import pickle
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import io
|
| 8 |
-
import cv2
|
| 9 |
-
import pytesseract
|
| 10 |
-
from sklearn.metrics import roc_auc_score, accuracy_score, classification_report
|
| 11 |
-
import plotly.graph_objects as go
|
| 12 |
-
import plotly.express as px
|
| 13 |
-
from datetime import datetime
|
| 14 |
-
import requests
|
| 15 |
-
import json
|
| 16 |
-
import base64
|
| 17 |
-
|
| 18 |
-
# Custom CSS for styling
|
| 19 |
-
def local_css():
|
| 20 |
-
st.markdown("""
|
| 21 |
-
<style>
|
| 22 |
-
.main-header {
|
| 23 |
-
font-size: 3rem;
|
| 24 |
-
color: #2E86AB;
|
| 25 |
-
text-align: center;
|
| 26 |
-
margin-bottom: 2rem;
|
| 27 |
-
font-weight: bold;
|
| 28 |
-
}
|
| 29 |
-
.urdu-text {
|
| 30 |
-
font-family: 'Jameel Noori Nastaleeq', 'Noto Sans Arabic';
|
| 31 |
-
font-size: 1.2rem;
|
| 32 |
-
direction: rtl;
|
| 33 |
-
}
|
| 34 |
-
.risk-high { background-color: #ffcccc; padding: 10px; border-radius: 5px; }
|
| 35 |
-
.risk-medium { background-color: #fff3cd; padding: 10px; border-radius: 5px; }
|
| 36 |
-
.risk-low { background-color: #d4edda; padding: 10px; border-radius: 5px; }
|
| 37 |
-
.priority-box {
|
| 38 |
-
border: 2px solid #2E86AB;
|
| 39 |
-
padding: 20px;
|
| 40 |
-
border-radius: 10px;
|
| 41 |
-
margin: 10px 0;
|
| 42 |
-
}
|
| 43 |
-
</style>
|
| 44 |
-
""", unsafe_allow_html=True)
|
| 45 |
-
|
| 46 |
-
# Initialize session state
|
| 47 |
-
def init_session_state():
|
| 48 |
-
if 'language' not in st.session_state:
|
| 49 |
-
st.session_state.language = 'English'
|
| 50 |
-
if 'patient_data' not in st.session_state:
|
| 51 |
-
st.session_state.patient_data = {}
|
| 52 |
-
if 'risk_scores' not in st.session_state:
|
| 53 |
-
st.session_state.risk_scores = {}
|
| 54 |
-
|
| 55 |
-
# Load models with error handling
|
| 56 |
-
@st.cache_resource
|
| 57 |
-
def load_models():
|
| 58 |
-
try:
|
| 59 |
-
heart_model = joblib.load("
|
| 60 |
-
diabetes_model = joblib.load("
|
| 61 |
-
hypertension_model = joblib.load("hypertension_model.pkl")
|
| 62 |
-
return heart_model, diabetes_model, hypertension_model
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Error loading models: {str(e)}")
|
| 65 |
-
return None, None, None
|
| 66 |
-
|
| 67 |
-
# Urdu translations
|
| 68 |
-
URDU_TRANSLATIONS = {
|
| 69 |
-
"AI-Priority OPD System": "AI-ترجیحی OPD سسٹم",
|
| 70 |
-
"Patient Information": "مریض کی معلومات",
|
| 71 |
-
"Name": "نام",
|
| 72 |
-
"Age": "عمر",
|
| 73 |
-
"Gender": "جنس",
|
| 74 |
-
"Contact": "رابطہ نمبر",
|
| 75 |
-
"Medical History": "طبی تاریخ",
|
| 76 |
-
"Vital Signs": "اہم علامات",
|
| 77 |
-
"Blood Pressure (systolic)": "بلڈ پریشر (سسٹولک)",
|
| 78 |
-
"Blood Pressure (diastolic)": "بلڈ پریشر ڈائیسٹولک)",
|
| 79 |
-
"Heart Rate": "دل کی دھڑکن",
|
| 80 |
-
"Cholesterol Level": "کولیسٹرول کی سطح",
|
| 81 |
-
"Blood Glucose": "خون میں گلوکوز",
|
| 82 |
-
"BMI": "باڈی ماس انڈیکس",
|
| 83 |
-
"Symptoms": "علامات",
|
| 84 |
-
"Chest Pain": "سینے میں درد",
|
| 85 |
-
"Shortness of Breath": "سانس لینے میں دشواری",
|
| 86 |
-
"Fatigue": "تھکاوٹ",
|
| 87 |
-
"Upload Prescription": "نسخہ اپ لوڈ کریں",
|
| 88 |
-
"Calculate Risk Score": "خطرے کا اسکور معلوم کریں",
|
| 89 |
-
"High Priority - Emergency Care Required": "اعلی ترجیح - ہنگامی علاج کی ضرورت",
|
| 90 |
-
"Medium Priority - Same Day Consultation": "درمیانی ترجیح - اسی دن مشورہ",
|
| 91 |
-
"Low Priority - Routine Appointment": "کم ترجیح - معمول کی ملاقات",
|
| 92 |
-
"Healthcare Chatbot": "ہیلتھ کیئر چیٹ بوٹ",
|
| 93 |
-
"Ask health-related questions": "صحت سے متعلق سوالات پوچھیں"
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
class OCRProcessor:
|
| 97 |
-
def __init__(self):
|
| 98 |
-
pass
|
| 99 |
-
|
| 100 |
-
def preprocess_image(self, image):
|
| 101 |
-
"""Enhance image for better OCR accuracy"""
|
| 102 |
-
try:
|
| 103 |
-
# Convert to grayscale
|
| 104 |
-
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 105 |
-
|
| 106 |
-
# Apply thresholding
|
| 107 |
-
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 108 |
-
|
| 109 |
-
# Remove noise
|
| 110 |
-
kernel = np.ones((1, 1), np.uint8)
|
| 111 |
-
processed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
| 112 |
-
|
| 113 |
-
return processed
|
| 114 |
-
except Exception as e:
|
| 115 |
-
st.error(f"Image processing error: {str(e)}")
|
| 116 |
-
return np.array(image)
|
| 117 |
-
|
| 118 |
-
def extract_text(self, image):
|
| 119 |
-
"""Extract text from prescription image"""
|
| 120 |
-
try:
|
| 121 |
-
processed_image = self.preprocess_image(image)
|
| 122 |
-
|
| 123 |
-
# Configure Tesseract for medical text
|
| 124 |
-
custom_config = r'--oem 3 --psm 6 -l eng'
|
| 125 |
-
text = pytesseract.image_to_string(processed_image, config=custom_config)
|
| 126 |
-
|
| 127 |
-
return text.strip()
|
| 128 |
-
except Exception as e:
|
| 129 |
-
st.error(f"OCR Error: {str(e)}")
|
| 130 |
-
return ""
|
| 131 |
-
|
| 132 |
-
class HealthcareChatbot:
|
| 133 |
-
def __init__(self):
|
| 134 |
-
self.health_tips = {
|
| 135 |
-
'heart': [
|
| 136 |
-
"Maintain a healthy diet low in saturated fats",
|
| 137 |
-
"Exercise regularly for at least 30 minutes daily",
|
| 138 |
-
"Monitor blood pressure regularly",
|
| 139 |
-
"Avoid smoking and limit alcohol consumption"
|
| 140 |
-
],
|
| 141 |
-
'diabetes': [
|
| 142 |
-
"Monitor blood sugar levels regularly",
|
| 143 |
-
"Follow a balanced diet with controlled carbohydrates",
|
| 144 |
-
"Take medications as prescribed",
|
| 145 |
-
"Stay physically active"
|
| 146 |
-
],
|
| 147 |
-
'hypertension': [
|
| 148 |
-
"Reduce salt intake in your diet",
|
| 149 |
-
"Practice stress management techniques",
|
| 150 |
-
"Maintain healthy body weight",
|
| 151 |
-
"Limit caffeine consumption"
|
| 152 |
-
]
|
| 153 |
-
}
|
| 154 |
-
|
| 155 |
-
def get_response(self, query, language='English'):
|
| 156 |
-
"""Generate chatbot response"""
|
| 157 |
-
query_lower = query.lower()
|
| 158 |
-
|
| 159 |
-
# Simple rule-based responses
|
| 160 |
-
if any(word in query_lower for word in ['heart', 'cardiac', 'chest pain']):
|
| 161 |
-
tips = self.health_tips['heart']
|
| 162 |
-
elif any(word in query_lower for word in ['diabetes', 'sugar', 'glucose']):
|
| 163 |
-
tips = self.health_tips['diabetes']
|
| 164 |
-
elif any(word in query_lower for word in ['blood pressure', 'hypertension']):
|
| 165 |
-
tips = self.health_tips['hypertension']
|
| 166 |
-
else:
|
| 167 |
-
tips = ["Maintain regular health checkups", "Follow your doctor's advice", "Stay hydrated and eat balanced meals"]
|
| 168 |
-
|
| 169 |
-
response = f"Based on your query, here are some health tips:\n" + "\n".join([f"• {tip}" for tip in tips])
|
| 170 |
-
|
| 171 |
-
if language == 'Urdu':
|
| 172 |
-
# Simple Urdu translations (in a real app, use proper translation API)
|
| 173 |
-
response = "آپ کے سوال کے مطابق، یہاں کچھ صحت کے نکات ہیں:\n" + "\n".join([f"• {tip}" for tip in tips])
|
| 174 |
-
|
| 175 |
-
return response
|
| 176 |
-
|
| 177 |
-
def calculate_priority_score(heart_risk, diabetes_risk, hypertension_risk):
|
| 178 |
-
"""Calculate integrated priority score"""
|
| 179 |
-
# Weighted priority scoring based on clinical severity
|
| 180 |
-
priority = (
|
| 181 |
-
heart_risk * 0.4 +
|
| 182 |
-
diabetes_risk * 0.3 +
|
| 183 |
-
hypertension_risk * 0.3
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
return priority
|
| 187 |
-
|
| 188 |
-
def get_priority_recommendation(priority_score, language='English'):
|
| 189 |
-
"""Get priority-based recommendation"""
|
| 190 |
-
if priority_score > 0.8:
|
| 191 |
-
if language == 'Urdu':
|
| 192 |
-
return "EMERGENCY_CARE", "اعلی ترجیح - ہنگامی علاج کی ضرورت", "risk-high"
|
| 193 |
-
else:
|
| 194 |
-
return "EMERGENCY_CARE", "High Priority - Emergency Care Required", "risk-high"
|
| 195 |
-
elif priority_score > 0.6:
|
| 196 |
-
if language == 'Urdu':
|
| 197 |
-
return "SAME_DAY_CONSULT", "درمیانی ترجیح - اسی دن مشورہ", "risk-medium"
|
| 198 |
-
else:
|
| 199 |
-
return "SAME_DAY_CONSULT", "Medium Priority - Same Day Consultation", "risk-medium"
|
| 200 |
-
else:
|
| 201 |
-
if language == 'Urdu':
|
| 202 |
-
return "ROUTINE_APPOINTMENT", "کم ترجیح - معمول کی ملاقات", "risk-low"
|
| 203 |
-
else:
|
| 204 |
-
return "ROUTINE_APPOINTMENT", "Low Priority - Routine Appointment", "risk-low"
|
| 205 |
-
|
| 206 |
-
def main():
|
| 207 |
-
# Page configuration
|
| 208 |
-
st.set_page_config(
|
| 209 |
-
page_title="AI-Priority OPD System",
|
| 210 |
-
page_icon="🏥",
|
| 211 |
-
layout="wide",
|
| 212 |
-
initial_sidebar_state="expanded"
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
# Load custom CSS
|
| 216 |
-
local_css()
|
| 217 |
-
init_session_state()
|
| 218 |
-
|
| 219 |
-
# Load models
|
| 220 |
-
heart_model, diabetes_model, hypertension_model = load_models()
|
| 221 |
-
|
| 222 |
-
if heart_model is None or diabetes_model is None or hypertension_model is None:
|
| 223 |
-
st.error("Failed to load ML models. Please check model files.")
|
| 224 |
-
return
|
| 225 |
-
|
| 226 |
-
# Initialize processors
|
| 227 |
-
ocr_processor = OCRProcessor()
|
| 228 |
-
chatbot = HealthcareChatbot()
|
| 229 |
-
|
| 230 |
-
# Language selector in sidebar
|
| 231 |
-
with st.sidebar:
|
| 232 |
-
st.image("https://via.placeholder.com/150x50/2E86AB/FFFFFF?text=AI-OPD", use_column_width=True)
|
| 233 |
-
language = st.radio("Select Language / زبان منتخب کریں",
|
| 234 |
-
["English", "Urdu"],
|
| 235 |
-
key="language_selector")
|
| 236 |
-
|
| 237 |
-
st.markdown("---")
|
| 238 |
-
st.subheader("Quick Actions")
|
| 239 |
-
if st.button("New Patient Assessment" if language == "English" else "نیا مریض تشخیص"):
|
| 240 |
-
st.session_state.patient_data = {}
|
| 241 |
-
st.session_state.risk_scores = {}
|
| 242 |
-
|
| 243 |
-
# Main header
|
| 244 |
-
if language == "English":
|
| 245 |
-
st.markdown('<h1 class="main-header">🏥 AI-Priority OPD System</h1>', unsafe_allow_html=True)
|
| 246 |
-
st.markdown("### Smart Patient Triage and Priority Management")
|
| 247 |
-
else:
|
| 248 |
-
st.markdown('<h1 class="main-header">🏥 AI-ترجیحی OPD سسٹم</h1>', unsafe_allow_html=True)
|
| 249 |
-
st.markdown("### ذہین مریض کی درجہ بندی اور ترجیحی انتظام")
|
| 250 |
-
|
| 251 |
-
# Create tabs
|
| 252 |
-
tab1, tab2, tab3, tab4 = st.tabs([
|
| 253 |
-
"Patient Assessment" if language == "English" else "مریض تشخیص",
|
| 254 |
-
"Prescription OCR" if language == "English" else "نسخہ OCR",
|
| 255 |
-
"Health Chatbot" if language == "English" else "ہیلتھ چیٹ بوٹ",
|
| 256 |
-
"Analytics" if language == "English" else "تجزیات"
|
| 257 |
-
])
|
| 258 |
-
|
| 259 |
-
with tab1:
|
| 260 |
-
# Patient Assessment Form
|
| 261 |
-
if language == "English":
|
| 262 |
-
st.header("Patient Information")
|
| 263 |
-
else:
|
| 264 |
-
st.header("مریض کی معلومات")
|
| 265 |
-
|
| 266 |
-
col1, col2 = st.columns(2)
|
| 267 |
-
|
| 268 |
-
with col1:
|
| 269 |
-
# Basic Information
|
| 270 |
-
if language == "English":
|
| 271 |
-
name = st.text_input("Full Name")
|
| 272 |
-
age = st.number_input("Age", min_value=0, max_value=120, value=30)
|
| 273 |
-
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 274 |
-
contact = st.text_input("Contact Number")
|
| 275 |
-
else:
|
| 276 |
-
name = st.text_input("مکمل نام")
|
| 277 |
-
age = st.number_input("عمر", min_value=0, max_value=120, value=30)
|
| 278 |
-
gender = st.selectbox("جنس", ["مرد", "عورت", "دیگر"])
|
| 279 |
-
contact = st.text_input("رابطہ نمبر")
|
| 280 |
-
|
| 281 |
-
with col2:
|
| 282 |
-
# Vital Signs
|
| 283 |
-
if language == "English":
|
| 284 |
-
st.subheader("Vital Signs")
|
| 285 |
-
bp_systolic = st.number_input("Blood Pressure (systolic)", min_value=50, max_value=250, value=120)
|
| 286 |
-
bp_diastolic = st.number_input("Blood Pressure (diastolic)", min_value=30, max_value=150, value=80)
|
| 287 |
-
heart_rate = st.number_input("Heart Rate (bpm)", min_value=30, max_value=200, value=72)
|
| 288 |
-
cholesterol = st.number_input("Cholesterol Level (mg/dL)", min_value=100, max_value=400, value=200)
|
| 289 |
-
glucose = st.number_input("Blood Glucose (mg/dL)", min_value=50, max_value=500, value=100)
|
| 290 |
-
bmi = st.number_input("BMI", min_value=10.0, max_value=50.0, value=22.0, step=0.1)
|
| 291 |
-
else:
|
| 292 |
-
st.subheader("اہم علامات")
|
| 293 |
-
bp_systolic = st.number_input("بلڈ پریشر (سسٹولک)", min_value=50, max_value=250, value=120)
|
| 294 |
-
bp_diastolic = st.number_input("بلڈ پریشر (ڈائیسٹولک)", min_value=30, max_value=150, value=80)
|
| 295 |
-
heart_rate = st.number_input("دل کی دھڑکن (bpm)", min_value=30, max_value=200, value=72)
|
| 296 |
-
cholesterol = st.number_input("کولیسٹرول کی سطح (mg/dL)", min_value=100, max_value=400, value=200)
|
| 297 |
-
glucose = st.number_input("خون میں گلوکوز (mg/dL)", min_value=50, max_value=500, value=100)
|
| 298 |
-
bmi = st.number_input("باڈی ماس انڈیکس", min_value=10.0, max_value=50.0, value=22.0, step=0.1)
|
| 299 |
-
|
| 300 |
-
# Symptoms
|
| 301 |
-
if language == "English":
|
| 302 |
-
st.subheader("Symptoms")
|
| 303 |
-
col3, col4 = st.columns(2)
|
| 304 |
-
with col3:
|
| 305 |
-
chest_pain = st.checkbox("Chest Pain")
|
| 306 |
-
shortness_breath = st.checkbox("Shortness of Breath")
|
| 307 |
-
with col4:
|
| 308 |
-
fatigue = st.checkbox("Fatigue")
|
| 309 |
-
dizziness = st.checkbox("Dizziness")
|
| 310 |
-
else:
|
| 311 |
-
st.subheader("علامات")
|
| 312 |
-
col3, col4 = st.columns(2)
|
| 313 |
-
with col3:
|
| 314 |
-
chest_pain = st.checkbox("سینے میں درد")
|
| 315 |
-
shortness_breath = st.checkbox("سانس لینے میں دشواری")
|
| 316 |
-
with col4:
|
| 317 |
-
fatigue = st.checkbox("تھکاوٹ")
|
| 318 |
-
dizziness = st.checkbox("چکر آنا")
|
| 319 |
-
|
| 320 |
-
# Risk Assessment Button
|
| 321 |
-
if language == "English":
|
| 322 |
-
assess_button = st.button("🚀 Calculate Risk Score & Priority", use_container_width=True)
|
| 323 |
-
else:
|
| 324 |
-
assess_button = st.button("🚀 خطرے کا اسکور اور ترجیح معلوم کریں", use_container_width=True)
|
| 325 |
-
|
| 326 |
-
if assess_button:
|
| 327 |
-
try:
|
| 328 |
-
# Prepare feature arrays (adjust based on your model requirements)
|
| 329 |
-
heart_features = np.array([[age, bp_systolic, cholesterol, heart_rate, 1 if chest_pain else 0]])
|
| 330 |
-
diabetes_features = np.array([[age, glucose, bmi, cholesterol]])
|
| 331 |
-
hypertension_features = np.array([[age, bp_systolic, bp_diastolic, bmi]])
|
| 332 |
-
|
| 333 |
-
# Get predictions
|
| 334 |
-
heart_risk = heart_model.predict_proba(heart_features)[0][1]
|
| 335 |
-
diabetes_risk = diabetes_model.predict_proba(diabetes_features)[0][1]
|
| 336 |
-
hypertension_risk = hypertension_model.predict_proba(hypertension_features)[0][1]
|
| 337 |
-
|
| 338 |
-
# Calculate priority score
|
| 339 |
-
priority_score = calculate_priority_score(heart_risk, diabetes_risk, hypertension_risk)
|
| 340 |
-
priority_level, recommendation, risk_class = get_priority_recommendation(priority_score, language)
|
| 341 |
-
|
| 342 |
-
# Store results
|
| 343 |
-
st.session_state.risk_scores = {
|
| 344 |
-
'heart': heart_risk,
|
| 345 |
-
'diabetes': diabetes_risk,
|
| 346 |
-
'hypertension': hypertension_risk,
|
| 347 |
-
'priority': priority_score,
|
| 348 |
-
'recommendation': recommendation,
|
| 349 |
-
'level': priority_level
|
| 350 |
-
}
|
| 351 |
-
|
| 352 |
-
# Display results
|
| 353 |
-
st.markdown("---")
|
| 354 |
-
|
| 355 |
-
# Risk Scores Visualization
|
| 356 |
-
col5, col6, col7, col8 = st.columns(4)
|
| 357 |
-
|
| 358 |
-
with col5:
|
| 359 |
-
fig = go.Figure(go.Indicator(
|
| 360 |
-
mode = "gauge+number+delta",
|
| 361 |
-
value = heart_risk,
|
| 362 |
-
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 363 |
-
title = {'text': "Heart Disease Risk"},
|
| 364 |
-
gauge = {'axis': {'range': [0, 1]},
|
| 365 |
-
'bar': {'color': "red"},
|
| 366 |
-
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 367 |
-
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 368 |
-
{'range': [0.7, 1], 'color': "red"}]}))
|
| 369 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 370 |
-
|
| 371 |
-
with col6:
|
| 372 |
-
fig = go.Figure(go.Indicator(
|
| 373 |
-
mode = "gauge+number+delta",
|
| 374 |
-
value = diabetes_risk,
|
| 375 |
-
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 376 |
-
title = {'text': "Diabetes Risk"},
|
| 377 |
-
gauge = {'axis': {'range': [0, 1]},
|
| 378 |
-
'bar': {'color': "orange"},
|
| 379 |
-
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 380 |
-
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 381 |
-
{'range': [0.7, 1], 'color': "red"}]}))
|
| 382 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 383 |
-
|
| 384 |
-
with col7:
|
| 385 |
-
fig = go.Figure(go.Indicator(
|
| 386 |
-
mode = "gauge+number+delta",
|
| 387 |
-
value = hypertension_risk,
|
| 388 |
-
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 389 |
-
title = {'text': "Hypertension Risk"},
|
| 390 |
-
gauge = {'axis': {'range': [0, 1]},
|
| 391 |
-
'bar': {'color': "blue"},
|
| 392 |
-
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 393 |
-
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 394 |
-
{'range': [0.7, 1], 'color': "red"}]}))
|
| 395 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 396 |
-
|
| 397 |
-
with col8:
|
| 398 |
-
fig = go.Figure(go.Indicator(
|
| 399 |
-
mode = "gauge+number+delta",
|
| 400 |
-
value = priority_score,
|
| 401 |
-
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 402 |
-
title = {'text': "Priority Score"},
|
| 403 |
-
gauge = {'axis': {'range': [0, 1]},
|
| 404 |
-
'bar': {'color': "purple"},
|
| 405 |
-
'steps': [{'range': [0, 0.6], 'color': "lightgreen"},
|
| 406 |
-
{'range': [0.6, 0.8], 'color': "yellow"},
|
| 407 |
-
{'range': [0.8, 1], 'color': "red"}]}))
|
| 408 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 409 |
-
|
| 410 |
-
# Priority Recommendation
|
| 411 |
-
st.markdown(f'<div class="priority-box {risk_class}">', unsafe_allow_html=True)
|
| 412 |
-
if language == "English":
|
| 413 |
-
st.markdown(f"## 🎯 Priority Recommendation: {recommendation}")
|
| 414 |
-
st.markdown(f"**Overall Risk Score:** {priority_score:.3f}")
|
| 415 |
-
st.markdown(f"**Recommended Action:** {priority_level.replace('_', ' ').title()}")
|
| 416 |
-
else:
|
| 417 |
-
st.markdown(f"## 🎯 ترجیحی سفارش: {recommendation}")
|
| 418 |
-
st.markdown(f"**کل خطرے کا اسکور:** {priority_score:.3f}")
|
| 419 |
-
st.markdown(f"**سفارش کردہ عمل:** {priority_level.replace('_', ' ').title()}")
|
| 420 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 421 |
-
|
| 422 |
-
except Exception as e:
|
| 423 |
-
st.error(f"Error in risk assessment: {str(e)}")
|
| 424 |
-
|
| 425 |
-
with tab2:
|
| 426 |
-
# Prescription OCR
|
| 427 |
-
if language == "English":
|
| 428 |
-
st.header("Prescription OCR Analysis")
|
| 429 |
-
st.write("Upload a prescription image to extract medication information")
|
| 430 |
-
else:
|
| 431 |
-
st.header("نسخہ OCR تجزیہ")
|
| 432 |
-
st.write("دوائی کی معلومات نکالنے کے لیے
|
| 433 |
-
|
| 434 |
-
uploaded_file = st.file_uploader(
|
| 435 |
-
"Upload Prescription Image" if language == "English" else "نسخہ تصویر اپ لوڈ کریں",
|
| 436 |
-
type=['png', 'jpg', 'jpeg']
|
| 437 |
-
)
|
| 438 |
-
|
| 439 |
-
if uploaded_file is not None:
|
| 440 |
-
image = Image.open(uploaded_file)
|
| 441 |
-
st.image(image, caption="Uploaded Prescription", use_column_width=True)
|
| 442 |
-
|
| 443 |
-
if st.button("Extract Text" if language == "English" else "متن نکالیں"):
|
| 444 |
-
with st.spinner("Processing prescription..." if language == "English" else "نسخہ پروسیس ہو رہا ہے..."):
|
| 445 |
-
extracted_text = ocr_processor.extract_text(image)
|
| 446 |
-
|
| 447 |
-
if extracted_text:
|
| 448 |
-
if language == "English":
|
| 449 |
-
st.success("✅ Text extracted successfully!")
|
| 450 |
-
st.subheader("Extracted Text:")
|
| 451 |
-
else:
|
| 452 |
-
st.success("✅ متن کامیابی سے نکال لیا گیا!")
|
| 453 |
-
st.subheader("نکالا گیا متن:")
|
| 454 |
-
|
| 455 |
-
st.text_area("", extracted_text, height=200)
|
| 456 |
-
|
| 457 |
-
# Simple accuracy estimation (in real scenario, use validation dataset)
|
| 458 |
-
word_count = len(extracted_text.split())
|
| 459 |
-
non_empty_chars = len([c for c in extracted_text if c.strip()])
|
| 460 |
-
|
| 461 |
-
if word_count > 5 and non_empty_chars > 20:
|
| 462 |
-
estimated_accuracy = min(85, (word_count / max(1, word_count)) * 100)
|
| 463 |
-
st.metric("Estimated OCR Accuracy", f"{estimated_accuracy:.1f}%")
|
| 464 |
-
else:
|
| 465 |
-
st.warning("Low confidence in OCR extraction")
|
| 466 |
-
|
| 467 |
-
else:
|
| 468 |
-
if language == "English":
|
| 469 |
-
st.error("No text could be extracted from the image")
|
| 470 |
-
else:
|
| 471 |
-
st.error("تصویر سے کوئی متن نہیں نکالا جا سکا")
|
| 472 |
-
|
| 473 |
-
with tab3:
|
| 474 |
-
# Healthcare Chatbot
|
| 475 |
-
if language == "English":
|
| 476 |
-
st.header("Healthcare Assistant Chatbot")
|
| 477 |
-
st.write("Ask health-related questions and get personalized advice")
|
| 478 |
-
else:
|
| 479 |
-
st.header("ہیلتھ کیئر اسسٹنٹ چیٹ بوٹ")
|
| 480 |
-
st.write("صحت سے متعلق سوالات پوچھیں اور ذاتی مشورہ حاصل کریں")
|
| 481 |
-
|
| 482 |
-
# Initialize chat history
|
| 483 |
-
if 'chat_history' not in st.session_state:
|
| 484 |
-
st.session_state.chat_history = []
|
| 485 |
-
|
| 486 |
-
# Display chat history
|
| 487 |
-
for message in st.session_state.chat_history:
|
| 488 |
-
with st.chat_message(message["role"]):
|
| 489 |
-
st.markdown(message["content"])
|
| 490 |
-
|
| 491 |
-
# Chat input
|
| 492 |
-
if prompt := st.chat_input("Type your health question here..." if language == "English" else "اپنا صحت کا سوال یہاں ٹائپ کریں..."):
|
| 493 |
-
# Add user message to chat history
|
| 494 |
-
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 495 |
-
with st.chat_message("user"):
|
| 496 |
-
st.markdown(prompt)
|
| 497 |
-
|
| 498 |
-
# Generate bot response
|
| 499 |
-
with st.chat_message("assistant"):
|
| 500 |
-
with st.spinner("Thinking..." if language == "English" else "سوچ رہا ہوں..."):
|
| 501 |
-
response = chatbot.get_response(prompt, language)
|
| 502 |
-
st.markdown(response)
|
| 503 |
-
|
| 504 |
-
# Add assistant response to chat history
|
| 505 |
-
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 506 |
-
|
| 507 |
-
with tab4:
|
| 508 |
-
# Analytics Dashboard
|
| 509 |
-
if language == "English":
|
| 510 |
-
st.header("System Analytics & Performance")
|
| 511 |
-
else:
|
| 512 |
-
st.header("سسٹم تجزیات اور کارکردگی")
|
| 513 |
-
|
| 514 |
-
col9, col10, col11 = st.columns(3)
|
| 515 |
-
|
| 516 |
-
with col9:
|
| 517 |
-
# Mock accuracy metrics (replace with actual validation)
|
| 518 |
-
st.metric("Diagnostic Accuracy", "87%", "2%")
|
| 519 |
-
with col10:
|
| 520 |
-
st.metric("OCR Accuracy", "82%", "3%")
|
| 521 |
-
with col11:
|
| 522 |
-
st.metric("Risk Scoring AUC", "0.85", "0.02")
|
| 523 |
-
|
| 524 |
-
# Performance charts
|
| 525 |
-
if language == "English":
|
| 526 |
-
st.subheader("Priority Distribution")
|
| 527 |
-
else:
|
| 528 |
-
st.subheader("ترجیحی تقسیم")
|
| 529 |
-
|
| 530 |
-
# Mock data for demonstration
|
| 531 |
-
priority_data = pd.DataFrame({
|
| 532 |
-
'Priority': ['Emergency', 'Same Day', 'Routine'],
|
| 533 |
-
'Count': [25, 45, 30]
|
| 534 |
-
})
|
| 535 |
-
|
| 536 |
-
fig = px.pie(priority_data, values='Count', names='Priority',
|
| 537 |
-
title="Patient Priority Distribution")
|
| 538 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 539 |
-
|
| 540 |
-
# Model performance
|
| 541 |
-
if language == "English":
|
| 542 |
-
st.subheader("Model Performance Metrics")
|
| 543 |
-
else:
|
| 544 |
-
st.subheader("ماڈل کارکردگی کے پیمانے")
|
| 545 |
-
|
| 546 |
-
performance_data = pd.DataFrame({
|
| 547 |
-
'Model': ['Heart Disease', 'Diabetes', 'Hypertension'],
|
| 548 |
-
'Accuracy': [0.88, 0.85, 0.86],
|
| 549 |
-
'AUC': [0.89, 0.84, 0.87]
|
| 550 |
-
})
|
| 551 |
-
|
| 552 |
-
fig = px.bar(performance_data, x='Model', y=['Accuracy', 'AUC'],
|
| 553 |
-
title="Model Performance Comparison", barmode='group')
|
| 554 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 555 |
-
|
| 556 |
-
if __name__ == "__main__":
|
| 557 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import pickle
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import cv2
|
| 9 |
+
import pytesseract
|
| 10 |
+
from sklearn.metrics import roc_auc_score, accuracy_score, classification_report
|
| 11 |
+
import plotly.graph_objects as go
|
| 12 |
+
import plotly.express as px
|
| 13 |
+
from datetime import datetime
|
| 14 |
+
import requests
|
| 15 |
+
import json
|
| 16 |
+
import base64
|
| 17 |
+
|
| 18 |
+
# Custom CSS for styling
|
| 19 |
+
def local_css():
|
| 20 |
+
st.markdown("""
|
| 21 |
+
<style>
|
| 22 |
+
.main-header {
|
| 23 |
+
font-size: 3rem;
|
| 24 |
+
color: #2E86AB;
|
| 25 |
+
text-align: center;
|
| 26 |
+
margin-bottom: 2rem;
|
| 27 |
+
font-weight: bold;
|
| 28 |
+
}
|
| 29 |
+
.urdu-text {
|
| 30 |
+
font-family: 'Jameel Noori Nastaleeq', 'Noto Sans Arabic';
|
| 31 |
+
font-size: 1.2rem;
|
| 32 |
+
direction: rtl;
|
| 33 |
+
}
|
| 34 |
+
.risk-high { background-color: #ffcccc; padding: 10px; border-radius: 5px; }
|
| 35 |
+
.risk-medium { background-color: #fff3cd; padding: 10px; border-radius: 5px; }
|
| 36 |
+
.risk-low { background-color: #d4edda; padding: 10px; border-radius: 5px; }
|
| 37 |
+
.priority-box {
|
| 38 |
+
border: 2px solid #2E86AB;
|
| 39 |
+
padding: 20px;
|
| 40 |
+
border-radius: 10px;
|
| 41 |
+
margin: 10px 0;
|
| 42 |
+
}
|
| 43 |
+
</style>
|
| 44 |
+
""", unsafe_allow_html=True)
|
| 45 |
+
|
| 46 |
+
# Initialize session state
|
| 47 |
+
def init_session_state():
|
| 48 |
+
if 'language' not in st.session_state:
|
| 49 |
+
st.session_state.language = 'English'
|
| 50 |
+
if 'patient_data' not in st.session_state:
|
| 51 |
+
st.session_state.patient_data = {}
|
| 52 |
+
if 'risk_scores' not in st.session_state:
|
| 53 |
+
st.session_state.risk_scores = {}
|
| 54 |
+
|
| 55 |
+
# Load models with error handling
|
| 56 |
+
@st.cache_resource
|
| 57 |
+
def load_models():
|
| 58 |
+
try:
|
| 59 |
+
heart_model = joblib.load("best_model.pkl")
|
| 60 |
+
diabetes_model = joblib.load("best_diabetes_model.pkl")
|
| 61 |
+
hypertension_model = joblib.load("hypertension_model.pkl")
|
| 62 |
+
return heart_model, diabetes_model, hypertension_model
|
| 63 |
+
except Exception as e:
|
| 64 |
+
st.error(f"Error loading models: {str(e)}")
|
| 65 |
+
return None, None, None
|
| 66 |
+
|
| 67 |
+
# Urdu translations
|
| 68 |
+
URDU_TRANSLATIONS = {
|
| 69 |
+
"AI-Priority OPD System": "AI-ترجیحی OPD سسٹم",
|
| 70 |
+
"Patient Information": "مریض کی معلومات",
|
| 71 |
+
"Name": "نام",
|
| 72 |
+
"Age": "عمر",
|
| 73 |
+
"Gender": "جنس",
|
| 74 |
+
"Contact": "رابطہ نمبر",
|
| 75 |
+
"Medical History": "طبی تاریخ",
|
| 76 |
+
"Vital Signs": "اہم علامات",
|
| 77 |
+
"Blood Pressure (systolic)": "بلڈ پریشر (سسٹولک)",
|
| 78 |
+
"Blood Pressure (diastolic)": "بلڈ پریشر ڈائیسٹولک)",
|
| 79 |
+
"Heart Rate": "دل کی دھڑکن",
|
| 80 |
+
"Cholesterol Level": "کولیسٹرول کی سطح",
|
| 81 |
+
"Blood Glucose": "خون میں گلوکوز",
|
| 82 |
+
"BMI": "باڈی ماس انڈیکس",
|
| 83 |
+
"Symptoms": "علامات",
|
| 84 |
+
"Chest Pain": "سینے میں درد",
|
| 85 |
+
"Shortness of Breath": "سانس لینے میں دشواری",
|
| 86 |
+
"Fatigue": "تھکاوٹ",
|
| 87 |
+
"Upload Prescription": "نسخہ اپ لوڈ کریں",
|
| 88 |
+
"Calculate Risk Score": "خطرے کا اسکور معلوم کریں",
|
| 89 |
+
"High Priority - Emergency Care Required": "اعلی ترجیح - ہنگامی علاج کی ضرورت",
|
| 90 |
+
"Medium Priority - Same Day Consultation": "درمیانی ترجیح - اسی دن مشورہ",
|
| 91 |
+
"Low Priority - Routine Appointment": "کم ترجیح - معمول کی ملاقات",
|
| 92 |
+
"Healthcare Chatbot": "ہیلتھ کیئر چیٹ بوٹ",
|
| 93 |
+
"Ask health-related questions": "صحت سے متعلق سوالات پوچھیں"
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
class OCRProcessor:
|
| 97 |
+
def __init__(self):
|
| 98 |
+
pass
|
| 99 |
+
|
| 100 |
+
def preprocess_image(self, image):
|
| 101 |
+
"""Enhance image for better OCR accuracy"""
|
| 102 |
+
try:
|
| 103 |
+
# Convert to grayscale
|
| 104 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 105 |
+
|
| 106 |
+
# Apply thresholding
|
| 107 |
+
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 108 |
+
|
| 109 |
+
# Remove noise
|
| 110 |
+
kernel = np.ones((1, 1), np.uint8)
|
| 111 |
+
processed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
| 112 |
+
|
| 113 |
+
return processed
|
| 114 |
+
except Exception as e:
|
| 115 |
+
st.error(f"Image processing error: {str(e)}")
|
| 116 |
+
return np.array(image)
|
| 117 |
+
|
| 118 |
+
def extract_text(self, image):
|
| 119 |
+
"""Extract text from prescription image"""
|
| 120 |
+
try:
|
| 121 |
+
processed_image = self.preprocess_image(image)
|
| 122 |
+
|
| 123 |
+
# Configure Tesseract for medical text
|
| 124 |
+
custom_config = r'--oem 3 --psm 6 -l eng'
|
| 125 |
+
text = pytesseract.image_to_string(processed_image, config=custom_config)
|
| 126 |
+
|
| 127 |
+
return text.strip()
|
| 128 |
+
except Exception as e:
|
| 129 |
+
st.error(f"OCR Error: {str(e)}")
|
| 130 |
+
return ""
|
| 131 |
+
|
| 132 |
+
class HealthcareChatbot:
|
| 133 |
+
def __init__(self):
|
| 134 |
+
self.health_tips = {
|
| 135 |
+
'heart': [
|
| 136 |
+
"Maintain a healthy diet low in saturated fats",
|
| 137 |
+
"Exercise regularly for at least 30 minutes daily",
|
| 138 |
+
"Monitor blood pressure regularly",
|
| 139 |
+
"Avoid smoking and limit alcohol consumption"
|
| 140 |
+
],
|
| 141 |
+
'diabetes': [
|
| 142 |
+
"Monitor blood sugar levels regularly",
|
| 143 |
+
"Follow a balanced diet with controlled carbohydrates",
|
| 144 |
+
"Take medications as prescribed",
|
| 145 |
+
"Stay physically active"
|
| 146 |
+
],
|
| 147 |
+
'hypertension': [
|
| 148 |
+
"Reduce salt intake in your diet",
|
| 149 |
+
"Practice stress management techniques",
|
| 150 |
+
"Maintain healthy body weight",
|
| 151 |
+
"Limit caffeine consumption"
|
| 152 |
+
]
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
def get_response(self, query, language='English'):
|
| 156 |
+
"""Generate chatbot response"""
|
| 157 |
+
query_lower = query.lower()
|
| 158 |
+
|
| 159 |
+
# Simple rule-based responses
|
| 160 |
+
if any(word in query_lower for word in ['heart', 'cardiac', 'chest pain']):
|
| 161 |
+
tips = self.health_tips['heart']
|
| 162 |
+
elif any(word in query_lower for word in ['diabetes', 'sugar', 'glucose']):
|
| 163 |
+
tips = self.health_tips['diabetes']
|
| 164 |
+
elif any(word in query_lower for word in ['blood pressure', 'hypertension']):
|
| 165 |
+
tips = self.health_tips['hypertension']
|
| 166 |
+
else:
|
| 167 |
+
tips = ["Maintain regular health checkups", "Follow your doctor's advice", "Stay hydrated and eat balanced meals"]
|
| 168 |
+
|
| 169 |
+
response = f"Based on your query, here are some health tips:\n" + "\n".join([f"• {tip}" for tip in tips])
|
| 170 |
+
|
| 171 |
+
if language == 'Urdu':
|
| 172 |
+
# Simple Urdu translations (in a real app, use proper translation API)
|
| 173 |
+
response = "آپ کے سوال کے مطابق، یہاں کچھ صحت کے نکات ہیں:\n" + "\n".join([f"• {tip}" for tip in tips])
|
| 174 |
+
|
| 175 |
+
return response
|
| 176 |
+
|
| 177 |
+
def calculate_priority_score(heart_risk, diabetes_risk, hypertension_risk):
|
| 178 |
+
"""Calculate integrated priority score"""
|
| 179 |
+
# Weighted priority scoring based on clinical severity
|
| 180 |
+
priority = (
|
| 181 |
+
heart_risk * 0.4 +
|
| 182 |
+
diabetes_risk * 0.3 +
|
| 183 |
+
hypertension_risk * 0.3
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
return priority
|
| 187 |
+
|
| 188 |
+
def get_priority_recommendation(priority_score, language='English'):
|
| 189 |
+
"""Get priority-based recommendation"""
|
| 190 |
+
if priority_score > 0.8:
|
| 191 |
+
if language == 'Urdu':
|
| 192 |
+
return "EMERGENCY_CARE", "اعلی ترجیح - ہنگامی علاج کی ضرورت", "risk-high"
|
| 193 |
+
else:
|
| 194 |
+
return "EMERGENCY_CARE", "High Priority - Emergency Care Required", "risk-high"
|
| 195 |
+
elif priority_score > 0.6:
|
| 196 |
+
if language == 'Urdu':
|
| 197 |
+
return "SAME_DAY_CONSULT", "درمیانی ترجیح - اسی دن مشورہ", "risk-medium"
|
| 198 |
+
else:
|
| 199 |
+
return "SAME_DAY_CONSULT", "Medium Priority - Same Day Consultation", "risk-medium"
|
| 200 |
+
else:
|
| 201 |
+
if language == 'Urdu':
|
| 202 |
+
return "ROUTINE_APPOINTMENT", "کم ترجیح - معمول کی ملاقات", "risk-low"
|
| 203 |
+
else:
|
| 204 |
+
return "ROUTINE_APPOINTMENT", "Low Priority - Routine Appointment", "risk-low"
|
| 205 |
+
|
| 206 |
+
def main():
|
| 207 |
+
# Page configuration
|
| 208 |
+
st.set_page_config(
|
| 209 |
+
page_title="AI-Priority OPD System",
|
| 210 |
+
page_icon="🏥",
|
| 211 |
+
layout="wide",
|
| 212 |
+
initial_sidebar_state="expanded"
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Load custom CSS
|
| 216 |
+
local_css()
|
| 217 |
+
init_session_state()
|
| 218 |
+
|
| 219 |
+
# Load models
|
| 220 |
+
heart_model, diabetes_model, hypertension_model = load_models()
|
| 221 |
+
|
| 222 |
+
if heart_model is None or diabetes_model is None or hypertension_model is None:
|
| 223 |
+
st.error("Failed to load ML models. Please check model files.")
|
| 224 |
+
return
|
| 225 |
+
|
| 226 |
+
# Initialize processors
|
| 227 |
+
ocr_processor = OCRProcessor()
|
| 228 |
+
chatbot = HealthcareChatbot()
|
| 229 |
+
|
| 230 |
+
# Language selector in sidebar
|
| 231 |
+
with st.sidebar:
|
| 232 |
+
st.image("https://via.placeholder.com/150x50/2E86AB/FFFFFF?text=AI-OPD", use_column_width=True)
|
| 233 |
+
language = st.radio("Select Language / زبان منتخب کریں",
|
| 234 |
+
["English", "Urdu"],
|
| 235 |
+
key="language_selector")
|
| 236 |
+
|
| 237 |
+
st.markdown("---")
|
| 238 |
+
st.subheader("Quick Actions")
|
| 239 |
+
if st.button("New Patient Assessment" if language == "English" else "نیا مریض تشخیص"):
|
| 240 |
+
st.session_state.patient_data = {}
|
| 241 |
+
st.session_state.risk_scores = {}
|
| 242 |
+
|
| 243 |
+
# Main header
|
| 244 |
+
if language == "English":
|
| 245 |
+
st.markdown('<h1 class="main-header">🏥 AI-Priority OPD System</h1>', unsafe_allow_html=True)
|
| 246 |
+
st.markdown("### Smart Patient Triage and Priority Management")
|
| 247 |
+
else:
|
| 248 |
+
st.markdown('<h1 class="main-header">🏥 AI-ترجیحی OPD سسٹم</h1>', unsafe_allow_html=True)
|
| 249 |
+
st.markdown("### ذہین مریض کی درجہ بندی اور ترجیحی انتظام")
|
| 250 |
+
|
| 251 |
+
# Create tabs
|
| 252 |
+
tab1, tab2, tab3, tab4 = st.tabs([
|
| 253 |
+
"Patient Assessment" if language == "English" else "مریض تشخیص",
|
| 254 |
+
"Prescription OCR" if language == "English" else "نسخہ OCR",
|
| 255 |
+
"Health Chatbot" if language == "English" else "ہیلتھ چیٹ بوٹ",
|
| 256 |
+
"Analytics" if language == "English" else "تجزیات"
|
| 257 |
+
])
|
| 258 |
+
|
| 259 |
+
with tab1:
|
| 260 |
+
# Patient Assessment Form
|
| 261 |
+
if language == "English":
|
| 262 |
+
st.header("Patient Information")
|
| 263 |
+
else:
|
| 264 |
+
st.header("مریض کی معلومات")
|
| 265 |
+
|
| 266 |
+
col1, col2 = st.columns(2)
|
| 267 |
+
|
| 268 |
+
with col1:
|
| 269 |
+
# Basic Information
|
| 270 |
+
if language == "English":
|
| 271 |
+
name = st.text_input("Full Name")
|
| 272 |
+
age = st.number_input("Age", min_value=0, max_value=120, value=30)
|
| 273 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 274 |
+
contact = st.text_input("Contact Number")
|
| 275 |
+
else:
|
| 276 |
+
name = st.text_input("مکمل نام")
|
| 277 |
+
age = st.number_input("عمر", min_value=0, max_value=120, value=30)
|
| 278 |
+
gender = st.selectbox("جنس", ["مرد", "عورت", "دیگر"])
|
| 279 |
+
contact = st.text_input("رابطہ نمبر")
|
| 280 |
+
|
| 281 |
+
with col2:
|
| 282 |
+
# Vital Signs
|
| 283 |
+
if language == "English":
|
| 284 |
+
st.subheader("Vital Signs")
|
| 285 |
+
bp_systolic = st.number_input("Blood Pressure (systolic)", min_value=50, max_value=250, value=120)
|
| 286 |
+
bp_diastolic = st.number_input("Blood Pressure (diastolic)", min_value=30, max_value=150, value=80)
|
| 287 |
+
heart_rate = st.number_input("Heart Rate (bpm)", min_value=30, max_value=200, value=72)
|
| 288 |
+
cholesterol = st.number_input("Cholesterol Level (mg/dL)", min_value=100, max_value=400, value=200)
|
| 289 |
+
glucose = st.number_input("Blood Glucose (mg/dL)", min_value=50, max_value=500, value=100)
|
| 290 |
+
bmi = st.number_input("BMI", min_value=10.0, max_value=50.0, value=22.0, step=0.1)
|
| 291 |
+
else:
|
| 292 |
+
st.subheader("اہم علامات")
|
| 293 |
+
bp_systolic = st.number_input("بلڈ پریشر (سسٹولک)", min_value=50, max_value=250, value=120)
|
| 294 |
+
bp_diastolic = st.number_input("بلڈ پریشر (ڈائیسٹولک)", min_value=30, max_value=150, value=80)
|
| 295 |
+
heart_rate = st.number_input("دل کی دھڑکن (bpm)", min_value=30, max_value=200, value=72)
|
| 296 |
+
cholesterol = st.number_input("کولیسٹرول کی سطح (mg/dL)", min_value=100, max_value=400, value=200)
|
| 297 |
+
glucose = st.number_input("خون میں گلوکوز (mg/dL)", min_value=50, max_value=500, value=100)
|
| 298 |
+
bmi = st.number_input("باڈی ماس انڈیکس", min_value=10.0, max_value=50.0, value=22.0, step=0.1)
|
| 299 |
+
|
| 300 |
+
# Symptoms
|
| 301 |
+
if language == "English":
|
| 302 |
+
st.subheader("Symptoms")
|
| 303 |
+
col3, col4 = st.columns(2)
|
| 304 |
+
with col3:
|
| 305 |
+
chest_pain = st.checkbox("Chest Pain")
|
| 306 |
+
shortness_breath = st.checkbox("Shortness of Breath")
|
| 307 |
+
with col4:
|
| 308 |
+
fatigue = st.checkbox("Fatigue")
|
| 309 |
+
dizziness = st.checkbox("Dizziness")
|
| 310 |
+
else:
|
| 311 |
+
st.subheader("علامات")
|
| 312 |
+
col3, col4 = st.columns(2)
|
| 313 |
+
with col3:
|
| 314 |
+
chest_pain = st.checkbox("سینے میں درد")
|
| 315 |
+
shortness_breath = st.checkbox("سانس لینے میں دشواری")
|
| 316 |
+
with col4:
|
| 317 |
+
fatigue = st.checkbox("تھکاوٹ")
|
| 318 |
+
dizziness = st.checkbox("چکر آنا")
|
| 319 |
+
|
| 320 |
+
# Risk Assessment Button
|
| 321 |
+
if language == "English":
|
| 322 |
+
assess_button = st.button("🚀 Calculate Risk Score & Priority", use_container_width=True)
|
| 323 |
+
else:
|
| 324 |
+
assess_button = st.button("🚀 خطرے کا اسکور اور ترجیح معلوم کریں", use_container_width=True)
|
| 325 |
+
|
| 326 |
+
if assess_button:
|
| 327 |
+
try:
|
| 328 |
+
# Prepare feature arrays (adjust based on your model requirements)
|
| 329 |
+
heart_features = np.array([[age, bp_systolic, cholesterol, heart_rate, 1 if chest_pain else 0]])
|
| 330 |
+
diabetes_features = np.array([[age, glucose, bmi, cholesterol]])
|
| 331 |
+
hypertension_features = np.array([[age, bp_systolic, bp_diastolic, bmi]])
|
| 332 |
+
|
| 333 |
+
# Get predictions
|
| 334 |
+
heart_risk = heart_model.predict_proba(heart_features)[0][1]
|
| 335 |
+
diabetes_risk = diabetes_model.predict_proba(diabetes_features)[0][1]
|
| 336 |
+
hypertension_risk = hypertension_model.predict_proba(hypertension_features)[0][1]
|
| 337 |
+
|
| 338 |
+
# Calculate priority score
|
| 339 |
+
priority_score = calculate_priority_score(heart_risk, diabetes_risk, hypertension_risk)
|
| 340 |
+
priority_level, recommendation, risk_class = get_priority_recommendation(priority_score, language)
|
| 341 |
+
|
| 342 |
+
# Store results
|
| 343 |
+
st.session_state.risk_scores = {
|
| 344 |
+
'heart': heart_risk,
|
| 345 |
+
'diabetes': diabetes_risk,
|
| 346 |
+
'hypertension': hypertension_risk,
|
| 347 |
+
'priority': priority_score,
|
| 348 |
+
'recommendation': recommendation,
|
| 349 |
+
'level': priority_level
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
# Display results
|
| 353 |
+
st.markdown("---")
|
| 354 |
+
|
| 355 |
+
# Risk Scores Visualization
|
| 356 |
+
col5, col6, col7, col8 = st.columns(4)
|
| 357 |
+
|
| 358 |
+
with col5:
|
| 359 |
+
fig = go.Figure(go.Indicator(
|
| 360 |
+
mode = "gauge+number+delta",
|
| 361 |
+
value = heart_risk,
|
| 362 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 363 |
+
title = {'text': "Heart Disease Risk"},
|
| 364 |
+
gauge = {'axis': {'range': [0, 1]},
|
| 365 |
+
'bar': {'color': "red"},
|
| 366 |
+
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 367 |
+
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 368 |
+
{'range': [0.7, 1], 'color': "red"}]}))
|
| 369 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 370 |
+
|
| 371 |
+
with col6:
|
| 372 |
+
fig = go.Figure(go.Indicator(
|
| 373 |
+
mode = "gauge+number+delta",
|
| 374 |
+
value = diabetes_risk,
|
| 375 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 376 |
+
title = {'text': "Diabetes Risk"},
|
| 377 |
+
gauge = {'axis': {'range': [0, 1]},
|
| 378 |
+
'bar': {'color': "orange"},
|
| 379 |
+
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 380 |
+
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 381 |
+
{'range': [0.7, 1], 'color': "red"}]}))
|
| 382 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 383 |
+
|
| 384 |
+
with col7:
|
| 385 |
+
fig = go.Figure(go.Indicator(
|
| 386 |
+
mode = "gauge+number+delta",
|
| 387 |
+
value = hypertension_risk,
|
| 388 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 389 |
+
title = {'text': "Hypertension Risk"},
|
| 390 |
+
gauge = {'axis': {'range': [0, 1]},
|
| 391 |
+
'bar': {'color': "blue"},
|
| 392 |
+
'steps': [{'range': [0, 0.3], 'color': "lightgreen"},
|
| 393 |
+
{'range': [0.3, 0.7], 'color': "yellow"},
|
| 394 |
+
{'range': [0.7, 1], 'color': "red"}]}))
|
| 395 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 396 |
+
|
| 397 |
+
with col8:
|
| 398 |
+
fig = go.Figure(go.Indicator(
|
| 399 |
+
mode = "gauge+number+delta",
|
| 400 |
+
value = priority_score,
|
| 401 |
+
domain = {'x': [0, 1], 'y': [0, 1]},
|
| 402 |
+
title = {'text': "Priority Score"},
|
| 403 |
+
gauge = {'axis': {'range': [0, 1]},
|
| 404 |
+
'bar': {'color': "purple"},
|
| 405 |
+
'steps': [{'range': [0, 0.6], 'color': "lightgreen"},
|
| 406 |
+
{'range': [0.6, 0.8], 'color': "yellow"},
|
| 407 |
+
{'range': [0.8, 1], 'color': "red"}]}))
|
| 408 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 409 |
+
|
| 410 |
+
# Priority Recommendation
|
| 411 |
+
st.markdown(f'<div class="priority-box {risk_class}">', unsafe_allow_html=True)
|
| 412 |
+
if language == "English":
|
| 413 |
+
st.markdown(f"## 🎯 Priority Recommendation: {recommendation}")
|
| 414 |
+
st.markdown(f"**Overall Risk Score:** {priority_score:.3f}")
|
| 415 |
+
st.markdown(f"**Recommended Action:** {priority_level.replace('_', ' ').title()}")
|
| 416 |
+
else:
|
| 417 |
+
st.markdown(f"## 🎯 ترجیحی سفارش: {recommendation}")
|
| 418 |
+
st.markdown(f"**کل خطرے کا اسکور:** {priority_score:.3f}")
|
| 419 |
+
st.markdown(f"**سفارش کردہ عمل:** {priority_level.replace('_', ' ').title()}")
|
| 420 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 421 |
+
|
| 422 |
+
except Exception as e:
|
| 423 |
+
st.error(f"Error in risk assessment: {str(e)}")
|
| 424 |
+
|
| 425 |
+
with tab2:
|
| 426 |
+
# Prescription OCR
|
| 427 |
+
if language == "English":
|
| 428 |
+
st.header("Prescription OCR Analysis")
|
| 429 |
+
st.write("Upload a prescription image to extract medication information")
|
| 430 |
+
else:
|
| 431 |
+
st.header("نسخہ OCR تجزیہ")
|
| 432 |
+
st.write("دوائی کی معلومات نکالنے کے لیے نسخہ کی تصویر اپ لوڈ کریں")
|
| 433 |
+
|
| 434 |
+
uploaded_file = st.file_uploader(
|
| 435 |
+
"Upload Prescription Image" if language == "English" else "نسخہ تصویر اپ لوڈ کریں",
|
| 436 |
+
type=['png', 'jpg', 'jpeg']
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
if uploaded_file is not None:
|
| 440 |
+
image = Image.open(uploaded_file)
|
| 441 |
+
st.image(image, caption="Uploaded Prescription", use_column_width=True)
|
| 442 |
+
|
| 443 |
+
if st.button("Extract Text" if language == "English" else "متن نکالیں"):
|
| 444 |
+
with st.spinner("Processing prescription..." if language == "English" else "نسخہ پروسیس ہو رہا ہے..."):
|
| 445 |
+
extracted_text = ocr_processor.extract_text(image)
|
| 446 |
+
|
| 447 |
+
if extracted_text:
|
| 448 |
+
if language == "English":
|
| 449 |
+
st.success("✅ Text extracted successfully!")
|
| 450 |
+
st.subheader("Extracted Text:")
|
| 451 |
+
else:
|
| 452 |
+
st.success("✅ متن کامیابی سے نکال لیا گیا!")
|
| 453 |
+
st.subheader("نکالا گیا متن:")
|
| 454 |
+
|
| 455 |
+
st.text_area("", extracted_text, height=200)
|
| 456 |
+
|
| 457 |
+
# Simple accuracy estimation (in real scenario, use validation dataset)
|
| 458 |
+
word_count = len(extracted_text.split())
|
| 459 |
+
non_empty_chars = len([c for c in extracted_text if c.strip()])
|
| 460 |
+
|
| 461 |
+
if word_count > 5 and non_empty_chars > 20:
|
| 462 |
+
estimated_accuracy = min(85, (word_count / max(1, word_count)) * 100)
|
| 463 |
+
st.metric("Estimated OCR Accuracy", f"{estimated_accuracy:.1f}%")
|
| 464 |
+
else:
|
| 465 |
+
st.warning("Low confidence in OCR extraction")
|
| 466 |
+
|
| 467 |
+
else:
|
| 468 |
+
if language == "English":
|
| 469 |
+
st.error("No text could be extracted from the image")
|
| 470 |
+
else:
|
| 471 |
+
st.error("تصویر سے کوئی متن نہیں نکالا جا سکا")
|
| 472 |
+
|
| 473 |
+
with tab3:
|
| 474 |
+
# Healthcare Chatbot
|
| 475 |
+
if language == "English":
|
| 476 |
+
st.header("Healthcare Assistant Chatbot")
|
| 477 |
+
st.write("Ask health-related questions and get personalized advice")
|
| 478 |
+
else:
|
| 479 |
+
st.header("ہیلتھ کیئر اسسٹنٹ چیٹ بوٹ")
|
| 480 |
+
st.write("صحت سے متعلق سوالات پوچھیں اور ذاتی مشورہ حاصل کریں")
|
| 481 |
+
|
| 482 |
+
# Initialize chat history
|
| 483 |
+
if 'chat_history' not in st.session_state:
|
| 484 |
+
st.session_state.chat_history = []
|
| 485 |
+
|
| 486 |
+
# Display chat history
|
| 487 |
+
for message in st.session_state.chat_history:
|
| 488 |
+
with st.chat_message(message["role"]):
|
| 489 |
+
st.markdown(message["content"])
|
| 490 |
+
|
| 491 |
+
# Chat input
|
| 492 |
+
if prompt := st.chat_input("Type your health question here..." if language == "English" else "اپنا صحت کا سوال یہاں ٹائپ کریں..."):
|
| 493 |
+
# Add user message to chat history
|
| 494 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 495 |
+
with st.chat_message("user"):
|
| 496 |
+
st.markdown(prompt)
|
| 497 |
+
|
| 498 |
+
# Generate bot response
|
| 499 |
+
with st.chat_message("assistant"):
|
| 500 |
+
with st.spinner("Thinking..." if language == "English" else "سوچ رہا ہوں..."):
|
| 501 |
+
response = chatbot.get_response(prompt, language)
|
| 502 |
+
st.markdown(response)
|
| 503 |
+
|
| 504 |
+
# Add assistant response to chat history
|
| 505 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 506 |
+
|
| 507 |
+
with tab4:
|
| 508 |
+
# Analytics Dashboard
|
| 509 |
+
if language == "English":
|
| 510 |
+
st.header("System Analytics & Performance")
|
| 511 |
+
else:
|
| 512 |
+
st.header("سسٹم تجزیات اور کارکردگی")
|
| 513 |
+
|
| 514 |
+
col9, col10, col11 = st.columns(3)
|
| 515 |
+
|
| 516 |
+
with col9:
|
| 517 |
+
# Mock accuracy metrics (replace with actual validation)
|
| 518 |
+
st.metric("Diagnostic Accuracy", "87%", "2%")
|
| 519 |
+
with col10:
|
| 520 |
+
st.metric("OCR Accuracy", "82%", "3%")
|
| 521 |
+
with col11:
|
| 522 |
+
st.metric("Risk Scoring AUC", "0.85", "0.02")
|
| 523 |
+
|
| 524 |
+
# Performance charts
|
| 525 |
+
if language == "English":
|
| 526 |
+
st.subheader("Priority Distribution")
|
| 527 |
+
else:
|
| 528 |
+
st.subheader("ترجیحی تقسیم")
|
| 529 |
+
|
| 530 |
+
# Mock data for demonstration
|
| 531 |
+
priority_data = pd.DataFrame({
|
| 532 |
+
'Priority': ['Emergency', 'Same Day', 'Routine'],
|
| 533 |
+
'Count': [25, 45, 30]
|
| 534 |
+
})
|
| 535 |
+
|
| 536 |
+
fig = px.pie(priority_data, values='Count', names='Priority',
|
| 537 |
+
title="Patient Priority Distribution")
|
| 538 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 539 |
+
|
| 540 |
+
# Model performance
|
| 541 |
+
if language == "English":
|
| 542 |
+
st.subheader("Model Performance Metrics")
|
| 543 |
+
else:
|
| 544 |
+
st.subheader("ماڈل کارکردگی کے پیمانے")
|
| 545 |
+
|
| 546 |
+
performance_data = pd.DataFrame({
|
| 547 |
+
'Model': ['Heart Disease', 'Diabetes', 'Hypertension'],
|
| 548 |
+
'Accuracy': [0.88, 0.85, 0.86],
|
| 549 |
+
'AUC': [0.89, 0.84, 0.87]
|
| 550 |
+
})
|
| 551 |
+
|
| 552 |
+
fig = px.bar(performance_data, x='Model', y=['Accuracy', 'AUC'],
|
| 553 |
+
title="Model Performance Comparison", barmode='group')
|
| 554 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 555 |
+
|
| 556 |
+
if __name__ == "__main__":
|
| 557 |
main()
|