Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ from sklearn.ensemble import RandomForestClassifier
|
|
| 20 |
from sklearn.naive_bayes import GaussianNB
|
| 21 |
from sklearn.metrics import accuracy_score
|
| 22 |
|
|
|
|
| 23 |
# Suppress TensorFlow warnings
|
| 24 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 25 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
@@ -99,7 +100,7 @@ for model_name, model_obj in models.items():
|
|
| 99 |
acc = accuracy_score(y_test, y_pred) # Calculate accuracy
|
| 100 |
trained_models[model_name] = {'model': model_obj, 'accuracy': acc}
|
| 101 |
|
| 102 |
-
# Helper Functions for Chatbot
|
| 103 |
def bag_of_words(s, words):
|
| 104 |
"""Convert user input to bag-of-words vector."""
|
| 105 |
bag = [0] * len(words)
|
|
@@ -184,7 +185,7 @@ def generate_suggestions(emotion):
|
|
| 184 |
("Relaxation Video", "https://youtu.be/m1vaUGtyo-A"),
|
| 185 |
],
|
| 186 |
}
|
| 187 |
-
|
| 188 |
# Create a markdown string for clickable suggestions in a table format
|
| 189 |
formatted_suggestions = ["### Suggestions"]
|
| 190 |
formatted_suggestions.append(f"Since you’re feeling {emotion}, you might find these links particularly helpful. Don’t hesitate to explore:")
|
|
@@ -215,7 +216,6 @@ def get_health_professionals_and_map(location, query):
|
|
| 215 |
popup=f"{place['name']}"
|
| 216 |
).add_to(map_)
|
| 217 |
return professionals, map_._repr_html_()
|
| 218 |
-
|
| 219 |
return [], "" # Return empty list if no professionals found
|
| 220 |
except Exception as e:
|
| 221 |
return [], "" # Return empty list on exception
|
|
@@ -230,43 +230,19 @@ def app_function_chatbot(user_input, location, query, history):
|
|
| 230 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
| 231 |
|
| 232 |
# Disease Prediction Logic
|
| 233 |
-
# def predict_disease(symptoms):
|
| 234 |
-
# """Predict disease based on input symptoms."""
|
| 235 |
-
# valid_symptoms = [s for s in symptoms if s is not None]
|
| 236 |
-
# if len(valid_symptoms) < 3:
|
| 237 |
-
# return "Please select at least 3 symptoms for a better prediction."
|
| 238 |
-
# input_test = np.zeros(len(X_train.columns)) # Create an array for feature input
|
| 239 |
-
# for symptom in symptoms:
|
| 240 |
-
# if symptom in X_train.columns:
|
| 241 |
-
# input_test[X_train.columns.get_loc(symptom)] = 1
|
| 242 |
-
# predictions = {}
|
| 243 |
-
# for model_name, info in trained_models.items():
|
| 244 |
-
# prediction = info['model'].predict([input_test])[0]
|
| 245 |
-
# predicted_disease = label_encoder_train.inverse_transform([prediction])[0]
|
| 246 |
-
# predictions[model_name] = predicted_disease
|
| 247 |
-
|
| 248 |
-
# # Create a Markdown table for displaying predictions
|
| 249 |
-
# markdown_output = ["### Predicted Diseases"]
|
| 250 |
-
# markdown_output.append("| Model | Predicted Disease |")
|
| 251 |
-
# markdown_output.append("|-------|------------------|") # Table headers
|
| 252 |
-
# for model_name, disease in predictions.items():
|
| 253 |
-
# markdown_output.append(f"| {model_name} | {disease} |")
|
| 254 |
-
|
| 255 |
-
# return "\n".join(markdown_output)
|
| 256 |
def predict_disease(symptoms):
|
| 257 |
"""Predict disease based on input symptoms."""
|
| 258 |
# Filter out None values
|
| 259 |
valid_symptoms = [s for s in symptoms if s is not None]
|
| 260 |
-
|
| 261 |
# Ensure at least 3 symptoms are selected
|
| 262 |
if len(valid_symptoms) < 3:
|
| 263 |
return "Please select at least 3 symptoms for a better prediction."
|
| 264 |
-
|
| 265 |
input_test = np.zeros(len(X_train.columns)) # Create an array for feature input
|
| 266 |
for symptom in valid_symptoms:
|
| 267 |
if symptom in X_train.columns:
|
| 268 |
input_test[X_train.columns.get_loc(symptom)] = 1
|
| 269 |
-
|
| 270 |
predictions = {}
|
| 271 |
for model_name, info in trained_models.items():
|
| 272 |
prediction = info['model'].predict([input_test])[0]
|
|
@@ -279,151 +255,56 @@ def predict_disease(symptoms):
|
|
| 279 |
markdown_output.append("|-------|------------------|") # Table headers
|
| 280 |
for model_name, disease in predictions.items():
|
| 281 |
markdown_output.append(f"| {model_name} | {disease} |")
|
| 282 |
-
|
| 283 |
-
return "\n".join(markdown_output)
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
from gradio.components import HTML
|
| 287 |
-
|
| 288 |
-
# Custom CSS for styling
|
| 289 |
-
custom_css = """
|
| 290 |
-
/* Importing Google Fonts */
|
| 291 |
-
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
|
| 292 |
|
| 293 |
-
|
| 294 |
-
body {
|
| 295 |
-
font-family: 'Roboto', sans-serif;
|
| 296 |
-
background-color: #f0f4f7; /* Light background for better contrast */
|
| 297 |
-
}
|
| 298 |
-
|
| 299 |
-
/* Header Styling */
|
| 300 |
-
h1, h2, h3, h4 {
|
| 301 |
-
font-weight: bold; /* Make all headings bold */
|
| 302 |
-
color: #3c6487; /* Theme color for headings */
|
| 303 |
-
}
|
| 304 |
-
|
| 305 |
-
h1 {
|
| 306 |
-
font-size: 2.5rem; /* Bigger header size */
|
| 307 |
-
background: linear-gradient(135deg, #3c6487, #355f7a); /* Gradient using your color */
|
| 308 |
-
color: #ffffff;
|
| 309 |
-
border-radius: 12px;
|
| 310 |
-
padding: 15px;
|
| 311 |
-
text-align: center;
|
| 312 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* Shadow effect */
|
| 313 |
-
margin-bottom: 20px; /* Spacing below the header */
|
| 314 |
-
}
|
| 315 |
-
|
| 316 |
-
/* Button Styling */
|
| 317 |
-
.gr-button {
|
| 318 |
-
background-color: #3c6487; /* Button background */
|
| 319 |
-
color: white;
|
| 320 |
-
border-radius: 8px;
|
| 321 |
-
padding: 10px 15px; /* Adjusted padding */
|
| 322 |
-
font-size: 16px; /* Font size for buttons */
|
| 323 |
-
border: none; /* No border */
|
| 324 |
-
cursor: pointer; /* Pointer on hover */
|
| 325 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2); /* Shadow on button */
|
| 326 |
-
display: inline-block; /* Inline-block to wrap text */
|
| 327 |
-
position: relative; /* For pseudo-element positioning */
|
| 328 |
-
text-decoration: none; /* Remove default underline */
|
| 329 |
-
}
|
| 330 |
-
|
| 331 |
-
/* Button hover states */
|
| 332 |
-
.gr-button:hover {
|
| 333 |
-
background: linear-gradient(to right, #a0c4e1, #3c6487); /* Light blue gradient on hover */
|
| 334 |
-
transition: background 0.3s; /* Ease the background change */
|
| 335 |
-
}
|
| 336 |
-
|
| 337 |
-
/* Add a blue underline effect */
|
| 338 |
-
.gr-button::after {
|
| 339 |
-
content: ""; /* Empty content for underline */
|
| 340 |
-
display: block;
|
| 341 |
-
width: 100%; /* Full width */
|
| 342 |
-
height: 3px; /* Height of the underline */
|
| 343 |
-
background: #3c6487; /* Underline color */
|
| 344 |
-
position: absolute;
|
| 345 |
-
bottom: -5px; /* Position it below the text */
|
| 346 |
-
left: 0;
|
| 347 |
-
transform: scaleX(0); /* Initially scale to 0 (invisible) */
|
| 348 |
-
transition: transform 0.3s; /* Smooth transition for the underline */
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
.gr-button:hover::after {
|
| 352 |
-
transform: scaleX(1); /* Scale to full width on hover */
|
| 353 |
-
}
|
| 354 |
-
|
| 355 |
-
/* Input and Textarea Styling */
|
| 356 |
-
textarea, input {
|
| 357 |
-
background: white; /* Input background */
|
| 358 |
-
color: black; /* Text color */
|
| 359 |
-
border: 2px solid #3c6487; /* Matching border color */
|
| 360 |
-
padding: 10px;
|
| 361 |
-
font-size: 1rem;
|
| 362 |
-
border-radius: 10px;
|
| 363 |
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Light shadow for inputs */
|
| 364 |
-
}
|
| 365 |
-
|
| 366 |
-
textarea:focus, input:focus {
|
| 367 |
-
border-color: #ae1c93; /* Highlight border color on focus */
|
| 368 |
-
box-shadow: 0 0 5px rgba(174, 28, 147, 0.5); /* Shadow on focus */
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
/* DataFrame Container Styling */
|
| 372 |
-
.df-container {
|
| 373 |
-
background: white; /* Background for data frames */
|
| 374 |
-
color: black; /* Text color */
|
| 375 |
-
border: 2px solid #3c6487; /* Matching border color for data frames */
|
| 376 |
-
border-radius: 10px;
|
| 377 |
-
padding: 10px;
|
| 378 |
-
font-size: 14px;
|
| 379 |
-
max-height: 400px; /* Maximum height for scrolling */
|
| 380 |
-
overflow-y: auto; /* Enable scrolling */
|
| 381 |
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Light shadow for data frame */
|
| 382 |
-
}
|
| 383 |
-
|
| 384 |
-
/* Suggestions Markdown Formatting */
|
| 385 |
-
.markdown {
|
| 386 |
-
padding: 15px; /* Padding for Markdown sections */
|
| 387 |
-
border-radius: 10px; /* Round corners for better appearance */
|
| 388 |
-
background-color: #eaeff1; /* Light background for suggestions */
|
| 389 |
-
border: 1px solid #3c6487; /* Border to distinguish */
|
| 390 |
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Light shadow */
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
@media (max-width: 768px) {
|
| 394 |
-
h1 {
|
| 395 |
-
font-size: 2rem; /* Smaller font size for smaller screens */
|
| 396 |
-
padding: 10px;
|
| 397 |
-
}
|
| 398 |
-
|
| 399 |
-
.gr-button {
|
| 400 |
-
font-size: 0.9rem; /* Adjusted size for mobile */
|
| 401 |
-
padding: 8px 16px; /* Adjust padding for mobile */
|
| 402 |
-
width: auto; /* Maintain auto width */
|
| 403 |
-
}
|
| 404 |
|
| 405 |
-
textarea, input {
|
| 406 |
-
width: 100%; /* Full width for inputs */
|
| 407 |
-
margin-bottom: 10px; /* Spacing between inputs */
|
| 408 |
-
}
|
| 409 |
-
}
|
| 410 |
-
"""
|
| 411 |
# Gradio Application Interface
|
| 412 |
-
with gr.Blocks(
|
| 413 |
gr.HTML("<h1>🌟 Well-Being Companion</h1>")
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
with gr.Tab("Well-Being Chatbot"):
|
| 416 |
with gr.Row():
|
| 417 |
user_input = gr.Textbox(label="Please Enter Your Message Here", placeholder="Type your message here...", max_lines=3)
|
| 418 |
-
location = gr.Textbox(label="Please Enter Your Current Location
|
| 419 |
query = gr.Textbox(label="Search Health Professionals Nearby", placeholder="E.g., Health Professionals", max_lines=1)
|
| 420 |
-
|
| 421 |
submit_chatbot = gr.Button(value="Submit Your Message", variant="primary", icon="fa-paper-plane")
|
| 422 |
-
|
| 423 |
chatbot = gr.Chatbot(label="Chat History", show_label=True)
|
| 424 |
sentiment = gr.Textbox(label="Detected Sentiment", show_label=True)
|
| 425 |
emotion = gr.Textbox(label="Detected Emotion", show_label=True)
|
| 426 |
-
|
| 427 |
suggestions_markdown = gr.Markdown(label="Suggestions")
|
| 428 |
professionals = gr.DataFrame(label="Nearby Health Professionals", headers=["Name", "Address"])
|
| 429 |
map_html = gr.HTML(label="Interactive Map")
|
|
@@ -435,14 +316,14 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 435 |
)
|
| 436 |
|
| 437 |
with gr.Tab("Disease Prediction"):
|
| 438 |
-
symptom1 = gr.Dropdown(choices=[None] + X_train.columns
|
| 439 |
-
symptom2 = gr.Dropdown(choices=[None] + X_train.columns
|
| 440 |
-
symptom3 = gr.Dropdown(choices=[None] + X_train.columns
|
| 441 |
-
symptom4 = gr.Dropdown(choices=[None] + X_train.columns
|
| 442 |
-
symptom5 = gr.Dropdown(choices=[None] + X_train.columns
|
| 443 |
-
|
| 444 |
submit_disease = gr.Button(value="Predict Disease", variant="primary", icon="fa-stethoscope")
|
| 445 |
-
|
| 446 |
disease_prediction_result = gr.Markdown(label="Predicted Diseases")
|
| 447 |
|
| 448 |
submit_disease.click(
|
|
@@ -452,6 +333,5 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 452 |
outputs=disease_prediction_result
|
| 453 |
)
|
| 454 |
|
| 455 |
-
|
| 456 |
# Launch the Gradio application
|
| 457 |
app.launch()
|
|
|
|
| 20 |
from sklearn.naive_bayes import GaussianNB
|
| 21 |
from sklearn.metrics import accuracy_score
|
| 22 |
|
| 23 |
+
|
| 24 |
# Suppress TensorFlow warnings
|
| 25 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 26 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
| 100 |
acc = accuracy_score(y_test, y_pred) # Calculate accuracy
|
| 101 |
trained_models[model_name] = {'model': model_obj, 'accuracy': acc}
|
| 102 |
|
| 103 |
+
# Helper Functions for Chatbot (no changes)
|
| 104 |
def bag_of_words(s, words):
|
| 105 |
"""Convert user input to bag-of-words vector."""
|
| 106 |
bag = [0] * len(words)
|
|
|
|
| 185 |
("Relaxation Video", "https://youtu.be/m1vaUGtyo-A"),
|
| 186 |
],
|
| 187 |
}
|
| 188 |
+
|
| 189 |
# Create a markdown string for clickable suggestions in a table format
|
| 190 |
formatted_suggestions = ["### Suggestions"]
|
| 191 |
formatted_suggestions.append(f"Since you’re feeling {emotion}, you might find these links particularly helpful. Don’t hesitate to explore:")
|
|
|
|
| 216 |
popup=f"{place['name']}"
|
| 217 |
).add_to(map_)
|
| 218 |
return professionals, map_._repr_html_()
|
|
|
|
| 219 |
return [], "" # Return empty list if no professionals found
|
| 220 |
except Exception as e:
|
| 221 |
return [], "" # Return empty list on exception
|
|
|
|
| 230 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
| 231 |
|
| 232 |
# Disease Prediction Logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
def predict_disease(symptoms):
|
| 234 |
"""Predict disease based on input symptoms."""
|
| 235 |
# Filter out None values
|
| 236 |
valid_symptoms = [s for s in symptoms if s is not None]
|
|
|
|
| 237 |
# Ensure at least 3 symptoms are selected
|
| 238 |
if len(valid_symptoms) < 3:
|
| 239 |
return "Please select at least 3 symptoms for a better prediction."
|
| 240 |
+
|
| 241 |
input_test = np.zeros(len(X_train.columns)) # Create an array for feature input
|
| 242 |
for symptom in valid_symptoms:
|
| 243 |
if symptom in X_train.columns:
|
| 244 |
input_test[X_train.columns.get_loc(symptom)] = 1
|
| 245 |
+
|
| 246 |
predictions = {}
|
| 247 |
for model_name, info in trained_models.items():
|
| 248 |
prediction = info['model'].predict([input_test])[0]
|
|
|
|
| 255 |
markdown_output.append("|-------|------------------|") # Table headers
|
| 256 |
for model_name, disease in predictions.items():
|
| 257 |
markdown_output.append(f"| {model_name} | {disease} |")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
return "\n".join(markdown_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
# Gradio Application Interface
|
| 262 |
+
with gr.Blocks() as app:
|
| 263 |
gr.HTML("<h1>🌟 Well-Being Companion</h1>")
|
| 264 |
|
| 265 |
+
# Theme Dropdown
|
| 266 |
+
themes = [
|
| 267 |
+
"calm_seafoam",
|
| 268 |
+
"Ranko_test"
|
| 269 |
+
]
|
| 270 |
+
|
| 271 |
+
theme_dropdown = gr.Dropdown(choices=themes, label="Select Theme")
|
| 272 |
+
|
| 273 |
+
toggle_dark = gr.Button(value="Toggle Dark").style(full_width=True)
|
| 274 |
+
|
| 275 |
+
# Theme changing logic
|
| 276 |
+
theme_dropdown.change(
|
| 277 |
+
None,
|
| 278 |
+
None,
|
| 279 |
+
app,
|
| 280 |
+
_js=f"""
|
| 281 |
+
(theme) => {{
|
| 282 |
+
let themeElem = document.createElement('link');
|
| 283 |
+
themeElem.rel = 'stylesheet';
|
| 284 |
+
themeElem.href = '/themes/' + theme + '.css'; // Path to the theme CSS files
|
| 285 |
+
document.head.appendChild(themeElem);
|
| 286 |
+
}}
|
| 287 |
+
"""
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
toggle_dark.click(None, _js="""
|
| 291 |
+
() => {
|
| 292 |
+
document.body.classList.toggle('dark');
|
| 293 |
+
}
|
| 294 |
+
""")
|
| 295 |
+
|
| 296 |
with gr.Tab("Well-Being Chatbot"):
|
| 297 |
with gr.Row():
|
| 298 |
user_input = gr.Textbox(label="Please Enter Your Message Here", placeholder="Type your message here...", max_lines=3)
|
| 299 |
+
location = gr.Textbox(label="Please Enter Your Current Location", placeholder="E.g., Honolulu", max_lines=1)
|
| 300 |
query = gr.Textbox(label="Search Health Professionals Nearby", placeholder="E.g., Health Professionals", max_lines=1)
|
| 301 |
+
|
| 302 |
submit_chatbot = gr.Button(value="Submit Your Message", variant="primary", icon="fa-paper-plane")
|
| 303 |
+
|
| 304 |
chatbot = gr.Chatbot(label="Chat History", show_label=True)
|
| 305 |
sentiment = gr.Textbox(label="Detected Sentiment", show_label=True)
|
| 306 |
emotion = gr.Textbox(label="Detected Emotion", show_label=True)
|
| 307 |
+
|
| 308 |
suggestions_markdown = gr.Markdown(label="Suggestions")
|
| 309 |
professionals = gr.DataFrame(label="Nearby Health Professionals", headers=["Name", "Address"])
|
| 310 |
map_html = gr.HTML(label="Interactive Map")
|
|
|
|
| 316 |
)
|
| 317 |
|
| 318 |
with gr.Tab("Disease Prediction"):
|
| 319 |
+
symptom1 = gr.Dropdown(choices=[None] + list(X_train.columns), label="Select Symptom 1", value=None)
|
| 320 |
+
symptom2 = gr.Dropdown(choices=[None] + list(X_train.columns), label="Select Symptom 2", value=None)
|
| 321 |
+
symptom3 = gr.Dropdown(choices=[None] + list(X_train.columns), label="Select Symptom 3", value=None)
|
| 322 |
+
symptom4 = gr.Dropdown(choices=[None] + list(X_train.columns), label="Select Symptom 4", value=None)
|
| 323 |
+
symptom5 = gr.Dropdown(choices=[None] + list(X_train.columns), label="Select Symptom 5", value=None)
|
| 324 |
+
|
| 325 |
submit_disease = gr.Button(value="Predict Disease", variant="primary", icon="fa-stethoscope")
|
| 326 |
+
|
| 327 |
disease_prediction_result = gr.Markdown(label="Predicted Diseases")
|
| 328 |
|
| 329 |
submit_disease.click(
|
|
|
|
| 333 |
outputs=disease_prediction_result
|
| 334 |
)
|
| 335 |
|
|
|
|
| 336 |
# Launch the Gradio application
|
| 337 |
app.launch()
|