Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Load the Hugging Face pipelines | |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") | |
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") | |
# Define the categories for customer feedback | |
CATEGORIES = ["Pricing", "Feature", "Customer Service", "Delivery", "Quality"] | |
# Streamlit app UI | |
st.title("Customer Feedback Categorization with Sentiment Analysis") | |
st.markdown( | |
""" | |
This app uses Hugging Face models to detect the topics and intent of customer feedback | |
and determine the sentiment (positive or negative) for each relevant category. | |
A single feedback may belong to multiple categories, such as Pricing, Feature, and Customer Service. | |
""" | |
) | |
# Input text box for customer feedback | |
feedback_input = st.text_area( | |
"Enter customer feedback:", | |
placeholder="Type your feedback here...", | |
height=200 | |
) | |
# Confidence threshold for zero-shot classification | |
threshold = st.slider( | |
"Confidence Threshold", | |
min_value=0.0, | |
max_value=1.0, | |
value=0.2, | |
step=0.05, | |
help="Categories with scores above this threshold will be displayed." | |
) | |
# Classify button | |
if st.button("Classify Feedback"): | |
if not feedback_input.strip(): | |
st.error("Please provide valid feedback text.") | |
else: | |
# Perform zero-shot classification | |
classification_result = classifier(feedback_input, CATEGORIES) | |
# Filter categories with scores above the threshold | |
relevant_categories = { | |
label: round(score, 4) | |
for label, score in zip(classification_result["labels"], classification_result["scores"]) | |
if score >= threshold | |
} | |
# Check if there are any relevant categories | |
if relevant_categories: | |
st.subheader("Categorized Feedback with Sentiment Analysis") | |
for category, score in relevant_categories.items(): | |
# Extract the part of feedback relevant to the category for sentiment analysis | |
sentiment_result = sentiment_analyzer(feedback_input) | |
sentiment_label = sentiment_result[0]["label"] | |
sentiment_score = round(sentiment_result[0]["score"], 4) | |
# Display the category, confidence score, and sentiment result | |
st.write(f"### **{category}**") | |
st.write(f"- Confidence: {score}") | |
st.write(f"- Sentiment: {sentiment_label} (Score: {sentiment_score})") | |
else: | |
st.warning("No categories matched the selected confidence threshold.") |