ZennyKenny commited on
Commit
33c57aa
Β·
verified Β·
1 Parent(s): 962079a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -37,7 +37,7 @@ def classify_comments(categories):
37
  sentiments.append(sentiment)
38
  df['comment_sentiment'] = sentiments
39
  df['comment_category'] = assigned_categories
40
- return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)
41
 
42
  # Function to generate visualizations
43
  def visualize_output():
@@ -81,6 +81,7 @@ def visualize_output():
81
  avg_nps_positive = df[df['comment_sentiment'] == 'POSITIVE']['customer_nps'].mean()
82
  avg_nps_negative = df[df['comment_sentiment'] == 'NEGATIVE']['customer_nps'].mean()
83
  avg_nps_by_category = df.groupby('comment_category')['customer_nps'].mean().reset_index()
 
84
 
85
  kpi_visualization = f"""
86
  **Average NPS Scores:**
@@ -89,9 +90,19 @@ def visualize_output():
89
  - Negative Sentiment: {avg_nps_negative:.2f}
90
  **Average NPS by Category:**
91
  {avg_nps_by_category.to_markdown(index=False)}
 
 
92
  """
93
 
94
- return sentiment_pie, category_pie, stacked_bar, kpi_visualization
 
 
 
 
 
 
 
 
95
 
96
  # Gradio Interface
97
  with gr.Blocks() as nps:
@@ -130,6 +141,7 @@ with gr.Blocks() as nps:
130
  category_pie = gr.Plot(label="Comment Category Distribution")
131
  stacked_bar = gr.Plot(label="Sentiment by Comment Category")
132
  kpi_visualization = gr.Markdown()
 
133
 
134
  # Function to load data from uploaded CSV
135
  def load_data(file):
@@ -141,7 +153,7 @@ with gr.Blocks() as nps:
141
  else:
142
  return "Error: Uploaded file is not a CSV."
143
  # Check for required columns
144
- required_columns = ['customer_comment', 'customer_nps']
145
  if not all(col in custom_df.columns for col in required_columns):
146
  return f"Error: Uploaded CSV must contain the following columns: {', '.join(required_columns)}"
147
  df = custom_df
@@ -180,7 +192,7 @@ with gr.Blocks() as nps:
180
  )
181
  visualize_btn.click(
182
  fn=visualize_output,
183
- outputs=[sentiment_pie, category_pie, stacked_bar, kpi_visualization]
184
  )
185
 
186
  nps.launch(share=True)
 
37
  sentiments.append(sentiment)
38
  df['comment_sentiment'] = sentiments
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():
 
81
  avg_nps_positive = df[df['comment_sentiment'] == 'POSITIVE']['customer_nps'].mean()
82
  avg_nps_negative = df[df['comment_sentiment'] == 'NEGATIVE']['customer_nps'].mean()
83
  avg_nps_by_category = df.groupby('comment_category')['customer_nps'].mean().reset_index()
84
+ avg_nps_by_segment = df.groupby('customer_segment')['customer_nps'].mean().reset_index()
85
 
86
  kpi_visualization = f"""
87
  **Average NPS Scores:**
 
90
  - Negative Sentiment: {avg_nps_negative:.2f}
91
  **Average NPS by Category:**
92
  {avg_nps_by_category.to_markdown(index=False)}
93
+ **Average NPS by Segment:**
94
+ {avg_nps_by_segment.to_markdown(index=False)}
95
  """
96
 
97
+ # Pie Chart of Sentiment by Customer Segment
98
+ sentiment_by_segment = df.groupby(['customer_segment', 'comment_sentiment']).size().unstack()
99
+ sentiment_by_segment_pie = px.pie(
100
+ sentiment_by_segment,
101
+ title="Sentiment by Customer Segment",
102
+ labels={'value': 'Count', 'customer_segment': 'Segment', 'comment_sentiment': 'Sentiment'}
103
+ )
104
+
105
+ return sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie
106
 
107
  # Gradio Interface
108
  with gr.Blocks() as nps:
 
141
  category_pie = gr.Plot(label="Comment Category Distribution")
142
  stacked_bar = gr.Plot(label="Sentiment by Comment Category")
143
  kpi_visualization = gr.Markdown()
144
+ sentiment_by_segment_pie = gr.Plot(label="Sentiment by Customer Segment")
145
 
146
  # Function to load data from uploaded CSV
147
  def load_data(file):
 
153
  else:
154
  return "Error: Uploaded file is not a CSV."
155
  # Check for required columns
156
+ required_columns = ['customer_id', 'customer_comment', 'customer_nps', 'customer_segment']
157
  if not all(col in custom_df.columns for col in required_columns):
158
  return f"Error: Uploaded CSV must contain the following columns: {', '.join(required_columns)}"
159
  df = custom_df
 
192
  )
193
  visualize_btn.click(
194
  fn=visualize_output,
195
+ outputs=[sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie]
196
  )
197
 
198
  nps.launch(share=True)