Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,35 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"Pancreatitis": {"hr": (90, 110), "rr": (16, 24), "temp": (99, 102)}
|
| 24 |
}
|
| 25 |
|
|
@@ -88,16 +105,55 @@ def train_with_random_state(random_state):
|
|
| 88 |
|
| 89 |
# --- Evaluate model ---
|
| 90 |
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
|
| 91 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
-
#
|
| 95 |
app = gr.Interface(
|
| 96 |
-
fn=
|
| 97 |
-
inputs=gr.Number(label="Random State
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
|
|
|
| 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 matplotlib.pyplot as plt
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings("ignore")
|
| 14 |
|
| 15 |
+
def train_and_evaluate(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 |
+
"Encephalitis": {"hr": (90, 110), "rr": (18, 24), "temp": (100.4, 103)},
|
| 23 |
+
"Prostate Cancer": {"hr": (60, 80), "rr": (12, 18), "temp": (97.5, 99)},
|
| 24 |
+
"Malaria": {"hr": (90, 120), "rr": (20, 30), "temp": (100.4, 104)},
|
| 25 |
+
"Lymphoma": {"hr": (80, 100), "rr": (16, 22), "temp": (98.6, 100.4)},
|
| 26 |
+
"Pneumonia": {"hr": (90, 110), "rr": (20, 30), "temp": (100.4, 103)},
|
| 27 |
+
"Tuberculosis": {"hr": (80, 100), "rr": (20, 28), "temp": (99, 101)},
|
| 28 |
+
"Asthma": {"hr": (80, 100), "rr": (18, 30), "temp": (97.5, 99)},
|
| 29 |
+
"Chronic Bronchitis": {"hr": (70, 90), "rr": (16, 24), "temp": (97.5, 99.5)},
|
| 30 |
+
"COPD": {"hr": (80, 100), "rr": (18, 28), "temp": (97.5, 99.5)},
|
| 31 |
+
"Rubella": {"hr": (70, 90), "rr": (14, 20), "temp": (99, 101)},
|
| 32 |
+
"Multiple Sclerosis": {"hr": (60, 80), "rr": (12, 18), "temp": (97.5, 99)},
|
| 33 |
+
"Plague": {"hr": (100, 130), "rr": (20, 30), "temp": (101, 104)},
|
| 34 |
+
"Ebola": {"hr": (110, 140), "rr": (22, 32), "temp": (101, 105)},
|
| 35 |
+
"Rheumatoid Arthritis": {"hr": (70, 90), "rr": (14, 20), "temp": (98, 100)},
|
| 36 |
+
"Obesity": {"hr": (70, 90), "rr": (14, 20), "temp": (97.5, 99)},
|
| 37 |
+
"Metabolic Syndrome": {"hr": (70, 90), "rr": (14, 20), "temp": (97.5, 99)},
|
| 38 |
+
"Cataracts": {"hr": (60, 80), "rr": (12, 18), "temp": (97.5, 98.6)},
|
| 39 |
+
"Tonsillitis": {"hr": (80, 100), "rr": (16, 24), "temp": (99, 101)},
|
| 40 |
"Pancreatitis": {"hr": (90, 110), "rr": (16, 24), "temp": (99, 102)}
|
| 41 |
}
|
| 42 |
|
|
|
|
| 105 |
|
| 106 |
# --- Evaluate model ---
|
| 107 |
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
|
| 108 |
+
return accuracy
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def find_best_random_state_with_plot(range_pair):
|
| 112 |
+
min_rs, max_rs = int(range_pair[0]), int(range_pair[1])
|
| 113 |
+
|
| 114 |
+
if min_rs >= max_rs:
|
| 115 |
+
return "Invalid range: min should be less than max.", None
|
| 116 |
+
|
| 117 |
+
best_accuracy = 0
|
| 118 |
+
best_rs = min_rs
|
| 119 |
+
accuracies = []
|
| 120 |
+
|
| 121 |
+
for rs in range(min_rs, max_rs + 1):
|
| 122 |
+
acc = train_and_evaluate(rs)
|
| 123 |
+
accuracies.append((rs, acc))
|
| 124 |
+
if acc > best_accuracy:
|
| 125 |
+
best_accuracy = acc
|
| 126 |
+
best_rs = rs
|
| 127 |
+
|
| 128 |
+
# Create a matplotlib figure
|
| 129 |
+
plt.figure(figsize=(10, 6))
|
| 130 |
+
rs_values = [x[0] for x in accuracies]
|
| 131 |
+
acc_values = [x[1] for x in accuracies]
|
| 132 |
+
plt.plot(rs_values, acc_values, marker='o')
|
| 133 |
+
plt.title('Model Accuracy vs Random State')
|
| 134 |
+
plt.xlabel('Random State')
|
| 135 |
+
plt.ylabel('Accuracy')
|
| 136 |
+
plt.grid(True)
|
| 137 |
+
plt.tight_layout()
|
| 138 |
+
|
| 139 |
+
# Build report string
|
| 140 |
+
report = f"Best random_state: {best_rs} with accuracy: {best_accuracy:.2%}\n\nAll accuracies:\n"
|
| 141 |
+
for rs, acc in accuracies:
|
| 142 |
+
report += f"random_state={rs}: Accuracy={acc:.2%}\n"
|
| 143 |
+
|
| 144 |
+
return report, plt
|
| 145 |
|
| 146 |
|
| 147 |
+
# Gradio Interface with text and plot output
|
| 148 |
app = gr.Interface(
|
| 149 |
+
fn=find_best_random_state_with_plot,
|
| 150 |
+
inputs=gr.inputs.Tuple([gr.Number(label="Min Random State", value=50, precision=0),
|
| 151 |
+
gr.Number(label="Max Random State", value=60, precision=0)]),
|
| 152 |
+
outputs=[gr.Textbox(label="Search Results"), gr.Plot(label="Accuracy vs Random State")],
|
| 153 |
+
title="Find Best Random State for Model Training with Accuracy Plot",
|
| 154 |
+
description="Input a range (min, max) of random_state values (integers). The model will train and evaluate for each random_state, returning the best one and a plot of accuracy vs random_state."
|
| 155 |
+
|
| 156 |
+
theme="soft"
|
| 157 |
)
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|