SarahMakk commited on
Commit
882d1dd
·
verified ·
1 Parent(s): 4754a73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -48
app.py CHANGED
@@ -1,43 +1,20 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Load the Hugging Face models
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
- # Function to map sentiment to a rating (1 to 5)
12
- def sentiment_to_rating(sentiment_label, sentiment_score):
13
- """
14
- Convert sentiment analysis results (label and score) to a rating from 1 (most negative) to 5 (most positive).
15
- """
16
- if sentiment_label == "POSITIVE":
17
- if sentiment_score >= 0.8:
18
- return 5 # Most positive
19
- elif sentiment_score >= 0.6:
20
- return 4
21
- elif sentiment_score >= 0.4:
22
- return 3
23
- else:
24
- return 2
25
- elif sentiment_label == "NEGATIVE":
26
- if sentiment_score >= 0.8:
27
- return 1 # Most negative
28
- elif sentiment_score >= 0.6:
29
- return 2
30
- elif sentiment_score >= 0.4:
31
- return 3
32
- else:
33
- return 4
34
-
35
  # Streamlit app UI
36
- st.title("Customer Feedback Categorization and Sentiment Analysis")
37
  st.markdown(
38
  """
39
- This app can detect the topics and intent of customer feedback
40
- and determine the sentiment (rating from 1 to 5) for each relevant category.
 
41
  """
42
  )
43
 
@@ -48,7 +25,7 @@ feedback_input = st.text_area(
48
  height=200
49
  )
50
 
51
- # Confidence threshold for displaying categories
52
  threshold = st.slider(
53
  "Confidence Threshold",
54
  min_value=0.0,
@@ -59,7 +36,7 @@ threshold = st.slider(
59
  )
60
 
61
  # Classify button
62
- if st.button("Analyze Feedback"):
63
  if not feedback_input.strip():
64
  st.error("Please provide valid feedback text.")
65
  else:
@@ -68,29 +45,24 @@ if st.button("Analyze Feedback"):
68
 
69
  # Filter categories with scores above the threshold
70
  relevant_categories = {
71
- label: score
72
  for label, score in zip(classification_result["labels"], classification_result["scores"])
73
  if score >= threshold
74
  }
75
 
 
76
  if relevant_categories:
77
- st.subheader("Categorized Feedback and Sentiment Ratings")
78
-
79
- # Perform sentiment analysis for the feedback
80
- sentiment_result = sentiment_analyzer(feedback_input)
81
- sentiment_score = sentiment_result[0]["score"]
82
- sentiment_label = sentiment_result[0]["label"]
83
 
84
- # Display results for each category
85
  for category, score in relevant_categories.items():
86
- sentiment_rating = sentiment_to_rating(sentiment_label, sentiment_score)
87
- st.write(
88
- f"**Category**: {category}\n"
89
- f"**Category Score**: {score:.4f}\n"
90
- f"**Sentiment**: {sentiment_label} ({sentiment_score:.4f})\n"
91
- f"**Rating**: {sentiment_rating}/5"
92
- )
93
- st.markdown("---")
 
94
  else:
95
- st.warning("No categories matched the selected confidence threshold.")
96
-
 
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
 
 
25
  height=200
26
  )
27
 
28
+ # Confidence threshold for zero-shot classification
29
  threshold = st.slider(
30
  "Confidence Threshold",
31
  min_value=0.0,
 
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:
 
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.")