Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,15 +20,14 @@ classifier = pipeline("text-classification", model="distilbert/distilbert-base-u
|
|
| 20 |
generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 21 |
|
| 22 |
# Function to classify customer comments
|
| 23 |
-
# https://huggingface.co/docs/hub/spaces-zerogpu
|
| 24 |
@spaces.GPU
|
| 25 |
-
def classify_comments():
|
| 26 |
sentiments = []
|
| 27 |
categories = []
|
| 28 |
results = []
|
| 29 |
for comment in df['customer_comment']:
|
| 30 |
sentiment = classifier(comment)[0]['label']
|
| 31 |
-
category_list = [box
|
| 32 |
category_str = ', '.join([cat.strip() for cat in category_list])
|
| 33 |
prompt = f"What category best describes this comment? '{comment}' Please answer using only the name of the category: {category_str}."
|
| 34 |
category = generator(prompt, max_length=30)[0]['generated_text']
|
|
@@ -48,54 +47,27 @@ with gr.Blocks() as nps:
|
|
| 48 |
category_boxes = gr.State([]) # Store category input boxes as state
|
| 49 |
|
| 50 |
def display_categories(categories):
|
| 51 |
-
category_column.children = [] # Clear previous categories
|
| 52 |
-
for i, cat in enumerate(categories):
|
| 53 |
-
with category_column:
|
| 54 |
-
with gr.Row():
|
| 55 |
-
gr.Markdown(f"- {cat}")
|
| 56 |
-
remove_btn = gr.Button("X", elem_id=f"remove_{i}", interactive=True)
|
| 57 |
-
remove_btn.click(fn=lambda x=cat: remove_category(x, categories), inputs=[], outputs=category_boxes)
|
| 58 |
-
category_column.children = [] # Reset children to clear previous categories
|
| 59 |
-
for i, cat in enumerate(categories):
|
| 60 |
-
with category_column:
|
| 61 |
-
with gr.Row():
|
| 62 |
-
gr.Markdown(f"- {cat}")
|
| 63 |
-
remove_btn = gr.Button("X", elem_id=f"remove_{i}", interactive=True)
|
| 64 |
-
remove_btn.click(fn=lambda x=cat: remove_category(x, categories), inputs=[], outputs=category_boxes)
|
| 65 |
category_components = []
|
| 66 |
for i, cat in enumerate(categories):
|
| 67 |
with gr.Row():
|
| 68 |
gr.Markdown(f"- {cat}")
|
| 69 |
remove_btn = gr.Button("X", elem_id=f"remove_{i}", interactive=True)
|
| 70 |
remove_btn.click(fn=lambda x=cat: remove_category(x, categories), inputs=[], outputs=category_boxes)
|
|
|
|
| 71 |
return category_components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
| 74 |
add_category_btn = gr.Button("Add Category")
|
| 75 |
add_category_btn.click(fn=add_category, inputs=[category_boxes, category_input], outputs=category_boxes)
|
| 76 |
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 77 |
-
|
| 78 |
-
category_list.remove(category) # Remove selected category
|
| 79 |
-
return category_list
|
| 80 |
-
components = []
|
| 81 |
-
for i, cat in enumerate(categories):
|
| 82 |
-
row = gr.Row([
|
| 83 |
-
gr.Markdown(f"- {cat}"),
|
| 84 |
-
gr.Button("X", elem_id=f"remove_{i}", interactive=True).click(fn=lambda x=cat: remove_category(x, categories), inputs=[], outputs=category_boxes)
|
| 85 |
-
])
|
| 86 |
-
components.append(row)
|
| 87 |
-
return components
|
| 88 |
-
for i, cat in enumerate(categories):
|
| 89 |
-
row = gr.Row([
|
| 90 |
-
gr.Textbox(value=cat, label=f"Category {i+1}", interactive=True),
|
| 91 |
-
gr.Button("X", elem_id=f"remove_{i}")
|
| 92 |
-
])
|
| 93 |
-
components.append(row)
|
| 94 |
-
return components
|
| 95 |
-
category_column = gr.Column()
|
| 96 |
-
add_category_btn.click(fn=add_category, inputs=[category_boxes, category_input], outputs=category_boxes)
|
| 97 |
-
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 98 |
-
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 99 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
| 100 |
template_btn = gr.Button("Use Template")
|
| 101 |
gr.Markdown("# NPS Comment Categorization")
|
|
@@ -128,4 +100,4 @@ with gr.Blocks() as nps:
|
|
| 128 |
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 129 |
classify_btn.click(fn=classify_comments, inputs=category_boxes, outputs=output)
|
| 130 |
|
| 131 |
-
nps.launch()
|
|
|
|
| 20 |
generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 21 |
|
| 22 |
# Function to classify customer comments
|
|
|
|
| 23 |
@spaces.GPU
|
| 24 |
+
def classify_comments(category_boxes):
|
| 25 |
sentiments = []
|
| 26 |
categories = []
|
| 27 |
results = []
|
| 28 |
for comment in df['customer_comment']:
|
| 29 |
sentiment = classifier(comment)[0]['label']
|
| 30 |
+
category_list = [box for box in category_boxes if box.strip() != '']
|
| 31 |
category_str = ', '.join([cat.strip() for cat in category_list])
|
| 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']
|
|
|
|
| 47 |
category_boxes = gr.State([]) # Store category input boxes as state
|
| 48 |
|
| 49 |
def display_categories(categories):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
category_components = []
|
| 51 |
for i, cat in enumerate(categories):
|
| 52 |
with gr.Row():
|
| 53 |
gr.Markdown(f"- {cat}")
|
| 54 |
remove_btn = gr.Button("X", elem_id=f"remove_{i}", interactive=True)
|
| 55 |
remove_btn.click(fn=lambda x=cat: remove_category(x, categories), inputs=[], outputs=category_boxes)
|
| 56 |
+
category_components.append(gr.Row())
|
| 57 |
return category_components
|
| 58 |
+
|
| 59 |
+
def remove_category(category, category_list):
|
| 60 |
+
category_list.remove(category) # Remove selected category
|
| 61 |
+
return category_list
|
| 62 |
+
|
| 63 |
+
category_column = gr.Column()
|
| 64 |
+
|
| 65 |
with gr.Row():
|
| 66 |
category_input = gr.Textbox(label="New Category", placeholder="Enter category name")
|
| 67 |
add_category_btn = gr.Button("Add Category")
|
| 68 |
add_category_btn.click(fn=add_category, inputs=[category_boxes, category_input], outputs=category_boxes)
|
| 69 |
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 70 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
uploaded_file = gr.File(label="Upload CSV", type="filepath")
|
| 72 |
template_btn = gr.Button("Use Template")
|
| 73 |
gr.Markdown("# NPS Comment Categorization")
|
|
|
|
| 100 |
category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
|
| 101 |
classify_btn.click(fn=classify_comments, inputs=category_boxes, outputs=output)
|
| 102 |
|
| 103 |
+
nps.launch()
|