Spaces:
Sleeping
Sleeping
import gradio as gr | |
import nltk | |
import numpy as np | |
import tflearn | |
import random | |
import json | |
import pickle | |
import torch | |
from nltk.tokenize import word_tokenize | |
from nltk.stem.lancaster import LancasterStemmer | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline | |
import requests | |
import pandas as pd | |
import os | |
# 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 (Chatbot) | |
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 | |
# Sentiment Analysis | |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") | |
model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") | |
def analyze_sentiment(user_input): | |
inputs = tokenizer_sentiment(user_input, 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 f"**Predicted Sentiment:** {sentiment}" | |
# Emotion Detection | |
tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base") | |
model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base") | |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion) | |
def detect_emotion(user_input): | |
result = pipe(user_input) | |
emotion = result[0]['label'] | |
return emotion | |
def provide_suggestions(emotion): | |
suggestions = pd.DataFrame(columns=["Subject", "Article URL", "Video URL"]) | |
if emotion == 'joy': | |
suggestions = suggestions.append({ | |
"Subject": "Relaxation Techniques", | |
"Article URL": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation", | |
"Video URL": "https://youtu.be/m1vaUGtyo-A" | |
}, ignore_index=True) | |
suggestions = suggestions.append({ | |
"Subject": "Dealing with Stress", | |
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", | |
"Video URL": "https://youtu.be/MIc299Flibs" | |
}, ignore_index=True) | |
elif emotion == 'anger': | |
suggestions = suggestions.append({ | |
"Subject": "Managing Anger", | |
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", | |
"Video URL": "https://youtu.be/MIc299Flibs" | |
}, ignore_index=True) | |
elif emotion == 'fear': | |
suggestions = suggestions.append({ | |
"Subject": "Coping with Anxiety", | |
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", | |
"Video URL": "https://youtu.be/yGKKz185M5o" | |
}, ignore_index=True) | |
elif emotion == 'sadness': | |
suggestions = suggestions.append({ | |
"Subject": "Dealing with Sadness", | |
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", | |
"Video URL": "https://youtu.be/-e-4Kx5px_I" | |
}, ignore_index=True) | |
elif emotion == 'surprise': | |
suggestions = suggestions.append({ | |
"Subject": "Managing Stress", | |
"Article URL": "https://www.health.harvard.edu/health-a-to-z", | |
"Video URL": "https://youtu.be/m1vaUGtyo-A" | |
}, ignore_index=True) | |
return suggestions | |
# Google Places API integration | |
api_key = os.environ.get("GOOGLE_API_KEY") # Get API key from environment variable | |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json" | |
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json" | |
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) | |
return response.json() if response.status_code == 200 else None | |
def get_place_details(place_id, api_key): | |
params = { | |
"place_id": place_id, | |
"key": api_key | |
} | |
response = requests.get(places_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 {} | |
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', []) | |
for place in results: | |
place_id = place.get("place_id", "N/A") # Handle missing place_id | |
name = place.get("name", "N/A") # Handle missing name | |
address = place.get("formatted_address", "N/A") # Handle missing address | |
website = place.get("website", "Not available") | |
details = get_place_details(place_id, api_key) if place_id != "N/A" else {} #Avoid error if place_id is missing | |
phone_number = details.get("phone_number", "Not available") | |
all_results.append([name, address, phone_number, website]) | |
next_page_token = data.get('next_page_token') | |
if not next_page_token: | |
break | |
else: | |
break | |
return all_results | |
def search_wellness_professionals(location): | |
query = "therapist OR counselor OR mental health professional" | |
radius = 50000 | |
try: | |
google_places_data = get_all_places(query, location, radius, api_key) | |
if google_places_data: | |
df = pd.DataFrame(google_places_data, columns=["Name", "Address", "Phone", "Website"]) | |
return df | |
else: | |
return pd.DataFrame([["No data found.", "", "", ""]], columns=["Name", "Address", "Phone", "Website"]) | |
except Exception as e: | |
return pd.DataFrame([["Error fetching data: " + str(e), "", "", ""]], columns=["Name", "Address", "Phone", "Website"]) | |
# Gradio Interface | |
def gradio_interface(message, location, state): | |
history = state or [] # If state is None, initialize it as an empty list | |
# Stage 1: Mental Health Chatbot | |
history, _ = chat(message, history) | |
# Stage 2: Sentiment Analysis | |
sentiment = analyze_sentiment(message) | |
# Stage 3: Emotion Detection and Suggestions | |
emotion = detect_emotion(message) | |
suggestions = provide_suggestions(emotion) | |
# Stage 4: Search for Wellness Professionals | |
try: | |
wellness_results = search_wellness_professionals(location) | |
except Exception as e: | |
wellness_results = pd.DataFrame([["Error: " + str(e), "", "", ""]], columns=["Name", "Address", "Phone", "Website"]) | |
# Return the results in a tabular form within the Gradio interface | |
return history, sentiment, emotion, suggestions, wellness_results, history # Last 'history' is for state | |
# Gradio interface setup | |
iface = gr.Interface( | |
fn=gradio_interface, | |
inputs=[ | |
gr.Textbox(label="Enter your message", placeholder="How are you feeling today?"), | |
gr.Textbox(label="Enter your location (e.g., 'Hawaii, USA')", placeholder="Enter your location"), | |
gr.State() # To maintain state (chat history) | |
], | |
outputs=[ | |
gr.Chatbot(label="Chatbot Responses"), | |
gr.Textbox(label="Sentiment Analysis"), | |
gr.Textbox(label="Emotion Detected"), | |
gr.DataFrame(label="Suggested Articles & Videos"), | |
gr.DataFrame(label="Nearby Wellness Professionals"), | |
gr.State() # To maintain state (chat history) | |
], | |
live=True, | |
title="Mental Health Chatbot with Wellness Professional Search", | |
description="This chatbot provides mental health support with sentiment analysis, emotion detection, suggestions, and a list of nearby wellness professionals." | |
) | |
# Check for API key; if not found, print an error message | |
if api_key is None: | |
print("Error: GOOGLE_MAPS_API_KEY environment variable not set. Please set this environment variable with your Google Maps API key.") | |
# Launch the interface | |
iface.launch(debug=True, share=True) | |