Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -76,27 +76,10 @@ def load_data():
|
|
76 |
|
77 |
# Replace prognosis values with numerical categories
|
78 |
df.replace({'prognosis': disease_dict}, inplace=True)
|
|
|
79 |
|
80 |
-
# Check unique values in prognosis for debugging
|
81 |
-
print("Unique values in prognosis after mapping:", df['prognosis'].unique())
|
82 |
-
|
83 |
-
# Ensure prognosis is purely numerical after mapping
|
84 |
-
if df['prognosis'].dtype == 'object': # Check for unmapped entries
|
85 |
-
raise ValueError(f"The prognosis contains unmapped values: {df['prognosis'].unique()}")
|
86 |
-
|
87 |
-
df['prognosis'] = df['prognosis'].astype(int) # Convert to integer
|
88 |
-
|
89 |
-
df = df.infer_objects() # Remove 'copy' argument
|
90 |
-
|
91 |
-
# Similar process for the testing data
|
92 |
tr.replace({'prognosis': disease_dict}, inplace=True)
|
93 |
-
|
94 |
-
# Ensure it is also numerical
|
95 |
-
if tr['prognosis'].dtype == 'object':
|
96 |
-
raise ValueError(f"Testing data prognosis contains unmapped values: {tr['prognosis'].unique()}")
|
97 |
-
|
98 |
-
tr['prognosis'] = tr['prognosis'].astype(int) # Convert to integer
|
99 |
-
tr = tr.infer_objects() # Remove 'copy' argument
|
100 |
|
101 |
return df, tr, disease_dict
|
102 |
|
@@ -109,7 +92,7 @@ y_test = tr['prognosis']
|
|
109 |
|
110 |
# Encode the target variable with LabelEncoder if still in string format
|
111 |
le = LabelEncoder()
|
112 |
-
y_encoded = le.fit_transform(y)
|
113 |
|
114 |
def train_models():
|
115 |
models = {
|
@@ -119,7 +102,7 @@ def train_models():
|
|
119 |
}
|
120 |
trained_models = {}
|
121 |
for model_name, model_obj in models.items():
|
122 |
-
model_obj.fit(X, y_encoded)
|
123 |
acc = accuracy_score(y_test, model_obj.predict(X_test))
|
124 |
trained_models[model_name] = (model_obj, acc)
|
125 |
return trained_models
|
@@ -138,27 +121,6 @@ def predict_disease(model, symptoms):
|
|
138 |
"confidence": confidence
|
139 |
}
|
140 |
|
141 |
-
def disease_prediction_interface(symptoms):
|
142 |
-
symptoms_selected = [s for s in symptoms if s != "None"]
|
143 |
-
|
144 |
-
if len(symptoms_selected) < 3:
|
145 |
-
return ["Please select at least 3 symptoms for accurate prediction."]
|
146 |
-
|
147 |
-
results = []
|
148 |
-
for model_name, (model, acc) in trained_models.items():
|
149 |
-
prediction_info = predict_disease(model, symptoms_selected)
|
150 |
-
predicted_disease = prediction_info["disease"]
|
151 |
-
confidence_score = prediction_info["confidence"]
|
152 |
-
|
153 |
-
result = f"{model_name} Prediction: Predicted Disease: **{predicted_disease}**"
|
154 |
-
if confidence_score is not None:
|
155 |
-
result += f" (Confidence: {confidence_score:.2f})"
|
156 |
-
result += f" (Accuracy: {acc * 100:.2f}%)"
|
157 |
-
|
158 |
-
results.append(result)
|
159 |
-
|
160 |
-
return results
|
161 |
-
|
162 |
# Helper Functions (for chatbot)
|
163 |
def bag_of_words(s, words):
|
164 |
bag = [0] * len(words)
|
@@ -242,8 +204,26 @@ def app_function(user_input, location, query, symptoms, history):
|
|
242 |
emotion_result, cleaned_emotion = detect_emotion(user_input)
|
243 |
suggestions = generate_suggestions(cleaned_emotion)
|
244 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
245 |
-
disease_results = disease_prediction_interface(symptoms)
|
246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
return (
|
248 |
chatbot_history,
|
249 |
sentiment_result,
|
@@ -256,65 +236,7 @@ def app_function(user_input, location, query, symptoms, history):
|
|
256 |
|
257 |
# CSS Styling
|
258 |
custom_css = """
|
259 |
-
|
260 |
-
font-family: 'Roboto', sans-serif;
|
261 |
-
background-color: #3c6487;
|
262 |
-
color: white;
|
263 |
-
}
|
264 |
-
h1 {
|
265 |
-
background: #ffffff;
|
266 |
-
color: #000000;
|
267 |
-
border-radius: 8px;
|
268 |
-
padding: 10px;
|
269 |
-
font-weight: bold;
|
270 |
-
text-align: center;
|
271 |
-
font-size: 2.5rem;
|
272 |
-
}
|
273 |
-
textarea, input {
|
274 |
-
background: transparent;
|
275 |
-
color: black;
|
276 |
-
border: 2px solid orange;
|
277 |
-
padding: 8px;
|
278 |
-
font-size: 1rem;
|
279 |
-
caret-color: black;
|
280 |
-
outline: none;
|
281 |
-
border-radius: 8px;
|
282 |
-
}
|
283 |
-
textarea:focus, input:focus {
|
284 |
-
background: transparent;
|
285 |
-
color: black;
|
286 |
-
border: 2px solid orange;
|
287 |
-
outline: none;
|
288 |
-
}
|
289 |
-
.df-container {
|
290 |
-
background: white;
|
291 |
-
color: black;
|
292 |
-
border: 2px solid orange;
|
293 |
-
border-radius: 10px;
|
294 |
-
padding: 10px;
|
295 |
-
font-size: 14px;
|
296 |
-
max-height: 400px;
|
297 |
-
height: auto;
|
298 |
-
overflow-y: auto;
|
299 |
-
}
|
300 |
-
#suggestions-title {
|
301 |
-
text-align: center !important;
|
302 |
-
font-weight: bold !important;
|
303 |
-
color: white !important;
|
304 |
-
font-size: 4.2rem !important;
|
305 |
-
margin-bottom: 20px !important;
|
306 |
-
}
|
307 |
-
.gr-button {
|
308 |
-
background-color: #ae1c93;
|
309 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.06);
|
310 |
-
transition: background-color 0.3s ease;
|
311 |
-
}
|
312 |
-
.gr-button:hover {
|
313 |
-
background-color: #8f167b;
|
314 |
-
}
|
315 |
-
.gr-button:active {
|
316 |
-
background-color: #7f156b;
|
317 |
-
}
|
318 |
"""
|
319 |
|
320 |
# Gradio Application
|
@@ -352,4 +274,5 @@ with gr.Blocks(css=custom_css) as app:
|
|
352 |
outputs=[chatbot, sentiment, emotion, suggestions, professionals, map_html, disease_predictions],
|
353 |
)
|
354 |
|
|
|
355 |
app.launch()
|
|
|
76 |
|
77 |
# Replace prognosis values with numerical categories
|
78 |
df.replace({'prognosis': disease_dict}, inplace=True)
|
79 |
+
df['prognosis'] = df['prognosis'].astype(int)
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
tr.replace({'prognosis': disease_dict}, inplace=True)
|
82 |
+
tr['prognosis'] = tr['prognosis'].astype(int)
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
return df, tr, disease_dict
|
85 |
|
|
|
92 |
|
93 |
# Encode the target variable with LabelEncoder if still in string format
|
94 |
le = LabelEncoder()
|
95 |
+
y_encoded = le.fit_transform(y)
|
96 |
|
97 |
def train_models():
|
98 |
models = {
|
|
|
102 |
}
|
103 |
trained_models = {}
|
104 |
for model_name, model_obj in models.items():
|
105 |
+
model_obj.fit(X, y_encoded)
|
106 |
acc = accuracy_score(y_test, model_obj.predict(X_test))
|
107 |
trained_models[model_name] = (model_obj, acc)
|
108 |
return trained_models
|
|
|
121 |
"confidence": confidence
|
122 |
}
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
# Helper Functions (for chatbot)
|
125 |
def bag_of_words(s, words):
|
126 |
bag = [0] * len(words)
|
|
|
204 |
emotion_result, cleaned_emotion = detect_emotion(user_input)
|
205 |
suggestions = generate_suggestions(cleaned_emotion)
|
206 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
|
|
207 |
|
208 |
+
# Disease prediction logic
|
209 |
+
symptoms_selected = [s for s in symptoms if s != "None"]
|
210 |
+
if len(symptoms_selected) < 3:
|
211 |
+
disease_results = ["Please select at least 3 symptoms for accurate prediction."]
|
212 |
+
else:
|
213 |
+
results = []
|
214 |
+
for model_name, (model, acc) in trained_models.items():
|
215 |
+
prediction_info = predict_disease(model, symptoms_selected)
|
216 |
+
predicted_disease = prediction_info["disease"]
|
217 |
+
confidence_score = prediction_info["confidence"]
|
218 |
+
|
219 |
+
result = f"{model_name} Prediction: Predicted Disease: **{predicted_disease}**"
|
220 |
+
if confidence_score is not None:
|
221 |
+
result += f" (Confidence: {confidence_score:.2f})"
|
222 |
+
result += f" (Accuracy: {acc * 100:.2f}%)"
|
223 |
+
|
224 |
+
results.append(result)
|
225 |
+
disease_results = results
|
226 |
+
|
227 |
return (
|
228 |
chatbot_history,
|
229 |
sentiment_result,
|
|
|
236 |
|
237 |
# CSS Styling
|
238 |
custom_css = """
|
239 |
+
/* Your custom CSS styles go here */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
"""
|
241 |
|
242 |
# Gradio Application
|
|
|
274 |
outputs=[chatbot, sentiment, emotion, suggestions, professionals, map_html, disease_predictions],
|
275 |
)
|
276 |
|
277 |
+
# Launch the Gradio application
|
278 |
app.launch()
|