Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,17 +6,16 @@ import pickle
|
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
| 8 |
import folium
|
|
|
|
| 9 |
from nltk.tokenize import word_tokenize
|
| 10 |
from nltk.stem.lancaster import LancasterStemmer
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
|
|
|
| 12 |
import tensorflow as tf
|
| 13 |
import tflearn
|
| 14 |
-
import torch
|
| 15 |
-
import pandas as pd
|
| 16 |
import time
|
| 17 |
from bs4 import BeautifulSoup
|
| 18 |
import re # Added for regex operations
|
| 19 |
-
import os
|
| 20 |
|
| 21 |
# Google Places API endpoint
|
| 22 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
|
@@ -53,9 +52,10 @@ tokenizer, emotion_model = load_model()
|
|
| 53 |
|
| 54 |
# Google Places API query function
|
| 55 |
def get_places_data(query, location, radius=5000, api_key="GOOGLE_API_KEY"):
|
|
|
|
| 56 |
params = {
|
| 57 |
"query": query,
|
| 58 |
-
"location":
|
| 59 |
"radius": radius,
|
| 60 |
"key": api_key
|
| 61 |
}
|
|
@@ -150,14 +150,51 @@ def emotion_and_chatbot(user_input, history, query, location):
|
|
| 150 |
sentiment = analyze_sentiment(user_input)
|
| 151 |
emotion_response = f"Emotion Detected: {emotion}. Sentiment: {sentiment}"
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Search Places (for wellness or other queries)
|
| 154 |
places_data = get_places_data(query, location)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
places_map = create_map(places_data) if places_data else "No places found."
|
| 156 |
|
| 157 |
# Chatbot response
|
| 158 |
history, _ = chatbot(user_input, history)
|
| 159 |
|
| 160 |
-
return emotion_response, places_map, history, history
|
| 161 |
|
| 162 |
# Gradio interface setup
|
| 163 |
iface = gr.Interface(
|
|
@@ -171,6 +208,7 @@ iface = gr.Interface(
|
|
| 171 |
outputs=[
|
| 172 |
gr.Textbox(label="Emotion and Sentiment"),
|
| 173 |
gr.HTML(label="Places Map"),
|
|
|
|
| 174 |
gr.Chatbot(label="Chatbot History"),
|
| 175 |
"state"
|
| 176 |
],
|
|
@@ -180,4 +218,4 @@ iface = gr.Interface(
|
|
| 180 |
|
| 181 |
# Launch Gradio app
|
| 182 |
if __name__ == "__main__":
|
| 183 |
-
iface.launch(debug=True)
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
| 8 |
import folium
|
| 9 |
+
import pandas as pd
|
| 10 |
from nltk.tokenize import word_tokenize
|
| 11 |
from nltk.stem.lancaster import LancasterStemmer
|
| 12 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 13 |
+
import torch
|
| 14 |
import tensorflow as tf
|
| 15 |
import tflearn
|
|
|
|
|
|
|
| 16 |
import time
|
| 17 |
from bs4 import BeautifulSoup
|
| 18 |
import re # Added for regex operations
|
|
|
|
| 19 |
|
| 20 |
# Google Places API endpoint
|
| 21 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
|
|
|
| 52 |
|
| 53 |
# Google Places API query function
|
| 54 |
def get_places_data(query, location, radius=5000, api_key="GOOGLE_API_KEY"):
|
| 55 |
+
latitude, longitude = map(float, location.split(","))
|
| 56 |
params = {
|
| 57 |
"query": query,
|
| 58 |
+
"location": f"{latitude},{longitude}",
|
| 59 |
"radius": radius,
|
| 60 |
"key": api_key
|
| 61 |
}
|
|
|
|
| 150 |
sentiment = analyze_sentiment(user_input)
|
| 151 |
emotion_response = f"Emotion Detected: {emotion}. Sentiment: {sentiment}"
|
| 152 |
|
| 153 |
+
# Provide suggestions based on emotion
|
| 154 |
+
suggestions = {
|
| 155 |
+
"joy": ["Relaxation Techniques", "Dealing with Stress", "Emotional Wellness Toolkit"],
|
| 156 |
+
"anger": ["Stress Management Tips", "Dealing with Anger", "Emotional Wellness Toolkit"],
|
| 157 |
+
"fear": ["Mindfulness Practices", "Coping with Anxiety", "Emotional Wellness Toolkit"],
|
| 158 |
+
"sadness": ["Dealing with Anxiety", "Emotional Wellness Toolkit"],
|
| 159 |
+
"surprise": ["Managing Stress", "Coping Strategies"]
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
# Suggested articles and video links
|
| 163 |
+
if emotion in suggestions:
|
| 164 |
+
resources = suggestions[emotion]
|
| 165 |
+
links = {
|
| 166 |
+
"Relaxation Techniques": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation",
|
| 167 |
+
"Dealing with Stress": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 168 |
+
"Emotional Wellness Toolkit": "https://www.nih.gov/health-information/emotional-wellness-toolkit",
|
| 169 |
+
"Stress Management Tips": "https://www.health.harvard.edu/health-a-to-z",
|
| 170 |
+
"Dealing with Anger": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 171 |
+
"Mindfulness Practices": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation",
|
| 172 |
+
"Coping with Anxiety": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety",
|
| 173 |
+
"Managing Stress": "https://www.health.harvard.edu/health-a-to-z",
|
| 174 |
+
"Coping Strategies": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
st.write("Useful Resources:")
|
| 178 |
+
for resource in resources:
|
| 179 |
+
st.markdown(f"[{resource}]({links[resource]})")
|
| 180 |
+
|
| 181 |
+
st.write("Relaxation Videos:")
|
| 182 |
+
st.markdown("[Watch on YouTube](https://youtu.be/m1vaUGtyo-A)")
|
| 183 |
+
|
| 184 |
# Search Places (for wellness or other queries)
|
| 185 |
places_data = get_places_data(query, location)
|
| 186 |
+
places_df = pd.DataFrame(places_data)
|
| 187 |
+
|
| 188 |
+
# Tabular output for places
|
| 189 |
+
places_table = places_df[['name', 'vicinity', 'geometry']].head(10).to_html(classes='table table-bordered') if not places_df.empty else "No places found."
|
| 190 |
+
|
| 191 |
+
# Generate Map
|
| 192 |
places_map = create_map(places_data) if places_data else "No places found."
|
| 193 |
|
| 194 |
# Chatbot response
|
| 195 |
history, _ = chatbot(user_input, history)
|
| 196 |
|
| 197 |
+
return emotion_response, places_map, places_table, history, history
|
| 198 |
|
| 199 |
# Gradio interface setup
|
| 200 |
iface = gr.Interface(
|
|
|
|
| 208 |
outputs=[
|
| 209 |
gr.Textbox(label="Emotion and Sentiment"),
|
| 210 |
gr.HTML(label="Places Map"),
|
| 211 |
+
gr.HTML(label="Places Table"),
|
| 212 |
gr.Chatbot(label="Chatbot History"),
|
| 213 |
"state"
|
| 214 |
],
|
|
|
|
| 218 |
|
| 219 |
# Launch Gradio app
|
| 220 |
if __name__ == "__main__":
|
| 221 |
+
iface.launch(debug=True)
|