Spaces:
Sleeping
Sleeping
File size: 4,052 Bytes
ebc6394 df6a94f bab9362 acd5a3a ebc6394 882d1dd 273b099 80d4bd4 ebc6394 bab9362 ebc6394 882d1dd ebc6394 882d1dd df6a94f bab9362 ebc6394 df6a94f ebc6394 882d1dd ebc6394 df6a94f ebc6394 df6a94f bab9362 df6a94f ebc6394 df6a94f ebc6394 5190f16 df6a94f 882d1dd df6a94f bab9362 |
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 |
import streamlit as st
from transformers import pipeline
import nltk
# Download NLTK data for sentence tokenization
nltk.download('punkt_tab')
# Load the Hugging Face pipelines
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli")
sentiment_analyzer = pipeline("sentiment-analysis", model="SarahMakk/CustomModel_amazon_sentiment_moshew_128_10k")
# Define the categories for customer feedback
CATEGORIES = ["Pricing", "Feature", "Customer Service", "Delivery", "Quality"]
# Define the fixed confidence threshold
CONFIDENCE_THRESHOLD = 0.8
# 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.
The feedback is split into sentences, and each sentence is categorized and analyzed for sentiment.
Only categories with a confidence score >= 0.8 are displayed.
"""
)
# Input text box for customer feedback
feedback_input = st.text_area(
"Enter customer feedback:",
placeholder="Type your feedback here...",
height=200,
value="I was shocked to see the price tag on this new gadget—it’s way too expensive for what it offers, especially compared to competitors! Despite the issues I faced with my order, the customer service team's effort to rectify the situation was commendable, though their follow-up could have used some improvement for full satisfaction."
)
# Classify button
if st.button("Classify Feedback"):
if not feedback_input.strip():
st.error("Please provide valid feedback text.")
else:
# Split the feedback into sentences
sentences = nltk.sent_tokenize(feedback_input)
if not sentences:
st.error("Could not split feedback into sentences.")
st.stop()
# Dictionary to store results for each category
category_results = {category: [] for category in CATEGORIES}
# Process each sentence
for sentence in sentences:
# Perform zero-shot classification on the sentence
classification_result = classifier(sentence, CATEGORIES, multi_label=True)
# Get categories with scores above the threshold
for label, score in zip(classification_result["labels"], classification_result["scores"]):
if score >= CONFIDENCE_THRESHOLD:
# Perform sentiment analysis on the sentence
sentiment_result = sentiment_analyzer(sentence)
sentiment_label = sentiment_result[0]["label"]
sentiment_score = round(sentiment_result[0]["score"], 4)
# Store the result for the category
category_results[label].append({
"sentence": sentence,
"confidence": round(score, 4),
"sentiment": sentiment_label,
"sentiment_score": sentiment_score
})
# Check if there are any relevant categories
st.subheader("Categorized Feedback with Sentiment Analysis")
found_categories = False
for category, results in category_results.items():
if results: # If the category has any sentences
found_categories = True
st.write(f"### **{category}**")
for result in results:
st.write(f"- **Sentence**: {result['sentence']}")
st.write(f" - Confidence: {result['confidence']}")
st.write(f" - Sentiment: {result['sentiment']} (Score: {result['sentiment_score']})")
st.write("") # Add a blank line for readability
if not found_categories:
st.warning("No categories met the confidence threshold of 0.8.") |