Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,73 +21,59 @@ generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
| 21 |
|
| 22 |
# Function to classify customer comments
|
| 23 |
@spaces.GPU
|
| 24 |
-
def classify_comments(
|
| 25 |
sentiments = []
|
| 26 |
-
|
| 27 |
for comment in df['customer_comment']:
|
|
|
|
| 28 |
sentiment = classifier(comment)[0]['label']
|
| 29 |
-
|
| 30 |
-
category_str = ', '.join(
|
| 31 |
prompt = f"What category best describes this comment? '{comment}' Please answer using only the name of the category: {category_str}."
|
| 32 |
category = generator(prompt, max_length=30)[0]['generated_text']
|
| 33 |
-
|
| 34 |
sentiments.append(sentiment)
|
| 35 |
df['comment_sentiment'] = sentiments
|
| 36 |
-
df['comment_category'] =
|
| 37 |
return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)
|
| 38 |
|
| 39 |
# Gradio Interface
|
| 40 |
with gr.Blocks() as nps:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
category_list.append(new_category.strip()) # Add new category
|
| 44 |
-
return category_list
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 50 |
def display_categories(categories):
|
| 51 |
-
|
| 52 |
-
for i, cat in enumerate(categories):
|
| 53 |
-
with gr.Row():
|
| 54 |
-
gr.Markdown(f"- {cat}")
|
| 55 |
-
remove_btn = gr.Button("X", elem_id=f"remove_{i}", interactive=True)
|
| 56 |
-
remove_btn.click(
|
| 57 |
-
fn=remove_category,
|
| 58 |
-
inputs=[gr.State(cat), category_boxes],
|
| 59 |
-
outputs=category_boxes
|
| 60 |
-
)
|
| 61 |
-
category_components.append(gr.Row())
|
| 62 |
-
return category_components
|
| 63 |
-
|
| 64 |
-
category_boxes = gr.State([]) # Store category input boxes as state
|
| 65 |
-
category_column = gr.Column()
|
| 66 |
|
|
|
|
| 67 |
with gr.Row():
|
| 68 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
| 69 |
add_category_btn = gr.Button("Add Category")
|
| 70 |
-
|
| 71 |
-
fn=add_category,
|
| 72 |
-
inputs=[category_boxes, category_input],
|
| 73 |
-
outputs=category_boxes
|
| 74 |
-
)
|
| 75 |
-
category_boxes.change(
|
| 76 |
-
fn=display_categories,
|
| 77 |
-
inputs=category_boxes,
|
| 78 |
-
outputs=category_column
|
| 79 |
-
)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
| 82 |
template_btn = gr.Button("Use Template")
|
| 83 |
gr.Markdown("# NPS Comment Categorization")
|
|
|
|
|
|
|
| 84 |
classify_btn = gr.Button("Classify Comments")
|
| 85 |
output = gr.HTML()
|
| 86 |
|
|
|
|
| 87 |
def load_data(file):
|
| 88 |
if file is not None:
|
| 89 |
file.seek(0) # Reset file pointer
|
| 90 |
-
import io
|
| 91 |
if file.name.endswith('.csv'):
|
| 92 |
custom_df = pd.read_csv(file, encoding='utf-8')
|
| 93 |
else:
|
|
@@ -100,14 +86,35 @@ with gr.Blocks() as nps:
|
|
| 100 |
else:
|
| 101 |
return "No file uploaded."
|
| 102 |
|
| 103 |
-
|
| 104 |
-
template_btn.click(fn=lambda: "Using Template Dataset", outputs=output)
|
| 105 |
-
|
| 106 |
def use_template():
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
nps.launch(share=True)
|
|
|
|
| 21 |
|
| 22 |
# Function to classify customer comments
|
| 23 |
@spaces.GPU
|
| 24 |
+
def classify_comments(categories):
|
| 25 |
sentiments = []
|
| 26 |
+
assigned_categories = []
|
| 27 |
for comment in df['customer_comment']:
|
| 28 |
+
# Classify sentiment
|
| 29 |
sentiment = classifier(comment)[0]['label']
|
| 30 |
+
# Generate category
|
| 31 |
+
category_str = ', '.join(categories)
|
| 32 |
prompt = f"What category best describes this comment? '{comment}' Please answer using only the name of the category: {category_str}."
|
| 33 |
category = generator(prompt, max_length=30)[0]['generated_text']
|
| 34 |
+
assigned_categories.append(category)
|
| 35 |
sentiments.append(sentiment)
|
| 36 |
df['comment_sentiment'] = sentiments
|
| 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
|
| 43 |
+
categories = gr.State([])
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Function to add a category
|
| 46 |
+
def add_category(categories, new_category):
|
| 47 |
+
if new_category.strip() != "" and len(categories) < 5: # Limit to 5 categories
|
| 48 |
+
categories.append(new_category.strip())
|
| 49 |
+
return categories, f"Categories: {', '.join(categories)}"
|
| 50 |
|
| 51 |
+
# Function to display categories
|
| 52 |
def display_categories(categories):
|
| 53 |
+
return gr.Column.update(visible=True, value=[gr.Markdown(f"- {cat}") for cat in categories])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# UI for adding categories
|
| 56 |
with gr.Row():
|
| 57 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
| 58 |
add_category_btn = gr.Button("Add Category")
|
| 59 |
+
category_status = gr.Markdown("Categories: None")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Display added categories
|
| 62 |
+
category_display = gr.Column(visible=False)
|
| 63 |
+
|
| 64 |
+
# File upload and template buttons
|
| 65 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
| 66 |
template_btn = gr.Button("Use Template")
|
| 67 |
gr.Markdown("# NPS Comment Categorization")
|
| 68 |
+
|
| 69 |
+
# Classify button
|
| 70 |
classify_btn = gr.Button("Classify Comments")
|
| 71 |
output = gr.HTML()
|
| 72 |
|
| 73 |
+
# Function to load data from uploaded CSV
|
| 74 |
def load_data(file):
|
| 75 |
if file is not None:
|
| 76 |
file.seek(0) # Reset file pointer
|
|
|
|
| 77 |
if file.name.endswith('.csv'):
|
| 78 |
custom_df = pd.read_csv(file, encoding='utf-8')
|
| 79 |
else:
|
|
|
|
| 86 |
else:
|
| 87 |
return "No file uploaded."
|
| 88 |
|
| 89 |
+
# Function to use template categories
|
|
|
|
|
|
|
| 90 |
def use_template():
|
| 91 |
+
template_categories = ["Product Experience", "Customer Support", "Price of Service", "Other"]
|
| 92 |
+
return template_categories, f"Categories: {', '.join(template_categories)}"
|
| 93 |
+
|
| 94 |
+
# Event handlers
|
| 95 |
+
add_category_btn.click(
|
| 96 |
+
fn=add_category,
|
| 97 |
+
inputs=[categories, category_input],
|
| 98 |
+
outputs=[categories, category_status]
|
| 99 |
+
)
|
| 100 |
+
categories.change(
|
| 101 |
+
fn=display_categories,
|
| 102 |
+
inputs=categories,
|
| 103 |
+
outputs=category_display
|
| 104 |
+
)
|
| 105 |
+
uploaded_file.change(
|
| 106 |
+
fn=load_data,
|
| 107 |
+
inputs=uploaded_file,
|
| 108 |
+
outputs=output
|
| 109 |
+
)
|
| 110 |
+
template_btn.click(
|
| 111 |
+
fn=use_template,
|
| 112 |
+
outputs=[categories, category_status]
|
| 113 |
+
)
|
| 114 |
+
classify_btn.click(
|
| 115 |
+
fn=classify_comments,
|
| 116 |
+
inputs=categories,
|
| 117 |
+
outputs=output
|
| 118 |
+
)
|
| 119 |
|
| 120 |
nps.launch(share=True)
|