Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,6 +48,7 @@ gmaps = googlemaps.Client(key=os.getenv("GOOGLE_API_KEY"))
|
|
48 |
|
49 |
# Helper Functions
|
50 |
def bag_of_words(s, words):
|
|
|
51 |
bag = [0] * len(words)
|
52 |
s_words = word_tokenize(s)
|
53 |
s_words = [stemmer.stem(word.lower()) for word in s_words if word.isalnum()]
|
@@ -58,6 +59,7 @@ def bag_of_words(s, words):
|
|
58 |
return np.array(bag)
|
59 |
|
60 |
def generate_chatbot_response(message, history):
|
|
|
61 |
history = history or []
|
62 |
try:
|
63 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
@@ -73,6 +75,7 @@ def generate_chatbot_response(message, history):
|
|
73 |
return history, response
|
74 |
|
75 |
def analyze_sentiment(user_input):
|
|
|
76 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
77 |
with torch.no_grad():
|
78 |
outputs = model_sentiment(**inputs)
|
@@ -81,6 +84,7 @@ def analyze_sentiment(user_input):
|
|
81 |
return f"Sentiment: {sentiment_map[sentiment_class]}"
|
82 |
|
83 |
def detect_emotion(user_input):
|
|
|
84 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
85 |
result = pipe(user_input)
|
86 |
emotion = result[0]["label"].lower().strip()
|
@@ -95,35 +99,42 @@ def detect_emotion(user_input):
|
|
95 |
return emotion_map.get(emotion, "Unknown π€"), emotion
|
96 |
|
97 |
def generate_suggestions(emotion):
|
|
|
98 |
emotion_key = emotion.lower()
|
99 |
suggestions = {
|
100 |
"joy": [
|
101 |
-
["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation" target="_blank">Visit</a>'],
|
102 |
-
["
|
103 |
-
["
|
|
|
104 |
],
|
105 |
"anger": [
|
106 |
-
["
|
107 |
-
["Stress Tips", '<a href="https://www.
|
|
|
|
|
108 |
],
|
109 |
"fear": [
|
110 |
-
["
|
111 |
-
["
|
|
|
|
|
112 |
],
|
113 |
"sadness": [
|
114 |
-
["
|
|
|
|
|
115 |
],
|
116 |
"surprise": [
|
117 |
-
["Managing
|
|
|
118 |
["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
|
119 |
],
|
120 |
-
"neutral": [
|
121 |
-
["General Well-Being Tips", '<a href="https://www.psychologytoday.com" target="_blank">Visit</a>'],
|
122 |
-
],
|
123 |
}
|
124 |
return suggestions.get(emotion_key, [["No specific suggestions available.", ""]])
|
125 |
|
126 |
def get_health_professionals_and_map(location, query):
|
|
|
127 |
try:
|
128 |
if not location or not query:
|
129 |
return ["Please provide both location and query."], ""
|
@@ -148,18 +159,18 @@ def get_health_professionals_and_map(location, query):
|
|
148 |
|
149 |
# Main Application Logic
|
150 |
def app_function(user_input, location, query, history):
|
151 |
-
chatbot_history, _ = generate_chatbot_response(user_input, history)
|
152 |
-
sentiment_result = analyze_sentiment(user_input)
|
153 |
-
emotion_result, cleaned_emotion = detect_emotion(user_input)
|
154 |
-
suggestions = generate_suggestions(cleaned_emotion)
|
155 |
-
professionals, map_html = get_health_professionals_and_map(location, query)
|
156 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
157 |
|
158 |
# Gradio Interface
|
159 |
custom_css = """
|
160 |
body {
|
161 |
font-family: 'Roboto', sans-serif;
|
162 |
-
background: linear-gradient(135deg
|
163 |
color: white;
|
164 |
}
|
165 |
|
@@ -174,48 +185,43 @@ h1 {
|
|
174 |
}
|
175 |
|
176 |
textarea, input {
|
177 |
-
background:
|
178 |
-
color:
|
179 |
border: 2px solid orange;
|
|
|
180 |
font-size: 1rem;
|
181 |
-
|
|
|
182 |
border-radius: 8px;
|
183 |
-
caret-color: white; /* White typing cursor */
|
184 |
-
outline: none; /* No focus outline */
|
185 |
}
|
186 |
|
187 |
-
/* Ensure the text stays white and background black when focused */
|
188 |
textarea:focus, input:focus {
|
189 |
-
background:
|
190 |
-
color:
|
191 |
border: 2px solid orange;
|
192 |
outline: none;
|
193 |
}
|
194 |
|
195 |
-
/* Prevent hover effects from breaking consistency */
|
196 |
textarea:hover, input:hover {
|
197 |
-
background:
|
198 |
-
color:
|
199 |
border: 2px solid orange;
|
200 |
-
outline: none;
|
201 |
}
|
202 |
|
203 |
-
|
204 |
-
background:
|
205 |
-
color:
|
|
|
|
|
206 |
padding: 10px;
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
cursor: pointer;
|
212 |
-
}
|
213 |
-
|
214 |
-
button:hover {
|
215 |
-
box-shadow: 0px 4px 8px rgba(255, 165, 0, 0.5);
|
216 |
}
|
217 |
"""
|
218 |
|
|
|
219 |
with gr.Blocks(css=custom_css) as app:
|
220 |
gr.HTML("<h1>π Well-Being Companion</h1>")
|
221 |
with gr.Row():
|
@@ -225,15 +231,15 @@ with gr.Blocks(css=custom_css) as app:
|
|
225 |
chatbot = gr.Chatbot(label="Chat History")
|
226 |
sentiment = gr.Textbox(label="Detected Sentiment")
|
227 |
emotion = gr.Textbox(label="Detected Emotion")
|
228 |
-
suggestions = gr.DataFrame(headers=["Title", "Link"])
|
229 |
professionals = gr.Textbox(label="Nearby Professionals", lines=6)
|
230 |
map_html = gr.HTML(label="Interactive Map")
|
231 |
-
submit = gr.Button("Submit")
|
232 |
|
233 |
submit.click(
|
234 |
app_function,
|
235 |
inputs=[user_input, location, query, chatbot],
|
236 |
-
outputs=[chatbot, sentiment, emotion, suggestions, professionals, map_html]
|
237 |
)
|
238 |
|
239 |
app.launch()
|
|
|
48 |
|
49 |
# Helper Functions
|
50 |
def bag_of_words(s, words):
|
51 |
+
"""Convert user input to bag-of-words vector."""
|
52 |
bag = [0] * len(words)
|
53 |
s_words = word_tokenize(s)
|
54 |
s_words = [stemmer.stem(word.lower()) for word in s_words if word.isalnum()]
|
|
|
59 |
return np.array(bag)
|
60 |
|
61 |
def generate_chatbot_response(message, history):
|
62 |
+
"""Generate chatbot response and maintain conversation history."""
|
63 |
history = history or []
|
64 |
try:
|
65 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
|
|
75 |
return history, response
|
76 |
|
77 |
def analyze_sentiment(user_input):
|
78 |
+
"""Analyze sentiment and map to emojis."""
|
79 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
80 |
with torch.no_grad():
|
81 |
outputs = model_sentiment(**inputs)
|
|
|
84 |
return f"Sentiment: {sentiment_map[sentiment_class]}"
|
85 |
|
86 |
def detect_emotion(user_input):
|
87 |
+
"""Detect emotions based on input."""
|
88 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
89 |
result = pipe(user_input)
|
90 |
emotion = result[0]["label"].lower().strip()
|
|
|
99 |
return emotion_map.get(emotion, "Unknown π€"), emotion
|
100 |
|
101 |
def generate_suggestions(emotion):
|
102 |
+
"""Return relevant suggestions based on detected emotions."""
|
103 |
emotion_key = emotion.lower()
|
104 |
suggestions = {
|
105 |
"joy": [
|
106 |
+
["Relaxation Techniques", '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Visit</a>'],
|
107 |
+
["Dealing with Stress", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
|
108 |
+
["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
|
109 |
+
["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
|
110 |
],
|
111 |
"anger": [
|
112 |
+
["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
|
113 |
+
["Stress Management Tips", '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Visit</a>'],
|
114 |
+
["Dealing with Anger", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
|
115 |
+
["Relaxation Video", '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch</a>'],
|
116 |
],
|
117 |
"fear": [
|
118 |
+
["Mindfulness Practices", '<a href="https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation" target="_blank">Visit</a>'],
|
119 |
+
["Coping with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
|
120 |
+
["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
|
121 |
+
["Relaxation Video", '<a href="https://youtu.be/yGKKz185M5o" target="_blank">Watch</a>'],
|
122 |
],
|
123 |
"sadness": [
|
124 |
+
["Emotional Wellness Toolkit", '<a href="https://www.nih.gov/health-information/emotional-wellness-toolkit" target="_blank">Visit</a>'],
|
125 |
+
["Dealing with Anxiety", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
|
126 |
+
["Relaxation Video", '<a href="https://youtu.be/-e-4Kx5px_I" target="_blank">Watch</a>'],
|
127 |
],
|
128 |
"surprise": [
|
129 |
+
["Managing Stress", '<a href="https://www.health.harvard.edu/health-a-to-z" target="_blank">Visit</a>'],
|
130 |
+
["Coping Strategies", '<a href="https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety" target="_blank">Visit</a>'],
|
131 |
["Relaxation Video", '<a href="https://youtu.be/m1vaUGtyo-A" target="_blank">Watch</a>'],
|
132 |
],
|
|
|
|
|
|
|
133 |
}
|
134 |
return suggestions.get(emotion_key, [["No specific suggestions available.", ""]])
|
135 |
|
136 |
def get_health_professionals_and_map(location, query):
|
137 |
+
"""Search nearby healthcare professionals using Google Maps API."""
|
138 |
try:
|
139 |
if not location or not query:
|
140 |
return ["Please provide both location and query."], ""
|
|
|
159 |
|
160 |
# Main Application Logic
|
161 |
def app_function(user_input, location, query, history):
|
162 |
+
chatbot_history, _ = generate_chatbot_response(user_input, history) # Generate chatbot response
|
163 |
+
sentiment_result = analyze_sentiment(user_input) # Sentiment detection
|
164 |
+
emotion_result, cleaned_emotion = detect_emotion(user_input) # Emotion detection
|
165 |
+
suggestions = generate_suggestions(cleaned_emotion) # Generate suggestions based on emotion
|
166 |
+
professionals, map_html = get_health_professionals_and_map(location, query) # Find nearby professionals and map
|
167 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
168 |
|
169 |
# Gradio Interface
|
170 |
custom_css = """
|
171 |
body {
|
172 |
font-family: 'Roboto', sans-serif;
|
173 |
+
background: linear-gradient(135deg, #0d0d0d, #ff5722); /* Background gradient */
|
174 |
color: white;
|
175 |
}
|
176 |
|
|
|
185 |
}
|
186 |
|
187 |
textarea, input {
|
188 |
+
background: transparent;
|
189 |
+
color: black;
|
190 |
border: 2px solid orange;
|
191 |
+
padding: 8px;
|
192 |
font-size: 1rem;
|
193 |
+
caret-color: black;
|
194 |
+
outline: none;
|
195 |
border-radius: 8px;
|
|
|
|
|
196 |
}
|
197 |
|
|
|
198 |
textarea:focus, input:focus {
|
199 |
+
background: transparent;
|
200 |
+
color: black;
|
201 |
border: 2px solid orange;
|
202 |
outline: none;
|
203 |
}
|
204 |
|
|
|
205 |
textarea:hover, input:hover {
|
206 |
+
background: transparent;
|
207 |
+
color: black;
|
208 |
border: 2px solid orange;
|
|
|
209 |
}
|
210 |
|
211 |
+
.df-container {
|
212 |
+
background: white;
|
213 |
+
color: black;
|
214 |
+
border: 2px solid orange;
|
215 |
+
border-radius: 10px;
|
216 |
padding: 10px;
|
217 |
+
font-size: 14px;
|
218 |
+
max-height: 400px; /* Extends the height of the table */
|
219 |
+
height: auto;
|
220 |
+
overflow-y: auto; /* Adds scroll if content overflows */
|
|
|
|
|
|
|
|
|
|
|
221 |
}
|
222 |
"""
|
223 |
|
224 |
+
# Gradio Application
|
225 |
with gr.Blocks(css=custom_css) as app:
|
226 |
gr.HTML("<h1>π Well-Being Companion</h1>")
|
227 |
with gr.Row():
|
|
|
231 |
chatbot = gr.Chatbot(label="Chat History")
|
232 |
sentiment = gr.Textbox(label="Detected Sentiment")
|
233 |
emotion = gr.Textbox(label="Detected Emotion")
|
234 |
+
suggestions = gr.DataFrame(headers=["Title", "Link"]) # Table for suggestions
|
235 |
professionals = gr.Textbox(label="Nearby Professionals", lines=6)
|
236 |
map_html = gr.HTML(label="Interactive Map")
|
237 |
+
submit = gr.Button(value="Submit", variant="primary")
|
238 |
|
239 |
submit.click(
|
240 |
app_function,
|
241 |
inputs=[user_input, location, query, chatbot],
|
242 |
+
outputs=[chatbot, sentiment, emotion, suggestions, professionals, map_html],
|
243 |
)
|
244 |
|
245 |
app.launch()
|