Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,11 +73,10 @@ def chatbot_response(message, history):
|
|
| 73 |
history.append((message, response))
|
| 74 |
return history, response
|
| 75 |
|
| 76 |
-
#
|
| 77 |
emotion_tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 78 |
emotion_model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 79 |
|
| 80 |
-
# Detect emotion
|
| 81 |
def detect_emotion(user_input):
|
| 82 |
pipe = pipeline("text-classification", model=emotion_model, tokenizer=emotion_tokenizer)
|
| 83 |
try:
|
|
@@ -95,7 +94,7 @@ def detect_emotion(user_input):
|
|
| 95 |
except Exception as e:
|
| 96 |
return f"Error detecting emotion: {str(e)} π₯"
|
| 97 |
|
| 98 |
-
# Sentiment analysis
|
| 99 |
sentiment_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 100 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 101 |
|
|
@@ -115,95 +114,117 @@ def analyze_sentiment(user_input):
|
|
| 115 |
def generate_suggestions(emotion):
|
| 116 |
suggestions = {
|
| 117 |
"π Joy": [
|
| 118 |
-
{"Title": "Mindful Meditation
|
| 119 |
-
{"Title": "
|
| 120 |
],
|
| 121 |
"π’ Sadness": [
|
| 122 |
-
{"Title": "
|
| 123 |
-
{"Title": "
|
| 124 |
],
|
| 125 |
"π Anger": [
|
| 126 |
-
{"Title": "Anger Management
|
| 127 |
-
{"Title": "
|
| 128 |
],
|
| 129 |
}
|
| 130 |
-
return suggestions.get(emotion, [{"Title": "
|
| 131 |
-
|
| 132 |
-
#
|
| 133 |
-
def
|
| 134 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
# Chatbot response
|
| 136 |
history, chatbot_reply = chatbot_response(user_input, history)
|
| 137 |
|
| 138 |
-
# Emotion
|
| 139 |
emotion = detect_emotion(user_input)
|
| 140 |
|
| 141 |
-
# Sentiment
|
| 142 |
sentiment = analyze_sentiment(user_input)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
detected_emotion = emotion.split(": ")[-1]
|
| 146 |
suggestions = generate_suggestions(detected_emotion)
|
| 147 |
suggestions_df = pd.DataFrame(suggestions)
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
# Custom CSS for
|
| 152 |
custom_css = """
|
| 153 |
body {
|
| 154 |
background: linear-gradient(135deg, #28a745, #218838);
|
| 155 |
-
font-family:
|
| 156 |
color: black;
|
| 157 |
}
|
| 158 |
-
#component-0 span {
|
| 159 |
-
color: white;
|
| 160 |
-
}
|
| 161 |
button {
|
| 162 |
-
background-color: #
|
| 163 |
color: white;
|
| 164 |
-
padding:
|
| 165 |
font-size: 16px;
|
| 166 |
-
border-radius:
|
| 167 |
cursor: pointer;
|
| 168 |
}
|
| 169 |
button:hover {
|
| 170 |
-
background-color: #
|
| 171 |
}
|
| 172 |
-
input[type="text"]
|
| 173 |
-
textarea {
|
| 174 |
background: #ffffff;
|
| 175 |
color: #000000;
|
| 176 |
-
border: solid 1px #ced4da;
|
| 177 |
-
padding: 10px;
|
| 178 |
font-size: 14px;
|
| 179 |
-
border
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
"""
|
| 182 |
|
| 183 |
# Gradio UI
|
| 184 |
with gr.Blocks(css=custom_css) as interface:
|
| 185 |
gr.Markdown("# π± **Well-being Companion**")
|
| 186 |
-
gr.Markdown("### Empowering
|
| 187 |
|
|
|
|
| 188 |
with gr.Row():
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
with gr.Row():
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
with gr.Row():
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
with gr.Row():
|
| 200 |
-
|
| 201 |
-
|
|
|
|
| 202 |
submit_button.click(
|
| 203 |
well_being_app,
|
| 204 |
-
inputs=[
|
| 205 |
-
outputs=[
|
| 206 |
)
|
| 207 |
|
| 208 |
-
# Launch
|
| 209 |
interface.launch()
|
|
|
|
| 73 |
history.append((message, response))
|
| 74 |
return history, response
|
| 75 |
|
| 76 |
+
# Emotion detection transformer model
|
| 77 |
emotion_tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 78 |
emotion_model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 79 |
|
|
|
|
| 80 |
def detect_emotion(user_input):
|
| 81 |
pipe = pipeline("text-classification", model=emotion_model, tokenizer=emotion_tokenizer)
|
| 82 |
try:
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
return f"Error detecting emotion: {str(e)} π₯"
|
| 96 |
|
| 97 |
+
# Sentiment analysis model
|
| 98 |
sentiment_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 99 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 100 |
|
|
|
|
| 114 |
def generate_suggestions(emotion):
|
| 115 |
suggestions = {
|
| 116 |
"π Joy": [
|
| 117 |
+
{"Title": "Mindful Meditation π§", "Link": "https://www.helpguide.org/meditation"},
|
| 118 |
+
{"Title": "Learn a new skill β¨", "Link": "https://www.skillshare.com/"},
|
| 119 |
],
|
| 120 |
"π’ Sadness": [
|
| 121 |
+
{"Title": "Talk to a professional π¬", "Link": "https://www.betterhelp.com/"},
|
| 122 |
+
{"Title": "Mental health toolkit π οΈ", "Link": "https://www.psychologytoday.com/"},
|
| 123 |
],
|
| 124 |
"π Anger": [
|
| 125 |
+
{"Title": "Anger Management Tips π₯", "Link": "https://www.mentalhealth.org.uk"},
|
| 126 |
+
{"Title": "Stress Relieving Exercises πΏ", "Link": "https://www.calm.com/"},
|
| 127 |
],
|
| 128 |
}
|
| 129 |
+
return suggestions.get(emotion, [{"Title": "Wellness Resources π", "Link": "https://www.helpguide.org/wellness"}])
|
| 130 |
+
|
| 131 |
+
# Dummy Function for Location Query Simulation (replace this with actual map/search integration)
|
| 132 |
+
def search_nearby_professionals(location, query):
|
| 133 |
+
"""Simulate searching for nearby professionals and returning results."""
|
| 134 |
+
return [
|
| 135 |
+
{"Name": "Wellness Center One", "Address": "123 Wellness Way"},
|
| 136 |
+
{"Name": "Mental Health Clinic", "Address": "456 Recovery Road"},
|
| 137 |
+
{"Name": "Therapists Hub", "Address": "789 Peace Avenue"},
|
| 138 |
+
] if location and query else []
|
| 139 |
+
|
| 140 |
+
def well_being_app(user_input, location, query, history):
|
| 141 |
+
"""Main function for chatbot, emotion detection, sentiment, suggestions, and location query."""
|
| 142 |
# Chatbot response
|
| 143 |
history, chatbot_reply = chatbot_response(user_input, history)
|
| 144 |
|
| 145 |
+
# Emotion Detection
|
| 146 |
emotion = detect_emotion(user_input)
|
| 147 |
|
| 148 |
+
# Sentiment Analysis
|
| 149 |
sentiment = analyze_sentiment(user_input)
|
| 150 |
|
| 151 |
+
# Suggestions
|
| 152 |
detected_emotion = emotion.split(": ")[-1]
|
| 153 |
suggestions = generate_suggestions(detected_emotion)
|
| 154 |
suggestions_df = pd.DataFrame(suggestions)
|
| 155 |
|
| 156 |
+
# Nearby Professionals (Location Query)
|
| 157 |
+
professionals = search_nearby_professionals(location, query)
|
| 158 |
+
|
| 159 |
+
return history, sentiment, emotion, suggestions_df, professionals
|
| 160 |
|
| 161 |
+
# Custom CSS for beautification
|
| 162 |
custom_css = """
|
| 163 |
body {
|
| 164 |
background: linear-gradient(135deg, #28a745, #218838);
|
| 165 |
+
font-family: Arial, sans-serif;
|
| 166 |
color: black;
|
| 167 |
}
|
|
|
|
|
|
|
|
|
|
| 168 |
button {
|
| 169 |
+
background-color: #1abc9c;
|
| 170 |
color: white;
|
| 171 |
+
padding: 10px 20px;
|
| 172 |
font-size: 16px;
|
| 173 |
+
border-radius: 8px;
|
| 174 |
cursor: pointer;
|
| 175 |
}
|
| 176 |
button:hover {
|
| 177 |
+
background-color: #16a085;
|
| 178 |
}
|
| 179 |
+
textarea, input[type="text"] {
|
|
|
|
| 180 |
background: #ffffff;
|
| 181 |
color: #000000;
|
|
|
|
|
|
|
| 182 |
font-size: 14px;
|
| 183 |
+
border: 1px solid #ced4da;
|
| 184 |
+
padding: 10px;
|
| 185 |
+
border-radius: 5px;
|
| 186 |
}
|
| 187 |
"""
|
| 188 |
|
| 189 |
# Gradio UI
|
| 190 |
with gr.Blocks(css=custom_css) as interface:
|
| 191 |
gr.Markdown("# π± **Well-being Companion**")
|
| 192 |
+
gr.Markdown("### Empowering Your Mental Health Journey with AI π")
|
| 193 |
|
| 194 |
+
# Input Section
|
| 195 |
with gr.Row():
|
| 196 |
+
gr.Textbox(label="Your Message", lines=2, placeholder="How can I support you today?", elem_id="message_input")
|
| 197 |
+
gr.Textbox(label="Location", placeholder="Enter your location (e.g., New York City)")
|
| 198 |
+
gr.Textbox(label="Search Query", placeholder="Professionals nearby? (e.g., doctors, therapists)")
|
| 199 |
+
submit_button = gr.Button("Submit")
|
| 200 |
+
|
| 201 |
+
# Chatbot Section
|
| 202 |
+
with gr.Row():
|
| 203 |
+
chatbot_title = "### Chatbot Response"
|
| 204 |
+
chatbot_output = gr.Chatbot(label=None)
|
| 205 |
+
|
| 206 |
+
# Sentiment and Emotion Section
|
| 207 |
with gr.Row():
|
| 208 |
+
gr.Markdown("### Sentiment Analysis")
|
| 209 |
+
sentiment_output = gr.Textbox(label=None)
|
| 210 |
+
gr.Markdown("### Detected Emotion")
|
| 211 |
+
emotion_output = gr.Textbox(label=None)
|
| 212 |
+
|
| 213 |
+
# Suggestions Section
|
| 214 |
with gr.Row():
|
| 215 |
+
gr.Markdown("### Suggestions")
|
| 216 |
+
suggestions_output = gr.DataFrame(headers=["Title", "Link"], interactive=False, max_height=300)
|
| 217 |
+
|
| 218 |
+
# Location Search Results Section
|
| 219 |
with gr.Row():
|
| 220 |
+
gr.Markdown("### Nearby Professionals")
|
| 221 |
+
location_output = gr.DataFrame(headers=["Name", "Address"], interactive=False, max_height=300)
|
| 222 |
+
|
| 223 |
submit_button.click(
|
| 224 |
well_being_app,
|
| 225 |
+
inputs=["message_input", "Location", "Search Query", chatbot_output],
|
| 226 |
+
outputs=[chatbot_output, sentiment_output, emotion_output, suggestions_output, location_output],
|
| 227 |
)
|
| 228 |
|
| 229 |
+
# Launch the app
|
| 230 |
interface.launch()
|