Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
import pandas as pd
|
4 |
import spaces
|
|
|
5 |
|
6 |
# Load dataset
|
7 |
from datasets import load_dataset
|
@@ -37,6 +38,56 @@ def classify_comments(categories):
|
|
37 |
df['comment_category'] = assigned_categories
|
38 |
return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Gradio Interface
|
41 |
with gr.Blocks() as nps:
|
42 |
# State to store categories
|
@@ -68,6 +119,13 @@ with gr.Blocks() as nps:
|
|
68 |
classify_btn = gr.Button("Classify Comments")
|
69 |
output = gr.HTML()
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# Function to load data from uploaded CSV
|
72 |
def load_data(file):
|
73 |
if file is not None:
|
@@ -76,8 +134,10 @@ with gr.Blocks() as nps:
|
|
76 |
custom_df = pd.read_csv(file, encoding='utf-8')
|
77 |
else:
|
78 |
return "Error: Uploaded file is not a CSV."
|
79 |
-
|
80 |
-
|
|
|
|
|
81 |
global df
|
82 |
df = custom_df
|
83 |
return "Custom CSV loaded successfully!"
|
@@ -113,5 +173,9 @@ with gr.Blocks() as nps:
|
|
113 |
inputs=categories,
|
114 |
outputs=output
|
115 |
)
|
|
|
|
|
|
|
|
|
116 |
|
117 |
nps.launch(share=True)
|
|
|
2 |
from transformers import pipeline
|
3 |
import pandas as pd
|
4 |
import spaces
|
5 |
+
import plotly.express as px
|
6 |
|
7 |
# Load dataset
|
8 |
from datasets import load_dataset
|
|
|
38 |
df['comment_category'] = assigned_categories
|
39 |
return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)
|
40 |
|
41 |
+
# Function to generate visualizations
|
42 |
+
def visualize_output():
|
43 |
+
# Pie Chart of Sentiment
|
44 |
+
sentiment_counts = df['comment_sentiment'].value_counts()
|
45 |
+
sentiment_pie = px.pie(
|
46 |
+
values=sentiment_counts.values,
|
47 |
+
names=sentiment_counts.index,
|
48 |
+
title="Sentiment Distribution",
|
49 |
+
hover_data=[sentiment_counts.values],
|
50 |
+
labels={'value': 'Count', 'names': 'Sentiment'}
|
51 |
+
)
|
52 |
+
sentiment_pie.update_traces(textinfo='percent+label', hovertemplate="Sentiment: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
|
53 |
+
|
54 |
+
# Pie Chart of Comment Categories
|
55 |
+
category_counts = df['comment_category'].value_counts()
|
56 |
+
category_pie = px.pie(
|
57 |
+
values=category_counts.values,
|
58 |
+
names=category_counts.index,
|
59 |
+
title="Comment Category Distribution",
|
60 |
+
hover_data=[category_counts.values],
|
61 |
+
labels={'value': 'Count', 'names': 'Category'}
|
62 |
+
)
|
63 |
+
category_pie.update_traces(textinfo='percent+label', hovertemplate="Category: %{label}<br>Count: %{value}<br>Percentage: %{percent}")
|
64 |
+
|
65 |
+
# Stacked Bar Chart of Sentiment by Category
|
66 |
+
sentiment_by_category = df.groupby(['comment_category', 'comment_sentiment']).size().unstack()
|
67 |
+
stacked_bar = px.bar(
|
68 |
+
sentiment_by_category,
|
69 |
+
barmode='stack',
|
70 |
+
title="Sentiment by Comment Category",
|
71 |
+
labels={'value': 'Count', 'comment_category': 'Category', 'comment_sentiment': 'Sentiment'}
|
72 |
+
)
|
73 |
+
|
74 |
+
# KPI Visualizations
|
75 |
+
avg_nps = df['customer_nps'].mean()
|
76 |
+
avg_nps_positive = df[df['comment_sentiment'] == 'POSITIVE']['customer_nps'].mean()
|
77 |
+
avg_nps_negative = df[df['comment_sentiment'] == 'NEGATIVE']['customer_nps'].mean()
|
78 |
+
avg_nps_by_category = df.groupby('comment_category')['customer_nps'].mean().reset_index()
|
79 |
+
|
80 |
+
kpi_visualization = f"""
|
81 |
+
**Average NPS Scores:**
|
82 |
+
- Overall: {avg_nps:.2f}
|
83 |
+
- Positive Sentiment: {avg_nps_positive:.2f}
|
84 |
+
- Negative Sentiment: {avg_nps_negative:.2f}
|
85 |
+
**Average NPS by Category:**
|
86 |
+
{avg_nps_by_category.to_markdown(index=False)}
|
87 |
+
"""
|
88 |
+
|
89 |
+
return sentiment_pie, category_pie, stacked_bar, kpi_visualization
|
90 |
+
|
91 |
# Gradio Interface
|
92 |
with gr.Blocks() as nps:
|
93 |
# State to store categories
|
|
|
119 |
classify_btn = gr.Button("Classify Comments")
|
120 |
output = gr.HTML()
|
121 |
|
122 |
+
# Visualize button
|
123 |
+
visualize_btn = gr.Button("Visualize Output")
|
124 |
+
sentiment_pie = gr.Plot(label="Sentiment Distribution")
|
125 |
+
category_pie = gr.Plot(label="Comment Category Distribution")
|
126 |
+
stacked_bar = gr.Plot(label="Sentiment by Comment Category")
|
127 |
+
kpi_visualization = gr.Markdown()
|
128 |
+
|
129 |
# Function to load data from uploaded CSV
|
130 |
def load_data(file):
|
131 |
if file is not None:
|
|
|
134 |
custom_df = pd.read_csv(file, encoding='utf-8')
|
135 |
else:
|
136 |
return "Error: Uploaded file is not a CSV."
|
137 |
+
# Check for required columns
|
138 |
+
required_columns = ['customer_comment', 'customer_nps']
|
139 |
+
if not all(col in custom_df.columns for col in required_columns):
|
140 |
+
return f"Error: Uploaded CSV must contain the following columns: {', '.join(required_columns)}"
|
141 |
global df
|
142 |
df = custom_df
|
143 |
return "Custom CSV loaded successfully!"
|
|
|
173 |
inputs=categories,
|
174 |
outputs=output
|
175 |
)
|
176 |
+
visualize_btn.click(
|
177 |
+
fn=visualize_output,
|
178 |
+
outputs=[sentiment_pie, category_pie, stacked_bar, kpi_visualization]
|
179 |
+
)
|
180 |
|
181 |
nps.launch(share=True)
|