Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,13 +13,32 @@ from transformers import (
|
|
13 |
from datasets import load_dataset # For loading datasets (e.g., speaker embeddings)
|
14 |
import torch # For tensor operations
|
15 |
import soundfile as sf # For saving audio as .wav files
|
|
|
16 |
|
17 |
##########################################
|
18 |
# Streamlit application title and input
|
19 |
##########################################
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
##########################################
|
25 |
# Step 1: Sentiment Analysis Function
|
@@ -32,11 +51,11 @@ def analyze_dominant_emotion(user_review):
|
|
32 |
"text-classification",
|
33 |
model="Thea231/jhartmann_emotion_finetuning",
|
34 |
return_all_scores=True
|
35 |
-
) # Load
|
36 |
|
37 |
emotion_results = emotion_classifier(user_review)[0] # Get emotion scores for the review
|
38 |
dominant_emotion = max(emotion_results, key=lambda x: x['score']) # Find the emotion with the highest confidence
|
39 |
-
return dominant_emotion
|
40 |
|
41 |
##########################################
|
42 |
# Step 2: Response Generation Function
|
@@ -45,11 +64,11 @@ def response_gen(user_review):
|
|
45 |
"""
|
46 |
Generate a response based on the sentiment of the user's review.
|
47 |
"""
|
48 |
-
#
|
49 |
dominant_emotion = analyze_dominant_emotion(user_review) # Get the dominant emotion
|
50 |
emotion_label = dominant_emotion['label'].lower() # Extract emotion label
|
51 |
|
52 |
-
# Define response templates for each emotion
|
53 |
emotion_prompts = {
|
54 |
"anger": (
|
55 |
"Customer complaint: '{review}'\n\n"
|
@@ -67,7 +86,53 @@ def response_gen(user_review):
|
|
67 |
"- Invites them to explore loyalty programs\n\n"
|
68 |
"Response:"
|
69 |
),
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
}
|
72 |
|
73 |
# Format the prompt with the user's review
|
|
|
13 |
from datasets import load_dataset # For loading datasets (e.g., speaker embeddings)
|
14 |
import torch # For tensor operations
|
15 |
import soundfile as sf # For saving audio as .wav files
|
16 |
+
import sentencepiece # Required by SpeechT5Processor for tokenization
|
17 |
|
18 |
##########################################
|
19 |
# Streamlit application title and input
|
20 |
##########################################
|
21 |
+
|
22 |
+
# Display a deep blue title in a large, visually appealing font
|
23 |
+
st.markdown(
|
24 |
+
"<h1 style='text-align: center; color: #00008B; font-size: 50px;'>🚀 Just Comment</h1>",
|
25 |
+
unsafe_allow_html=True
|
26 |
+
) # Set deep blue title
|
27 |
+
|
28 |
+
# Display a gentle, warm subtitle below the title
|
29 |
+
st.markdown(
|
30 |
+
"<h3 style='text-align: center; color: #5D6D7E; font-style: italic;'>I'm listening to you, my friend~</h3>",
|
31 |
+
unsafe_allow_html=True
|
32 |
+
) # Set a friendly subtitle
|
33 |
+
|
34 |
+
# Add a text area for user input with placeholder and tooltip
|
35 |
+
text = st.text_area(
|
36 |
+
"Enter your comment",
|
37 |
+
placeholder="Type something here...",
|
38 |
+
height=100,
|
39 |
+
help="Write a comment you would like us to respond to!" # Provide tooltip
|
40 |
+
) # Create text input field
|
41 |
+
|
42 |
|
43 |
##########################################
|
44 |
# Step 1: Sentiment Analysis Function
|
|
|
51 |
"text-classification",
|
52 |
model="Thea231/jhartmann_emotion_finetuning",
|
53 |
return_all_scores=True
|
54 |
+
) # Load our fine-tuned emotion classification model
|
55 |
|
56 |
emotion_results = emotion_classifier(user_review)[0] # Get emotion scores for the review
|
57 |
dominant_emotion = max(emotion_results, key=lambda x: x['score']) # Find the emotion with the highest confidence
|
58 |
+
return dominant_emotion # Return the dominant emotion (as a dict with label and score)
|
59 |
|
60 |
##########################################
|
61 |
# Step 2: Response Generation Function
|
|
|
64 |
"""
|
65 |
Generate a response based on the sentiment of the user's review.
|
66 |
"""
|
67 |
+
# Get dominant emotion for the input
|
68 |
dominant_emotion = analyze_dominant_emotion(user_review) # Get the dominant emotion
|
69 |
emotion_label = dominant_emotion['label'].lower() # Extract emotion label
|
70 |
|
71 |
+
# Define response templates for each emotion
|
72 |
emotion_prompts = {
|
73 |
"anger": (
|
74 |
"Customer complaint: '{review}'\n\n"
|
|
|
86 |
"- Invites them to explore loyalty programs\n\n"
|
87 |
"Response:"
|
88 |
),
|
89 |
+
"disgust": (
|
90 |
+
"Customer quality concern: '{review}'\n\n"
|
91 |
+
"As a customer service representative, craft a response that:\n"
|
92 |
+
"- Immediately acknowledges the product issue.\n"
|
93 |
+
"- Explains measures taken in quality control.\n"
|
94 |
+
"- Provides clear return/replacement instructions.\n"
|
95 |
+
"- Offers a goodwill gesture (1-3 sentences).\n\n"
|
96 |
+
"Response:"
|
97 |
+
),
|
98 |
+
"fear": (
|
99 |
+
"Customer safety concern: '{review}'\n\n"
|
100 |
+
"As a customer service representative, craft a reassuring response that:\n"
|
101 |
+
"- Directly addresses the safety worries.\n"
|
102 |
+
"- References relevant certifications or standards.\n"
|
103 |
+
"- Offers a dedicated support contact.\n"
|
104 |
+
"- Provides a satisfaction guarantee (1-3 sentences).\n\n"
|
105 |
+
"Response:"
|
106 |
+
),
|
107 |
+
"neutral": (
|
108 |
+
"Customer feedback: '{review}'\n\n"
|
109 |
+
"As a customer service representative, craft a balanced response that:\n"
|
110 |
+
"- Provides additional relevant product information.\n"
|
111 |
+
"- Highlights key service features.\n"
|
112 |
+
"- Politely requests more detailed feedback.\n"
|
113 |
+
"- Maintains a professional tone (1-3 sentences).\n\n"
|
114 |
+
"Response:"
|
115 |
+
),
|
116 |
+
"sadness": (
|
117 |
+
"Customer disappointment: '{review}'\n\n"
|
118 |
+
"As a customer service representative, craft an empathetic response that:\n"
|
119 |
+
"- Shows genuine understanding of the issue.\n"
|
120 |
+
"- Proposes a personalized recovery solution.\n"
|
121 |
+
"- Offers extended support options.\n"
|
122 |
+
"- Maintains a positive outlook (1-3 sentences).\n\n"
|
123 |
+
"Response:"
|
124 |
+
),
|
125 |
+
"surprise": (
|
126 |
+
"Customer enthusiastic feedback: '{review}'\n\n"
|
127 |
+
"As a customer service representative, craft a response that:\n"
|
128 |
+
"- Matches the customer's positive energy.\n"
|
129 |
+
"- Highlights unexpected product benefits.\n"
|
130 |
+
"- Invites the customer to join community events or programs.\n"
|
131 |
+
"- Maintains the brand's voice (1-3 sentences).\n\n"
|
132 |
+
"Response:"
|
133 |
+
),
|
134 |
+
|
135 |
+
|
136 |
}
|
137 |
|
138 |
# Format the prompt with the user's review
|