Deva1211 commited on
Commit
327ca1a
·
1 Parent(s): e4c9fc7

Fixed model behaviour

Browse files
Files changed (1) hide show
  1. app.py +131 -32
app.py CHANGED
@@ -103,20 +103,18 @@ def respond(message, history):
103
  # Tokenize
104
  input_ids = tokenizer.encode(conversation, return_tensors="pt")
105
 
106
- # Generate response with conservative settings for DialoGPT
107
  with torch.no_grad():
108
  chat_history_ids = model.generate(
109
  input_ids,
110
- max_length=input_ids.shape[-1] + 50, # Shorter responses
111
- num_beams=5,
112
- no_repeat_ngram_size=3,
113
- do_sample=True,
114
- early_stopping=True,
115
- pad_token_id=tokenizer.eos_token_id,
116
- eos_token_id=tokenizer.eos_token_id,
117
  temperature=0.9,
 
 
 
118
  top_k=50,
119
- top_p=0.9
 
120
  )
121
 
122
  # Decode only the new response
@@ -138,42 +136,143 @@ def respond(message, history):
138
  return "I hear you, and I want you to know that I'm here for you. Sometimes I need a moment to find the right words."
139
 
140
  def add_empathy_to_response(response, user_message):
141
- """Add Aura's empathetic touch to the raw response"""
142
- # Detect emotional keywords to tailor empathy
143
- emotional_words = ['sad', 'angry', 'frustrated', 'worried', 'scared', 'lonely', 'hurt', 'upset', 'stressed']
 
 
 
 
144
  user_lower = user_message.lower()
145
 
146
- empathetic_starters = [
147
- "I hear you, and ",
148
- "That sounds really difficult. ",
149
- "I can imagine that feels heavy. ",
150
- "Thank you for sharing that with me. "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  ]
152
 
153
- # Choose appropriate empathetic starter
154
- if any(word in user_lower for word in emotional_words):
155
- starter = "I can imagine that feels really difficult. "
156
- elif "not good enough" in user_lower or "failure" in user_lower:
157
- starter = "I hear you, and those feelings are so valid. "
158
- else:
159
- starter = "I hear you. "
160
 
161
- # Clean up the response and add empathy
162
- cleaned_response = response.replace("Human:", "").replace("Aura:", "").strip()
163
- return f"{starter}{cleaned_response}"
 
 
164
 
165
  def get_fallback_aura_response(user_message):
166
- """Provide appropriate fallback responses based on user input"""
 
167
  user_lower = user_message.lower()
168
 
169
  if "not good enough" in user_lower:
170
- return "I hear you, and I want you to know that those feelings of not being good enough are so valid. It takes courage to share that."
 
 
 
 
 
 
171
  elif "what" in user_lower and "do" in user_lower:
172
- return "I can hear that you're looking for some direction. Rather than telling you what to do, I'm wondering - what feels most important to you right now?"
 
 
 
 
 
 
 
 
 
 
 
 
173
  elif len(user_message.strip()) <= 2: # Short messages like "??"
174
- return "I'm here with you. Sometimes it's hard to find words, and that's okay. Take your time."
 
 
 
 
 
175
  else:
176
- return "I hear you, and I want you to know that I'm here to listen. Your feelings matter to me."
 
 
 
 
 
 
 
 
177
 
178
  # Create Gradio interface
179
  with gr.Blocks(title="Aura - Your Supportive Friend") as demo:
 
103
  # Tokenize
104
  input_ids = tokenizer.encode(conversation, return_tensors="pt")
105
 
106
+ # Generate response with improved settings for more creative, less repetitive responses
107
  with torch.no_grad():
108
  chat_history_ids = model.generate(
109
  input_ids,
110
+ max_length=1250,
 
 
 
 
 
 
111
  temperature=0.9,
112
+ top_p=0.9,
113
+ repetition_penalty=1.2,
114
+ do_sample=True,
115
  top_k=50,
116
+ pad_token_id=tokenizer.eos_token_id,
117
+ no_repeat_ngram_size=3
118
  )
119
 
120
  # Decode only the new response
 
136
  return "I hear you, and I want you to know that I'm here for you. Sometimes I need a moment to find the right words."
137
 
138
  def add_empathy_to_response(response, user_message):
139
+ """Add Aura's empathetic touch to the raw response with high variety"""
140
+ import random
141
+
142
+ # Clean up the response first
143
+ cleaned_response = response.replace("Human:", "").replace("Aura:", "").strip()
144
+
145
+ # Detect context and emotions to provide varied, appropriate responses
146
  user_lower = user_message.lower()
147
 
