Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ import googlemaps
|
|
| 13 |
import folium
|
| 14 |
import torch
|
| 15 |
|
| 16 |
-
# Suppress TensorFlow
|
| 17 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 18 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 19 |
|
|
@@ -21,7 +21,7 @@ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
| 21 |
nltk.download("punkt")
|
| 22 |
stemmer = LancasterStemmer()
|
| 23 |
|
| 24 |
-
# Load
|
| 25 |
with open("intents.json") as file:
|
| 26 |
intents_data = json.load(file)
|
| 27 |
|
|
@@ -100,7 +100,7 @@ def detect_emotion(user_input):
|
|
| 100 |
return emotion_map.get(emotion, "Unknown π€")
|
| 101 |
|
| 102 |
def generate_suggestions(emotion):
|
| 103 |
-
"""Provide suggestions
|
| 104 |
suggestions = {
|
| 105 |
"joy": [
|
| 106 |
["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation" target="_blank">Visit</a>'],
|
|
@@ -114,7 +114,7 @@ def generate_suggestions(emotion):
|
|
| 114 |
],
|
| 115 |
"fear": [
|
| 116 |
["Coping with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety" target="_blank">Visit</a>'],
|
| 117 |
-
["Mindfulness
|
| 118 |
],
|
| 119 |
"sadness": [
|
| 120 |
["Overcoming Sadness", '<a href="https://youtu.be/-e-4Kx5px_I" target="_blank">Watch</a>'],
|
|
@@ -151,7 +151,7 @@ def get_health_professionals_and_map(location, query):
|
|
| 151 |
except Exception as e:
|
| 152 |
return [f"An error occurred: {str(e)}"], ""
|
| 153 |
|
| 154 |
-
#
|
| 155 |
def app_function(user_message, location, query, history):
|
| 156 |
chatbot_history, _ = chatbot(user_message, history)
|
| 157 |
sentiment = analyze_sentiment(user_message)
|
|
@@ -160,58 +160,61 @@ def app_function(user_message, location, query, history):
|
|
| 160 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 161 |
return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
|
| 162 |
|
| 163 |
-
#
|
| 164 |
custom_css = """
|
| 165 |
body {
|
| 166 |
background: linear-gradient(135deg, #000000, #ff5722);
|
| 167 |
font-family: 'Roboto', sans-serif;
|
| 168 |
color: white;
|
| 169 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
button {
|
| 171 |
background: linear-gradient(45deg, #ff5722, #ff9800) !important;
|
| 172 |
border: none;
|
| 173 |
-
border-radius: 8px;
|
| 174 |
padding: 12px 20px;
|
| 175 |
-
cursor: pointer;
|
| 176 |
-
color: white;
|
| 177 |
font-size: 16px;
|
|
|
|
|
|
|
|
|
|
| 178 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
|
| 179 |
}
|
| 180 |
-
button:hover {
|
| 181 |
-
background: linear-gradient(45deg, #ff9800, #ff5722) !important;
|
| 182 |
-
}
|
| 183 |
textarea, input {
|
| 184 |
background: black !important;
|
| 185 |
color: white !important;
|
| 186 |
-
padding: 12px;
|
| 187 |
-
border: 1px solid #ff5722 !important;
|
| 188 |
-
border-radius: 8px;
|
| 189 |
-
}
|
| 190 |
-
.gr-dataframe {
|
| 191 |
-
background-color: black !important;
|
| 192 |
-
color: white !important;
|
| 193 |
-
overflow-y: scroll;
|
| 194 |
-
height: 300px;
|
| 195 |
border: 1px solid #ff5722;
|
|
|
|
| 196 |
}
|
| 197 |
"""
|
| 198 |
|
| 199 |
-
# Gradio
|
| 200 |
with gr.Blocks(css=custom_css) as app:
|
| 201 |
-
gr.
|
| 202 |
-
gr.
|
| 203 |
|
| 204 |
with gr.Row():
|
| 205 |
user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
|
| 206 |
location = gr.Textbox(label="Your Location", placeholder="Enter your location...")
|
| 207 |
-
query = gr.Textbox(label="Health Query", placeholder="Search for
|
| 208 |
|
| 209 |
chatbot_history = gr.Chatbot(label="Chat History")
|
| 210 |
sentiment_output = gr.Textbox(label="Detected Sentiment")
|
| 211 |
emotion_output = gr.Textbox(label="Detected Emotion")
|
| 212 |
-
suggestions_table = gr.DataFrame(headers=["
|
| 213 |
professionals_output = gr.Textbox(label="Nearby Health Professionals", lines=5)
|
| 214 |
-
map_output = gr.HTML(label="Map")
|
| 215 |
|
| 216 |
submit_button = gr.Button("Submit")
|
| 217 |
|
|
|
|
| 13 |
import folium
|
| 14 |
import torch
|
| 15 |
|
| 16 |
+
# Suppress TensorFlow GPU usage and warnings
|
| 17 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 18 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 19 |
|
|
|
|
| 21 |
nltk.download("punkt")
|
| 22 |
stemmer = LancasterStemmer()
|
| 23 |
|
| 24 |
+
# Load chatbot training data
|
| 25 |
with open("intents.json") as file:
|
| 26 |
intents_data = json.load(file)
|
| 27 |
|
|
|
|
| 100 |
return emotion_map.get(emotion, "Unknown π€")
|
| 101 |
|
| 102 |
def generate_suggestions(emotion):
|
| 103 |
+
"""Provide suggestions for the detected emotion."""
|
| 104 |
suggestions = {
|
| 105 |
"joy": [
|
| 106 |
["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation" target="_blank">Visit</a>'],
|
|
|
|
| 114 |
],
|
| 115 |
"fear": [
|
| 116 |
["Coping with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety" target="_blank">Visit</a>'],
|
| 117 |
+
["Mindfulness Practices", '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch</a>'],
|
| 118 |
],
|
| 119 |
"sadness": [
|
| 120 |
["Overcoming Sadness", '<a href="https://youtu.be/-e-4Kx5px_I" target="_blank">Watch</a>'],
|
|
|
|
| 151 |
except Exception as e:
|
| 152 |
return [f"An error occurred: {str(e)}"], ""
|
| 153 |
|
| 154 |
+
# Application Logic
|
| 155 |
def app_function(user_message, location, query, history):
|
| 156 |
chatbot_history, _ = chatbot(user_message, history)
|
| 157 |
sentiment = analyze_sentiment(user_message)
|
|
|
|
| 160 |
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 161 |
return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
|
| 162 |
|
| 163 |
+
# CSS Styling for Centered and Bigger Titles
|
| 164 |
custom_css = """
|
| 165 |
body {
|
| 166 |
background: linear-gradient(135deg, #000000, #ff5722);
|
| 167 |
font-family: 'Roboto', sans-serif;
|
| 168 |
color: white;
|
| 169 |
}
|
| 170 |
+
h1 {
|
| 171 |
+
font-size: 4rem;
|
| 172 |
+
font-weight: bold;
|
| 173 |
+
text-align: center;
|
| 174 |
+
margin-bottom: 10px;
|
| 175 |
+
text-shadow: 3px 3px 8px rgba(0, 0, 0, 0.7);
|
| 176 |
+
}
|
| 177 |
+
h3 {
|
| 178 |
+
font-size: 2rem;
|
| 179 |
+
text-align: center;
|
| 180 |
+
margin-bottom: 40px;
|
| 181 |
+
font-weight: lighter;
|
| 182 |
+
color: white;
|
| 183 |
+
}
|
| 184 |
button {
|
| 185 |
background: linear-gradient(45deg, #ff5722, #ff9800) !important;
|
| 186 |
border: none;
|
|
|
|
| 187 |
padding: 12px 20px;
|
|
|
|
|
|
|
| 188 |
font-size: 16px;
|
| 189 |
+
border-radius: 8px;
|
| 190 |
+
color: white;
|
| 191 |
+
cursor: pointer;
|
| 192 |
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
|
| 193 |
}
|
|
|
|
|
|
|
|
|
|
| 194 |
textarea, input {
|
| 195 |
background: black !important;
|
| 196 |
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
border: 1px solid #ff5722;
|
| 198 |
+
border-radius: 8px;
|
| 199 |
}
|
| 200 |
"""
|
| 201 |
|
| 202 |
+
# Gradio Application
|
| 203 |
with gr.Blocks(css=custom_css) as app:
|
| 204 |
+
gr.HTML("<h1>π Well-Being Companion</h1>")
|
| 205 |
+
gr.HTML("<h3>Empowering Your Mental Health Journey π</h3>")
|
| 206 |
|
| 207 |
with gr.Row():
|
| 208 |
user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
|
| 209 |
location = gr.Textbox(label="Your Location", placeholder="Enter your location...")
|
| 210 |
+
query = gr.Textbox(label="Health Query", placeholder="Search for professionals like therapists...")
|
| 211 |
|
| 212 |
chatbot_history = gr.Chatbot(label="Chat History")
|
| 213 |
sentiment_output = gr.Textbox(label="Detected Sentiment")
|
| 214 |
emotion_output = gr.Textbox(label="Detected Emotion")
|
| 215 |
+
suggestions_table = gr.DataFrame(headers=["Title", "Link"], label="Suggestions")
|
| 216 |
professionals_output = gr.Textbox(label="Nearby Health Professionals", lines=5)
|
| 217 |
+
map_output = gr.HTML(label="Map of Nearby Professionals")
|
| 218 |
|
| 219 |
submit_button = gr.Button("Submit")
|
| 220 |
|