Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,11 @@ from transformers import (
|
|
9 |
SpeechT5HifiGan,
|
10 |
AutoModelForCausalLM,
|
11 |
AutoTokenizer
|
12 |
-
) # For
|
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
|
@@ -45,99 +46,84 @@ def response_gen(user_review):
|
|
45 |
"""
|
46 |
Generate a concise and logical response based on the sentiment of the user's comment.
|
47 |
"""
|
48 |
-
|
49 |
dominant_emotion = analyze_dominant_emotion(user_review) # Get the dominant emotion of the user's comment
|
50 |
emotion_label = dominant_emotion['label'].lower() # Extract the emotion label in lowercase format
|
51 |
|
52 |
# Define response templates for each emotion
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
)
|
119 |
-
},
|
120 |
-
"surprise": {
|
121 |
-
"prompt": (
|
122 |
-
"Customer enthusiastic feedback: '{review}'\n\n"
|
123 |
-
"As a customer service representative, craft a response that:\n"
|
124 |
-
"- Matches customer's positive energy appropriately\n"
|
125 |
-
"- Highlights unexpected product benefits\n"
|
126 |
-
"- Invites to user community/events\n"
|
127 |
-
"- Maintains brand voice (3-4 sentences)\n\n"
|
128 |
-
"Response:"
|
129 |
-
)
|
130 |
-
}
|
131 |
-
|
132 |
# Select the appropriate prompt based on the user's emotion, or default to neutral
|
133 |
prompt = emotion_prompts.get(
|
134 |
-
emotion_label,
|
135 |
f"Neutral feedback: '{user_review}'\n\nWrite a professional and concise response (50-200 words max).\n\nResponse:"
|
136 |
)
|
137 |
|
138 |
# Load the tokenizer and language model for text generation
|
139 |
-
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B")
|
140 |
-
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B")
|
141 |
|
142 |
inputs = tokenizer(prompt, return_tensors="pt") # Tokenize the input prompt
|
143 |
outputs = model.generate(
|
@@ -150,7 +136,7 @@ def response_gen(user_review):
|
|
150 |
|
151 |
# Decode the generated response back into text
|
152 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
153 |
-
print(f" {response}") #
|
154 |
return response # Return the generated response
|
155 |
|
156 |
##########################################
|
@@ -158,9 +144,8 @@ def response_gen(user_review):
|
|
158 |
##########################################
|
159 |
def sound_gen(response):
|
160 |
"""
|
161 |
-
Convert the generated response to speech and save as a .wav file.
|
162 |
"""
|
163 |
-
# Load the pre-trained TTS models for speech synthesis
|
164 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") # Pre-trained processor for TTS
|
165 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts") # Pre-trained TTS model
|
166 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan") # Vocoder for generating waveforms
|
@@ -179,10 +164,8 @@ def sound_gen(response):
|
|
179 |
|
180 |
# Save the audio as a .wav file
|
181 |
sf.write("customer_service_response.wav", speech.numpy(), samplerate=16000)
|
182 |
-
# Play the generated audio in the Streamlit app
|
183 |
st.audio("customer_service_response.wav") # Embed an audio player in the web app
|
184 |
|
185 |
-
|
186 |
##########################################
|
187 |
# Main Function
|
188 |
##########################################
|
@@ -197,4 +180,4 @@ def main():
|
|
197 |
|
198 |
# Run the main function when the script is executed
|
199 |
if __name__ == "__main__":
|
200 |
-
main()
|
|
|
9 |
SpeechT5HifiGan,
|
10 |
AutoModelForCausalLM,
|
11 |
AutoTokenizer
|
12 |
+
) # For sentiment analysis, text-to-speech, and text generation
|
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 # For tokenization (required by SpeechT5Processor)
|
17 |
|
18 |
##########################################
|
19 |
# Streamlit application title and input
|
|
|
46 |
"""
|
47 |
Generate a concise and logical response based on the sentiment of the user's comment.
|
48 |
"""
|
|
|
49 |
dominant_emotion = analyze_dominant_emotion(user_review) # Get the dominant emotion of the user's comment
|
50 |
emotion_label = dominant_emotion['label'].lower() # Extract the emotion label in lowercase format
|
51 |
|
52 |
# Define response templates for each emotion
|
53 |
+
emotion_prompts = {
|
54 |
+
"anger": (
|
55 |
+
f"Customer complaint: '{user_review}'\n\n"
|
56 |
+
"As a customer service representative, craft a professional response that:\n"
|
57 |
+
"- Begins with sincere apology and acknowledgment\n"
|
58 |
+
"- Clearly explains solution process with concrete steps\n"
|
59 |
+
"- Offers appropriate compensation/redemption\n"
|
60 |
+
"- Keeps tone humble and solution-focused (3-4 sentences)\n\n"
|
61 |
+
"Response:"
|
62 |
+
),
|
63 |
+
"disgust": (
|
64 |
+
f"Customer quality concern: '{user_review}'\n\n"
|
65 |
+
"As a customer service representative, craft a response that:\n"
|
66 |
+
"- Immediately acknowledges the product issue\n"
|
67 |
+
"- Explains quality control measures being taken\n"
|
68 |
+
"- Provides clear return/replacement instructions\n"
|
69 |
+
"- Offers goodwill gesture (3-4 sentences)\n\n"
|
70 |
+
"Response:"
|
71 |
+
),
|
72 |
+
"fear": (
|
73 |
+
f"Customer safety concern: '{user_review}'\n\n"
|
74 |
+
"As a customer service representative, craft a reassuring response that:\n"
|
75 |
+
"- Directly addresses the safety worries\n"
|
76 |
+
"- References relevant certifications/standards\n"
|
77 |
+
"- Offers dedicated support contact\n"
|
78 |
+
"- Provides satisfaction guarantee (3-4 sentences)\n\n"
|
79 |
+
"Response:"
|
80 |
+
),
|
81 |
+
"joy": (
|
82 |
+
f"Customer review: '{user_review}'\n\n"
|
83 |
+
"As a customer service representative, craft a concise and enthusiastic response that:\n"
|
84 |
+
"- Thanks the customer for their feedback\n"
|
85 |
+
"- Acknowledges both positive and constructive comments\n"
|
86 |
+
"- Invites them to explore loyalty programs\n\n"
|
87 |
+
"Response:"
|
88 |
+
),
|
89 |
+
"neutral": (
|
90 |
+
f"Customer feedback: '{user_review}'\n\n"
|
91 |
+
"As a customer service representative, craft a balanced response that:\n"
|
92 |
+
"- Provides additional relevant product information\n"
|
93 |
+
"- Highlights key service features\n"
|
94 |
+
"- Politely requests more detailed feedback\n"
|
95 |
+
"- Maintains professional tone (3-4 sentences)\n\n"
|
96 |
+
"Response:"
|
97 |
+
),
|
98 |
+
"sadness": (
|
99 |
+
f"Customer disappointment: '{user_review}'\n\n"
|
100 |
+
"As a customer service representative, craft an empathetic response that:\n"
|
101 |
+
"- Shows genuine understanding of the issue\n"
|
102 |
+
"- Proposes personalized recovery solution\n"
|
103 |
+
"- Offers extended support options\n"
|
104 |
+
"- Maintains positive outlook (3-4 sentences)\n\n"
|
105 |
+
"Response:"
|
106 |
+
),
|
107 |
+
"surprise": (
|
108 |
+
f"Customer enthusiastic feedback: '{user_review}'\n\n"
|
109 |
+
"As a customer service representative, craft a response that:\n"
|
110 |
+
"- Matches customer's positive energy appropriately\n"
|
111 |
+
"- Highlights unexpected product benefits\n"
|
112 |
+
"- Invites to user community/events\n"
|
113 |
+
"- Maintains brand voice (3-4 sentences)\n\n"
|
114 |
+
"Response:"
|
115 |
+
)
|
116 |
+
}
|
117 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
# Select the appropriate prompt based on the user's emotion, or default to neutral
|
119 |
prompt = emotion_prompts.get(
|
120 |
+
emotion_label,
|
121 |
f"Neutral feedback: '{user_review}'\n\nWrite a professional and concise response (50-200 words max).\n\nResponse:"
|
122 |
)
|
123 |
|
124 |
# Load the tokenizer and language model for text generation
|
125 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B") # Load tokenizer for processing text inputs
|
126 |
+
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B") # Load language model for response generation
|
127 |
|
128 |
inputs = tokenizer(prompt, return_tensors="pt") # Tokenize the input prompt
|
129 |
outputs = model.generate(
|
|
|
136 |
|
137 |
# Decode the generated response back into text
|
138 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
139 |
+
print(f"Generated response: {response}") # Print the response for debugging
|
140 |
return response # Return the generated response
|
141 |
|
142 |
##########################################
|
|
|
144 |
##########################################
|
145 |
def sound_gen(response):
|
146 |
"""
|
147 |
+
Convert the generated response to speech and save it as a .wav file.
|
148 |
"""
|
|
|
149 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") # Pre-trained processor for TTS
|
150 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts") # Pre-trained TTS model
|
151 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan") # Vocoder for generating waveforms
|
|
|
164 |
|
165 |
# Save the audio as a .wav file
|
166 |
sf.write("customer_service_response.wav", speech.numpy(), samplerate=16000)
|
|
|
167 |
st.audio("customer_service_response.wav") # Embed an audio player in the web app
|
168 |
|
|
|
169 |
##########################################
|
170 |
# Main Function
|
171 |
##########################################
|
|
|
180 |
|
181 |
# Run the main function when the script is executed
|
182 |
if __name__ == "__main__":
|
183 |
+
main()
|