148
+ # Context-specific empathetic starters with high variety
149
+ if "not good enough" in user_lower or "failure" in user_lower or "worthless" in user_lower:
150
+ starters = [
151
+ "Those feelings are so valid, and I want you to know that ",
152
+ "It takes real courage to share those feelings with me. ",
153
+ "I can hear how much pain you're carrying right now. ",
154
+ "That inner voice can be so harsh sometimes. ",
155
+ "What you're feeling makes complete sense, and "
156
+ ]
157
+ elif "tough day" in user_lower or "bad day" in user_lower or "difficult" in user_lower:
158
+ starters = [
159
+ "Some days really are harder than others, aren't they? ",
160
+ "It sounds like today has been especially challenging. ",
161
+ "Days like this can feel so heavy. ",
162
+ "I'm sorry you're going through such a rough time. ",
163
+ "Thank you for trusting me with how you're feeling today. "
164
+ ]
165
+ elif "overwhelmed" in user_lower or "stressed" in user_lower:
166
+ starters = [
167
+ "That feeling of overwhelm can be so intense. ",
168
+ "It sounds like there's a lot weighing on you right now. ",
169
+ "When everything feels like too much, it's really hard. ",
170
+ "I can imagine how exhausting that must feel. ",
171
+ "Sometimes life piles up in ways that feel impossible to manage. "
172
+ ]
173
+ elif "listen" in user_lower or "someone" in user_lower:
174
+ starters = [
175
+ "I'm right here with you, and I'm listening. ",
176
+ "You have my full attention, and I want you to know ",
177
+ "Thank you for reaching out. I'm here, and ",
178
+ "Sometimes we all need someone to just be present with us. ",
179
+ "I'm grateful you felt safe sharing this with me. "
180
+ ]
181
+ elif any(word in user_lower for word in ['hurt', 'pain', 'sad', 'crying']):
182
+ starters = [
183
+ "I can hear how much you're hurting right now. ",
184
+ "Pain like this is so real and so valid. ",
185
+ "It takes strength to sit with feelings like these. ",
186
+ "Your pain matters, and I want you to know ",
187
+ "I'm holding space for everything you're feeling. "
188
+ ]
189
+ elif "fractured" in user_lower or "injury" in user_lower or "hurt" in user_lower:
190
+ starters = [
191
+ "Oh no, that sounds incredibly painful! ",
192
+ "I'm so sorry that happened to you. ",
193
+ "That must be really scary and painful. ",
194
+ "Injuries like that are no small thing. ",
195
+ "I can only imagine how much that must hurt. "
196
+ ]
197
+ else:
198
+ # General varied empathetic responses
199
+ starters = [
200
+ "Thank you for sharing that with me. ",
201
+ "I can hear that this is important to you. ",
202
+ "It sounds like you're going through something difficult. ",
203
+ "I'm here with you in this moment. ",
204
+ "What you're experiencing sounds really challenging. ",
205
+ "I want you to know that your feelings are completely valid. ",
206
+ "It takes courage to open up like this. ",
207
+ "I'm grateful you felt safe sharing this with me. "
208
+ ]
209
+
210
+ # Choose a random starter for variety
211
+ starter = random.choice(starters)
212
+
213
+ # Add thoughtful questions sometimes to show deeper engagement
214
+ questions = [
215
+ " What's been the hardest part for you?",
216
+ " How long have you been carrying this?",
217
+ " Is there anything that's helped, even a little?",
218
+ " What do you need most right now?",
219
+ " How are you taking care of yourself through this?",
220
+ ""
221
  ]
222
 
223
+ # Add question 30% of the time for engagement variety
224
+ question = random.choice(questions) if random.random() < 0.3 else ""
 
 
 
 
 
225
 
226
+ # Combine everything
227
+ if cleaned_response:
228
+ return f"{starter}{cleaned_response}{question}"
229
+ else:
230
+ return f"{starter}I'm here to listen to whatever you need to share{question}"
231
 
232
  def get_fallback_aura_response(user_message):
233
+ """Provide appropriate fallback responses based on user input with variety"""
234
+ import random
235
  user_lower = user_message.lower()
236
 
237
  if "not good enough" in user_lower:
238
+ responses = [
239
+ "Those feelings of not being good enough are so painful, and I want you to know they're completely valid. It takes real courage to share something so vulnerable.",
240
+ "I can hear how much you're struggling with those feelings about yourself. That inner critic can be so harsh, can't it?",
241
+ "What you're feeling makes complete sense, and I'm grateful you trusted me with something so personal. You're not alone in feeling this way.",
242
+ "That's such a heavy feeling to carry. I want you to know that sharing this with me shows real strength, even when you don't feel strong."
243
+ ]
244
+ return random.choice(responses)
245
  elif "what" in user_lower and "do" in user_lower:
246
+ responses = [
247
+ "I can hear that you're looking for some direction. Rather than telling you what to do, I'm wondering - what feels most important to you right now?",
248
+ "It sounds like you're feeling a bit lost about next steps. Sometimes when we're overwhelmed, it helps to start with what you're feeling rather than what you should do. What's going on for you?",
249
+ "That feeling of not knowing what to do can be so frustrating. Instead of focusing on actions right now, what would feel most supportive for you in this moment?"
250
+ ]
251
+ return random.choice(responses)
252
+ elif "tough day" in user_lower or "bad day" in user_lower:
253
+ responses = [
254
+ "Some days are just harder than others, aren't they? I'm here with you through this difficult time.",
255
+ "I'm sorry today has been so rough for you. Would it help to talk about what's been making it especially difficult?",
256
+ "Days like this can feel so heavy. Thank you for reaching out - you don't have to go through this alone."
257
+ ]
258
+ return random.choice(responses)
259
  elif len(user_message.strip()) <= 2: # Short messages like "??"
260
+ responses = [
261
+ "I'm here with you. Sometimes it's hard to find words, and that's completely okay. Take your time.",
262
+ "You don't need to have the perfect words. I'm here to listen to whatever you're feeling, however you want to express it.",
263
+ "Sometimes feelings are too big for words, and that's alright. I'm here with you in this moment."
264
+ ]
265
+ return random.choice(responses)
266
  else:
267
+ # General varied empathetic fallback responses
268
+ responses = [
269
+ "Thank you for sharing that with me. I can hear that something important is going on for you, and I want you to know I'm here to listen.",
270
+ "I'm grateful you felt safe reaching out. Whatever you're going through, you don't have to face it alone.",
271
+ "It sounds like you're going through something difficult right now. I'm here with you, and your feelings matter to me.",
272
+ "I can sense that this is weighing on you. Thank you for trusting me with what you're experiencing.",
273
+ "Whatever brought you here today, I want you to know that I'm here to listen without judgment. You're not alone."
274
+ ]
275
+ return random.choice(responses)
276
 
277
  # Create Gradio interface
278
  with gr.Blocks(title="Aura - Your Supportive Friend") as demo: