Spaces:
Running
on
Zero
Running
on
Zero
revert
Browse files
app.py
CHANGED
@@ -26,10 +26,6 @@ def classify_comments(categories):
|
|
26 |
global df # Ensure we're modifying the global DataFrame
|
27 |
sentiments = []
|
28 |
assigned_categories = []
|
29 |
-
|
30 |
-
# Debugging output
|
31 |
-
print("Classifying comments...")
|
32 |
-
|
33 |
for comment in df['customer_comment']:
|
34 |
# Classify sentiment
|
35 |
sentiment = classifier(comment)[0]['label']
|
@@ -39,60 +35,38 @@ def classify_comments(categories):
|
|
39 |
category = generator(prompt, max_length=30)[0]['generated_text']
|
40 |
assigned_categories.append(category)
|
41 |
sentiments.append(sentiment)
|
42 |
-
|
43 |
df['comment_sentiment'] = sentiments
|
44 |
df['comment_category'] = assigned_categories
|
45 |
-
|
46 |
-
# Debugging output
|
47 |
-
print(df.head())
|
48 |
-
print(df['comment_sentiment'].value_counts())
|
49 |
-
print(df['comment_category'].value_counts())
|
50 |
-
|
51 |
return df[['customer_id', 'customer_comment', 'comment_sentiment', 'comment_category', 'customer_nps', 'customer_segment']].to_html(index=False)
|
52 |
|
53 |
# Function to generate visualizations
|
54 |
-
@spaces.GPU
|
55 |
def visualize_output():
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
return None, None, None, "Error: DataFrame is empty. Please check the data or classification step.", None
|
61 |
-
|
62 |
-
# Check for required columns
|
63 |
-
required_columns = ['comment_sentiment', 'comment_category', 'customer_nps', 'customer_segment']
|
64 |
-
if not all(col in df.columns for col in required_columns):
|
65 |
-
return None, None, None, "Error: Required columns are missing. Please classify comments first.", None
|
66 |
-
|
67 |
-
# Explicitly convert data types
|
68 |
-
df['comment_sentiment'] = df['comment_sentiment'].astype(str)
|
69 |
-
df['comment_category'] = df['comment_category'].astype(str)
|
70 |
-
df['customer_nps'] = pd.to_numeric(df['customer_nps'], errors='coerce')
|
71 |
-
df['customer_segment'] = df['customer_segment'].astype(str)
|
72 |
-
|
73 |
-
# Drop NaN values
|
74 |
-
df = df.dropna(subset=['comment_sentiment', 'comment_category', 'customer_nps', 'customer_segment'])
|
75 |
-
|
76 |
-
# Debugging output
|
77 |
-
print(df.head())
|
78 |
-
print(df['comment_sentiment'].value_counts())
|
79 |
-
print(df['comment_category'].value_counts())
|
80 |
|
81 |
# Pie Chart of Sentiment
|
82 |
sentiment_counts = df['comment_sentiment'].value_counts()
|
83 |
sentiment_pie = px.pie(
|
84 |
values=sentiment_counts.values,
|
85 |
names=sentiment_counts.index,
|
86 |
-
title="Sentiment Distribution"
|
|
|
|
|
87 |
)
|
|
|
88 |
|
89 |
# Pie Chart of Comment Categories
|
90 |
category_counts = df['comment_category'].value_counts()
|
91 |
category_pie = px.pie(
|
92 |
values=category_counts.values,
|
93 |
names=category_counts.index,
|
94 |
-
title="Comment Category Distribution"
|
|
|
|
|
95 |
)
|
|
|
96 |
|
97 |
# Stacked Bar Chart of Sentiment by Category
|
98 |
sentiment_by_category = df.groupby(['comment_category', 'comment_sentiment']).size().unstack()
|
@@ -125,36 +99,44 @@ def visualize_output():
|
|
125 |
sentiment_by_segment = df.groupby(['customer_segment', 'comment_sentiment']).size().unstack()
|
126 |
sentiment_by_segment_pie = px.pie(
|
127 |
sentiment_by_segment,
|
128 |
-
title="Sentiment by Customer Segment"
|
|
|
129 |
)
|
130 |
|
131 |
return sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie
|
132 |
|
133 |
# Gradio Interface
|
134 |
with gr.Blocks() as nps:
|
|
|
135 |
categories = gr.State([])
|
136 |
|
|
|
137 |
def add_category(categories, new_category):
|
138 |
-
if new_category.strip() != "" and len(categories) < 5:
|
139 |
categories.append(new_category.strip())
|
140 |
return categories, "", f"**Categories:**\n" + "\n".join([f"- {cat}" for cat in categories])
|
141 |
|
|
|
142 |
def reset_categories():
|
143 |
return [], "**Categories:**\n- None"
|
144 |
|
|
|
145 |
with gr.Row():
|
146 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
147 |
add_category_btn = gr.Button("Add Category")
|
148 |
reset_btn = gr.Button("Reset Categories")
|
149 |
category_status = gr.Markdown("**Categories:**\n- None")
|
150 |
|
|
|
151 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
152 |
template_btn = gr.Button("Use Template")
|
153 |
gr.Markdown("# NPS Comment Categorization")
|
154 |
|
|
|
155 |
classify_btn = gr.Button("Classify Comments")
|
156 |
output = gr.HTML()
|
157 |
|
|
|
158 |
visualize_btn = gr.Button("Visualize Output")
|
159 |
sentiment_pie = gr.Plot(label="Sentiment Distribution")
|
160 |
category_pie = gr.Plot(label="Comment Category Distribution")
|
@@ -162,9 +144,56 @@ with gr.Blocks() as nps:
|
|
162 |
kpi_visualization = gr.Markdown()
|
163 |
sentiment_by_segment_pie = gr.Plot(label="Sentiment by Customer Segment")
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
-
nps.launch(share=True)
|
|
|
26 |
global df # Ensure we're modifying the global DataFrame
|
27 |
sentiments = []
|
28 |
assigned_categories = []
|
|
|
|
|
|
|
|
|
29 |
for comment in df['customer_comment']:
|
30 |
# Classify sentiment
|
31 |
sentiment = classifier(comment)[0]['label']
|
|
|
35 |
category = generator(prompt, max_length=30)[0]['generated_text']
|
36 |
assigned_categories.append(category)
|
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():
|
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()
|
|
|
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:
|
110 |
+
# State to store categories
|
111 |
categories = gr.State([])
|
112 |
|
113 |
+
# Function to add a category
|
114 |
def add_category(categories, new_category):
|
115 |
+
if new_category.strip() != "" and len(categories) < 5: # Limit to 5 categories
|
116 |
categories.append(new_category.strip())
|
117 |
return categories, "", f"**Categories:**\n" + "\n".join([f"- {cat}" for cat in categories])
|
118 |
|
119 |
+
# Function to reset categories
|
120 |
def reset_categories():
|
121 |
return [], "**Categories:**\n- None"
|
122 |
|
123 |
+
# UI for adding categories
|
124 |
with gr.Row():
|
125 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
126 |
add_category_btn = gr.Button("Add Category")
|
127 |
reset_btn = gr.Button("Reset Categories")
|
128 |
category_status = gr.Markdown("**Categories:**\n- None")
|
129 |
|
130 |
+
# File upload and template buttons
|
131 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
132 |
template_btn = gr.Button("Use Template")
|
133 |
gr.Markdown("# NPS Comment Categorization")
|
134 |
|
135 |
+
# Classify button
|
136 |
classify_btn = gr.Button("Classify Comments")
|
137 |
output = gr.HTML()
|
138 |
|
139 |
+
# Visualize button
|
140 |
visualize_btn = gr.Button("Visualize Output")
|
141 |
sentiment_pie = gr.Plot(label="Sentiment Distribution")
|
142 |
category_pie = gr.Plot(label="Comment Category Distribution")
|
|
|
144 |
kpi_visualization = gr.Markdown()
|
145 |
sentiment_by_segment_pie = gr.Plot(label="Sentiment by Customer Segment")
|
146 |
|
147 |
+
# Function to load data from uploaded CSV
|
148 |
+
def load_data(file):
|
149 |
+
global df # Ensure we're modifying the global DataFrame
|
150 |
+
if file is not None:
|
151 |
+
file.seek(0) # Reset file pointer
|
152 |
+
if file.name.endswith('.csv'):
|
153 |
+
custom_df = pd.read_csv(file, encoding='utf-8')
|
154 |
+
else:
|
155 |
+
return "Error: Uploaded file is not a CSV."
|
156 |
+
# Check for required columns
|
157 |
+
required_columns = ['customer_id', 'customer_comment', 'customer_nps', 'customer_segment']
|
158 |
+
if not all(col in custom_df.columns for col in required_columns):
|
159 |
+
return f"Error: Uploaded CSV must contain the following columns: {', '.join(required_columns)}"
|
160 |
+
df = custom_df
|
161 |
+
return "Custom CSV loaded successfully!"
|
162 |
+
else:
|
163 |
+
return "No file uploaded."
|
164 |
+
|
165 |
+
# Function to use template categories
|
166 |
+
def use_template():
|
167 |
+
template_categories = ["Product Experience", "Customer Support", "Price of Service", "Other"]
|
168 |
+
return template_categories, f"**Categories:**\n" + "\n".join([f"- {cat}" for cat in template_categories])
|
169 |
+
|
170 |
+
# Event handlers
|
171 |
+
add_category_btn.click(
|
172 |
+
fn=add_category,
|
173 |
+
inputs=[categories, category_input],
|
174 |
+
outputs=[categories, category_input, category_status]
|
175 |
+
)
|
176 |
+
reset_btn.click(
|
177 |
+
fn=reset_categories,
|
178 |
+
outputs=[categories, category_status]
|
179 |
+
)
|
180 |
+
uploaded_file.change(
|
181 |
+
fn=load_data,
|
182 |
+
inputs=uploaded_file,
|
183 |
+
outputs=output
|
184 |
+
)
|
185 |
+
template_btn.click(
|
186 |
+
fn=use_template,
|
187 |
+
outputs=[categories, category_status]
|
188 |
+
)
|
189 |
+
classify_btn.click(
|
190 |
+
fn=classify_comments,
|
191 |
+
inputs=categories,
|
192 |
+
outputs=output
|
193 |
+
)
|
194 |
+
visualize_btn.click(
|
195 |
+
fn=visualize_output,
|
196 |
+
outputs=[sentiment_pie, category_pie, stacked_bar, kpi_visualization, sentiment_by_segment_pie]
|
197 |
+
)
|
198 |
|
199 |
+
nps.launch(share=True)
|