Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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 |
-
|
47 |
return None, None, None, "Error: Please classify comments before visualizing.", None
|
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 |
# 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:
|