Spaces:
Running
Running
File size: 9,202 Bytes
5526f12 6a85efc 5526f12 b204d6b 9e4badf a735e8f 9e4badf 2bee048 fb3f6b2 6a85efc fb3f6b2 57c2123 cbdb218 57c2123 45f3db1 57c2123 a735e8f 57c2123 cbdb218 57c2123 5526f12 6a85efc 298c5bd 6a85efc 57c2123 6a85efc 298c5bd 6a85efc 298c5bd a735e8f 57c2123 a735e8f b204d6b 2bee048 fb3f6b2 6a85efc fb3f6b2 57c2123 a735e8f 298c5bd 6a85efc b204d6b 6a85efc b204d6b 6a85efc b204d6b 02117b7 cbdb218 02117b7 cbdb218 02117b7 cbdb218 02117b7 cbdb218 02117b7 cbdb218 02117b7 cbdb218 02117b7 cbdb218 02117b7 a735e8f b204d6b 02117b7 b204d6b 57c2123 a735e8f 57c2123 b204d6b a735e8f 57c2123 a735e8f b204d6b a735e8f b204d6b a735e8f b204d6b 5526f12 6a85efc 5526f12 298c5bd 6a85efc 02117b7 6a85efc a735e8f b204d6b a735e8f b204d6b a735e8f 6a85efc b204d6b a735e8f 6a85efc 57c2123 a735e8f 6a85efc 57c2123 a735e8f b204d6b 6a85efc b189e5a 6a85efc a735e8f 6a85efc b204d6b a735e8f 57c2123 a735e8f 6a85efc a735e8f 298c5bd 02117b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
##########################################
# Step 0: Import required libraries
##########################################
import streamlit as st # For building the web application interface
from transformers import (
pipeline,
SpeechT5Processor,
SpeechT5ForTextToSpeech,
SpeechT5HifiGan,
AutoModelForCausalLM,
AutoTokenizer
) # For sentiment analysis, text-to-speech, and text generation
from datasets import load_dataset # For loading datasets (e.g., speaker embeddings)
import torch # For tensor operations
import soundfile as sf # For saving audio as .wav files
import sentencepiece # Required by SpeechT5Processor for tokenization
##########################################
# Streamlit application title and input
##########################################
# Display a colorful, large title in a visually appealing font
st.markdown(
"<h1 style='text-align: center; color: #FF5720; font-size: 50px;'>Just Comment</h1>",
unsafe_allow_html=True
) # Use HTML and CSS to set a custom title design
# Display a smaller, gentle and warm subtitle below the title
st.markdown(
"<h3 style='text-align: center; color: #5D6D7E; font-style: italic;'>I'm listening to you, my friend~</h3>",
unsafe_allow_html=True
) # Use HTML for a friendly and soft-styled subtitle
# Add a well-designed text area for user input
text = st.text_area(
"Enter your comment",
placeholder="Type something here...",
height=280,
help="Write a comment you would like us to analyze and respond to!" # Provide a helpful tooltip
)
##########################################
# Step 1: Sentiment Analysis Function
##########################################
def analyze_dominant_emotion(user_review):
"""
Analyze the dominant emotion in the user's comment using a fine-tuned text classification model.
"""
emotion_classifier = pipeline(
"text-classification",
model="Thea231/jhartmann_emotion_finetuning",
return_all_scores=True
) # Load the fine-tuned text classification model
emotion_results = emotion_classifier(user_review)[0] # Perform sentiment analysis on the input text
dominant_emotion = max(emotion_results, key=lambda x: x['score']) # Identify the emotion with the highest confidence
return dominant_emotion # Return the dominant emotion (label and score)
##########################################
# Step 2: Response Generation Function
##########################################
def response_gen(user_review):
"""
Generate a concise and logical response based on the sentiment of the user's comment.
"""
dominant_emotion = analyze_dominant_emotion(user_review) # Get the dominant emotion of the user's comment
emotion_label = dominant_emotion['label'].lower() # Extract the emotion label in lowercase format
# Define response templates for each emotion
emotion_prompts = {
"anger": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a professional response that:\n"
"- Begins with sincere apology and acknowledgment\n"
"- Clearly explains solution process with concrete steps\n"
"- Offers appropriate compensation/redemption\n"
"- Keeps tone humble and solution-focused (3-4 sentences)\n\n"
"Response:"
),
"disgust": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a response that:\n"
"- Immediately acknowledges the product issue\n"
"- Explains quality control measures being taken\n"
"- Provides clear return/replacement instructions\n"
"- Offers goodwill gesture (3-4 sentences)\n\n"
"Response:"
),
"fear": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a reassuring response that:\n"
"- Directly addresses the safety worries\n"
"- References relevant certifications/standards\n"
"- Offers dedicated support contact\n"
"- Provides satisfaction guarantee (3-4 sentences)\n\n"
"Response:"
),
"joy": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a concise and enthusiastic response that:\n"
"- Thanks the customer for their feedback\n"
"- Acknowledges both positive and constructive comments\n"
"- Invites them to explore loyalty programs\n\n"
"Response:"
),
"neutral": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a balanced response that:\n"
"- Provides additional relevant product information\n"
"- Highlights key service features\n"
"- Politely requests more detailed feedback\n"
"- Maintains professional tone (3-4 sentences)\n\n"
"Response:"
),
"sadness": (
f"'{user_review}'\n\n"
"As a customer service representative, craft an empathetic response that:\n"
"- Shows genuine understanding of the issue\n"
"- Proposes personalized recovery solution\n"
"- Offers extended support options\n"
"- Maintains positive outlook (3-4 sentences)\n\n"
"Response:"
),
"surprise": (
f"'{user_review}'\n\n"
"As a customer service representative, craft a response that:\n"
"- Matches customer's positive energy appropriately\n"
"- Highlights unexpected product benefits\n"
"- Invites to user community/events\n"
"- Maintains brand voice (3-4 sentences)\n\n"
"Response:"
)
}
# Select the appropriate prompt based on the user's emotion
prompt = emotion_prompts.get(
emotion_label,
f"Neutral feedback: '{user_review}'\n\nWrite a professional and concise response (50-200 words max).\n\nResponse:"
)
# Load the tokenizer and language model for response generation
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B") # Load tokenizer for text processing
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B") # Load language model for text generation
inputs = tokenizer(prompt, return_tensors="pt") # Tokenize the input prompt
outputs = model.generate(
**inputs,
max_new_tokens=300, # Limit generated tokens to ensure concise responses
min_length=75, # Ensure the generated response is logical and complete
no_repeat_ngram_size=2, # Avoid repetitive phrases
temperature=0.7 # Add randomness for natural-sounding responses
)
# Decode the generated response back into text
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Generated response: {response}") # Debugging: print the response
return response # Return the generated response
##########################################
# Step 3: Text-to-Speech Conversion Function
##########################################
def sound_gen(response):
"""
Convert the generated response to speech and save it as a .wav file.
"""
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts") # Load processor for TTS
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts") # Load pre-trained TTS model
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan") # Load vocoder for waveform generation
# Load neutral female voice embedding from dataset
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") # Load speaker embeddings
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0) # Use a default speaker embedding
# Process the input text and generate a spectrogram
inputs = processor(text=response, return_tensors="pt")
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
# Use vocoder to convert the spectrogram into a waveform
with torch.no_grad():
speech = vocoder(spectrogram)
# Save the audio file as .wav
sf.write("customer_service_response.wav", speech.numpy(), samplerate=16000)
# Create an auto-playing audio player in Streamlit
st.audio("customer_service_response.wav", start_time=0) # Enable audio playback with autoplay
##########################################
# Main Function
##########################################
def main():
"""
Main function to handle sentiment analysis, response generation, and text-to-speech functionalities.
"""
if text: # Check if the user has entered a comment
response = response_gen(text) # Generate a concise and logical response
st.markdown(
f"<p style='color:#2ECC71; font-size:20px;'>{response}</p>",
unsafe_allow_html=True
) # Display the response in a styled font
sound_gen(response) # Convert the response to speech and play it
# Execute the main function
if __name__ == "__main__":
main()
|