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

Fixed behaviour issue

Browse files
Files changed (2) hide show
  1. app.py +78 -26
  2. test_model.py +68 -0
app.py CHANGED
@@ -88,49 +88,92 @@ def respond(message, history):
88
  if check_crisis_keywords(message):
89
  return get_crisis_response()
90
 
91
- # Build conversation history with Aura's system prompt
92
- conversation = AURA_SYSTEM_PROMPT + tokenizer.eos_token
93
 
94
- for user_msg, bot_msg in history:
95
- conversation += f"Human: {user_msg}{tokenizer.eos_token}Aura: {bot_msg}{tokenizer.eos_token}"
96
 
97
- # Add current message with Aura context
98
- conversation += f"Human: {message}{tokenizer.eos_token}Aura: "
 
 
 
99
 
100
  # Tokenize
101
  input_ids = tokenizer.encode(conversation, return_tensors="pt")
102
 
103
- # Limit input length to prevent memory issues
104
- if input_ids.shape[1] > 800:
105
- input_ids = input_ids[:, -800:]
106
-
107
- # Generate response with more empathetic parameters
108
  with torch.no_grad():
109
- output = model.generate(
110
  input_ids,
111
- max_new_tokens=120, # Slightly longer for empathetic responses
 
 
112
  do_sample=True,
113
- top_p=0.85, # More focused responses
114
- temperature=0.7, # Less random, more consistent
115
  pad_token_id=tokenizer.eos_token_id,
116
  eos_token_id=tokenizer.eos_token_id,
117
- no_repeat_ngram_size=3,
118
- repetition_penalty=1.1
 
119
  )
120
 
121
- # Decode response
122
- raw_response = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True).strip()
 
 
 
123
 
124
- # Format response with Aura's personality
125
- if raw_response:
126
- formatted_response = format_aura_response(raw_response)
127
- return formatted_response
 
128
  else:
129
- return "I hear you, and I want you to know that I'm here to listen. Sometimes it takes a moment to find the right words."
130
 
131
  except Exception as e:
132
  print(f"Error: {e}")
133
- return "I'm sorry, I'm having trouble responding right now. But please know that I'm here for you, and your feelings are valid."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  # Create Gradio interface
136
  with gr.Blocks(title="Aura - Your Supportive Friend") as demo:
@@ -187,4 +230,13 @@ with gr.Blocks(title="Aura - Your Supportive Friend") as demo:
187
 
188
  if __name__ == "__main__":
189
  demo.queue()
190
- demo.launch(share=True) # This creates a public link
 
 
 
 
 
 
 
 
 
 
88
  if check_crisis_keywords(message):
89
  return get_crisis_response()
90
 
91
+ # Build conversation history - SIMPLIFIED for DialoGPT
92
+ conversation = ""
93
 
94
+ # Only include last 2-3 exchanges to avoid overwhelming the model
95
+ recent_history = history[-2:] if len(history) > 2 else history
96
 
97
+ for user_msg, bot_msg in recent_history:
98
+ conversation += f"{user_msg}{tokenizer.eos_token}{bot_msg}{tokenizer.eos_token}"
99
+
100
+ # Add current message
101
+ conversation += f"{message}{tokenizer.eos_token}"
102
 
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
123
+ raw_response = tokenizer.decode(
124
+ chat_history_ids[:, input_ids.shape[-1]:][0],
125
+ skip_special_tokens=True
126
+ ).strip()
127
 
128
+ # Apply Aura's empathetic formatting to the response
129
+ if raw_response and len(raw_response) > 1:
130
+ # Add empathetic framing
131
+ aura_response = add_empathy_to_response(raw_response, message)
132
+ return aura_response
133
  else:
134
+ return get_fallback_aura_response(message)
135
 
136
  except Exception as e:
137
  print(f"Error: {e}")
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:
 
230
 
231
  if __name__ == "__main__":
232
  demo.queue()
233
+ # Check if running on Hugging Face Spaces
234
+ import os
235
+ if "SPACE_ID" in os.environ:
236
+ # Running on HF Spaces - public by default
237
+ demo.launch()
238
+ print(f"🌐 Your chatbot is publicly available at: https://huggingface.co/spaces/{os.environ.get('SPACE_AUTHOR_NAME', 'your-username')}/{os.environ.get('SPACE_REPO_NAME', 'chatbot')}")
239
+ else:
240
+ # Running locally - create public link
241
+ demo.launch(share=True)
242
+ print("🌐 Public link generated above ⬆️")
test_model.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to validate DialoGPT model response generation
4
+ """
5
+
6
+ import torch
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
+
9
+ def test_model():
10
+ print("Loading DialoGPT-medium for testing...")
11
+
12
+ # Load model and tokenizer
13
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
14
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
15
+
16
+ if tokenizer.pad_token is None:
17
+ tokenizer.pad_token = tokenizer.eos_token
18
+
19
+ print("Model loaded successfully!")
20
+
21
+ # Test conversation
22
+ test_messages = [
23
+ "I feel sad today",
24
+ "What should I do?",
25
+ "Hello"
26
+ ]
27
+
28
+ for i, message in enumerate(test_messages):
29
+ print(f"\n--- Test {i+1}: '{message}' ---")
30
+
31
+ # Simple conversation format
32
+ conversation = f"{message}{tokenizer.eos_token}"
33
+ input_ids = tokenizer.encode(conversation, return_tensors="pt")
34
+
35
+ # Generate response with conservative settings
36
+ with torch.no_grad():
37
+ chat_history_ids = model.generate(
38
+ input_ids,
39
+ max_length=input_ids.shape[-1] + 50,
40
+ num_beams=5,
41
+ no_repeat_ngram_size=3,
42
+ do_sample=True,
43
+ early_stopping=True,
44
+ pad_token_id=tokenizer.eos_token_id,
45
+ eos_token_id=tokenizer.eos_token_id,
46
+ temperature=0.9,
47
+ top_k=50,
48
+ top_p=0.9
49
+ )
50
+
51
+ # Decode response
52
+ response = tokenizer.decode(
53
+ chat_history_ids[:, input_ids.shape[-1]:][0],
54
+ skip_special_tokens=True
55
+ ).strip()
56
+
57
+ print(f"Raw response: '{response}'")
58
+ print(f"Response length: {len(response)} characters")
59
+
60
+ if len(response) > 1:
61
+ print("✅ Good response generated")
62
+ else:
63
+ print("⚠️ Short/empty response")
64
+
65
+ print("\n✅ Model testing complete!")
66
+
67
+ if __name__ == "__main__":
68
+ test_model()