Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the Hugging Face pipelines
|
5 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
6 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
7 |
+
|
8 |
+
# Define the categories for customer feedback
|
9 |
+
CATEGORIES = ["Pricing", "Feature", "Customer Service", "Delivery", "Quality"]
|
10 |
+
|
11 |
+
# Streamlit app UI
|
12 |
+
st.title("Customer Feedback Categorization with Sentiment Analysis")
|
13 |
+
st.markdown(
|
14 |
+
"""
|
15 |
+
This app uses Hugging Face models to detect the topics and intent of customer feedback
|
16 |
+
and determine the sentiment (positive or negative) for each relevant category.
|
17 |
+
A single feedback may belong to multiple categories, such as Pricing, Feature, and Customer Service.
|
18 |
+
"""
|
19 |
+
)
|
20 |
+
|
21 |
+
# Input text box for customer feedback
|
22 |
+
feedback_input = st.text_area(
|
23 |
+
"Enter customer feedback:",
|
24 |
+
placeholder="Type your feedback here...",
|
25 |
+
height=200
|
26 |
+
)
|
27 |
+
|
28 |
+
# Confidence threshold for zero-shot classification
|
29 |
+
threshold = st.slider(
|
30 |
+
"Confidence Threshold",
|
31 |
+
min_value=0.0,
|
32 |
+
max_value=1.0,
|
33 |
+
value=0.2,
|
34 |
+
step=0.05,
|
35 |
+
help="Categories with scores above this threshold will be displayed."
|
36 |
+
)
|
37 |
+
|
38 |
+
# Classify button
|
39 |
+
if st.button("Classify Feedback"):
|
40 |
+
if not feedback_input.strip():
|
41 |
+
st.error("Please provide valid feedback text.")
|
42 |
+
else:
|
43 |
+
# Perform zero-shot classification
|
44 |
+
classification_result = classifier(feedback_input, CATEGORIES)
|
45 |
+
|
46 |
+
# Filter categories with scores above the threshold
|
47 |
+
relevant_categories = {
|
48 |
+
label: round(score, 4)
|
49 |
+
for label, score in zip(classification_result["labels"], classification_result["scores"])
|
50 |
+
if score >= threshold
|
51 |
+
}
|
52 |
+
|
53 |
+
# Check if there are any relevant categories
|
54 |
+
if relevant_categories:
|
55 |
+
st.subheader("Categorized Feedback with Sentiment Analysis")
|
56 |
+
|
57 |
+
for category, score in relevant_categories.items():
|
58 |
+
# Extract the part of feedback relevant to the category for sentiment analysis
|
59 |
+
sentiment_result = sentiment_analyzer(feedback_input)
|
60 |
+
sentiment_label = sentiment_result[0]["label"]
|
61 |
+
sentiment_score = round(sentiment_result[0]["score"], 4)
|
62 |
+
|
63 |
+
# Display the category, confidence score, and sentiment result
|
64 |
+
st.write(f"### **{category}**")
|
65 |
+
st.write(f"- Confidence: {score}")
|
66 |
+
st.write(f"- Sentiment: {sentiment_label} (Score: {sentiment_score})")
|
67 |
+
else:
|
68 |
+
st.warning("No categories matched the selected confidence threshold.")
|