ZennyKenny commited on
Commit
b7467c0
Β·
verified Β·
1 Parent(s): 8d2cc8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -60
app.py CHANGED
@@ -39,71 +39,88 @@ def classify_comments(categories):
39
  df['comment_category'] = assigned_categories
40
  return df[['customer_id', 'customer_comment', 'comment_sentiment', 'comment_category', 'customer_nps', 'customer_segment']].to_html(index=False)
41
 
42
- # Function to generate visualizations
43
  def visualize_output():
 
 
 
44
  # Ensure the required columns exist
45
  if 'comment_sentiment' not in df.columns or 'comment_category' not in df.columns:
46
- # Return 5 values (None for plots and an error message for markdown)
47
  return None, None, None, "Error: Please classify comments before visualizing.", None
48
 
49
- # Pie Chart of Sentiment
50
- sentiment_counts = df['comment_sentiment'].value_counts()
51
- sentiment_pie = px.pie(
52
- values=sentiment_counts.values,
53
- names=sentiment_counts.index,
54
- title="Sentiment Distribution",
55
- hover_data=[sentiment_counts.values],
56
- labels={'value': 'Count', 'names': 'Sentiment'}
57
- )
58
- sentiment_pie.update_traces(textinfo='percent+label', hovertemplate="Sentiment: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
59
-
60
- # Pie Chart of Comment Categories
61
- category_counts = df['comment_category'].value_counts()
62
- category_pie = px.pie(
63
- values=category_counts.values,
64
- names=category_counts.index,
65
- title="Comment Category Distribution",
66
- hover_data=[category_counts.values],
67
- labels={'value': 'Count', 'names': 'Category'}
68
- )
69
- category_pie.update_traces(textinfo='percent+label', hovertemplate="Category: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
70
-
71
- # Stacked Bar Chart of Sentiment by Category
72
- sentiment_by_category = df.groupby(['comment_category', 'comment_sentiment']).size().unstack()
73
- stacked_bar = px.bar(
74
- sentiment_by_category,
75
- barmode='stack',
76
- title="Sentiment by Comment Category",
77
- labels={'value': 'Count', 'comment_category': 'Category', 'comment_sentiment': 'Sentiment'}
78
- )
79
-
80
- # KPI Visualizations
81
- avg_nps = df['customer_nps'].mean()
82
- avg_nps_positive = df[df['comment_sentiment'] == 'POSITIVE']['customer_nps'].mean()
83
- avg_nps_negative = df[df['comment_sentiment'] == 'NEGATIVE']['customer_nps'].mean()
84
- avg_nps_by_category = df.groupby('comment_category')['customer_nps'].mean().reset_index()
85
- avg_nps_by_segment = df.groupby('customer_segment')['customer_nps'].mean().reset_index()
86
-
87
- kpi_visualization = f"""
88
- **Average NPS Scores:**
89
- - Overall: {avg_nps:.2f}
90
- - Positive Sentiment: {avg_nps_positive:.2f}
91
- - Negative Sentiment: {avg_nps_negative:.2f}
92
- **Average NPS by Category:**
93
- {avg_nps_by_category.to_markdown(index=False)}
94
- **Average NPS by Segment:**
95
- {avg_nps_by_segment.to_markdown(index=False)}
96
- """
97
-
98
- # Pie Chart of Sentiment by Customer Segment
99
- sentiment_by_segment = df.groupby(['customer_segment', 'comment_sentiment']).size().unstack()
100
- sentiment_by_segment_pie = px.pie(
101
- sentiment_by_segment,
102
- title="Sentiment by Customer Segment",
103
- labels={'value': 'Count', 'customer_segment': 'Segment', 'comment_sentiment': 'Sentiment'}
104
- )
105
-
106
- return sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  # Gradio Interface
109
  with gr.Blocks() as nps:
 
39
  df['comment_category'] = assigned_categories
40
  return df[['customer_id', 'customer_comment', 'comment_sentiment', 'comment_category', 'customer_nps', 'customer_segment']].to_html(index=False)
41
 
 
42
  def visualize_output():
43
+ # Debug: Print the columns in the DataFrame
44
+ print("Columns in DataFrame:", df.columns.tolist())
45
+
46
  # Ensure the required columns exist
47
  if 'comment_sentiment' not in df.columns or 'comment_category' not in df.columns:
48
+ print("Error: Required columns missing. Please classify comments first.")
49
  return None, None, None, "Error: Please classify comments before visualizing.", None
50
 
51
+ # Debug: Print the first few rows of the DataFrame
52
+ print("First few rows of DataFrame:")
53
+ print(df.head())
54
+
55
+ try:
56
+ # Pie Chart of Sentiment
57
+ sentiment_counts = df['comment_sentiment'].value_counts()
58
+ print("Sentiment counts:", sentiment_counts)
59
+ sentiment_pie = px.pie(
60
+ values=sentiment_counts.values,
61
+ names=sentiment_counts.index,
62
+ title="Sentiment Distribution",
63
+ hover_data=[sentiment_counts.values],
64
+ labels={'value': 'Count', 'names': 'Sentiment'}
65
+ )
66
+ sentiment_pie.update_traces(textinfo='percent+label', hovertemplate="Sentiment: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
67
+
68
+ # Pie Chart of Comment Categories
69
+ category_counts = df['comment_category'].value_counts()
70
+ print("Category counts:", category_counts)
71
+ category_pie = px.pie(
72
+ values=category_counts.values,
73
+ names=category_counts.index,
74
+ title="Comment Category Distribution",
75
+ hover_data=[category_counts.values],
76
+ labels={'value': 'Count', 'names': 'Category'}
77
+ )
78
+ category_pie.update_traces(textinfo='percent+label', hovertemplate="Category: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
79
+
80
+ # Stacked Bar Chart of Sentiment by Category
81
+ sentiment_by_category = df.groupby(['comment_category', 'comment_sentiment']).size().unstack()
82
+ print("Sentiment by Category:")
83
+ print(sentiment_by_category)
84
+ stacked_bar = px.bar(
85
+ sentiment_by_category,
86
+ barmode='stack',
87
+ title="Sentiment by Comment Category",
88
+ labels={'value': 'Count', 'comment_category': 'Category', 'comment_sentiment': 'Sentiment'}
89
+ )
90
+
91
+ # KPI Visualizations
92
+ avg_nps = df['customer_nps'].mean()
93
+ avg_nps_positive = df[df['comment_sentiment'] == 'POSITIVE']['customer_nps'].mean()
94
+ avg_nps_negative = df[df['comment_sentiment'] == 'NEGATIVE']['customer_nps'].mean()
95
+ avg_nps_by_category = df.groupby('comment_category')['customer_nps'].mean().reset_index()
96
+ avg_nps_by_segment = df.groupby('customer_segment')['customer_nps'].mean().reset_index()
97
+
98
+ kpi_visualization = f"""
99
+ **Average NPS Scores:**
100
+ - Overall: {avg_nps:.2f}
101
+ - Positive Sentiment: {avg_nps_positive:.2f}
102
+ - Negative Sentiment: {avg_nps_negative:.2f}
103
+ **Average NPS by Category:**
104
+ {avg_nps_by_category.to_markdown(index=False)}
105
+ **Average NPS by Segment:**
106
+ {avg_nps_by_segment.to_markdown(index=False)}
107
+ """
108
+
109
+ # Pie Chart of Sentiment by Customer Segment
110
+ sentiment_by_segment = df.groupby(['customer_segment', 'comment_sentiment']).size().unstack()
111
+ print("Sentiment by Segment:")
112
+ print(sentiment_by_segment)
113
+ sentiment_by_segment_pie = px.pie(
114
+ sentiment_by_segment,
115
+ title="Sentiment by Customer Segment",
116
+ labels={'value': 'Count', 'customer_segment': 'Segment', 'comment_sentiment': 'Sentiment'}
117
+ )
118
+
119
+ return sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie
120
+
121
+ except Exception as e:
122
+ print(f"Error in visualize_output: {e}")
123
+ return None, None, None, f"Error: {str(e)}", None
124
 
125
  # Gradio Interface
126
  with gr.Blocks() as nps: