kaoruhotarubi commited on
Commit
f861ab7
·
1 Parent(s): 303665d

fixed emotions issues

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -143,9 +143,8 @@ def chat(input_text):
143
  # Limit the size of the conversation history
144
  conversation_history = truncate_history(conversation_history, max_tokens=1024)
145
 
146
-
147
  # Update current emotion based on conversation history
148
- previus_emotion = current_emotion
149
  current_emotion = analyze_history(conversation_history)
150
 
151
  # Combine base prompt and conversation history
@@ -159,22 +158,22 @@ def chat(input_text):
159
  Respond concisely and directly to the user's input. Avoid repeating the user's input unless clarification is needed.
160
 
161
  Rena:"""
 
162
  # Tokenize and generate a response
163
  inputs = tokenizer(final_prompt, return_tensors="pt").to('cuda')
164
  outputs = model.generate(**inputs, max_new_tokens=300, do_sample=True, temperature=0.7, repetition_penalty=1.2, top_p=0.9)
165
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
166
 
167
- # Remove any artifacts from the response
168
  artifacts = [base_prompt, "### Conversation History ###", "Rena:", "Assistant:", "<|assistant|>", "<|user|>"]
169
  for artifact in artifacts:
170
  response = response.replace(artifact, "").strip()
171
 
172
- if input_text.lower() in response.lower():
173
- response = response.replace(input_text, "").strip()
174
-
175
- # Only add emotional context if the emotion changes significantly
176
- if current_emotion != previous_emotion and emotions.get(current_emotion):
177
- # Subtle integration of emotion
178
  if current_emotion == "concerned":
179
  emotional_prefix = "It sounds like there's something challenging! Let me help. 💖"
180
  elif current_emotion == "happy":
@@ -189,17 +188,10 @@ def chat(input_text):
189
  emotional_prefix = ""
190
 
191
  # Add the prefix naturally to the response
192
- if emotional_prefix:
193
- response = f"{emotional_prefix} {response}"
194
- else:
195
- # Keep the response clean
196
- response = response.strip()
197
-
198
-
199
 
200
  # Add Rena's response to the conversation history
201
  conversation_history.append(f"Rena: {response}")
202
-
203
 
204
  # Handle specific inputs
205
  if "who made you" in input_text.lower():
@@ -220,11 +212,10 @@ def chat(input_text):
220
  if "error" in input_text.lower() and not any("error" in msg.lower() for msg in conversation_history):
221
  witty_remark = random.choice(error_responses)
222
  response += f" {witty_remark}"
 
 
223
  if not response.strip():
224
  response = "Hmm, I’m not sure how to respond to that. Can you try rephrasing?"
225
-
226
-
227
-
228
 
229
  return response
230
 
@@ -233,6 +224,7 @@ def chat(input_text):
233
 
234
 
235
 
 
236
  # Custom CSS for avatar styling
237
  css = """
238
  #rena_avatar img {
 
143
  # Limit the size of the conversation history
144
  conversation_history = truncate_history(conversation_history, max_tokens=1024)
145
 
 
146
  # Update current emotion based on conversation history
147
+ previous_emotion = current_emotion # Fixed typo here
148
  current_emotion = analyze_history(conversation_history)
149
 
150
  # Combine base prompt and conversation history
 
158
  Respond concisely and directly to the user's input. Avoid repeating the user's input unless clarification is needed.
159
 
160
  Rena:"""
161
+
162
  # Tokenize and generate a response
163
  inputs = tokenizer(final_prompt, return_tensors="pt").to('cuda')
164
  outputs = model.generate(**inputs, max_new_tokens=300, do_sample=True, temperature=0.7, repetition_penalty=1.2, top_p=0.9)
165
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
166
 
167
+ # Remove artifacts and repeated user input
168
  artifacts = [base_prompt, "### Conversation History ###", "Rena:", "Assistant:", "<|assistant|>", "<|user|>"]
169
  for artifact in artifacts:
170
  response = response.replace(artifact, "").strip()
171
 
172
+ if input_text.strip().lower() in response.strip().lower():
173
+ response = response.replace(input_text.strip(), "").strip()
174
+
175
+ # Add emotional context if the emotion changes significantly
176
+ if current_emotion != previous_emotion:
 
177
  if current_emotion == "concerned":
178
  emotional_prefix = "It sounds like there's something challenging! Let me help. 💖"
179
  elif current_emotion == "happy":
 
188
  emotional_prefix = ""
189
 
190
  # Add the prefix naturally to the response
191
+ response = f"{emotional_prefix} {response}".strip()
 
 
 
 
 
 
192
 
193
  # Add Rena's response to the conversation history
194
  conversation_history.append(f"Rena: {response}")
 
195
 
196
  # Handle specific inputs
197
  if "who made you" in input_text.lower():
 
212
  if "error" in input_text.lower() and not any("error" in msg.lower() for msg in conversation_history):
213
  witty_remark = random.choice(error_responses)
214
  response += f" {witty_remark}"
215
+
216
+ # Handle fallback if response is empty
217
  if not response.strip():
218
  response = "Hmm, I’m not sure how to respond to that. Can you try rephrasing?"
 
 
 
219
 
220
  return response
221
 
 
224
 
225
 
226
 
227
+
228
  # Custom CSS for avatar styling
229
  css = """
230
  #rena_avatar img {