Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,13 +8,69 @@ st.set_page_config(
|
|
8 |
layout="centered"
|
9 |
)
|
10 |
|
11 |
-
st.markdown(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# ------- Load Pipelines -------
|
20 |
@st.cache_resource
|
@@ -26,6 +82,7 @@ def load_pipelines():
|
|
26 |
"branch service", "transaction delay", "account closure", "information error"
|
27 |
]
|
28 |
|
|
|
29 |
dtype = torch.float32
|
30 |
|
31 |
topic_classifier = pipeline(
|
@@ -36,14 +93,13 @@ def load_pipelines():
|
|
36 |
# Sentiment Analysis Model
|
37 |
sentiment_classifier = pipeline(
|
38 |
"sentiment-analysis",
|
39 |
-
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
40 |
)
|
41 |
|
42 |
# Reply Generation Model
|
43 |
model_name = "Leo66277/finetuned-tinyllama-customer-replies"
|
44 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
45 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
46 |
-
|
47 |
|
48 |
def generate_reply(text):
|
49 |
prompt_text = f"Please write a short, polite English customer service reply to the following customer comment:\n{text}"
|
@@ -65,14 +121,12 @@ def load_pipelines():
|
|
65 |
|
66 |
return topic_classifier, sentiment_classifier, generate_reply, topic_labels
|
67 |
|
68 |
-
topic_pipe, sentiment_pipe, reply_generator, topic_labels = load_pipelines()
|
69 |
|
70 |
-
|
71 |
-
st.markdown("### Enter a review and get instant analysis ↓")
|
72 |
example_review = "The people at the call center are inexperienced and lack proper training. I had to call multiple times to resolve a simple issue."
|
73 |
|
74 |
user_review = st.text_area(
|
75 |
-
"Please enter or paste a review
|
76 |
value=example_review,
|
77 |
height=120
|
78 |
)
|
@@ -81,34 +135,27 @@ if st.button("Analyze"):
|
|
81 |
if not user_review.strip():
|
82 |
st.warning("Please enter a valid review!")
|
83 |
else:
|
84 |
-
with st.spinner("Analyzing..."):
|
|
|
|
|
|
|
85 |
# Topic Classification
|
86 |
-
topic_result = topic_pipe(user_review, topic_labels, multi_label=False)
|
87 |
topic = topic_result['labels'][0]
|
88 |
-
topic_score = round(topic_result['scores'][0] * 100, 1)
|
89 |
|
90 |
# Sentiment Analysis
|
91 |
-
sentiment_result = sentiment_pipe(user_review)
|
92 |
sentiment = sentiment_result[0]['label']
|
93 |
-
sentiment_score = round(sentiment_result[0]['score'] * 100, 1)
|
94 |
|
95 |
# Auto Reply Generation
|
96 |
-
reply_text = reply_generator(user_review)
|
97 |
|
98 |
-
# Output Results - card style
|
99 |
col1, col2 = st.columns(2)
|
100 |
with col1:
|
101 |
-
st.
|
102 |
with col2:
|
103 |
-
st.
|
104 |
|
105 |
-
st.
|
106 |
-
st.write(f"> {reply_text}")
|
107 |
|
108 |
-
st.markdown(
|
109 |
-
"""
|
110 |
-
<hr/>
|
111 |
-
<p style='text-align: center; color: #bbb;'>© 2024 Review AI Assistant</p>
|
112 |
-
""",
|
113 |
-
unsafe_allow_html=True
|
114 |
-
)
|
|
|
8 |
layout="centered"
|
9 |
)
|
10 |
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
.main-header {
|
14 |
+
font-size: 2.2rem;
|
15 |
+
color: #1E3A8A;
|
16 |
+
text-align: center;
|
17 |
+
margin-bottom: 0;
|
18 |
+
padding-bottom: 0;
|
19 |
+
}
|
20 |
+
.sub-header {
|
21 |
+
font-size: 1rem;
|
22 |
+
color: #6B7280;
|
23 |
+
text-align: center;
|
24 |
+
margin-top: 0.3rem;
|
25 |
+
margin-bottom: 2rem;
|
26 |
+
}
|
27 |
+
.result-card {
|
28 |
+
padding: 1.2rem;
|
29 |
+
border-radius: 8px;
|
30 |
+
margin-bottom: 1rem;
|
31 |
+
}
|
32 |
+
.topic-card {
|
33 |
+
background-color: #ECFDF5;
|
34 |
+
border-left: 4px solid #10B981;
|
35 |
+
}
|
36 |
+
.sentiment-card {
|
37 |
+
background-color: #EFF6FF;
|
38 |
+
border-left: 4px solid #3B82F6;
|
39 |
+
}
|
40 |
+
.reply-card {
|
41 |
+
background-color: #F9FAFB;
|
42 |
+
border-left: 4px solid #6B7280;
|
43 |
+
padding: 1.5rem;
|
44 |
+
}
|
45 |
+
.result-label {
|
46 |
+
font-weight: bold;
|
47 |
+
margin-bottom: 0.5rem;
|
48 |
+
}
|
49 |
+
.stButton>button {
|
50 |
+
background-color: #2563EB;
|
51 |
+
color: white;
|
52 |
+
border: none;
|
53 |
+
padding: 0.5rem 2rem;
|
54 |
+
border-radius: 6px;
|
55 |
+
font-weight: 500;
|
56 |
+
}
|
57 |
+
.stButton>button:hover {
|
58 |
+
background-color: #1D4ED8;
|
59 |
+
}
|
60 |
+
.footer {
|
61 |
+
text-align: center;
|
62 |
+
color: #9CA3AF;
|
63 |
+
font-size: 0.8rem;
|
64 |
+
margin-top: 3rem;
|
65 |
+
}
|
66 |
+
.stSpinner {
|
67 |
+
text-align: center;
|
68 |
+
}
|
69 |
+
</style>
|
70 |
+
""", unsafe_allow_html=True)
|
71 |
+
|
72 |
+
st.markdown("<h1 class='main-header'>Smart Review Analysis Assistant</h1>", unsafe_allow_html=True)
|
73 |
+
st.markdown("<p class='sub-header'>Topic Recognition, Sentiment Analysis, and Auto Reply in One Click</p>", unsafe_allow_html=True)
|
74 |
|
75 |
# ------- Load Pipelines -------
|
76 |
@st.cache_resource
|
|
|
82 |
"branch service", "transaction delay", "account closure", "information error"
|
83 |
]
|
84 |
|
85 |
+
|
86 |
dtype = torch.float32
|
87 |
|
88 |
topic_classifier = pipeline(
|
|
|
93 |
# Sentiment Analysis Model
|
94 |
sentiment_classifier = pipeline(
|
95 |
"sentiment-analysis",
|
96 |
+
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
97 |
)
|
98 |
|
99 |
# Reply Generation Model
|
100 |
model_name = "Leo66277/finetuned-tinyllama-customer-replies"
|
101 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
102 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
103 |
|
104 |
def generate_reply(text):
|
105 |
prompt_text = f"Please write a short, polite English customer service reply to the following customer comment:\n{text}"
|
|
|
121 |
|
122 |
return topic_classifier, sentiment_classifier, generate_reply, topic_labels
|
123 |
|
|
|
124 |
|
125 |
+
st.markdown("### Enter a review for instant analysis")
|
|
|
126 |
example_review = "The people at the call center are inexperienced and lack proper training. I had to call multiple times to resolve a simple issue."
|
127 |
|
128 |
user_review = st.text_area(
|
129 |
+
"Please enter or paste a review below:",
|
130 |
value=example_review,
|
131 |
height=120
|
132 |
)
|
|
|
135 |
if not user_review.strip():
|
136 |
st.warning("Please enter a valid review!")
|
137 |
else:
|
138 |
+
with st.spinner("Analyzing your review..."):
|
139 |
+
if "topic_pipe" not in st.session_state:
|
140 |
+
st.session_state.topic_pipe, st.session_state.sentiment_pipe, st.session_state.reply_generator, st.session_state.topic_labels = load_pipelines()
|
141 |
+
|
142 |
# Topic Classification
|
143 |
+
topic_result = st.session_state.topic_pipe(user_review, st.session_state.topic_labels, multi_label=False)
|
144 |
topic = topic_result['labels'][0]
|
|
|
145 |
|
146 |
# Sentiment Analysis
|
147 |
+
sentiment_result = st.session_state.sentiment_pipe(user_review)
|
148 |
sentiment = sentiment_result[0]['label']
|
|
|
149 |
|
150 |
# Auto Reply Generation
|
151 |
+
reply_text = st.session_state.reply_generator(user_review)
|
152 |
|
|
|
153 |
col1, col2 = st.columns(2)
|
154 |
with col1:
|
155 |
+
st.markdown(f"<div class='result-card topic-card'><p class='result-label'>Topic:</p>{topic}</div>", unsafe_allow_html=True)
|
156 |
with col2:
|
157 |
+
st.markdown(f"<div class='result-card sentiment-card'><p class='result-label'>Sentiment:</p>{sentiment}</div>", unsafe_allow_html=True)
|
158 |
|
159 |
+
st.markdown(f"<div class='result-card reply-card'><p class='result-label'>Auto-reply Suggestion:</p>{reply_text}</div>", unsafe_allow_html=True)
|
|
|
160 |
|
161 |
+
st.markdown("<div class='footer'>© 2024 Review AI Assistant</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|