Spaces:
Runtime error
Runtime error
Commit ยท
31cc637
1
Parent(s): f7d0973
fixed emotions issues
Browse files
app.py
CHANGED
|
@@ -81,16 +81,15 @@ def analyze_history(history):
|
|
| 81 |
print(f"Sentiment analysis: {sentiment}, Score: {sentiment_score}")
|
| 82 |
|
| 83 |
# Determine sentiment-based emotion
|
|
|
|
| 84 |
if sentiment == "POSITIVE":
|
| 85 |
sentiment_emotion = "happy"
|
| 86 |
elif sentiment == "NEGATIVE":
|
| 87 |
sentiment_emotion = "thoughtful"
|
| 88 |
-
else:
|
| 89 |
-
sentiment_emotion = "curious"
|
| 90 |
|
| 91 |
# Combine results using weights
|
| 92 |
combined_scores = {emotion: keyword_counts.get(emotion, 0) for emotion in emotion_keywords}
|
| 93 |
-
combined_scores[sentiment_emotion] += sentiment_score * 1.5 # Adjust sentiment weight
|
| 94 |
|
| 95 |
# Debug combined scores
|
| 96 |
print(f"Combined scores: {combined_scores}")
|
|
@@ -99,24 +98,18 @@ def analyze_history(history):
|
|
| 99 |
max_score = max(combined_scores.values())
|
| 100 |
detected_emotions = [emotion for emotion, score in combined_scores.items() if score == max_score]
|
| 101 |
|
| 102 |
-
# Handle ties:
|
| 103 |
if len(detected_emotions) > 1:
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
# Choose the detected emotion if it's a clear winner
|
| 108 |
-
detected_emotion = detected_emotions[0]
|
| 109 |
print(f"Detected emotion: {detected_emotion}")
|
| 110 |
-
|
| 111 |
return detected_emotion
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
# Load the Rena avatar
|
| 121 |
rena_avatar = Image.open("assets/rena2.png") # Ensure the file exists
|
| 122 |
conversation_history = []
|
|
@@ -146,22 +139,19 @@ def chat(input_text):
|
|
| 146 |
conversation_history = truncate_history(conversation_history, max_tokens=1024)
|
| 147 |
|
| 148 |
# Update current emotion based on conversation history
|
| 149 |
-
previous_emotion = current_emotion
|
| 150 |
current_emotion = analyze_history(conversation_history)
|
| 151 |
|
| 152 |
# Combine base prompt and conversation history
|
| 153 |
history = "\n".join(conversation_history)
|
| 154 |
final_prompt = f"""{base_prompt}
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
Respond concisely and directly to the user's input. Avoid repeating the user's input unless clarification is needed.
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
-
|
| 165 |
|
| 166 |
# Tokenize and generate a response
|
| 167 |
inputs = tokenizer(final_prompt, return_tensors="pt").to('cuda')
|
|
@@ -169,7 +159,7 @@ def chat(input_text):
|
|
| 169 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 170 |
|
| 171 |
# Remove artifacts and repeated user input
|
| 172 |
-
artifacts = [base_prompt, "### Conversation History ###", "Rena:", "Assistant:", "<|assistant|>", "<|user|>"]
|
| 173 |
for artifact in artifacts:
|
| 174 |
response = response.replace(artifact, "").strip()
|
| 175 |
|
|
@@ -178,20 +168,7 @@ def chat(input_text):
|
|
| 178 |
|
| 179 |
# Add emotional context if the emotion changes significantly
|
| 180 |
if current_emotion != previous_emotion:
|
| 181 |
-
|
| 182 |
-
emotional_prefix = "It sounds like there's something challenging! Let me help. ๐"
|
| 183 |
-
elif current_emotion == "happy":
|
| 184 |
-
emotional_prefix = "I'm feeling great about this! ๐"
|
| 185 |
-
elif current_emotion == "playful":
|
| 186 |
-
emotional_prefix = "This sounds fun! Letโs dive in. ๐"
|
| 187 |
-
elif current_emotion == "flirty":
|
| 188 |
-
emotional_prefix = "Oh, you're making me blush! ๐"
|
| 189 |
-
elif current_emotion == "thoughtful":
|
| 190 |
-
emotional_prefix = "Hmm, let me think about this carefully... ๐ง"
|
| 191 |
-
else:
|
| 192 |
-
emotional_prefix = ""
|
| 193 |
-
|
| 194 |
-
# Add the prefix naturally to the response
|
| 195 |
response = f"{emotional_prefix} {response}".strip()
|
| 196 |
|
| 197 |
# Add Rena's response to the conversation history
|
|
@@ -229,6 +206,7 @@ def chat(input_text):
|
|
| 229 |
|
| 230 |
|
| 231 |
|
|
|
|
| 232 |
# Custom CSS for avatar styling
|
| 233 |
css = """
|
| 234 |
#rena_avatar img {
|
|
|
|
| 81 |
print(f"Sentiment analysis: {sentiment}, Score: {sentiment_score}")
|
| 82 |
|
| 83 |
# Determine sentiment-based emotion
|
| 84 |
+
sentiment_emotion = "curious" # Default
|
| 85 |
if sentiment == "POSITIVE":
|
| 86 |
sentiment_emotion = "happy"
|
| 87 |
elif sentiment == "NEGATIVE":
|
| 88 |
sentiment_emotion = "thoughtful"
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Combine results using weights
|
| 91 |
combined_scores = {emotion: keyword_counts.get(emotion, 0) for emotion in emotion_keywords}
|
| 92 |
+
combined_scores[sentiment_emotion] += sentiment_score * 1.5 # Adjust sentiment weight
|
| 93 |
|
| 94 |
# Debug combined scores
|
| 95 |
print(f"Combined scores: {combined_scores}")
|
|
|
|
| 98 |
max_score = max(combined_scores.values())
|
| 99 |
detected_emotions = [emotion for emotion, score in combined_scores.items() if score == max_score]
|
| 100 |
|
| 101 |
+
# Handle ties: Add variety by randomizing among ties
|
| 102 |
if len(detected_emotions) > 1:
|
| 103 |
+
detected_emotion = random.choice(detected_emotions)
|
| 104 |
+
print(f"Tie detected. Randomly chosen emotion: {detected_emotion}")
|
| 105 |
+
else:
|
| 106 |
+
detected_emotion = detected_emotions[0]
|
| 107 |
|
|
|
|
|
|
|
| 108 |
print(f"Detected emotion: {detected_emotion}")
|
|
|
|
| 109 |
return detected_emotion
|
| 110 |
|
| 111 |
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Load the Rena avatar
|
| 114 |
rena_avatar = Image.open("assets/rena2.png") # Ensure the file exists
|
| 115 |
conversation_history = []
|
|
|
|
| 139 |
conversation_history = truncate_history(conversation_history, max_tokens=1024)
|
| 140 |
|
| 141 |
# Update current emotion based on conversation history
|
| 142 |
+
previous_emotion = current_emotion
|
| 143 |
current_emotion = analyze_history(conversation_history)
|
| 144 |
|
| 145 |
# Combine base prompt and conversation history
|
| 146 |
history = "\n".join(conversation_history)
|
| 147 |
final_prompt = f"""{base_prompt}
|
| 148 |
+
### Instructions ###
|
| 149 |
+
Respond concisely and directly to the user's input. Avoid repeating the user's input unless clarification is needed.
|
|
|
|
| 150 |
|
| 151 |
+
### Conversation History ###
|
| 152 |
+
{history}
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
Rena:"""
|
| 155 |
|
| 156 |
# Tokenize and generate a response
|
| 157 |
inputs = tokenizer(final_prompt, return_tensors="pt").to('cuda')
|
|
|
|
| 159 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 160 |
|
| 161 |
# Remove artifacts and repeated user input
|
| 162 |
+
artifacts = [base_prompt, "### Conversation History ###", "Rena:", "Assistant:", "<|assistant|>", "<|user|>", "### Instructions ###"]
|
| 163 |
for artifact in artifacts:
|
| 164 |
response = response.replace(artifact, "").strip()
|
| 165 |
|
|
|
|
| 168 |
|
| 169 |
# Add emotional context if the emotion changes significantly
|
| 170 |
if current_emotion != previous_emotion:
|
| 171 |
+
emotional_prefix = emotions.get(current_emotion, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
response = f"{emotional_prefix} {response}".strip()
|
| 173 |
|
| 174 |
# Add Rena's response to the conversation history
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
+
|
| 210 |
# Custom CSS for avatar styling
|
| 211 |
css = """
|
| 212 |
#rena_avatar img {
|