File size: 2,654 Bytes
ebc6394
 
 
882d1dd
ebc6394
 
 
 
 
 
 
882d1dd
ebc6394
 
882d1dd
 
 
ebc6394
 
 
 
 
 
 
 
 
 
882d1dd
ebc6394
 
 
 
 
 
 
 
 
 
882d1dd
ebc6394
 
 
 
 
 
 
 
882d1dd
ebc6394
 
 
 
882d1dd
ebc6394
882d1dd
ebc6394
2d14efc
882d1dd
 
 
 
 
 
 
 
 
ebc6394
882d1dd
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
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.")