Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -98,6 +98,64 @@ def load_emotion_model():
|
|
98 |
|
99 |
tokenizer_emotion, model_emotion = load_emotion_model()
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
# Google Geocoding API setup to convert city name to latitude/longitude
|
102 |
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
103 |
|
@@ -152,116 +210,27 @@ def scrape_wellness_professionals(query, location):
|
|
152 |
if response.status_code == 200:
|
153 |
soup = BeautifulSoup(response.text, 'html.parser')
|
154 |
|
155 |
-
#
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
162 |
|
163 |
-
return
|
164 |
else:
|
165 |
-
return
|
166 |
-
|
167 |
-
# Updated function to get wellness professionals
|
168 |
-
def get_wellness_professionals(location, api_key):
|
169 |
-
query = "therapist OR counselor OR mental health professional OR marriage and family therapist OR psychotherapist OR psychiatrist OR psychologist OR nutritionist OR wellness doctor OR holistic practitioner OR integrative medicine OR chiropractor OR naturopath"
|
170 |
-
radius = 50000 # 50 km radius
|
171 |
-
|
172 |
-
# Get the latitude and longitude from the location input
|
173 |
-
lat, lon = get_lat_lon(location, api_key)
|
174 |
-
|
175 |
-
if lat is None or lon is None:
|
176 |
-
return "Unable to find coordinates for the given location."
|
177 |
-
|
178 |
-
# Using Google Places API to fetch wellness professionals
|
179 |
-
data = get_places_data(query, f"{lat},{lon}", radius, api_key)
|
180 |
-
|
181 |
-
if data:
|
182 |
-
results = data.get('results', [])
|
183 |
-
wellness_data = []
|
184 |
-
for place in results:
|
185 |
-
name = place.get("name")
|
186 |
-
address = place.get("formatted_address")
|
187 |
-
latitude = place.get("geometry", {}).get("location", {}).get("lat")
|
188 |
-
longitude = place.get("geometry", {}).get("location", {}).get("lng")
|
189 |
-
wellness_data.append([name, address, latitude, longitude])
|
190 |
-
return wellness_data
|
191 |
-
|
192 |
-
# Fallback to scraping if API is not available or fails
|
193 |
-
return scrape_wellness_professionals(query, location)
|
194 |
-
|
195 |
-
# Emotion detection function with suggestions
|
196 |
-
def detect_emotion(user_input):
|
197 |
-
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
198 |
-
result = pipe(user_input)
|
199 |
-
emotion = result[0]['label']
|
200 |
-
|
201 |
-
# Provide suggestions based on the detected emotion
|
202 |
-
if emotion == 'joy':
|
203 |
-
return ("You're feeling happy! Keep up the great mood!",
|
204 |
-
[("Relaxation Techniques", "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"),
|
205 |
-
("Dealing with Stress", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"),
|
206 |
-
("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit")],
|
207 |
-
"Watch on YouTube: https://youtu.be/m1vaUGtyo-A")
|
208 |
-
elif emotion == 'anger':
|
209 |
-
return ("You're feeling angry. It's okay to feel this way. Let's try to calm down.",
|
210 |
-
[("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit"),
|
211 |
-
("Stress Management Tips", "https://www.health.harvard.edu/health-a-to-z"),
|
212 |
-
("Dealing with Anger", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
|
213 |
-
"Watch on YouTube: https://youtu.be/MIc299Flibs")
|
214 |
-
elif emotion == 'fear':
|
215 |
-
return ("You're feeling fearful. Take a moment to breathe and relax.",
|
216 |
-
[("Mindfulness Practices", "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"),
|
217 |
-
("Coping with Anxiety", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"),
|
218 |
-
("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit")],
|
219 |
-
"Watch on YouTube: https://youtu.be/yGKKz185M5o")
|
220 |
-
elif emotion == 'sadness':
|
221 |
-
return ("You're feeling sad. It's okay to take a break.",
|
222 |
-
[("Emotional Wellness Toolkit", "https://www.nih.gov/health-information/emotional-wellness-toolkit"),
|
223 |
-
("Dealing with Anxiety", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
|
224 |
-
"Watch on YouTube: https://youtu.be/-e-4Kx5px_I")
|
225 |
-
elif emotion == 'surprise':
|
226 |
-
return ("You're feeling surprised. It's okay to feel neutral!",
|
227 |
-
[("Managing Stress", "https://www.health.harvard.edu/health-a-to-z"),
|
228 |
-
("Coping Strategies", "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety")],
|
229 |
-
"Watch on YouTube: https://youtu.be/m1vaUGtyo-A")
|
230 |
-
return ("Could not detect emotion.", [], "")
|
231 |
-
|
232 |
-
# Gradio interface setup for user interaction
|
233 |
-
def user_interface(message, location, history):
|
234 |
-
history, history = chat(message, history)
|
235 |
-
|
236 |
-
# Sentiment analysis
|
237 |
-
inputs = tokenizer_sentiment(message, return_tensors="pt")
|
238 |
-
outputs = model_sentiment(**inputs)
|
239 |
-
sentiment = ["Negative", "Neutral", "Positive"][torch.argmax(outputs.logits, dim=1).item()]
|
240 |
-
|
241 |
-
# Emotion detection
|
242 |
-
emotion_msg, resources, video_link = detect_emotion(message)
|
243 |
-
|
244 |
-
# Get wellness professionals
|
245 |
-
wellness_data = get_wellness_professionals(location, api_key)
|
246 |
-
|
247 |
-
# Display wellness professionals in a table format
|
248 |
-
wellness_df = pd.DataFrame(wellness_data, columns=["Name", "Address", "Latitude", "Longitude"])
|
249 |
-
|
250 |
-
return history, history, sentiment, emotion_msg, resources, video_link, wellness_df.to_html(escape=False)
|
251 |
-
|
252 |
-
# Gradio chatbot interface
|
253 |
-
chatbot = gr.Chatbot(label="Mental Health Chatbot")
|
254 |
-
location_input = gr.Textbox(label="Enter your location (latitude,longitude)", placeholder="e.g., 21.3,-157.8")
|
255 |
|
256 |
-
#
|
257 |
-
|
258 |
-
|
259 |
-
[
|
260 |
-
[chatbot, "state"
|
261 |
allow_flagging="never",
|
262 |
-
|
263 |
)
|
264 |
|
265 |
-
|
266 |
-
if __name__ == "__main__":
|
267 |
-
demo.launch()
|
|
|
98 |
|
99 |
tokenizer_emotion, model_emotion = load_emotion_model()
|
100 |
|
101 |
+
# Emotion detection function with suggestions in plain English
|
102 |
+
def detect_emotion(user_input):
|
103 |
+
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
104 |
+
result = pipe(user_input)
|
105 |
+
emotion = result[0]['label']
|
106 |
+
|
107 |
+
# Provide suggestions based on the detected emotion
|
108 |
+
if emotion == 'joy':
|
109 |
+
emotion_msg = "You're feeling happy! Keep up the great mood!"
|
110 |
+
resources = [
|
111 |
+
{"subject": "Relaxation Techniques", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
|
112 |
+
{"subject": "Dealing with Stress", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
113 |
+
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
|
114 |
+
]
|
115 |
+
video_link = "Watch on YouTube: https://youtu.be/m1vaUGtyo-A"
|
116 |
+
|
117 |
+
elif emotion == 'anger':
|
118 |
+
emotion_msg = "You're feeling angry. It's okay to feel this way. Let's try to calm down."
|
119 |
+
resources = [
|
120 |
+
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
121 |
+
{"subject": "Stress Management Tips", "link": "https://www.health.harvard.edu/health-a-to-z"},
|
122 |
+
{"subject": "Dealing with Anger", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
123 |
+
]
|
124 |
+
video_link = "Watch on YouTube: https://youtu.be/MIc299Flibs"
|
125 |
+
|
126 |
+
elif emotion == 'fear':
|
127 |
+
emotion_msg = "You're feeling fearful. Take a moment to breathe and relax."
|
128 |
+
resources = [
|
129 |
+
{"subject": "Mindfulness Practices", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
|
130 |
+
{"subject": "Coping with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
131 |
+
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
|
132 |
+
]
|
133 |
+
video_link = "Watch on YouTube: https://youtu.be/yGKKz185M5o"
|
134 |
+
|
135 |
+
elif emotion == 'sadness':
|
136 |
+
emotion_msg = "You're feeling sad. It's okay to take a break."
|
137 |
+
resources = [
|
138 |
+
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
139 |
+
{"subject": "Dealing with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
140 |
+
]
|
141 |
+
video_link = "Watch on YouTube: https://youtu.be/-e-4Kx5px_I"
|
142 |
+
|
143 |
+
elif emotion == 'surprise':
|
144 |
+
emotion_msg = "You're feeling surprised. It's okay to feel neutral!"
|
145 |
+
resources = [
|
146 |
+
{"subject": "Managing Stress", "link": "https://www.health.harvard.edu/health-a-to-z"},
|
147 |
+
{"subject": "Coping Strategies", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
148 |
+
]
|
149 |
+
video_link = "Watch on YouTube: https://youtu.be/m1vaUGtyo-A"
|
150 |
+
|
151 |
+
else:
|
152 |
+
emotion_msg = "Could not detect emotion."
|
153 |
+
resources = []
|
154 |
+
video_link = ""
|
155 |
+
|
156 |
+
return emotion_msg, resources, video_link
|
157 |
+
|
158 |
+
|
159 |
# Google Geocoding API setup to convert city name to latitude/longitude
|
160 |
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
161 |
|
|
|
210 |
if response.status_code == 200:
|
211 |
soup = BeautifulSoup(response.text, 'html.parser')
|
212 |
|
213 |
+
# Find the results based on HTML structure
|
214 |
+
# Note: This is a simplistic example, Google search results structure may change
|
215 |
+
result_divs = soup.find_all("div", class_="BVG0Nb")
|
216 |
+
|
217 |
+
results = []
|
218 |
+
for div in result_divs:
|
219 |
+
name = div.get_text()
|
220 |
+
link = div.find("a")["href"]
|
221 |
+
results.append({"name": name, "link": link})
|
222 |
|
223 |
+
return results
|
224 |
else:
|
225 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
+
# Initialize the chatbot interface
|
228 |
+
iface = gr.Interface(
|
229 |
+
fn=chat,
|
230 |
+
inputs=["text", "state"],
|
231 |
+
outputs=["chatbot", "state"],
|
232 |
allow_flagging="never",
|
233 |
+
live=True
|
234 |
)
|
235 |
|
236 |
+
iface.launch(share=True)
|
|
|
|