Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
|
|
| 1 |
import nltk
|
| 2 |
import numpy as np
|
| 3 |
import tflearn
|
| 4 |
-
import tensorflow
|
| 5 |
import random
|
| 6 |
import json
|
| 7 |
import pickle
|
| 8 |
-
import
|
| 9 |
from nltk.tokenize import word_tokenize
|
| 10 |
from nltk.stem.lancaster import LancasterStemmer
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 12 |
-
import torch
|
| 13 |
import requests
|
| 14 |
import pandas as pd
|
| 15 |
-
import time
|
| 16 |
-
from bs4 import BeautifulSoup
|
| 17 |
-
from selenium import webdriver
|
| 18 |
-
from selenium.webdriver.chrome.options import Options
|
| 19 |
-
import chromedriver_autoinstaller
|
| 20 |
-
import os
|
| 21 |
import tempfile
|
| 22 |
|
| 23 |
# Ensure necessary NLTK resources are downloaded
|
|
@@ -90,7 +83,7 @@ def chat(message, history):
|
|
| 90 |
history.append((message, response))
|
| 91 |
return history, history
|
| 92 |
|
| 93 |
-
# Sentiment Analysis
|
| 94 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 95 |
model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 96 |
|
|
@@ -102,7 +95,7 @@ def analyze_sentiment(user_input):
|
|
| 102 |
sentiment = ["Negative", "Neutral", "Positive"][predicted_class]
|
| 103 |
return f"**Predicted Sentiment:** {sentiment}"
|
| 104 |
|
| 105 |
-
# Emotion Detection
|
| 106 |
tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 107 |
model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 108 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
|
@@ -113,21 +106,52 @@ def detect_emotion(user_input):
|
|
| 113 |
return emotion
|
| 114 |
|
| 115 |
def provide_suggestions(emotion):
|
| 116 |
-
suggestions = ""
|
|
|
|
| 117 |
if emotion == 'joy':
|
| 118 |
-
suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
elif emotion == 'anger':
|
| 120 |
-
suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
elif emotion == 'fear':
|
| 122 |
-
suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
elif emotion == 'sadness':
|
| 124 |
-
suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
elif emotion == 'surprise':
|
| 126 |
-
suggestions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
return suggestions
|
| 128 |
|
| 129 |
-
# Google Places API
|
| 130 |
-
api_key = "
|
| 131 |
|
| 132 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
| 133 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
|
@@ -162,32 +186,15 @@ def get_all_places(query, location, radius, api_key):
|
|
| 162 |
break
|
| 163 |
return all_results
|
| 164 |
|
| 165 |
-
# Search Wellness Professionals
|
| 166 |
def search_wellness_professionals(location):
|
| 167 |
query = "therapist OR counselor OR mental health professional"
|
| 168 |
radius = 50000
|
| 169 |
google_places_data = get_all_places(query, location, radius, api_key)
|
| 170 |
-
|
| 171 |
-
# Check if data is found
|
| 172 |
if google_places_data:
|
| 173 |
df = pd.DataFrame(google_places_data, columns=["Name", "Address", "Website"])
|
| 174 |
-
|
| 175 |
-
# Create a temporary file to store the CSV
|
| 176 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 177 |
-
df.to_csv(temp_file, index=False)
|
| 178 |
-
temp_file.close() # Close the file so that Gradio can download it
|
| 179 |
-
|
| 180 |
-
return temp_file.name # Return the path to the temporary file
|
| 181 |
else:
|
| 182 |
-
|
| 183 |
-
dummy_df = pd.DataFrame([["No data found.", "", ""]], columns=["Name", "Address", "Website"])
|
| 184 |
-
|
| 185 |
-
# Create a temporary file for the dummy data
|
| 186 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 187 |
-
dummy_df.to_csv(temp_file, index=False)
|
| 188 |
-
temp_file.close() # Close the file
|
| 189 |
-
|
| 190 |
-
return temp_file.name # Return the path to the dummy file
|
| 191 |
|
| 192 |
# Gradio Interface
|
| 193 |
def gradio_interface(message, location, state):
|
|
@@ -221,7 +228,7 @@ iface = gr.Interface(
|
|
| 221 |
gr.Chatbot(label="Chat History"),
|
| 222 |
gr.Textbox(label="Sentiment Analysis"),
|
| 223 |
gr.Textbox(label="Detected Emotion"),
|
| 224 |
-
gr.
|
| 225 |
gr.File(label="Download Wellness Professionals CSV"),
|
| 226 |
gr.State() # One state output
|
| 227 |
],
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import nltk
|
| 3 |
import numpy as np
|
| 4 |
import tflearn
|
|
|
|
| 5 |
import random
|
| 6 |
import json
|
| 7 |
import pickle
|
| 8 |
+
import torch
|
| 9 |
from nltk.tokenize import word_tokenize
|
| 10 |
from nltk.stem.lancaster import LancasterStemmer
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
|
|
|
| 12 |
import requests
|
| 13 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import tempfile
|
| 15 |
|
| 16 |
# Ensure necessary NLTK resources are downloaded
|
|
|
|
| 83 |
history.append((message, response))
|
| 84 |
return history, history
|
| 85 |
|
| 86 |
+
# Sentiment Analysis
|
| 87 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 88 |
model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 89 |
|
|
|
|
| 95 |
sentiment = ["Negative", "Neutral", "Positive"][predicted_class]
|
| 96 |
return f"**Predicted Sentiment:** {sentiment}"
|
| 97 |
|
| 98 |
+
# Emotion Detection
|
| 99 |
tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 100 |
model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 101 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
|
|
|
| 106 |
return emotion
|
| 107 |
|
| 108 |
def provide_suggestions(emotion):
|
| 109 |
+
suggestions = pd.DataFrame(columns=["Subject", "Article URL", "Video URL"])
|
| 110 |
+
|
| 111 |
if emotion == 'joy':
|
| 112 |
+
suggestions = suggestions.append({
|
| 113 |
+
"Subject": "Relaxation Techniques",
|
| 114 |
+
"Article URL": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation",
|
| 115 |
+
"Video URL": "https://youtu.be/m1vaUGtyo-A"
|
| 116 |
+
}, ignore_index=True)
|
| 117 |
+
suggestions = suggestions.append({
|
| 118 |
+
"Subject": "Dealing with Stress",
|
| 119 |
+
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 120 |
+
"Video URL": "https://youtu.be/MIc299Flibs"
|
| 121 |
+
}, ignore_index=True)
|
| 122 |
+
|
| 123 |
elif emotion == 'anger':
|
| 124 |
+
suggestions = suggestions.append({
|
| 125 |
+
"Subject": "Managing Anger",
|
| 126 |
+
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 127 |
+
"Video URL": "https://youtu.be/MIc299Flibs"
|
| 128 |
+
}, ignore_index=True)
|
| 129 |
+
|
| 130 |
elif emotion == 'fear':
|
| 131 |
+
suggestions = suggestions.append({
|
| 132 |
+
"Subject": "Coping with Anxiety",
|
| 133 |
+
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 134 |
+
"Video URL": "https://youtu.be/yGKKz185M5o"
|
| 135 |
+
}, ignore_index=True)
|
| 136 |
+
|
| 137 |
elif emotion == 'sadness':
|
| 138 |
+
suggestions = suggestions.append({
|
| 139 |
+
"Subject": "Dealing with Sadness",
|
| 140 |
+
"Article URL": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 141 |
+
"Video URL": "https://youtu.be/-e-4Kx5px_I"
|
| 142 |
+
}, ignore_index=True)
|
| 143 |
+
|
| 144 |
elif emotion == 'surprise':
|
| 145 |
+
suggestions = suggestions.append({
|
| 146 |
+
"Subject": "Managing Stress",
|
| 147 |
+
"Article URL": "https://www.health.harvard.edu/health-a-to-z",
|
| 148 |
+
"Video URL": "https://youtu.be/m1vaUGtyo-A"
|
| 149 |
+
}, ignore_index=True)
|
| 150 |
+
|
| 151 |
return suggestions
|
| 152 |
|
| 153 |
+
# Google Places API to get nearby wellness professionals
|
| 154 |
+
api_key = "YOUR_GOOGLE_API_KEY" # Replace with your API key
|
| 155 |
|
| 156 |
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
| 157 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
|
|
|
| 186 |
break
|
| 187 |
return all_results
|
| 188 |
|
|
|
|
| 189 |
def search_wellness_professionals(location):
|
| 190 |
query = "therapist OR counselor OR mental health professional"
|
| 191 |
radius = 50000
|
| 192 |
google_places_data = get_all_places(query, location, radius, api_key)
|
|
|
|
|
|
|
| 193 |
if google_places_data:
|
| 194 |
df = pd.DataFrame(google_places_data, columns=["Name", "Address", "Website"])
|
| 195 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
else:
|
| 197 |
+
return pd.DataFrame([["No data found.", "", ""]], columns=["Name", "Address", "Website"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# Gradio Interface
|
| 200 |
def gradio_interface(message, location, state):
|
|
|
|
| 228 |
gr.Chatbot(label="Chat History"),
|
| 229 |
gr.Textbox(label="Sentiment Analysis"),
|
| 230 |
gr.Textbox(label="Detected Emotion"),
|
| 231 |
+
gr.Dataframe(label="Suggestions & Resources"),
|
| 232 |
gr.File(label="Download Wellness Professionals CSV"),
|
| 233 |
gr.State() # One state output
|
| 234 |
],
|