Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ import time
|
|
| 7 |
import re
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
import pandas as pd
|
| 10 |
-
from selenium import webdriver
|
| 11 |
-
from selenium.webdriver.chrome.options import Options
|
| 12 |
import chromedriver_autoinstaller
|
| 13 |
import os
|
| 14 |
import nltk
|
|
@@ -20,7 +18,6 @@ import json
|
|
| 20 |
import pickle
|
| 21 |
from nltk.tokenize import word_tokenize
|
| 22 |
from nltk.stem.lancaster import LancasterStemmer
|
| 23 |
-
import subprocess
|
| 24 |
|
| 25 |
# Ensure necessary NLTK resources are downloaded
|
| 26 |
nltk.download('punkt')
|
|
@@ -111,40 +108,6 @@ query = "therapist OR counselor OR mental health professional OR marriage and fa
|
|
| 111 |
location = "21.3,-157.8" # Center of Hawaii (Oahu)
|
| 112 |
radius = 50000 # 50 km radius
|
| 113 |
|
| 114 |
-
# Install Chrome and Chromedriver
|
| 115 |
-
def install_chrome_and_driver():
|
| 116 |
-
# Install Chrome (if not already installed)
|
| 117 |
-
os.system("apt-get update && apt-get install -y wget curl sudo")
|
| 118 |
-
os.system("wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb")
|
| 119 |
-
os.system("sudo dpkg -i google-chrome-stable_current_amd64.deb")
|
| 120 |
-
os.system("sudo apt-get install -y -f")
|
| 121 |
-
os.system("google-chrome-stable --version")
|
| 122 |
-
|
| 123 |
-
# Fix ownership of /etc/sudo.conf
|
| 124 |
-
os.system("sudo chown root:root /etc/sudo.conf")
|
| 125 |
-
|
| 126 |
-
# Verify Chrome installation
|
| 127 |
-
os.system("which google-chrome-stable")
|
| 128 |
-
if not os.path.exists("/usr/bin/google-chrome-stable"):
|
| 129 |
-
raise RuntimeError("Google Chrome was not installed correctly")
|
| 130 |
-
|
| 131 |
-
# Check if CUDA libraries are available and install them if present
|
| 132 |
-
try:
|
| 133 |
-
os.system("apt-get install -y cuda")
|
| 134 |
-
os.system("apt-get install -y libcudart.so.11.0")
|
| 135 |
-
except subprocess.CalledProcessError:
|
| 136 |
-
print("CUDA libraries not found or installation failed. Proceeding without GPU support.")
|
| 137 |
-
|
| 138 |
-
# Install Chromedriver (if not already installed)
|
| 139 |
-
chromedriver_autoinstaller.install()
|
| 140 |
-
|
| 141 |
-
# Verify Chromedriver installation
|
| 142 |
-
os.system("which chromedriver")
|
| 143 |
-
if not os.path.exists("/usr/local/bin/chromedriver"):
|
| 144 |
-
raise RuntimeError("ChromeDriver was not installed correctly")
|
| 145 |
-
|
| 146 |
-
install_chrome_and_driver()
|
| 147 |
-
|
| 148 |
# Function to send a request to Google Places API and fetch places data
|
| 149 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
| 150 |
params = {
|
|
@@ -184,8 +147,8 @@ def get_place_details(place_id, api_key):
|
|
| 184 |
else:
|
| 185 |
return {}
|
| 186 |
|
| 187 |
-
# Scrape website URL from Google Maps results (using Selenium)
|
| 188 |
-
def
|
| 189 |
chrome_options = Options()
|
| 190 |
chrome_options.add_argument("--headless")
|
| 191 |
chrome_options.add_argument("--no-sandbox")
|
|
@@ -206,7 +169,7 @@ def scrape_website_from_google_maps(place_name):
|
|
| 206 |
return website_url
|
| 207 |
|
| 208 |
# Scraping the website to extract phone number or email
|
| 209 |
-
def
|
| 210 |
phone_number = "Not available"
|
| 211 |
email = "Not available"
|
| 212 |
|
|
@@ -244,7 +207,7 @@ def get_all_places(query, location, radius, api_key):
|
|
| 244 |
address = place.get("formatted_address")
|
| 245 |
rating = place.get("rating", "Not available")
|
| 246 |
business_status = place.get("business_status", "Not available")
|
| 247 |
-
user_ratings_total = place.get("
|
| 248 |
website = place.get("website", "Not available")
|
| 249 |
types = ", ".join(place.get("types", []))
|
| 250 |
location = place.get("geometry", {}).get("location", {})
|
|
@@ -254,15 +217,15 @@ def get_all_places(query, location, radius, api_key):
|
|
| 254 |
details = get_place_details(place_id, api_key)
|
| 255 |
phone_number = details.get("phone_number", "Not available")
|
| 256 |
if phone_number == "Not available" and website != "Not available":
|
| 257 |
-
phone_number, email =
|
| 258 |
else:
|
| 259 |
email = "Not available"
|
| 260 |
|
| 261 |
if website == "Not available":
|
| 262 |
-
website =
|
| 263 |
|
| 264 |
all_results.append([name, address, phone_number, rating, business_status,
|
| 265 |
-
|
| 266 |
details.get("opening_hours", "Not available"),
|
| 267 |
details.get("reviews", "Not available"), email])
|
| 268 |
|
|
@@ -282,7 +245,7 @@ def save_to_csv(data, filename):
|
|
| 282 |
writer = csv.writer(file)
|
| 283 |
writer.writerow([
|
| 284 |
"Name", "Address", "Phone", "Rating", "Business Status",
|
| 285 |
-
"User
|
| 286 |
"Opening Hours", "Reviews", "Email"
|
| 287 |
])
|
| 288 |
writer.writerows(data)
|
|
@@ -306,7 +269,7 @@ with gr.Blocks() as demo:
|
|
| 306 |
|
| 307 |
# Model prediction for emotion detection
|
| 308 |
def predict_emotion(text):
|
| 309 |
-
pipe = pipeline("text-classification", model=model, tokenizer=
|
| 310 |
result = pipe(text)
|
| 311 |
emotion = result[0]['label']
|
| 312 |
return emotion
|
|
@@ -318,13 +281,13 @@ with gr.Blocks() as demo:
|
|
| 318 |
if emotion == 'joy':
|
| 319 |
return "You're feeling happy! Keep up the great mood!\nUseful Resources:\n[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)\n[Dealing with Stress](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)"
|
| 320 |
elif emotion == 'anger':
|
| 321 |
-
return "You're feeling angry. It's okay to feel this way. Let's try to calm down.\nUseful Resources:\n[Emotional Wellness
|
| 322 |
elif emotion == 'fear':
|
| 323 |
-
return "You're feeling fearful. Take a moment to breathe and relax.\nUseful Resources:\n[Mindfulness
|
| 324 |
elif emotion == 'sadness':
|
| 325 |
-
return "You're feeling sad. It's okay to take a break.\nUseful Resources:\n[Emotional
|
| 326 |
elif emotion == 'surprise':
|
| 327 |
-
return "You're feeling surprised. It's okay to feel neutral!\nUseful Resources:\n[Managing
|
| 328 |
|
| 329 |
emotion_output = gr.Textbox(label="Emotion Detected")
|
| 330 |
emotion_output.change(show_suggestions, inputs=emotion_output, outputs=gr.Textbox(label="Suggestions"))
|
|
@@ -347,10 +310,12 @@ with gr.Blocks() as demo:
|
|
| 347 |
message = message.lower()
|
| 348 |
|
| 349 |
try:
|
|
|
|
| 350 |
results = model.predict([bag_of_words(message, words)])
|
| 351 |
results_index = np.argmax(results)
|
| 352 |
tag = labels[results_index]
|
| 353 |
|
|
|
|
| 354 |
for tg in data["intents"]:
|
| 355 |
if tg['tag'] == tag:
|
| 356 |
responses = tg['responses']
|
|
@@ -369,7 +334,7 @@ with gr.Blocks() as demo:
|
|
| 369 |
|
| 370 |
# User input for text (sentiment analysis)
|
| 371 |
user_input_sentiment = gr.Textbox(lines=1, label="Enter text to analyze sentiment:")
|
| 372 |
-
|
| 373 |
# Prediction button for sentiment analysis
|
| 374 |
def predict_sentiment(text):
|
| 375 |
inputs = tokenizer_sentiment(text, return_tensors="pt")
|
|
@@ -383,13 +348,13 @@ with gr.Blocks() as demo:
|
|
| 383 |
user_input_sentiment.change(predict_sentiment, inputs=user_input_sentiment, outputs=sentiment_output)
|
| 384 |
|
| 385 |
# Button to fetch wellness professionals data
|
| 386 |
-
fetch_button = gr.Button("Fetch Wellness
|
| 387 |
-
data_output = gr.Dataframe(headers=["Name", "Address", "Phone", "Rating", "Business Status", "User
|
| 388 |
|
| 389 |
def fetch_data():
|
| 390 |
all_results = get_all_places(query, location, radius, api_key)
|
| 391 |
if all_results:
|
| 392 |
-
return pd.DataFrame(all_results, columns=["Name", "Address", "Phone", "Rating", "Business Status", "User
|
| 393 |
else:
|
| 394 |
return "No data found."
|
| 395 |
|
|
|
|
| 7 |
import re
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
import pandas as pd
|
|
|
|
|
|
|
| 10 |
import chromedriver_autoinstaller
|
| 11 |
import os
|
| 12 |
import nltk
|
|
|
|
| 18 |
import pickle
|
| 19 |
from nltk.tokenize import word_tokenize
|
| 20 |
from nltk.stem.lancaster import LancasterStemmer
|
|
|
|
| 21 |
|
| 22 |
# Ensure necessary NLTK resources are downloaded
|
| 23 |
nltk.download('punkt')
|
|
|
|
| 108 |
location = "21.3,-157.8" # Center of Hawaii (Oahu)
|
| 109 |
radius = 50000 # 50 km radius
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# Function to send a request to Google Places API and fetch places data
|
| 112 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
| 113 |
params = {
|
|
|
|
| 147 |
else:
|
| 148 |
return {}
|
| 149 |
|
| 150 |
+
# Scrape website URL from Google Maps results (using Selenium WebDriver)
|
| 151 |
+
def scrape_div_from_google_maps(place_name):
|
| 152 |
chrome_options = Options()
|
| 153 |
chrome_options.add_argument("--headless")
|
| 154 |
chrome_options.add_argument("--no-sandbox")
|
|
|
|
| 169 |
return website_url
|
| 170 |
|
| 171 |
# Scraping the website to extract phone number or email
|
| 172 |
+
def scrape_div_for_contact_info(website):
|
| 173 |
phone_number = "Not available"
|
| 174 |
email = "Not available"
|
| 175 |
|
|
|
|
| 207 |
address = place.get("formatted_address")
|
| 208 |
rating = place.get("rating", "Not available")
|
| 209 |
business_status = place.get("business_status", "Not available")
|
| 210 |
+
user_ratings_total = place.get("user_reviews_total", "Not available")
|
| 211 |
website = place.get("website", "Not available")
|
| 212 |
types = ", ".join(place.get("types", []))
|
| 213 |
location = place.get("geometry", {}).get("location", {})
|
|
|
|
| 217 |
details = get_place_details(place_id, api_key)
|
| 218 |
phone_number = details.get("phone_number", "Not available")
|
| 219 |
if phone_number == "Not available" and website != "Not available":
|
| 220 |
+
phone_number, email = scrape_div_for_contact_info(website)
|
| 221 |
else:
|
| 222 |
email = "Not available"
|
| 223 |
|
| 224 |
if website == "Not available":
|
| 225 |
+
website = scrape_div_from_google_maps(name)
|
| 226 |
|
| 227 |
all_results.append([name, address, phone_number, rating, business_status,
|
| 228 |
+
user_div_total, website, types, latitude, longitude,
|
| 229 |
details.get("opening_hours", "Not available"),
|
| 230 |
details.get("reviews", "Not available"), email])
|
| 231 |
|
|
|
|
| 245 |
writer = csv.writer(file)
|
| 246 |
writer.writerow([
|
| 247 |
"Name", "Address", "Phone", "Rating", "Business Status",
|
| 248 |
+
"User Reviews Total", "Website", "Types", "Latitude", "Longitude",
|
| 249 |
"Opening Hours", "Reviews", "Email"
|
| 250 |
])
|
| 251 |
writer.writerows(data)
|
|
|
|
| 269 |
|
| 270 |
# Model prediction for emotion detection
|
| 271 |
def predict_emotion(text):
|
| 272 |
+
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer_sentiment)
|
| 273 |
result = pipe(text)
|
| 274 |
emotion = result[0]['label']
|
| 275 |
return emotion
|
|
|
|
| 281 |
if emotion == 'joy':
|
| 282 |
return "You're feeling happy! Keep up the great mood!\nUseful Resources:\n[Relaxation Techniques](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)\n[Dealing with Stress](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n[Emotional Wellness Toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)"
|
| 283 |
elif emotion == 'anger':
|
| 284 |
+
return "You're feeling angry. It's okay to feel this way. Let's try to calm down.\nUseful Resources:\n[Emotional Wellness toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)\n[Stress management tips](https://www.health.harvard.edu/health-a-to-z)\n[Dealing with anger](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/MIc299Flibs)"
|
| 285 |
elif emotion == 'fear':
|
| 286 |
+
return "You're feeling fearful. Take a moment to breathe and relax.\nUseful Resources:\n[Mindfulness practices](https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation)\n[Coping with anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n[Emotional wellness toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/yGKKz185M5o)"
|
| 287 |
elif emotion == 'sadness':
|
| 288 |
+
return "You're feeling sad. It's okay to take a break.\nUseful Resources:\n[Emotional wellness toolkit](https://www.nih.gov/health-information/emotional-wellness-toolkit)\n[Dealing with anxiety](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/-e-4Kx5px_I)"
|
| 289 |
elif emotion == 'surprise':
|
| 290 |
+
return "You're feeling surprised. It's okay to feel neutral!\nUseful Resources:\n[Managing stress](https://www.health.harvard.edu/health-a-to-z)\n[Coping strategies](https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety)\n\nRelaxation Videos:\n[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)"
|
| 291 |
|
| 292 |
emotion_output = gr.Textbox(label="Emotion Detected")
|
| 293 |
emotion_output.change(show_suggestions, inputs=emotion_output, outputs=gr.Textbox(label="Suggestions"))
|
|
|
|
| 310 |
message = message.lower()
|
| 311 |
|
| 312 |
try:
|
| 313 |
+
# Predict the tag
|
| 314 |
results = model.predict([bag_of_words(message, words)])
|
| 315 |
results_index = np.argmax(results)
|
| 316 |
tag = labels[results_index]
|
| 317 |
|
| 318 |
+
# Match tag with intent and choose a random response
|
| 319 |
for tg in data["intents"]:
|
| 320 |
if tg['tag'] == tag:
|
| 321 |
responses = tg['responses']
|
|
|
|
| 334 |
|
| 335 |
# User input for text (sentiment analysis)
|
| 336 |
user_input_sentiment = gr.Textbox(lines=1, label="Enter text to analyze sentiment:")
|
| 337 |
+
|
| 338 |
# Prediction button for sentiment analysis
|
| 339 |
def predict_sentiment(text):
|
| 340 |
inputs = tokenizer_sentiment(text, return_tensors="pt")
|
|
|
|
| 348 |
user_input_sentiment.change(predict_sentiment, inputs=user_input_sentiment, outputs=sentiment_output)
|
| 349 |
|
| 350 |
# Button to fetch wellness professionals data
|
| 351 |
+
fetch_button = gr.Button("Fetch Wellness professionals data")
|
| 352 |
+
data_output = gr.Dataframe(headers=["Name", "Address", "Phone", "Rating", "Business Status", "User Reviews Total", "Website", "Types", "Latitude", "Longitude", "Opening Hours", "Reviews", "Email"])
|
| 353 |
|
| 354 |
def fetch_data():
|
| 355 |
all_results = get_all_places(query, location, radius, api_key)
|
| 356 |
if all_results:
|
| 357 |
+
return pd.DataFrame(all_results, columns=["Name", "Address", "Phone", "Rating", "Business Status", "User Reviews Total", "Website", "Types", "Latitude", "Longitude", "Opening Hours", "Reviews", "Email"])
|
| 358 |
else:
|
| 359 |
return "No data found."
|
| 360 |
|