Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ from nltk.stem.lancaster import LancasterStemmer
|
|
11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
12 |
import requests
|
13 |
import pandas as pd
|
|
|
14 |
|
15 |
# Ensure necessary NLTK resources are downloaded
|
16 |
nltk.download('punkt')
|
@@ -149,11 +150,13 @@ def provide_suggestions(emotion):
|
|
149 |
|
150 |
return suggestions
|
151 |
|
152 |
-
# Google Places API
|
153 |
-
api_key = "
|
|
|
|
|
|
|
154 |
|
155 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
156 |
-
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
157 |
params = {
|
158 |
"query": query,
|
159 |
"location": location,
|
@@ -165,6 +168,23 @@ def get_places_data(query, location, radius, api_key, next_page_token=None):
|
|
165 |
response = requests.get(url, params=params)
|
166 |
return response.json() if response.status_code == 200 else None
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
def get_all_places(query, location, radius, api_key):
|
169 |
all_results = []
|
170 |
next_page_token = None
|
@@ -173,11 +193,13 @@ def get_all_places(query, location, radius, api_key):
|
|
173 |
if data:
|
174 |
results = data.get('results', [])
|
175 |
for place in results:
|
176 |
-
place_id = place.get("place_id")
|
177 |
-
name = place.get("name")
|
178 |
-
address = place.get("formatted_address")
|
179 |
website = place.get("website", "Not available")
|
180 |
-
|
|
|
|
|
181 |
next_page_token = data.get('next_page_token')
|
182 |
if not next_page_token:
|
183 |
break
|
@@ -188,12 +210,16 @@ def get_all_places(query, location, radius, api_key):
|
|
188 |
def search_wellness_professionals(location):
|
189 |
query = "therapist OR counselor OR mental health professional"
|
190 |
radius = 50000
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
197 |
|
198 |
# Gradio Interface
|
199 |
def gradio_interface(message, location, state):
|
@@ -210,7 +236,10 @@ def gradio_interface(message, location, state):
|
|
210 |
suggestions = provide_suggestions(emotion)
|
211 |
|
212 |
# Stage 4: Search for Wellness Professionals
|
213 |
-
|
|
|
|
|
|
|
214 |
|
215 |
# Return the results in a tabular form within the Gradio interface
|
216 |
return history, sentiment, emotion, suggestions, wellness_results, history # Last 'history' is for state
|
@@ -236,5 +265,9 @@ iface = gr.Interface(
|
|
236 |
description="This chatbot provides mental health support with sentiment analysis, emotion detection, suggestions, and a list of nearby wellness professionals."
|
237 |
)
|
238 |
|
|
|
|
|
|
|
|
|
239 |
# Launch the interface
|
240 |
iface.launch(debug=True, share=True)
|
|
|
11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
12 |
import requests
|
13 |
import pandas as pd
|
14 |
+
import os
|
15 |
|
16 |
# Ensure necessary NLTK resources are downloaded
|
17 |
nltk.download('punkt')
|
|
|
150 |
|
151 |
return suggestions
|
152 |
|
153 |
+
# Google Places API integration
|
154 |
+
api_key = os.environ.get("GOOGLE_API_KEY") # Get API key from environment variable
|
155 |
+
|
156 |
+
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
157 |
+
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
|
158 |
|
159 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
|
|
160 |
params = {
|
161 |
"query": query,
|
162 |
"location": location,
|
|
|
168 |
response = requests.get(url, params=params)
|
169 |
return response.json() if response.status_code == 200 else None
|
170 |
|
171 |
+
def get_place_details(place_id, api_key):
|
172 |
+
params = {
|
173 |
+
"place_id": place_id,
|
174 |
+
"key": api_key
|
175 |
+
}
|
176 |
+
response = requests.get(places_details_url, params=params)
|
177 |
+
if response.status_code == 200:
|
178 |
+
details_data = response.json().get("result", {})
|
179 |
+
return {
|
180 |
+
"opening_hours": details_data.get("opening_hours", {}).get("weekday_text", "Not available"),
|
181 |
+
"reviews": details_data.get("reviews", "Not available"),
|
182 |
+
"phone_number": details_data.get("formatted_phone_number", "Not available"),
|
183 |
+
"website": details_data.get("website", "Not available")
|
184 |
+
}
|
185 |
+
else:
|
186 |
+
return {}
|
187 |
+
|
188 |
def get_all_places(query, location, radius, api_key):
|
189 |
all_results = []
|
190 |
next_page_token = None
|
|
|
193 |
if data:
|
194 |
results = data.get('results', [])
|
195 |
for place in results:
|
196 |
+
place_id = place.get("place_id", "N/A") # Handle missing place_id
|
197 |
+
name = place.get("name", "N/A") # Handle missing name
|
198 |
+
address = place.get("formatted_address", "N/A") # Handle missing address
|
199 |
website = place.get("website", "Not available")
|
200 |
+
details = get_place_details(place_id, api_key) if place_id != "N/A" else {} #Avoid error if place_id is missing
|
201 |
+
phone_number = details.get("phone_number", "Not available")
|
202 |
+
all_results.append([name, address, phone_number, website])
|
203 |
next_page_token = data.get('next_page_token')
|
204 |
if not next_page_token:
|
205 |
break
|
|
|
210 |
def search_wellness_professionals(location):
|
211 |
query = "therapist OR counselor OR mental health professional"
|
212 |
radius = 50000
|
213 |
+
try:
|
214 |
+
google_places_data = get_all_places(query, location, radius, api_key)
|
215 |
+
if google_places_data:
|
216 |
+
df = pd.DataFrame(google_places_data, columns=["Name", "Address", "Phone", "Website"])
|
217 |
+
return df
|
218 |
+
else:
|
219 |
+
return pd.DataFrame([["No data found.", "", "", ""]], columns=["Name", "Address", "Phone", "Website"])
|
220 |
+
except Exception as e:
|
221 |
+
return pd.DataFrame([["Error fetching data: " + str(e), "", "", ""]], columns=["Name", "Address", "Phone", "Website"])
|
222 |
+
|
223 |
|
224 |
# Gradio Interface
|
225 |
def gradio_interface(message, location, state):
|
|
|
236 |
suggestions = provide_suggestions(emotion)
|
237 |
|
238 |
# Stage 4: Search for Wellness Professionals
|
239 |
+
try:
|
240 |
+
wellness_results = search_wellness_professionals(location)
|
241 |
+
except Exception as e:
|
242 |
+
wellness_results = pd.DataFrame([["Error: " + str(e), "", "", ""]], columns=["Name", "Address", "Phone", "Website"])
|
243 |
|
244 |
# Return the results in a tabular form within the Gradio interface
|
245 |
return history, sentiment, emotion, suggestions, wellness_results, history # Last 'history' is for state
|
|
|
265 |
description="This chatbot provides mental health support with sentiment analysis, emotion detection, suggestions, and a list of nearby wellness professionals."
|
266 |
)
|
267 |
|
268 |
+
# Check for API key; if not found, print an error message
|
269 |
+
if api_key is None:
|
270 |
+
print("Error: GOOGLE_MAPS_API_KEY environment variable not set. Please set this environment variable with your Google Maps API key.")
|
271 |
+
|
272 |
# Launch the interface
|
273 |
iface.launch(debug=True, share=True)
|