Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,8 +83,7 @@ def chat(message, history):
|
|
| 83 |
response = f"An error occurred: {str(e)}"
|
| 84 |
|
| 85 |
history.append((message, response))
|
| 86 |
-
return history,
|
| 87 |
-
|
| 88 |
|
| 89 |
# Sentiment analysis setup
|
| 90 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
|
@@ -98,97 +97,157 @@ def load_emotion_model():
|
|
| 98 |
|
| 99 |
tokenizer_emotion, model_emotion = load_emotion_model()
|
| 100 |
|
| 101 |
-
# Emotion detection function with suggestions in plain English
|
| 102 |
def detect_emotion(user_input):
|
| 103 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
| 104 |
result = pipe(user_input)
|
| 105 |
emotion = result[0]['label']
|
| 106 |
|
| 107 |
-
#
|
| 108 |
if emotion == 'joy':
|
| 109 |
emotion_msg = "You're feeling happy! Keep up the great mood!"
|
| 110 |
resources = [
|
| 111 |
-
{"subject": "Relaxation Techniques", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
|
| 112 |
-
{"subject": "Dealing with Stress", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
| 113 |
-
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
|
| 114 |
]
|
| 115 |
-
video_link = "Watch on YouTube: https://youtu.be/m1vaUGtyo-A"
|
| 116 |
|
| 117 |
elif emotion == 'anger':
|
| 118 |
emotion_msg = "You're feeling angry. It's okay to feel this way. Let's try to calm down."
|
| 119 |
resources = [
|
| 120 |
-
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
| 121 |
-
{"subject": "Stress Management Tips", "link": "https://www.health.harvard.edu/health-a-to-z"},
|
| 122 |
-
{"subject": "Dealing with Anger", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
| 123 |
]
|
| 124 |
-
video_link = "Watch on YouTube: https://youtu.be/MIc299Flibs"
|
| 125 |
|
| 126 |
elif emotion == 'fear':
|
| 127 |
emotion_msg = "You're feeling fearful. Take a moment to breathe and relax."
|
| 128 |
resources = [
|
| 129 |
-
{"subject": "Mindfulness Practices", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
|
| 130 |
-
{"subject": "Coping with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
|
| 131 |
-
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"}
|
| 132 |
]
|
| 133 |
-
video_link = "Watch on YouTube: https://youtu.be/yGKKz185M5o"
|
| 134 |
|
| 135 |
elif emotion == 'sadness':
|
| 136 |
emotion_msg = "You're feeling sad. It's okay to take a break."
|
| 137 |
resources = [
|
| 138 |
-
{"subject": "Emotional Wellness Toolkit", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
|
| 139 |
-
{"subject": "Dealing with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
| 140 |
]
|
| 141 |
-
video_link = "Watch on YouTube: https://youtu.be/-e-4Kx5px_I"
|
| 142 |
|
| 143 |
elif emotion == 'surprise':
|
| 144 |
emotion_msg = "You're feeling surprised. It's okay to feel neutral!"
|
| 145 |
resources = [
|
| 146 |
-
{"subject": "Managing Stress", "link": "https://www.health.harvard.edu/health-a-to-z"},
|
| 147 |
-
{"subject": "Coping Strategies", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"}
|
| 148 |
]
|
| 149 |
-
video_link = "Watch on YouTube: https://youtu.be/m1vaUGtyo-A"
|
| 150 |
|
| 151 |
else:
|
| 152 |
emotion_msg = "Could not detect emotion."
|
| 153 |
resources = []
|
| 154 |
-
video_link = ""
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
def
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
if action == "Chat":
|
| 163 |
# Use chat function if 'Chat' button is clicked
|
| 164 |
-
history,
|
| 165 |
-
return history, history
|
| 166 |
-
|
| 167 |
elif action == "Detect Emotion":
|
| 168 |
-
#
|
| 169 |
-
emotion_msg,
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
result += f"Suggested Video: {video_link}"
|
| 174 |
-
return result, history
|
| 175 |
-
|
| 176 |
elif action == "Wellness Resources":
|
| 177 |
-
#
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
# Gradio
|
| 184 |
iface = gr.Interface(
|
| 185 |
fn=interface_function,
|
| 186 |
-
inputs=[
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
allow_flagging="never",
|
| 191 |
-
live=True
|
| 192 |
)
|
| 193 |
|
| 194 |
iface.launch(share=True)
|
|
|
|
| 83 |
response = f"An error occurred: {str(e)}"
|
| 84 |
|
| 85 |
history.append((message, response))
|
| 86 |
+
return history, response
|
|
|
|
| 87 |
|
| 88 |
# Sentiment analysis setup
|
| 89 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
|
|
|
| 97 |
|
| 98 |
tokenizer_emotion, model_emotion = load_emotion_model()
|
| 99 |
|
| 100 |
+
# Emotion detection function with suggestions in plain English and resources in table
|
| 101 |
def detect_emotion(user_input):
|
| 102 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
| 103 |
result = pipe(user_input)
|
| 104 |
emotion = result[0]['label']
|
| 105 |
|
| 106 |
+
# Define emotion-specific message and resources
|
| 107 |
if emotion == 'joy':
|
| 108 |
emotion_msg = "You're feeling happy! Keep up the great mood!"
|
| 109 |
resources = [
|
| 110 |
+
{"subject": "Relaxation Techniques", "heading": "Mindful Breathing Meditation", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation", "video_url": "https://youtu.be/m1vaUGtyo-A"},
|
| 111 |
+
{"subject": "Dealing with Stress", "heading": "Tips for Dealing with Anxiety", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", "video_url": "https://youtu.be/m1vaUGtyo-A"},
|
| 112 |
+
{"subject": "Emotional Wellness Toolkit", "heading": "Emotional Wellness Resources", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit", "video_url": "https://youtu.be/m1vaUGtyo-A"}
|
| 113 |
]
|
|
|
|
| 114 |
|
| 115 |
elif emotion == 'anger':
|
| 116 |
emotion_msg = "You're feeling angry. It's okay to feel this way. Let's try to calm down."
|
| 117 |
resources = [
|
| 118 |
+
{"subject": "Emotional Wellness Toolkit", "heading": "Managing Emotions", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit", "video_url": "https://youtu.be/MIc299Flibs"},
|
| 119 |
+
{"subject": "Stress Management Tips", "heading": "Managing Stress Effectively", "link": "https://www.health.harvard.edu/health-a-to-z", "video_url": "https://youtu.be/MIc299Flibs"},
|
| 120 |
+
{"subject": "Dealing with Anger", "heading": "Strategies to Calm Anger", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", "video_url": "https://youtu.be/MIc299Flibs"}
|
| 121 |
]
|
|
|
|
| 122 |
|
| 123 |
elif emotion == 'fear':
|
| 124 |
emotion_msg = "You're feeling fearful. Take a moment to breathe and relax."
|
| 125 |
resources = [
|
| 126 |
+
{"subject": "Mindfulness Practices", "heading": "Breathing Techniques", "link": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation", "video_url": "https://youtu.be/yGKKz185M5o"},
|
| 127 |
+
{"subject": "Coping with Anxiety", "heading": "Overcoming Fear", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", "video_url": "https://youtu.be/yGKKz185M5o"},
|
| 128 |
+
{"subject": "Emotional Wellness Toolkit", "heading": "Calming Your Mind", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit", "video_url": "https://youtu.be/yGKKz185M5o"}
|
| 129 |
]
|
|
|
|
| 130 |
|
| 131 |
elif emotion == 'sadness':
|
| 132 |
emotion_msg = "You're feeling sad. It's okay to take a break."
|
| 133 |
resources = [
|
| 134 |
+
{"subject": "Emotional Wellness Toolkit", "heading": "Restoring Your Emotional Health", "link": "https://www.nih.gov/health-information/emotional-wellness-toolkit", "video_url": "https://youtu.be/-e-4Kx5px_I"},
|
| 135 |
+
{"subject": "Dealing with Anxiety", "heading": "Coping Strategies for Stress", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", "video_url": "https://youtu.be/-e-4Kx5px_I"}
|
| 136 |
]
|
|
|
|
| 137 |
|
| 138 |
elif emotion == 'surprise':
|
| 139 |
emotion_msg = "You're feeling surprised. It's okay to feel neutral!"
|
| 140 |
resources = [
|
| 141 |
+
{"subject": "Managing Stress", "heading": "Relaxation Tips", "link": "https://www.health.harvard.edu/health-a-to-z", "video_url": "https://youtu.be/m1vaUGtyo-A"},
|
| 142 |
+
{"subject": "Coping Strategies", "heading": "Dealing with Unexpected Events", "link": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety", "video_url": "https://youtu.be/m1vaUGtyo-A"}
|
| 143 |
]
|
|
|
|
| 144 |
|
| 145 |
else:
|
| 146 |
emotion_msg = "Could not detect emotion."
|
| 147 |
resources = []
|
|
|
|
| 148 |
|
| 149 |
+
# Create a DataFrame for resources to display in table format
|
| 150 |
+
resource_df = pd.DataFrame(resources)
|
| 151 |
+
|
| 152 |
+
return emotion_msg, resource_df
|
| 153 |
+
|
| 154 |
+
# Google Geocoding API setup to convert city name to latitude/longitude
|
| 155 |
+
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json"
|
| 156 |
+
|
| 157 |
+
def get_lat_lon(location, api_key):
|
| 158 |
+
params = {
|
| 159 |
+
"address": location,
|
| 160 |
+
"key": api_key
|
| 161 |
+
}
|
| 162 |
+
response = requests.get(geocode_url, params=params)
|
| 163 |
+
if response.status_code == 200:
|
| 164 |
+
result = response.json()
|
| 165 |
+
if result['status'] == 'OK':
|
| 166 |
+
# Return the first result's latitude and longitude
|
| 167 |
+
location = result['results'][0]['geometry']['location']
|
| 168 |
+
return location['lat'], location['lng']
|
| 169 |
+
return None, None
|
| 170 |
+
|
| 171 |
+
# Google Places API setup for wellness professionals
|
| 172 |
+
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
| 173 |
+
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
|
| 174 |
+
api_key = os.getenv("GOOGLE_API_KEY") # Use environment variable for security
|
| 175 |
+
|
| 176 |
+
# Function to get places data using Google Places API
|
| 177 |
+
def get_places_data(query, location, radius, api_key, next_page_token=None):
|
| 178 |
+
params = {
|
| 179 |
+
'query': query,
|
| 180 |
+
'location': location,
|
| 181 |
+
'radius': radius,
|
| 182 |
+
'key': api_key
|
| 183 |
+
}
|
| 184 |
+
if next_page_token:
|
| 185 |
+
params['pagetoken'] = next_page_token
|
| 186 |
+
|
| 187 |
+
response = requests.get(url, params=params)
|
| 188 |
+
return response.json()
|
| 189 |
|
| 190 |
+
# Function to fetch wellness professionals
|
| 191 |
+
def get_wellness_professionals(location, api_key):
|
| 192 |
+
lat, lon = get_lat_lon(location, api_key)
|
| 193 |
+
|
| 194 |
+
if lat and lon:
|
| 195 |
+
places = get_places_data("wellness professional", f"{lat},{lon}", 10000, api_key)
|
| 196 |
+
|
| 197 |
+
if places and 'results' in places:
|
| 198 |
+
professionals = []
|
| 199 |
+
for place in places['results']:
|
| 200 |
+
name = place.get("name", "No name available")
|
| 201 |
+
rating = place.get("rating", "No rating available")
|
| 202 |
+
address = place.get("formatted_address", "No address available")
|
| 203 |
+
professionals.append({
|
| 204 |
+
"Name": name,
|
| 205 |
+
"Rating": rating,
|
| 206 |
+
"Address": address
|
| 207 |
+
})
|
| 208 |
+
|
| 209 |
+
professionals_df = pd.DataFrame(professionals)
|
| 210 |
+
return professionals_df
|
| 211 |
+
else:
|
| 212 |
+
return "No wellness professionals found nearby."
|
| 213 |
+
else:
|
| 214 |
+
return "Location not found. Please check the location."
|
| 215 |
|
| 216 |
+
# Gradio interface function to handle actions and outputs
|
| 217 |
+
def interface_function(message, action, location, history):
|
| 218 |
+
history = history or []
|
| 219 |
+
|
| 220 |
if action == "Chat":
|
| 221 |
# Use chat function if 'Chat' button is clicked
|
| 222 |
+
history, response = chat(message, history)
|
|
|
|
|
|
|
| 223 |
elif action == "Detect Emotion":
|
| 224 |
+
# Use emotion detection if 'Detect Emotion' button is clicked
|
| 225 |
+
emotion_msg, resource_df = detect_emotion(message)
|
| 226 |
+
response = emotion_msg
|
| 227 |
+
# Return the resource DataFrame as a table
|
| 228 |
+
return history, response, resource_df
|
|
|
|
|
|
|
|
|
|
| 229 |
elif action == "Wellness Resources":
|
| 230 |
+
# Use location to get wellness professionals if 'Wellness Resources' is clicked
|
| 231 |
+
if not location.strip():
|
| 232 |
+
response = "Please enter a valid location."
|
| 233 |
+
else:
|
| 234 |
+
professionals_df = get_wellness_professionals(location, api_key)
|
| 235 |
+
if isinstance(professionals_df, pd.DataFrame):
|
| 236 |
+
response = "Found wellness professionals nearby:"
|
| 237 |
+
return history, response, professionals_df
|
| 238 |
+
else:
|
| 239 |
+
response = professionals_df # If error message is returned
|
| 240 |
+
return history, response, None
|
| 241 |
+
|
| 242 |
+
return history, "Invalid action", None
|
| 243 |
|
| 244 |
+
# Gradio Interface with table outputs for emotion and wellness professionals
|
| 245 |
iface = gr.Interface(
|
| 246 |
fn=interface_function,
|
| 247 |
+
inputs=["text", "radio", "text", "state"],
|
| 248 |
+
outputs=["text", "dataframe"],
|
| 249 |
+
live=True,
|
| 250 |
+
allow_flagging="never"
|
|
|
|
|
|
|
| 251 |
)
|
| 252 |
|
| 253 |
iface.launch(share=True)
|