ZennyKenny commited on
Commit
511d4e8
Β·
verified Β·
1 Parent(s): 80cfb3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -24,7 +24,6 @@ generator = pipeline("text2text-generation", model="google/flan-t5-base")
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() != '']
@@ -44,29 +43,40 @@ with gr.Blocks() as nps:
44
  category_list.append(new_category.strip()) # Add new category
45
  return category_list
46
 
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")
@@ -79,9 +89,7 @@ with gr.Blocks() as nps:
79
  file.seek(0) # Reset file pointer
80
  import io
81
  if file.name.endswith('.csv'):
82
- file.seek(0) # Reset file pointer
83
  custom_df = pd.read_csv(file, encoding='utf-8')
84
- custom_df = pd.read_csv(io.StringIO(content))
85
  else:
86
  return "Error: Uploaded file is not a CSV."
87
  if 'customer_comment' not in custom_df.columns:
@@ -94,10 +102,12 @@ with gr.Blocks() as nps:
94
 
95
  uploaded_file.change(fn=load_data, inputs=uploaded_file, outputs=output)
96
  template_btn.click(fn=lambda: "Using Template Dataset", outputs=output)
 
97
  def use_template():
98
  return ["Product Experience", "Customer Support", "Price of Service", "Other"]
 
99
  template_btn.click(fn=use_template, outputs=category_boxes)
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()
 
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() != '']
 
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=lambda x=cat: remove_category(x, categories),
58
+ inputs=[],
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")
 
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:
94
  return "Error: Uploaded file is not a CSV."
95
  if 'customer_comment' not in custom_df.columns:
 
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)