Spaces:
Sleeping
Sleeping
File size: 5,910 Bytes
ebc6394 df6a94f bab9362 acd5a3a ebc6394 882d1dd 273b099 80d4bd4 ebc6394 bab9362 7789887 080f94f 7789887 080f94f 7789887 ebc6394 7789887 ebc6394 7789887 ebc6394 7789887 080f94f 7789887 ebc6394 7789887 ebc6394 3db0d3b ebc6394 882d1dd ebc6394 df6a94f ebc6394 df6a94f bab9362 df6a94f 3409295 df6a94f ebc6394 3409295 cb74933 3409295 cb74933 3409295 df6a94f 3409295 df6a94f ebc6394 5190f16 df6a94f 7789887 df6a94f 882d1dd df6a94f 3409295 7789887 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
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
# Custom CSS for background colors
st.markdown(
"""
<style>
/* Background color for the title */
.title-container {
background-color: #f0f0f0; /* Light gray background */
padding: 10px;
border-radius: 5px;
}
/* Background color for the description (st.markdown) */
.description-container {
background-color: #f0f0f0; /* Light gray background */
padding: 10px;
border-radius: 5px;
}
/* Background color for the text area (feedback_input) */
.stTextArea textarea {
background-color: #e6f3ff; /* Light blue background */
border-radius: 5px;
}
</style>
""",
unsafe_allow_html=True
)
# Streamlit app UI
# Title with icon
st.markdown(
"""
<div class="title-container">
<h1>📢 Customer Feedback Categorization with Sentiment Analysis</h1>
</div>
""",
unsafe_allow_html=True
)
# Description with background color
st.markdown(
"""
<div class="description-container">
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.
</div>
""",
unsafe_allow_html=True
)
# Input text box for customer feedback with background color
feedback_input = st.text_area(
"Enter customer feedback:",
placeholder="Type your feedback here...",
height=200
)
# 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)
raw_label = sentiment_result[0]["label"]
sentiment_score = round(sentiment_result[0]["score"], 4)
# Map the raw label to NEGATIVE or POSITIVE
if raw_label == "LABEL_0":
sentiment_label = "NEGATIVE"
sentiment_icon = "👎" # Thumbs-down icon for negative
sentiment_color = "red" # Red color for negative
elif raw_label == "LABEL_1":
sentiment_label = "POSITIVE"
sentiment_icon = "👍" # Thumbs-up icon for positive
sentiment_color = "green" # Green color for positive
else:
sentiment_label = raw_label # Fallback in case of unexpected label
# Store the result for the category
category_results[label].append({
"sentence": sentence,
"confidence": round(score, 4),
"sentiment": sentiment_label,
"sentiment_score": sentiment_score,
"sentiment_icon": sentiment_icon,
"sentiment_color": sentiment_color
})
# Check if there are any relevant categories
st.subheader("Categorized Feedback with Sentiment Analysis")
found_categories = False
for i, (category, results) in enumerate(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']}")
# Use st.markdown with HTML to display the sentiment with icon and color
st.markdown(
f" - Sentiment: {result['sentiment_icon']} "
f"<span style='color:{result['sentiment_color']}'>{result['sentiment']}</span> "
f"(Score: {result['sentiment_score']})",
unsafe_allow_html=True
)
# Add a horizontal divider after each category (except the last one)
if i < len(category_results) - 1 and any(category_results[cat] for cat in list(category_results.keys())[i+1:]):
st.markdown("---") # Horizontal line to separate categories
if not found_categories:
st.warning("No categories met the confidence threshold of 0.8.") |