import gradio as gr from transformers import AutoTokenizer, AutoModelForSequenceClassification from transformers import pipeline import requests import csv import time import re from bs4 import BeautifulSoup import pandas as pd from selenium import webdriver from selenium.webdriver.chrome.options import Options import chromedriver_autoinstaller import os import nltk import numpy as np import tflearn import tensorflow as tf import random import json import pickle from nltk.tokenize import word_tokenize from nltk.stem.lancaster import LancasterStemmer import subprocess # Ensure necessary NLTK resources are downloaded nltk.download('punkt') # Initialize the stemmer stemmer = LancasterStemmer() # Load intents.json try: with open("intents.json") as file: data = json.load(file) except FileNotFoundError: raise FileNotFoundError("Error: 'intents.json' file not found. Ensure it exists in the current directory.") # Load preprocessed data from pickle try: with open("data.pickle", "rb") as f: words, labels, training, output = pickle.load(f) except FileNotFoundError: raise FileNotFoundError("Error: 'data.pickle' file not found. Ensure it exists and matches the model.") # Build the model structure net = tflearn.input_data(shape=[None, len(training[0])]) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, 8) net = tflearn.fully_connected(net, len(output[0]), activation="softmax") net = tflearn.regression(net) # Load the trained model model = tflearn.DNN(net) try: model.load("MentalHealthChatBotmodel.tflearn") except FileNotFoundError: raise FileNotFoundError("Error: Trained model file 'MentalHealthChatBotmodel.tflearn' not found.") # Function to process user input into a bag-of-words format def bag_of_words(s, words): bag = [0 for _ in range(len(words))] s_words = word_tokenize(s) s_words = [stemmer.stem(word.lower()) for word in s_words if word.lower() in words] for se in s_words: for i, w in enumerate(words): if w == se: bag[i] = 1 return np.array(bag) # Chat function def chat(message, history): history = history or [] message = message.lower() try: # Predict the tag results = model.predict([bag_of_words(message, words)]) results_index = np.argmax(results) tag = labels[results_index] # Match tag with intent and choose a random response for tg in data["intents"]: if tg['tag'] == tag: responses = tg['responses'] response = random.choice(responses) break else: response = "I'm sorry, I didn't understand that. Could you please rephrase?" except Exception as e: response = f"An error occurred: {str(e)}" history.append((message, response)) return history, history # Load tokenizer and model for sentiment analysis tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") # Google Places API endpoint url = "https://maps.googleapis.com/maps/api/place/textsearch/json" places_details_url = "https://maps.googleapis.com/maps/api/place/details/json" # Your actual Google API Key (replace with your key) api_key = "AIzaSyCcfJzMFfuv_1LN7JPTJJYw_aS0A_SLeW0" # Replace with your own Google API key # Search query for wellness professionals in Hawaii 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 in Hawaii" # Approximate latitude and longitude for Hawaii (e.g., Oahu) location = "21.3,-157.8" # Center of Hawaii (Oahu) radius = 50000 # 50 km radius # Install Chrome and Chromedriver def install_chrome_and_driver(): # Install Chrome (if not already installed) os.system("apt-get update && apt-get install -y wget curl sudo") os.system("wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb") os.system("sudo dpkg -i google-chrome-stable_current_amd64.deb") os.system("sudo apt-get install -y -f") os.system("google-chrome-stable --version") # Fix ownership of /etc/sudo.conf os.system("sudo chown root:root /etc/sudo.conf") # Verify Chrome installation os.system("which google-chrome-stable") if not os.path.exists("/usr/bin/google-chrome-stable"): raise RuntimeError("Google Chrome was not installed correctly") # Check if CUDA libraries are available and install them if present try: os.system("apt-get install -y cuda") os.system("apt-get install -y libcudart.so.11.0") except subprocess.CalledProcessError: print("CUDA libraries not found or installation failed. Proceeding without GPU support.") # Install Chromedriver (if not already installed) chromedriver_autoinstaller.install() # Verify Chromedriver installation os.system("which chromedriver") if not os.path.exists("/usr/local/bin/chromedriver"): raise RuntimeError("ChromeDriver was not installed correctly") install_chrome_and_driver() # Function to send a request to Google Places API and fetch places data def get_places_data(query, location, radius, api_key, next_page_token=None): params = { "query": query, "location": location, "radius": radius, "key": api_key } if next_page_token: params["pagetoken"] = next_page_token response = requests.get(url, params=params) if response.status_code == 200: return response.json() else: return None # Function to fetch detailed information for a specific place using its place_id def get_place_details(place_id, api_key): details_url = places_details_url params = { "place_id": place_id, "key": api_key } response = requests.get(details_url, params=params) if response.status_code == 200: details_data = response.json().get("result", {}) return { "opening_hours": details_data.get("opening_hours", {}).get("weekday_text", "Not available"), "reviews": details_data.get("reviews", "Not available"), "phone_number": details_data.get("formatted_phone_number", "Not available"), "website": details_data.get("website", "Not available") } else: return {} # Scrape website URL from Google Maps results (using Selenium) def scrape_website_from_google_maps(place_name): chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(options=chrome_options) search_url = f"https://www.google.com/maps/search/{place_name.replace(' ', '+')}" driver.get(search_url) time.sleep(5) try: website_element = driver.find_element_by_xpath('//a[contains(@aria-label, "Visit") and contains(@aria-label, "website")]') website_url = website_element.get_attribute('href') except: website_url = "Not available" driver.quit() return website_url # Scraping the website to extract phone number or email def scrape_website_for_contact_info(website): phone_number = "Not available" email = "Not available" try: response = requests.get(website, timeout=5) soup = BeautifulSoup(response.content, 'html.parser') phone_match = re.search(r'\(?\+?[0-9]*\)?[0-9_\- \(\)]*', soup.get_text()) if phone_match: phone_number = phone_match.group() email_match = re.search(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', soup.get_text()) if email_match: email = email_match.group() except Exception as e: print(f"Error scraping website {website}: {e}") return phone_number, email # Function to fetch all places data including pagination def get_all_places(query, location, radius, api_key): all_results = [] next_page_token = None while True: data = get_places_data(query, location, radius, api_key, next_page_token) if data: results = data.get('results', []) if not results: break for place in results: place_id = place.get("place_id") name = place.get("name") address = place.get("formatted_address") rating = place.get("rating", "Not available") business_status = place.get("business_status", "Not available") user_ratings_total = place.get("user_ratings_total", "Not available") website = place.get("website", "Not available") types = ", ".join(place.get("types", [])) location = place.get("geometry", {}).get("location", {}) latitude = location.get("lat", "Not available") longitude = location.get("lng", "Not available") details = get_place_details(place_id, api_key) phone_number = details.get("phone_number", "Not available") if phone_number == "Not available" and website != "Not available": phone_number, email = scrape_website_for_contact_info(website) else: email = "Not available" if website == "Not available": website = scrape_website_from_google_maps(name) all_results.append([name, address, phone_number, rating, business_status, user_ratings_total, website, types, latitude, longitude, details.get("opening_hours", "Not available"), details.get("reviews", "Not available"), email]) next_page_token = data.get('next_page_token') if not next_page_token: break time.sleep(2) else: break return all_results # Function to save results to CSV file def save_to_csv(data, filename): with open(filename, mode='w', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow([ "Name", "Address", "Phone", "Rating", "Business Status", "User Ratings Total", "Website", "Types", "Latitude", "Longitude", "Opening Hours", "Reviews", "Email" ]) writer.writerows(data) print(f"Data saved to {filename}") # Main function to execute script def main(): google_places_data = get_all_places(query, location, radius, api_key) if google_places_data: save_to_csv(google_places_data, "wellness_professionals_hawaii.csv") else: print("No data found.") # Gradio UI setup with gr.Blocks() as demo: # Display header gr.Markdown("# Emotion Detection and Well-Being Suggestions") # User input for text (emotion detection) user_input_emotion = gr.Textbox(lines=1, label="How are you feeling today?") # Model prediction for emotion detection def predict_emotion(text): pipe = pipeline("text-classification", model=model, tokenizer=tokenizer) result = pipe(text) emotion = result[0]['label'] return emotion user_input_emotion.change(predict_emotion, inputs=user_input_emotion, outputs=gr.Textbox(label="Emotion Detected")) # Provide suggestions based on the detected emotion def show_suggestions(emotion): if emotion == 'joy': 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)" elif emotion == 'anger': 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)" elif emotion == 'fear': 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)" elif emotion == 'sadness': 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)" elif emotion == 'surprise': 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)" emotion_output = gr.Textbox(label="Emotion Detected") emotion_output.change(show_suggestions, inputs=emotion_output, outputs=gr.Textbox(label="Suggestions")) # Button for summary def show_summary(emotion): return f"Emotion Detected: {emotion}\nUseful Resources based on your mood:\n{show_suggestions(emotion)}" summary_button = gr.Button("Show Summary") summary_output = gr.Textbox(label="Summary") summary_button.click(show_summary, inputs=emotion_output, outputs=summary_output) # Chatbot functionality chatbot = gr.Chatbot(label="Chat") message_input = gr.Textbox(lines=1, label="Message") history_state = gr.State([]) def chat(message, history): history = history or [] message = message.lower() try: results = model.predict([bag_of_words(message, words)]) results_index = np.argmax(results) tag = labels[results_index] for tg in data["intents"]: if tg['tag'] == tag: responses = tg['responses'] response = random.choice(responses) break else: response = "I'm sorry, I didn't understand that. Could you please rephrase?" except Exception as e: response = f"An error occurred: {str(e)}" history.append((message, response)) return history, history message_input.submit(chat, inputs=[message_input, history_state], outputs=[chatbot, history_state]) # User input for text (sentiment analysis) user_input_sentiment = gr.Textbox(lines=1, label="Enter text to analyze sentiment:") # Prediction button for sentiment analysis def predict_sentiment(text): inputs = tokenizer_sentiment(text, return_tensors="pt") with torch.no_grad(): outputs = model_sentiment(**inputs) predicted_class = torch.argmax(outputs.logits, dim=1).item() sentiment = ["Negative", "Neutral", "Positive"][predicted_class] return sentiment sentiment_output = gr.Textbox(label="Predicted Sentiment") user_input_sentiment.change(predict_sentiment, inputs=user_input_sentiment, outputs=sentiment_output) # Button to fetch wellness professionals data fetch_button = gr.Button("Fetch Wellness Professionals Data") data_output = gr.Dataframe(headers=["Name", "Address", "Phone", "Rating", "Business Status", "User Ratings Total", "Website", "Types", "Latitude", "Longitude", "Opening Hours", "Reviews", "Email"]) def fetch_data(): all_results = get_all_places(query, location, radius, api_key) if all_results: return pd.DataFrame(all_results, columns=["Name", "Address", "Phone", "Rating", "Business Status", "User Ratings Total", "Website", "Types", "Latitude", "Longitude", "Opening Hours", "Reviews", "Email"]) else: return "No data found." fetch_button.click(fetch_data, inputs=None, outputs=data_output) # Launch Gradio interface demo.launch()