Spaces:
Runtime error
Runtime error
Commit ·
1d4d789
1
Parent(s): 77e64c9
fixed emotions issues
Browse files
app.py
CHANGED
|
@@ -53,10 +53,10 @@ emotions.update({
|
|
| 53 |
|
| 54 |
# Add keywords for new emotions in `analyze_history`
|
| 55 |
emotion_keywords = {
|
| 56 |
-
"happy": ["happy", "joy", "excited", "awesome"],
|
| 57 |
"playful": ["fun", "play", "joke", "quirky"],
|
| 58 |
"curious": ["curious", "wonder", "question", "thinking"],
|
| 59 |
-
"thoughtful": ["sad", "thoughtful", "hmm", "ponder"],
|
| 60 |
"concerned": ["error", "wrong", "problem", "issue", "stuck"],
|
| 61 |
"mischievous": ["trouble", "mischief", "sneaky", "prank"]
|
| 62 |
}
|
|
@@ -65,6 +65,7 @@ current_emotion = "happy"
|
|
| 65 |
|
| 66 |
# Analyze history for emotional state
|
| 67 |
def analyze_history(history):
|
|
|
|
| 68 |
recent_messages = " ".join(history[-5:]).lower()
|
| 69 |
print(f"Analyzing history: {recent_messages}") # Debug log
|
| 70 |
|
|
@@ -73,7 +74,7 @@ def analyze_history(history):
|
|
| 73 |
for emotion, keywords in emotion_keywords.items()}
|
| 74 |
print(f"Keyword counts: {keyword_counts}")
|
| 75 |
|
| 76 |
-
# Perform sentiment analysis
|
| 77 |
sentiment_result = sentiment_analyzer(recent_messages)
|
| 78 |
sentiment = sentiment_result[0]["label"]
|
| 79 |
sentiment_score = sentiment_result[0]["score"]
|
|
@@ -89,11 +90,22 @@ def analyze_history(history):
|
|
| 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 *
|
|
|
|
|
|
|
| 93 |
print(f"Combined scores: {combined_scores}")
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
print(f"Detected emotion: {detected_emotion}")
|
| 98 |
|
| 99 |
return detected_emotion
|
|
@@ -150,11 +162,23 @@ def chat(input_text):
|
|
| 150 |
for artifact in artifacts:
|
| 151 |
response = response.replace(artifact, "").strip()
|
| 152 |
|
| 153 |
-
if
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
else:
|
| 156 |
response = response.strip()
|
| 157 |
|
|
|
|
| 158 |
# Add Rena's response to the conversation history
|
| 159 |
conversation_history.append(f"Rena: {response}")
|
| 160 |
|
|
|
|
| 53 |
|
| 54 |
# Add keywords for new emotions in `analyze_history`
|
| 55 |
emotion_keywords = {
|
| 56 |
+
"happy": ["happy", "joy", "excited", "awesome", "great", "fantastic"],
|
| 57 |
"playful": ["fun", "play", "joke", "quirky"],
|
| 58 |
"curious": ["curious", "wonder", "question", "thinking"],
|
| 59 |
+
"thoughtful": ["sad", "thoughtful", "hmm", "ponder", "upset"],
|
| 60 |
"concerned": ["error", "wrong", "problem", "issue", "stuck"],
|
| 61 |
"mischievous": ["trouble", "mischief", "sneaky", "prank"]
|
| 62 |
}
|
|
|
|
| 65 |
|
| 66 |
# Analyze history for emotional state
|
| 67 |
def analyze_history(history):
|
| 68 |
+
# Join the last 5 messages for context
|
| 69 |
recent_messages = " ".join(history[-5:]).lower()
|
| 70 |
print(f"Analyzing history: {recent_messages}") # Debug log
|
| 71 |
|
|
|
|
| 74 |
for emotion, keywords in emotion_keywords.items()}
|
| 75 |
print(f"Keyword counts: {keyword_counts}")
|
| 76 |
|
| 77 |
+
# Perform sentiment analysis on recent messages
|
| 78 |
sentiment_result = sentiment_analyzer(recent_messages)
|
| 79 |
sentiment = sentiment_result[0]["label"]
|
| 80 |
sentiment_score = sentiment_result[0]["score"]
|
|
|
|
| 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 as needed
|
| 94 |
+
|
| 95 |
+
# Debug combined scores
|
| 96 |
print(f"Combined scores: {combined_scores}")
|
| 97 |
|
| 98 |
+
# Prevent frequent unnecessary changes by requiring a significant score difference
|
| 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: Default to the previous emotion or use the first detected one
|
| 103 |
+
if len(detected_emotions) > 1:
|
| 104 |
+
print(f"Tie detected. Using current emotion: {current_emotion}")
|
| 105 |
+
return current_emotion
|
| 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
|
|
|
|
| 162 |
for artifact in artifacts:
|
| 163 |
response = response.replace(artifact, "").strip()
|
| 164 |
|
| 165 |
+
# Only add emotional context if the emotion changes significantly
|
| 166 |
+
if current_emotion != previous_emotion and emotions.get(current_emotion):
|
| 167 |
+
# Subtle integration into the response
|
| 168 |
+
if current_emotion == "concerned":
|
| 169 |
+
response = f"That sounds like an issue! Let me help. 💖 {response}"
|
| 170 |
+
elif current_emotion == "happy":
|
| 171 |
+
response = f"I'm feeling great about this! 😊 {response}"
|
| 172 |
+
elif current_emotion == "playful":
|
| 173 |
+
response = f"This is fun! Let’s see what we can do. 😜 {response}"
|
| 174 |
+
elif current_emotion == "flirty":
|
| 175 |
+
response = f"You're making me blush! 😘 {response}"
|
| 176 |
+
elif current_emotion == "thoughtful":
|
| 177 |
+
response = f"Hmm, let me think about this carefully... 🧐 {response}"
|
| 178 |
else:
|
| 179 |
response = response.strip()
|
| 180 |
|
| 181 |
+
|
| 182 |
# Add Rena's response to the conversation history
|
| 183 |
conversation_history.append(f"Rena: {response}")
|
| 184 |
|