Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -40,48 +40,16 @@ def classify_comments(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 |
-
|
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 |
-
#
|
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 |
-
|
85 |
sentiment_by_category,
|
86 |
barmode='stack',
|
87 |
title="Sentiment by Comment Category",
|
@@ -90,37 +58,20 @@ def visualize_output():
|
|
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 |
-
|
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,
|
124 |
|
125 |
# Gradio Interface
|
126 |
with gr.Blocks() as nps:
|
|
|
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 |
# Ensure the required columns exist
|
44 |
if 'comment_sentiment' not in df.columns or 'comment_category' not in df.columns:
|
45 |
+
return None, "Error: Please classify comments before visualizing."
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
try:
|
48 |
+
# Bar Chart of Sentiment by Category
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
sentiment_by_category = df.groupby(['comment_category', 'comment_sentiment']).size().unstack()
|
50 |
print("Sentiment by Category:")
|
51 |
print(sentiment_by_category)
|
52 |
+
bar_chart = px.bar(
|
53 |
sentiment_by_category,
|
54 |
barmode='stack',
|
55 |
title="Sentiment by Comment Category",
|
|
|
58 |
|
59 |
# KPI Visualizations
|
60 |
avg_nps = df['customer_nps'].mean()
|
|
|
|
|
|
|
61 |
avg_nps_by_segment = df.groupby('customer_segment')['customer_nps'].mean().reset_index()
|
62 |
|
63 |
kpi_visualization = f"""
|
64 |
**Average NPS Scores:**
|
65 |
- Overall: {avg_nps:.2f}
|
|
|
|
|
|
|
|
|
66 |
**Average NPS by Segment:**
|
67 |
{avg_nps_by_segment.to_markdown(index=False)}
|
68 |
"""
|
69 |
|
70 |
+
return bar_chart, kpi_visualization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
except Exception as e:
|
73 |
print(f"Error in visualize_output: {e}")
|
74 |
+
return None, f"Error: {str(e)}"
|
75 |
|
76 |
# Gradio Interface
|
77 |
with gr.Blocks() as nps:
|