Spaces:
Sleeping
Sleeping
Update model.py
Browse files
model.py
CHANGED
|
@@ -338,11 +338,11 @@ def generate_response(emotions, tone="neutral", context=None):
|
|
| 338 |
"It’s wonderful to feel love! Want to share more?"
|
| 339 |
],
|
| 340 |
"nervousness": [
|
| 341 |
-
"It’s
|
| 342 |
-
"I
|
| 343 |
-
"
|
| 344 |
-
"
|
| 345 |
-
"
|
| 346 |
],
|
| 347 |
"optimism": [
|
| 348 |
"That’s great! Optimism can change everything. What’s giving you hope today? 🌞",
|
|
@@ -425,10 +425,19 @@ def generate_response(emotions, tone="neutral", context=None):
|
|
| 425 |
|
| 426 |
# Smooth Emotion Transitions
|
| 427 |
def smooth_emotion_transition(prev_emotions, current_emotions):
|
|
|
|
|
|
|
|
|
|
| 428 |
if "joy" in prev_emotions and "sadness" in current_emotions:
|
| 429 |
return "That’s quite a mix of emotions. Let’s talk about it! "
|
| 430 |
elif "anger" in prev_emotions and "relief" in current_emotions:
|
| 431 |
-
return "It sounds like you’ve been through a lot. "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
else:
|
| 433 |
return ""
|
| 434 |
|
|
@@ -442,10 +451,10 @@ def process_multiple_sentences(text, context):
|
|
| 442 |
emotions = predict_emotions(sentence)
|
| 443 |
all_emotions.extend(emotions)
|
| 444 |
|
| 445 |
-
|
| 446 |
-
context.update(sentence,
|
| 447 |
|
| 448 |
-
response = generate_response(
|
| 449 |
responses.append(response)
|
| 450 |
|
| 451 |
return merge_responses(responses, all_emotions)
|
|
@@ -455,25 +464,51 @@ def merge_responses(responses, emotions):
|
|
| 455 |
if len(responses) == 1:
|
| 456 |
return responses[0]
|
| 457 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
dominant_emotion = max(set(emotions), key=emotions.count)
|
| 459 |
|
| 460 |
-
|
| 461 |
-
|
|
|
|
| 462 |
elif "anger" in emotions and "relief" in emotions:
|
| 463 |
-
transition = "It sounds like you’ve been through a lot. "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
else:
|
| 465 |
transition = ""
|
| 466 |
|
| 467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
-
# Chatbot Response Function
|
| 470 |
def chatbot_response(text, context):
|
| 471 |
try:
|
| 472 |
if not text.strip():
|
| 473 |
-
return ""
|
| 474 |
|
| 475 |
emotions = predict_emotions(text)
|
| 476 |
-
tone =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
# Generate chatbot response
|
| 479 |
response = generate_response(emotions, tone, context)
|
|
|
|
| 338 |
"It’s wonderful to feel love! Want to share more?"
|
| 339 |
],
|
| 340 |
"nervousness": [
|
| 341 |
+
"It’s completely normal to feel this way. You’ve got this! 💪",
|
| 342 |
+
"I totally understand. Do you want some tips for staying calm?",
|
| 343 |
+
"Nerves can mean you care about doing well! What’s on your mind?",
|
| 344 |
+
"You’re going to do great! Want to talk through any concerns?",
|
| 345 |
+
"Feeling nervous shows this is important to you. How can I support you?"
|
| 346 |
],
|
| 347 |
"optimism": [
|
| 348 |
"That’s great! Optimism can change everything. What’s giving you hope today? 🌞",
|
|
|
|
| 425 |
|
| 426 |
# Smooth Emotion Transitions
|
| 427 |
def smooth_emotion_transition(prev_emotions, current_emotions):
|
| 428 |
+
if not prev_emotions:
|
| 429 |
+
return current_emotions
|
| 430 |
+
|
| 431 |
if "joy" in prev_emotions and "sadness" in current_emotions:
|
| 432 |
return "That’s quite a mix of emotions. Let’s talk about it! "
|
| 433 |
elif "anger" in prev_emotions and "relief" in current_emotions:
|
| 434 |
+
return "It sounds like you’ve been through a lot, but I’m glad you’re feeling some relief. "
|
| 435 |
+
elif "fear" in prev_emotions and "excitement" in current_emotions:
|
| 436 |
+
return "It’s completely normal to feel both excited and a little anxious. That means it’s important to you! "
|
| 437 |
+
elif "confusion" in prev_emotions and "realization" in current_emotions:
|
| 438 |
+
return "It sounds like things are becoming clearer for you. That’s great! "
|
| 439 |
+
elif "nervousness" in prev_emotions and "pride" in current_emotions:
|
| 440 |
+
return "You might have started off nervous, but it sounds like you’re feeling more confident now. "
|
| 441 |
else:
|
| 442 |
return ""
|
| 443 |
|
|
|
|
| 451 |
emotions = predict_emotions(sentence)
|
| 452 |
all_emotions.extend(emotions)
|
| 453 |
|
| 454 |
+
transition_text = smooth_emotion_transition(context.previous_emotions, emotions)
|
| 455 |
+
context.update(sentence, emotions, transition_text)
|
| 456 |
|
| 457 |
+
response = generate_response(emotions, context=context)
|
| 458 |
responses.append(response)
|
| 459 |
|
| 460 |
return merge_responses(responses, all_emotions)
|
|
|
|
| 464 |
if len(responses) == 1:
|
| 465 |
return responses[0]
|
| 466 |
|
| 467 |
+
# Define emotion categories
|
| 468 |
+
positive_emotions = {"joy", "admiration", "optimism", "pride", "relief", "love", "gratitude", "excitement", "approval", "curiosity", "desire", "realization"}
|
| 469 |
+
negative_emotions = {"sadness", "anger", "fear", "disappointment", "remorse", "grief", "disgust", "disapproval", "annoyance"}
|
| 470 |
+
neutral_emotions = {"neutral", "confusion", "nervousness", "surprise", "caring"}
|
| 471 |
+
|
| 472 |
+
# Find the dominant emotion (most frequently occurring)
|
| 473 |
dominant_emotion = max(set(emotions), key=emotions.count)
|
| 474 |
|
| 475 |
+
# Transition phrases based on emotion types
|
| 476 |
+
if any(e in positive_emotions for e in emotions) and any(e in negative_emotions for e in emotions):
|
| 477 |
+
transition = "I can see you’re experiencing a mix of emotions. Let’s explore this together. "
|
| 478 |
elif "anger" in emotions and "relief" in emotions:
|
| 479 |
+
transition = "It sounds like you’ve been through a lot, but I’m glad there’s some relief. "
|
| 480 |
+
elif "nervousness" in emotions and "excitement" in emotions:
|
| 481 |
+
transition = "It sounds like a mix of nerves and excitement—totally normal! You must really care about this. "
|
| 482 |
+
elif "fear" in emotions and "excitement" in emotions:
|
| 483 |
+
transition = "It’s completely natural to feel both excited and a little scared. That just means it matters to you! "
|
| 484 |
+
elif "disappointment" in emotions and "approval" in emotions:
|
| 485 |
+
transition = "I hear that there were some ups and downs in this experience. Let’s talk about both sides. "
|
| 486 |
+
elif "confusion" in emotions and "realization" in emotions:
|
| 487 |
+
transition = "It sounds like you were uncertain at first, but now things are becoming clearer. "
|
| 488 |
else:
|
| 489 |
transition = ""
|
| 490 |
|
| 491 |
+
# Merge responses with varied connectors
|
| 492 |
+
if len(responses) == 2:
|
| 493 |
+
return transition + " ".join(responses)
|
| 494 |
+
else:
|
| 495 |
+
# Use varied sentence connectors to avoid repetition
|
| 496 |
+
connectors = ["On another note,", "At the same time,", "Also,", "One more thing,", "Additionally,"]
|
| 497 |
+
merged_responses = [responses[0]] + [f"{random.choice(connectors)} {r}" for r in responses[1:]]
|
| 498 |
+
return transition + " ".join(merged_responses)
|
| 499 |
|
| 500 |
+
# Chatbot Response Function
|
| 501 |
def chatbot_response(text, context):
|
| 502 |
try:
|
| 503 |
if not text.strip():
|
| 504 |
+
return "I'm here for you. Feel free to share your thoughts."
|
| 505 |
|
| 506 |
emotions = predict_emotions(text)
|
| 507 |
+
tone = analyze_tone(text)
|
| 508 |
+
|
| 509 |
+
# Process multi-sentence input
|
| 510 |
+
if ". " in text:
|
| 511 |
+
return process_multiple_sentences(text, context)
|
| 512 |
|
| 513 |
# Generate chatbot response
|
| 514 |
response = generate_response(emotions, tone, context)
|