ZennyKenny commited on
Commit
67df231
Β·
verified Β·
1 Parent(s): b9c37e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -47
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(category_boxes):
25
  sentiments = []
26
- categories = []
27
  for comment in df['customer_comment']:
 
28
  sentiment = classifier(comment)[0]['label']
29
- category_list = [box for box in category_boxes if box.strip() != '']
30
- category_str = ', '.join([cat.strip() for cat in category_list])
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
- categories.append(category)
34
  sentiments.append(sentiment)
35
  df['comment_sentiment'] = sentiments
36
- df['comment_category'] = categories
37
  return df[['customer_comment', 'comment_sentiment', 'comment_category']].to_html(index=False)
38
 
39
  # Gradio Interface
40
  with gr.Blocks() as nps:
41
- def add_category(category_list, new_category):
42
- if new_category.strip() != "":
43
- category_list.append(new_category.strip()) # Add new category
44
- return category_list
45
 
46
- def remove_category(category, category_list):
47
- category_list.remove(category) # Remove selected category
48
- return category_list
 
 
49
 
 
50
  def display_categories(categories):
51
- category_components = []
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
- add_category_btn.click(
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
- uploaded_file.change(fn=load_data, inputs=uploaded_file, outputs=output)
104
- template_btn.click(fn=lambda: "Using Template Dataset", outputs=output)
105
-
106
  def use_template():
107
- return ["Product Experience", "Customer Support", "Price of Service", "Other"]
108
-
109
- template_btn.click(fn=use_template, outputs=category_boxes)
110
- category_boxes.change(fn=display_categories, inputs=category_boxes, outputs=category_column)
111
- classify_btn.click(fn=classify_comments, inputs=category_boxes, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)