Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,14 @@
|
|
| 2 |
# Step 0: Import required libraries
|
| 3 |
##########################################
|
| 4 |
import streamlit as st # For building the web application interface
|
| 5 |
-
from transformers import (
|
| 6 |
-
pipeline,
|
| 7 |
-
SpeechT5Processor,
|
| 8 |
-
SpeechT5ForTextToSpeech,
|
| 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
|
| 20 |
##########################################
|
| 21 |
# Display a colorful, large title in a visually appealing font
|
| 22 |
st.markdown(
|
| 23 |
-
"<h1 style='text-align: center; color: #
|
| 24 |
unsafe_allow_html=True
|
| 25 |
) # Use HTML and CSS to set a custom title design
|
| 26 |
|
|
@@ -34,7 +23,7 @@ st.markdown(
|
|
| 34 |
text = st.text_area(
|
| 35 |
"Enter your comment",
|
| 36 |
placeholder="Type something here...",
|
| 37 |
-
height=
|
| 38 |
help="Write a comment you would like us to analyze and respond to!" # Provide a helpful tooltip
|
| 39 |
)
|
| 40 |
|
|
@@ -70,7 +59,7 @@ def response_gen(user_review):
|
|
| 70 |
# Define response templates for each emotion
|
| 71 |
emotion_prompts = {
|
| 72 |
"anger": (
|
| 73 |
-
f"
|
| 74 |
"As a customer service representative, craft a professional response that:\n"
|
| 75 |
"- Begins with sincere apology and acknowledgment\n"
|
| 76 |
"- Clearly explains solution process with concrete steps\n"
|
|
@@ -79,7 +68,7 @@ def response_gen(user_review):
|
|
| 79 |
"Response:"
|
| 80 |
),
|
| 81 |
"disgust": (
|
| 82 |
-
f"
|
| 83 |
"As a customer service representative, craft a response that:\n"
|
| 84 |
"- Immediately acknowledges the product issue\n"
|
| 85 |
"- Explains quality control measures being taken\n"
|
|
@@ -88,7 +77,7 @@ def response_gen(user_review):
|
|
| 88 |
"Response:"
|
| 89 |
),
|
| 90 |
"fear": (
|
| 91 |
-
f"
|
| 92 |
"As a customer service representative, craft a reassuring response that:\n"
|
| 93 |
"- Directly addresses the safety worries\n"
|
| 94 |
"- References relevant certifications/standards\n"
|
|
@@ -97,7 +86,7 @@ def response_gen(user_review):
|
|
| 97 |
"Response:"
|
| 98 |
),
|
| 99 |
"joy": (
|
| 100 |
-
f"
|
| 101 |
"As a customer service representative, craft a concise and enthusiastic response that:\n"
|
| 102 |
"- Thanks the customer for their feedback\n"
|
| 103 |
"- Acknowledges both positive and constructive comments\n"
|
|
@@ -105,7 +94,7 @@ def response_gen(user_review):
|
|
| 105 |
"Response:"
|
| 106 |
),
|
| 107 |
"neutral": (
|
| 108 |
-
f"
|
| 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"
|
|
@@ -114,7 +103,7 @@ def response_gen(user_review):
|
|
| 114 |
"Response:"
|
| 115 |
),
|
| 116 |
"sadness": (
|
| 117 |
-
f"
|
| 118 |
"As a customer service representative, craft an empathetic response that:\n"
|
| 119 |
"- Shows genuine understanding of the issue\n"
|
| 120 |
"- Proposes personalized recovery solution\n"
|
|
@@ -123,7 +112,7 @@ def response_gen(user_review):
|
|
| 123 |
"Response:"
|
| 124 |
),
|
| 125 |
"surprise": (
|
| 126 |
-
f"
|
| 127 |
"As a customer service representative, craft a response that:\n"
|
| 128 |
"- Matches customer's positive energy appropriately\n"
|
| 129 |
"- Highlights unexpected product benefits\n"
|
|
@@ -154,7 +143,7 @@ def response_gen(user_review):
|
|
| 154 |
|
| 155 |
# Decode the generated response back into readable text
|
| 156 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 157 |
-
print(f"
|
| 158 |
return response # Return the generated response
|
| 159 |
|
| 160 |
##########################################
|
|
|
|
| 2 |
# Step 0: Import required libraries
|
| 3 |
##########################################
|
| 4 |
import streamlit as st # For building the web application interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import soundfile as sf # For saving audio as .wav files
|
|
|
|
| 6 |
|
| 7 |
##########################################
|
| 8 |
# Streamlit application title and input
|
| 9 |
##########################################
|
| 10 |
# Display a colorful, large title in a visually appealing font
|
| 11 |
st.markdown(
|
| 12 |
+
"<h1 style='text-align: center; color: #FF5720; font-size: 50px;'>Just Comment</h1>",
|
| 13 |
unsafe_allow_html=True
|
| 14 |
) # Use HTML and CSS to set a custom title design
|
| 15 |
|
|
|
|
| 23 |
text = st.text_area(
|
| 24 |
"Enter your comment",
|
| 25 |
placeholder="Type something here...",
|
| 26 |
+
height=280,
|
| 27 |
help="Write a comment you would like us to analyze and respond to!" # Provide a helpful tooltip
|
| 28 |
)
|
| 29 |
|
|
|
|
| 59 |
# Define response templates for each emotion
|
| 60 |
emotion_prompts = {
|
| 61 |
"anger": (
|
| 62 |
+
f"'{user_review}'\n\n"
|
| 63 |
"As a customer service representative, craft a professional response that:\n"
|
| 64 |
"- Begins with sincere apology and acknowledgment\n"
|
| 65 |
"- Clearly explains solution process with concrete steps\n"
|
|
|
|
| 68 |
"Response:"
|
| 69 |
),
|
| 70 |
"disgust": (
|
| 71 |
+
f"'{user_review}'\n\n"
|
| 72 |
"As a customer service representative, craft a response that:\n"
|
| 73 |
"- Immediately acknowledges the product issue\n"
|
| 74 |
"- Explains quality control measures being taken\n"
|
|
|
|
| 77 |
"Response:"
|
| 78 |
),
|
| 79 |
"fear": (
|
| 80 |
+
f"'{user_review}'\n\n"
|
| 81 |
"As a customer service representative, craft a reassuring response that:\n"
|
| 82 |
"- Directly addresses the safety worries\n"
|
| 83 |
"- References relevant certifications/standards\n"
|
|
|
|
| 86 |
"Response:"
|
| 87 |
),
|
| 88 |
"joy": (
|
| 89 |
+
f"'{user_review}'\n\n"
|
| 90 |
"As a customer service representative, craft a concise and enthusiastic response that:\n"
|
| 91 |
"- Thanks the customer for their feedback\n"
|
| 92 |
"- Acknowledges both positive and constructive comments\n"
|
|
|
|
| 94 |
"Response:"
|
| 95 |
),
|
| 96 |
"neutral": (
|
| 97 |
+
f"'{user_review}'\n\n"
|
| 98 |
"As a customer service representative, craft a balanced response that:\n"
|
| 99 |
"- Provides additional relevant product information\n"
|
| 100 |
"- Highlights key service features\n"
|
|
|
|
| 103 |
"Response:"
|
| 104 |
),
|
| 105 |
"sadness": (
|
| 106 |
+
f"'{user_review}'\n\n"
|
| 107 |
"As a customer service representative, craft an empathetic response that:\n"
|
| 108 |
"- Shows genuine understanding of the issue\n"
|
| 109 |
"- Proposes personalized recovery solution\n"
|
|
|
|
| 112 |
"Response:"
|
| 113 |
),
|
| 114 |
"surprise": (
|
| 115 |
+
f"'{user_review}'\n\n"
|
| 116 |
"As a customer service representative, craft a response that:\n"
|
| 117 |
"- Matches customer's positive energy appropriately\n"
|
| 118 |
"- Highlights unexpected product benefits\n"
|
|
|
|
| 143 |
|
| 144 |
# Decode the generated response back into readable text
|
| 145 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 146 |
+
print(f"{response}") # Print the response for debugging
|
| 147 |
return response # Return the generated response
|
| 148 |
|
| 149 |
##########################################
|