Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import Sequential
|
| 6 |
+
from tensorflow.keras.layers import Dense, LeakyReLU, Input
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 9 |
+
from sklearn.utils.class_weight import compute_class_weight
|
| 10 |
+
from imblearn.over_sampling import RandomOverSampler
|
| 11 |
+
import warnings
|
| 12 |
+
warnings.filterwarnings("ignore")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def train_with_random_state(random_state):
|
| 16 |
+
# --- Load dataset ---
|
| 17 |
+
df = pd.read_csv("updated_medical_data.csv")
|
| 18 |
+
|
| 19 |
+
# --- Disease-specific expected vital sign ranges ---
|
| 20 |
+
disease_vitals = {
|
| 21 |
+
"Macular Degeneration": {"hr": (60, 80), "rr": (12, 20), "temp": (97.5, 98.6)},
|
| 22 |
+
# ... [rest omitted for brevity] ...
|
| 23 |
+
"Pancreatitis": {"hr": (90, 110), "rr": (16, 24), "temp": (99, 102)}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# --- Feature engineering ---
|
| 27 |
+
def create_vital_flags(row):
|
| 28 |
+
disease = row['disease_label']
|
| 29 |
+
if disease in disease_vitals:
|
| 30 |
+
hr_low, hr_high = disease_vitals[disease]['hr']
|
| 31 |
+
rr_low, rr_high = disease_vitals[disease]['rr']
|
| 32 |
+
temp_low, temp_high = disease_vitals[disease]['temp']
|
| 33 |
+
else:
|
| 34 |
+
hr_low, hr_high = (60, 100)
|
| 35 |
+
rr_low, rr_high = (12, 20)
|
| 36 |
+
temp_low, temp_high = (97.5, 99)
|
| 37 |
+
return pd.Series({
|
| 38 |
+
'hr_high_flag': int(row['heart_rate'] > hr_high),
|
| 39 |
+
'hr_low_flag': int(row['heart_rate'] < hr_low),
|
| 40 |
+
'rr_high_flag': int(row['respiratory_rate'] > rr_high),
|
| 41 |
+
'rr_low_flag': int(row['respiratory_rate'] < rr_low),
|
| 42 |
+
'temp_high_flag': int(row['temperature'] > temp_high),
|
| 43 |
+
'temp_low_flag': int(row['temperature'] < temp_low),
|
| 44 |
+
'fever_flag': int(row['temperature'] >= 100.4)
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
flags_df = df.apply(create_vital_flags, axis=1)
|
| 48 |
+
df = pd.concat([df, flags_df], axis=1)
|
| 49 |
+
|
| 50 |
+
# --- Prepare features and labels ---
|
| 51 |
+
feature_cols = ['heart_rate', 'respiratory_rate', 'temperature', 'symptom_score',
|
| 52 |
+
'hr_high_flag', 'hr_low_flag', 'rr_high_flag', 'rr_low_flag',
|
| 53 |
+
'temp_high_flag', 'temp_low_flag', 'fever_flag']
|
| 54 |
+
|
| 55 |
+
X = df[feature_cols].values
|
| 56 |
+
encoder = LabelEncoder()
|
| 57 |
+
y = encoder.fit_transform(df['disease_label'])
|
| 58 |
+
|
| 59 |
+
# --- Balance the dataset ---
|
| 60 |
+
ros = RandomOverSampler(random_state=random_state)
|
| 61 |
+
X_resampled, y_resampled = ros.fit_resample(X, y)
|
| 62 |
+
|
| 63 |
+
# --- Scale features ---
|
| 64 |
+
scaler = StandardScaler()
|
| 65 |
+
X_scaled = scaler.fit_transform(X_resampled)
|
| 66 |
+
|
| 67 |
+
# --- Split data ---
|
| 68 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 69 |
+
X_scaled, y_resampled, test_size=0.2, random_state=random_state)
|
| 70 |
+
|
| 71 |
+
# --- Class weights ---
|
| 72 |
+
class_weights = compute_class_weight(class_weight='balanced', classes=np.unique(y_train), y=y_train)
|
| 73 |
+
class_weights = dict(enumerate(class_weights))
|
| 74 |
+
|
| 75 |
+
# --- Build model ---
|
| 76 |
+
model = Sequential([
|
| 77 |
+
Input(shape=(X_train.shape[1],)),
|
| 78 |
+
Dense(128),
|
| 79 |
+
LeakyReLU(),
|
| 80 |
+
Dense(64),
|
| 81 |
+
LeakyReLU(),
|
| 82 |
+
Dense(len(np.unique(y_train)), activation='softmax')
|
| 83 |
+
])
|
| 84 |
+
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
| 85 |
+
|
| 86 |
+
# --- Train model ---
|
| 87 |
+
model.fit(X_train, y_train, epochs=10, batch_size=32, class_weight=class_weights, verbose=0)
|
| 88 |
+
|
| 89 |
+
# --- Evaluate model ---
|
| 90 |
+
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
|
| 91 |
+
return f"🎯 Accuracy with random_state={random_state}: {accuracy:.2%}"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Define Gradio interface outside the function:
|
| 95 |
+
app = gr.Interface(
|
| 96 |
+
fn=train_with_random_state,
|
| 97 |
+
inputs=gr.Number(label="Random State Number", value=42, precision=0),
|
| 98 |
+
outputs=gr.Textbox(label="Model Accuracy"),
|
| 99 |
+
title="Disease Classification Trainer",
|
| 100 |
+
description="Enter a random state seed to train and evaluate the model."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
app.launch()
|