DreamStream-1 commited on
Commit
5d0e15d
Β·
verified Β·
1 Parent(s): a6192b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -46
app.py CHANGED
@@ -73,11 +73,10 @@ def chatbot_response(message, history):
73
  history.append((message, response))
74
  return history, response
75
 
76
- # Hugging Face transformers model for emotion detection
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 using Hugging Face
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 πŸ§˜β€β™‚οΈ", "Link": "https://www.helpguide.org/meditation"},
119
- {"Title": "Explore a new skill πŸš€", "Link": "https://www.skillshare.com/"},
120
  ],
121
  "😒 Sadness": [
122
- {"Title": "Improve mental resilience ✨", "Link": "https://www.psychologytoday.com/"},
123
- {"Title": "Reach out to a therapist πŸ’¬", "Link": "https://www.betterhelp.com/"},
124
  ],
125
  "😠 Anger": [
126
- {"Title": "Anger Management Guide πŸ”₯", "Link": "https://www.mentalhealth.org.uk/"},
127
- {"Title": "Calming Exercises 🌿", "Link": "https://www.calm.com/"},
128
  ],
129
  }
130
- return suggestions.get(emotion, [{"Title": "General Wellness Resources 🌈", "Link": "https://www.wellness.com/"}])
131
-
132
- # Main App Function
133
- def well_being_app(user_input, history):
134
- """Main function for chatbot, emotion detection, sentiment analysis, and suggestions."""
 
 
 
 
 
 
 
 
135
  # Chatbot response
136
  history, chatbot_reply = chatbot_response(user_input, history)
137
 
138
- # Emotion detection
139
  emotion = detect_emotion(user_input)
140
 
141
- # Sentiment analysis
142
  sentiment = analyze_sentiment(user_input)
143
 
144
- # Generating suggestions
145
  detected_emotion = emotion.split(": ")[-1]
146
  suggestions = generate_suggestions(detected_emotion)
147
  suggestions_df = pd.DataFrame(suggestions)
148
 
149
- return history, sentiment, emotion, suggestions_df
 
 
 
150
 
151
- # Custom CSS for Beautification
152
  custom_css = """
153
  body {
154
  background: linear-gradient(135deg, #28a745, #218838);
155
- font-family: 'Arial', sans-serif;
156
  color: black;
157
  }
158
- #component-0 span {
159
- color: white;
160
- }
161
  button {
162
- background-color: #20c997;
163
  color: white;
164
- padding: 12px 20px;
165
  font-size: 16px;
166
- border-radius: 12px;
167
  cursor: pointer;
168
  }
169
  button:hover {
170
- background-color: #17a2b8;
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-radius: 6px;
 
 
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 your well-being journey with AI πŸ’š")
187
 
 
188
  with gr.Row():
189
- user_input = gr.Textbox(lines=2, placeholder="How can I support you today?", label="Your Input")
190
-
 
 
 
 
 
 
 
 
 
191
  with gr.Row():
192
- submit_button = gr.Button("Submit", elem_id="submit")
193
-
 
 
 
 
194
  with gr.Row():
195
- chatbot_out = gr.Chatbot(label="Chat History")
196
- sentiment_out = gr.Textbox(label="Sentiment Analysis")
197
- emotion_out = gr.Textbox(label="Detected Emotion")
198
-
199
  with gr.Row():
200
- suggestions_out = gr.DataFrame(label="Suggestions", headers=["Title", "Link"])
201
-
 
202
  submit_button.click(
203
  well_being_app,
204
- inputs=[user_input, chatbot_out],
205
- outputs=[chatbot_out, sentiment_out, emotion_out, suggestions_out],
206
  )
207
 
208
- # Launch App
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()