Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipe
|
|
13 |
import torch
|
14 |
import tensorflow as tf
|
15 |
import tflearn
|
16 |
-
import
|
17 |
from bs4 import BeautifulSoup
|
18 |
import re # Added for regex operations
|
19 |
|
@@ -52,7 +52,13 @@ tokenizer, emotion_model = load_model()
|
|
52 |
|
53 |
# Google Places API query function
|
54 |
def get_places_data(query, location, radius=5000, api_key="GOOGLE_API_KEY"):
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
params = {
|
57 |
"query": query,
|
58 |
"location": f"{latitude},{longitude}",
|
@@ -144,54 +150,26 @@ def scrape_website_for_contact_info(website):
|
|
144 |
return phone_number, email
|
145 |
|
146 |
# Main Gradio interface for emotion detection and chatbot
|
147 |
-
def emotion_and_chatbot(user_input, history, query, location):
|
148 |
-
#
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
"Dealing with Stress": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
168 |
-
"Emotional Wellness Toolkit": "https://www.nih.gov/health-information/emotional-wellness-toolkit",
|
169 |
-
"Stress Management Tips": "https://www.health.harvard.edu/health-a-to-z",
|
170 |
-
"Dealing with Anger": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
171 |
-
"Mindfulness Practices": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation",
|
172 |
-
"Coping with Anxiety": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
173 |
-
"Managing Stress": "https://www.health.harvard.edu/health-a-to-z",
|
174 |
-
"Coping Strategies": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
|
175 |
-
}
|
176 |
-
|
177 |
-
st.write("Useful Resources:")
|
178 |
-
for resource in resources:
|
179 |
-
st.markdown(f"[{resource}]({links[resource]})")
|
180 |
-
|
181 |
-
st.write("Relaxation Videos:")
|
182 |
-
st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
|
183 |
-
|
184 |
-
# Search Places (for wellness or other queries)
|
185 |
-
places_data = get_places_data(query, location)
|
186 |
-
places_df = pd.DataFrame(places_data)
|
187 |
-
|
188 |
-
# Tabular output for places
|
189 |
-
places_table = places_df[['name', 'vicinity', 'geometry']].head(10).to_html(classes='table table-bordered') if not places_df.empty else "No places found."
|
190 |
-
|
191 |
-
# Generate Map
|
192 |
-
places_map = create_map(places_data) if places_data else "No places found."
|
193 |
-
|
194 |
-
# Chatbot response
|
195 |
history, _ = chatbot(user_input, history)
|
196 |
|
197 |
return emotion_response, places_map, places_table, history, history
|
@@ -203,7 +181,9 @@ iface = gr.Interface(
|
|
203 |
gr.Textbox(label="Enter your message", placeholder="How are you feeling?"),
|
204 |
"state", # Chat history
|
205 |
gr.Textbox(label="Search Query (e.g. wellness)", placeholder="e.g. therapist"),
|
206 |
-
gr.Textbox(label="Location (
|
|
|
|
|
207 |
],
|
208 |
outputs=[
|
209 |
gr.Textbox(label="Emotion and Sentiment"),
|
|
|
13 |
import torch
|
14 |
import tensorflow as tf
|
15 |
import tflearn
|
16 |
+
from geopy.geocoders import Nominatim
|
17 |
from bs4 import BeautifulSoup
|
18 |
import re # Added for regex operations
|
19 |
|
|
|
52 |
|
53 |
# Google Places API query function
|
54 |
def get_places_data(query, location, radius=5000, api_key="GOOGLE_API_KEY"):
|
55 |
+
# Use geopy to convert location name to coordinates
|
56 |
+
geolocator = Nominatim(user_agent="place_search")
|
57 |
+
location_obj = geolocator.geocode(location)
|
58 |
+
if location_obj is None:
|
59 |
+
return []
|
60 |
+
|
61 |
+
latitude, longitude = location_obj.latitude, location_obj.longitude
|
62 |
params = {
|
63 |
"query": query,
|
64 |
"location": f"{latitude},{longitude}",
|
|
|
150 |
return phone_number, email
|
151 |
|
152 |
# Main Gradio interface for emotion detection and chatbot
|
153 |
+
def emotion_and_chatbot(user_input, history, query, location, emotion_button, search_button):
|
154 |
+
# Handle emotion detection
|
155 |
+
if emotion_button:
|
156 |
+
emotion = detect_emotion(user_input)
|
157 |
+
sentiment = analyze_sentiment(user_input)
|
158 |
+
emotion_response = f"Emotion Detected: {emotion}. Sentiment: {sentiment}"
|
159 |
+
else:
|
160 |
+
emotion_response = "Click on 'Detect Emotion' to analyze your input."
|
161 |
+
|
162 |
+
# Handle place search (therapists or wellness centers)
|
163 |
+
if search_button:
|
164 |
+
places_data = get_places_data(query, location)
|
165 |
+
places_df = pd.DataFrame(places_data)
|
166 |
+
places_table = places_df[['name', 'vicinity', 'geometry']].head(10).to_html(classes='table table-bordered') if not places_df.empty else "No places found."
|
167 |
+
places_map = create_map(places_data) if places_data else "No places found."
|
168 |
+
else:
|
169 |
+
places_table = "Click on 'Search for Therapists' to search."
|
170 |
+
places_map = ""
|
171 |
+
|
172 |
+
# Handle chatbot conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
history, _ = chatbot(user_input, history)
|
174 |
|
175 |
return emotion_response, places_map, places_table, history, history
|
|
|
181 |
gr.Textbox(label="Enter your message", placeholder="How are you feeling?"),
|
182 |
"state", # Chat history
|
183 |
gr.Textbox(label="Search Query (e.g. wellness)", placeholder="e.g. therapist"),
|
184 |
+
gr.Textbox(label="Location (e.g. Lahore, Hawaii, Allama Iqbal Town)", placeholder="e.g. Lahore, Allama Iqbal Town"),
|
185 |
+
gr.Button("Detect Emotion"),
|
186 |
+
gr.Button("Search for Therapists")
|
187 |
],
|
188 |
outputs=[
|
189 |
gr.Textbox(label="Emotion and Sentiment"),
|