Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,7 @@ import googlemaps
|
|
| 13 |
import folium
|
| 14 |
import torch
|
| 15 |
import pandas as pd
|
| 16 |
-
from sklearn.
|
| 17 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 18 |
-
from sklearn.naive_bayes import GaussianNB
|
| 19 |
from sklearn.metrics import accuracy_score
|
| 20 |
|
| 21 |
# Suppress TensorFlow warnings
|
|
@@ -51,7 +49,11 @@ model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/e
|
|
| 51 |
# Google Maps API Client
|
| 52 |
gmaps = googlemaps.Client(key=os.getenv("GOOGLE_API_KEY"))
|
| 53 |
|
| 54 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
disease_dict = {
|
| 56 |
'Fungal infection': 0, 'Allergy': 1, 'GERD': 2, 'Chronic cholestasis': 3, 'Drug Reaction': 4,
|
| 57 |
'Peptic ulcer disease': 5, 'AIDS': 6, 'Diabetes ': 7, 'Gastroenteritis': 8, 'Bronchial Asthma': 9,
|
|
@@ -65,6 +67,37 @@ disease_dict = {
|
|
| 65 |
'Psoriasis': 39, 'Impetigo': 40
|
| 66 |
}
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Helper Functions for Chatbot
|
| 69 |
def bag_of_words(s, words):
|
| 70 |
"""Convert user input to bag-of-words vector."""
|
|
@@ -191,149 +224,22 @@ def app_function_chatbot(user_input, location, query, history):
|
|
| 191 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 192 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
def
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
# Encode diseases
|
| 200 |
-
df.replace({'prognosis': disease_dict}, inplace=True)
|
| 201 |
-
df = df.infer_objects(copy=False)
|
| 202 |
-
tr.replace({'prognosis': disease_dict}, inplace=True)
|
| 203 |
-
tr = tr.infer_objects(copy=False)
|
| 204 |
-
|
| 205 |
-
return df, tr
|
| 206 |
-
|
| 207 |
-
df, tr = load_data()
|
| 208 |
-
l1 = list(df.columns[:-1])
|
| 209 |
-
X = df[l1]
|
| 210 |
-
y = df['prognosis']
|
| 211 |
-
X_test = tr[l1]
|
| 212 |
-
y_test = tr['prognosis']
|
| 213 |
-
|
| 214 |
-
# Trained models
|
| 215 |
-
def train_models():
|
| 216 |
-
models = {
|
| 217 |
-
"Decision Tree": DecisionTreeClassifier(),
|
| 218 |
-
"Random Forest": RandomForestClassifier(),
|
| 219 |
-
"Naive Bayes": GaussianNB()
|
| 220 |
-
}
|
| 221 |
-
trained_models = {}
|
| 222 |
-
for model_name, model_obj in models.items():
|
| 223 |
-
model_obj.fit(X, y)
|
| 224 |
-
acc = accuracy_score(y_test, model_obj.predict(X_test))
|
| 225 |
-
trained_models[model_name] = (model_obj, acc)
|
| 226 |
-
return trained_models
|
| 227 |
-
|
| 228 |
-
trained_models = train_models()
|
| 229 |
-
|
| 230 |
-
def predict_disease(model, symptoms):
|
| 231 |
-
input_test = np.zeros(len(l1))
|
| 232 |
for symptom in symptoms:
|
| 233 |
-
if symptom in
|
| 234 |
-
input_test[
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
if len(symptoms_selected) < 3:
|
| 246 |
-
return "Please select at least 3 symptoms for accurate prediction."
|
| 247 |
-
|
| 248 |
-
results = []
|
| 249 |
-
for model_name, (model, acc) in trained_models.items():
|
| 250 |
-
prediction = predict_disease(model, symptoms_selected)
|
| 251 |
-
result = f"{model_name} Prediction: Predicted Disease: **{prediction}**"
|
| 252 |
-
result += f" (Accuracy: {acc * 100:.2f}%)"
|
| 253 |
-
results.append(result)
|
| 254 |
-
|
| 255 |
-
return "\n\n".join(results)
|
| 256 |
-
|
| 257 |
-
# CSS Styling for the Gradio Interface
|
| 258 |
-
custom_css = """
|
| 259 |
-
body {
|
| 260 |
-
font-family: 'Roboto', sans-serif;
|
| 261 |
-
background-color: #3c6487; /* Set the background color */
|
| 262 |
-
color: white;
|
| 263 |
-
}
|
| 264 |
-
|
| 265 |
-
h1 {
|
| 266 |
-
background: #ffffff;
|
| 267 |
-
color: #000000;
|
| 268 |
-
border-radius: 8px;
|
| 269 |
-
padding: 10px;
|
| 270 |
-
font-weight: bold;
|
| 271 |
-
text-align: center;
|
| 272 |
-
font-size: 2.5rem;
|
| 273 |
-
}
|
| 274 |
-
|
| 275 |
-
textarea, input {
|
| 276 |
-
background: transparent;
|
| 277 |
-
color: black;
|
| 278 |
-
border: 2px solid orange;
|
| 279 |
-
padding: 8px;
|
| 280 |
-
font-size: 1rem;
|
| 281 |
-
caret-color: black;
|
| 282 |
-
outline: none;
|
| 283 |
-
border-radius: 8px;
|
| 284 |
-
}
|
| 285 |
-
|
| 286 |
-
textarea:focus, input:focus {
|
| 287 |
-
background: transparent;
|
| 288 |
-
color: black;
|
| 289 |
-
border: 2px solid orange;
|
| 290 |
-
outline: none;
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
textarea:hover, input:hover {
|
| 294 |
-
background: transparent;
|
| 295 |
-
color: black;
|
| 296 |
-
border: 2px solid orange;
|
| 297 |
-
}
|
| 298 |
-
|
| 299 |
-
.df-container {
|
| 300 |
-
background: white;
|
| 301 |
-
color: black;
|
| 302 |
-
border: 2px solid orange;
|
| 303 |
-
border-radius: 10px;
|
| 304 |
-
padding: 10px;
|
| 305 |
-
font-size: 14px;
|
| 306 |
-
max-height: 400px;
|
| 307 |
-
height: auto;
|
| 308 |
-
overflow-y: auto;
|
| 309 |
-
}
|
| 310 |
-
|
| 311 |
-
#suggestions-title {
|
| 312 |
-
text-align: center !important; /* Ensure the centering is applied */
|
| 313 |
-
font-weight: bold !important; /* Ensure bold is applied */
|
| 314 |
-
color: white !important; /* Ensure color is applied */
|
| 315 |
-
font-size: 4.2rem !important; /* Ensure font size is applied */
|
| 316 |
-
margin-bottom: 20px !important; /* Ensure margin is applied */
|
| 317 |
-
}
|
| 318 |
-
|
| 319 |
-
/* Style for the submit button */
|
| 320 |
-
.gr-button {
|
| 321 |
-
background-color: #ae1c93; /* Set the background color to #ae1c93 */
|
| 322 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.06);
|
| 323 |
-
transition: background-color 0.3s ease;
|
| 324 |
-
}
|
| 325 |
-
|
| 326 |
-
.gr-button:hover {
|
| 327 |
-
background-color: #8f167b;
|
| 328 |
-
}
|
| 329 |
-
|
| 330 |
-
.gr-button:active {
|
| 331 |
-
background-color: #7f156b;
|
| 332 |
-
}
|
| 333 |
-
"""
|
| 334 |
-
|
| 335 |
-
# Gradio Application
|
| 336 |
-
with gr.Blocks(css=custom_css) as app:
|
| 337 |
gr.HTML("<h1>🌟 Well-Being Companion</h1>")
|
| 338 |
|
| 339 |
with gr.Tab("Mental Health Chatbot"):
|
|
@@ -361,19 +267,19 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 361 |
)
|
| 362 |
|
| 363 |
with gr.Tab("Disease Prediction"):
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
disease_prediction_result = gr.Textbox(label="Prediction")
|
| 373 |
|
| 374 |
submit_disease.click(
|
| 375 |
-
|
| 376 |
-
|
|
|
|
| 377 |
outputs=disease_prediction_result,
|
| 378 |
)
|
| 379 |
|
|
|
|
| 13 |
import folium
|
| 14 |
import torch
|
| 15 |
import pandas as pd
|
| 16 |
+
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
|
|
| 17 |
from sklearn.metrics import accuracy_score
|
| 18 |
|
| 19 |
# Suppress TensorFlow warnings
|
|
|
|
| 49 |
# Google Maps API Client
|
| 50 |
gmaps = googlemaps.Client(key=os.getenv("GOOGLE_API_KEY"))
|
| 51 |
|
| 52 |
+
# Load the disease dataset
|
| 53 |
+
df_train = pd.read_csv("Training.csv") # Change the file path as necessary
|
| 54 |
+
df_test = pd.read_csv("Testing.csv") # Change the file path as necessary
|
| 55 |
+
|
| 56 |
+
# Encode diseases
|
| 57 |
disease_dict = {
|
| 58 |
'Fungal infection': 0, 'Allergy': 1, 'GERD': 2, 'Chronic cholestasis': 3, 'Drug Reaction': 4,
|
| 59 |
'Peptic ulcer disease': 5, 'AIDS': 6, 'Diabetes ': 7, 'Gastroenteritis': 8, 'Bronchial Asthma': 9,
|
|
|
|
| 67 |
'Psoriasis': 39, 'Impetigo': 40
|
| 68 |
}
|
| 69 |
|
| 70 |
+
# Function to prepare data
|
| 71 |
+
def prepare_data(df):
|
| 72 |
+
# Split the dataset into features and target
|
| 73 |
+
X = df.iloc[:, :-1] # All columns except the last one (features)
|
| 74 |
+
y = df.iloc[:, -1] # The last column (target)
|
| 75 |
+
|
| 76 |
+
# Encode the target variable
|
| 77 |
+
label_encoder = LabelEncoder()
|
| 78 |
+
y_encoded = label_encoder.fit_transform(y)
|
| 79 |
+
|
| 80 |
+
return X, y_encoded, label_encoder
|
| 81 |
+
|
| 82 |
+
# Preparing training and testing data
|
| 83 |
+
X_train, y_train, label_encoder_train = prepare_data(df_train)
|
| 84 |
+
X_test, y_test, label_encoder_test = prepare_data(df_test)
|
| 85 |
+
|
| 86 |
+
# Define the models
|
| 87 |
+
models = {
|
| 88 |
+
"Decision Tree": DecisionTreeClassifier(),
|
| 89 |
+
"Random Forest": RandomForestClassifier(),
|
| 90 |
+
"Naive Bayes": GaussianNB()
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
# Train and evaluate models
|
| 94 |
+
trained_models = {}
|
| 95 |
+
for model_name, model_obj in models.items():
|
| 96 |
+
model_obj.fit(X_train, y_train) # Fit the model
|
| 97 |
+
y_pred = model_obj.predict(X_test) # Make predictions
|
| 98 |
+
acc = accuracy_score(y_test, y_pred) # Calculate accuracy
|
| 99 |
+
trained_models[model_name] = {'model': model_obj, 'accuracy': acc}
|
| 100 |
+
|
| 101 |
# Helper Functions for Chatbot
|
| 102 |
def bag_of_words(s, words):
|
| 103 |
"""Convert user input to bag-of-words vector."""
|
|
|
|
| 224 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 225 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
| 226 |
|
| 227 |
+
# Disease Prediction Logic
|
| 228 |
+
def predict_disease(symptoms):
|
| 229 |
+
"""Predict disease based on input symptoms."""
|
| 230 |
+
input_test = np.zeros(len(X_train.columns)) # Create an array for feature input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
for symptom in symptoms:
|
| 232 |
+
if symptom in X_train.columns:
|
| 233 |
+
input_test[X_train.columns.get_loc(symptom)] = 1
|
| 234 |
+
predictions = {}
|
| 235 |
+
for model_name, info in trained_models.items():
|
| 236 |
+
prediction = info['model'].predict([input_test])[0]
|
| 237 |
+
predicted_disease = label_encoder_train.inverse_transform([prediction])[0]
|
| 238 |
+
predictions[model_name] = predicted_disease
|
| 239 |
+
return predictions
|
| 240 |
+
|
| 241 |
+
# Gradio Application Interface
|
| 242 |
+
with gr.Blocks() as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
gr.HTML("<h1>🌟 Well-Being Companion</h1>")
|
| 244 |
|
| 245 |
with gr.Tab("Mental Health Chatbot"):
|
|
|
|
| 267 |
)
|
| 268 |
|
| 269 |
with gr.Tab("Disease Prediction"):
|
| 270 |
+
symptom1 = gr.Dropdown(X_train.columns.tolist(), label="Select Symptom 1")
|
| 271 |
+
symptom2 = gr.Dropdown(X_train.columns.tolist(), label="Select Symptom 2")
|
| 272 |
+
symptom3 = gr.Dropdown(X_train.columns.tolist(), label="Select Symptom 3")
|
| 273 |
+
symptom4 = gr.Dropdown(X_train.columns.tolist(), label="Select Symptom 4")
|
| 274 |
+
symptom5 = gr.Dropdown(X_train.columns.tolist(), label="Select Symptom 5")
|
| 275 |
+
|
| 276 |
+
submit_disease = gr.Button(value="Predict Disease", variant="primary")
|
| 277 |
+
disease_prediction_result = gr.Textbox(label="Predicted Diseases")
|
|
|
|
| 278 |
|
| 279 |
submit_disease.click(
|
| 280 |
+
lambda symptom1, symptom2, symptom3, symptom4, symptom5: predict_disease(
|
| 281 |
+
[symptom1, symptom2, symptom3, symptom4, symptom5]),
|
| 282 |
+
inputs=[symptom1, symptom2, symptom3, symptom4, symptom5],
|
| 283 |
outputs=disease_prediction_result,
|
| 284 |
)
|
| 285 |
|