Spaces:
Runtime error
Runtime error
Commit ·
da1b74c
1
Parent(s): 187e94f
fixed emotions issues
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import spaces
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
|
@@ -33,6 +33,7 @@ If you encounter unclear input, politely ask for clarification instead of guessi
|
|
| 33 |
|
| 34 |
Respond to the following input:
|
| 35 |
"""
|
|
|
|
| 36 |
|
| 37 |
# Emotional states
|
| 38 |
emotions = {
|
|
@@ -51,32 +52,52 @@ emotions.update({
|
|
| 51 |
})
|
| 52 |
|
| 53 |
# Add keywords for new emotions in `analyze_history`
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
current_emotion = "happy"
|
| 57 |
|
| 58 |
# Analyze history for emotional state
|
| 59 |
def analyze_history(history):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
else:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
|
|
|
| 33 |
|
| 34 |
Respond to the following input:
|
| 35 |
"""
|
| 36 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 37 |
|
| 38 |
# Emotional states
|
| 39 |
emotions = {
|
|
|
|
| 52 |
})
|
| 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 |
+
}
|
| 63 |
|
| 64 |
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 |
+
|
| 71 |
+
# Count keyword matches
|
| 72 |
+
keyword_counts = {emotion: sum(recent_messages.count(keyword) for keyword in keywords)
|
| 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"]
|
| 80 |
+
print(f"Sentiment analysis: {sentiment}, Score: {sentiment_score}")
|
| 81 |
+
|
| 82 |
+
# Determine sentiment-based emotion
|
| 83 |
+
if sentiment == "POSITIVE":
|
| 84 |
+
sentiment_emotion = "happy"
|
| 85 |
+
elif sentiment == "NEGATIVE":
|
| 86 |
+
sentiment_emotion = "thoughtful"
|
| 87 |
else:
|
| 88 |
+
sentiment_emotion = "curious"
|
| 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 * 2 # Give more weight to sentiment analysis
|
| 93 |
+
print(f"Combined scores: {combinsed_scores}")
|
| 94 |
+
|
| 95 |
+
# Choose the emotion with the highest score
|
| 96 |
+
detected_emotion = max(combined_scores, key=combined_scores.get)
|
| 97 |
+
print(f"Detected emotion: {detected_emotion}")
|
| 98 |
+
|
| 99 |
+
return detected_emotions
|
| 100 |
+
|
| 101 |
|
| 102 |
|
| 103 |
|