yangheng commited on
Commit
0dd0180
1 Parent(s): 0691ec4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -88
app.py CHANGED
@@ -19,6 +19,7 @@ from pyabsa import (
19
  )
20
  from pyabsa import ABSAInstruction
21
  from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
 
22
 
23
  download_all_available_datasets()
24
 
@@ -65,7 +66,7 @@ def get_aste_example(dataset):
65
 
66
 
67
  def get_acos_example(dataset):
68
- task = 'ACOS'
69
  dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
70
 
71
  for fname in dataset_file:
@@ -78,14 +79,16 @@ def get_acos_example(dataset):
78
  fin = open(f, "r", encoding="utf-8")
79
  lines.extend(fin.readlines())
80
  fin.close()
81
- lines = [line.split('####')[0] for line in lines]
82
  return sorted(set(lines), key=lines.index)
83
 
84
 
85
  try:
86
  from pyabsa import AspectTermExtraction as ATEPC
87
 
88
- atepc_dataset_items = {dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()}
 
 
89
  atepc_dataset_dict = {
90
  dataset.name: get_atepc_example(dataset.name)
91
  for dataset in ATEPC.ATEPCDatasetList()
@@ -102,7 +105,8 @@ try:
102
 
103
  aste_dataset_items = {dataset.name: dataset for dataset in ASTE.ASTEDatasetList()}
104
  aste_dataset_dict = {
105
- dataset.name: get_aste_example(dataset.name) for dataset in ASTE.ASTEDatasetList()
 
106
  }
107
  triplet_extractor = ASTE.AspectSentimentTripletExtractor(checkpoint="multilingual")
108
  except Exception as e:
@@ -114,9 +118,12 @@ except Exception as e:
114
  try:
115
  from pyabsa import ABSAInstruction
116
 
117
- acos_dataset_items = {dataset.name: dataset for dataset in ABSAInstruction.ACOSDatasetList()}
 
 
118
  acos_dataset_dict = {
119
- dataset.name: get_acos_example(dataset.name) for dataset in ABSAInstruction.ACOSDatasetList()
 
120
  }
121
  quadruple_extractor = ABSAInstruction.ABSAGenerator("multilingual")
122
  except Exception as e:
@@ -156,7 +163,7 @@ def perform_aste_inference(text, dataset):
156
 
157
  pred_triplets = pd.DataFrame(result["Triplets"])
158
  true_triplets = pd.DataFrame(result["True Triplets"])
159
- return pred_triplets, true_triplets, "{}".format(text)
160
 
161
 
162
  def perform_acos_inference(text, dataset):
@@ -165,108 +172,190 @@ def perform_acos_inference(text, dataset):
165
  random.randint(0, len(acos_dataset_dict[dataset]) - 1)
166
  ]
167
 
168
- raw_output = quadruple_extractor.predict(text.split('####')[0], max_length=128)
169
 
170
- result = raw_output['Quadruples']
171
  result = pd.DataFrame(result)
172
  return result, text
173
 
174
 
175
- demo = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
- with demo:
178
- with gr.Row():
 
 
179
 
180
- if quadruple_extractor:
181
- with gr.Row():
182
- with gr.Column():
183
- gr.Markdown("# <p align='center'> ABSA Quadruple Extraction (Experimental) </p>")
184
 
185
- acos_input_sentence = gr.Textbox(
186
- placeholder="Leave this box blank and choose a dataset will give you a random example...",
187
- label="Example:",
188
- )
189
- acos_dataset_ids = gr.Radio(
190
- choices=[dataset.name for dataset in ABSAInstruction.ACOSDatasetList()],
191
- value="Laptop14",
192
- label="Datasets",
193
- )
194
- acos_inference_button = gr.Button("Let's go!")
195
 
196
- acos_output_text = gr.TextArea(label="Example:")
197
- acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
198
 
199
- acos_inference_button.click(
200
- fn=perform_acos_inference,
201
- inputs=[acos_input_sentence, acos_dataset_ids],
202
- outputs=[acos_output_pred_df, acos_output_text],
203
- )
204
- with gr.Row():
205
- if triplet_extractor:
206
- with gr.Column():
207
- gr.Markdown("# <p align='center'>Aspect Sentiment Triplet Extraction !</p>")
208
 
 
 
 
209
  with gr.Row():
210
  with gr.Column():
211
- aste_input_sentence = gr.Textbox(
212
- placeholder="Leave this box blank and choose a dataset will give you a random example...",
213
- label="Example:",
214
- )
215
  gr.Markdown(
216
- "You can find code and dataset at [ASTE examples](https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)"
217
- )
218
- aste_dataset_ids = gr.Radio(
219
- choices=[dataset.name for dataset in ASTE.ASTEDatasetList()[:-1]],
220
- value="Restaurant14",
221
- label="Datasets",
222
  )
223
- aste_inference_button = gr.Button("Let's go!")
224
 
225
- aste_output_text = gr.TextArea(label="Example:")
226
- aste_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
227
- aste_output_true_df = gr.DataFrame(label="Original Triplets:")
228
-
229
- aste_inference_button.click(
230
- fn=perform_aste_inference,
231
- inputs=[aste_input_sentence, aste_dataset_ids],
232
- outputs=[aste_output_pred_df, aste_output_true_df, aste_output_text],
233
- )
234
- if aspect_extractor:
235
- with gr.Column():
236
- gr.Markdown(
237
- "# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>"
238
- )
239
- with gr.Row():
240
- with gr.Column():
241
- atepc_input_sentence = gr.Textbox(
242
  placeholder="Leave this box blank and choose a dataset will give you a random example...",
243
  label="Example:",
244
  )
245
- gr.Markdown(
246
- "You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)"
247
- )
248
- atepc_dataset_ids = gr.Radio(
249
- choices=[dataset.name for dataset in ATEPC.ATEPCDatasetList()[:-1]],
250
  value="Laptop14",
251
  label="Datasets",
252
  )
253
- atepc_inference_button = gr.Button("Let's go!")
254
-
255
- atepc_output_text = gr.TextArea(label="Example:")
256
- atepc_output_df = gr.DataFrame(label="Prediction Results:")
257
-
258
- atepc_inference_button.click(
259
- fn=perform_atepc_inference,
260
- inputs=[atepc_input_sentence, atepc_dataset_ids],
261
- outputs=[atepc_output_df, atepc_output_text],
 
 
 
 
262
  )
 
 
 
 
 
 
263
 
264
- gr.Markdown(
265
- """### GitHub Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
266
- ### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
267
- [![Downloads](https://pepy.tech/badge/pyabsa)](https://pepy.tech/project/pyabsa)
268
- [![Downloads](https://pepy.tech/badge/pyabsa/month)](https://pepy.tech/project/pyabsa)
269
- """
270
- )
271
-
272
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
  from pyabsa import ABSAInstruction
21
  from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
22
+ import requests
23
 
24
  download_all_available_datasets()
25
 
 
66
 
67
 
68
  def get_acos_example(dataset):
69
+ task = "ACOS"
70
  dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
71
 
72
  for fname in dataset_file:
 
79
  fin = open(f, "r", encoding="utf-8")
80
  lines.extend(fin.readlines())
81
  fin.close()
82
+ lines = [line.split("####")[0] for line in lines]
83
  return sorted(set(lines), key=lines.index)
84
 
85
 
86
  try:
87
  from pyabsa import AspectTermExtraction as ATEPC
88
 
89
+ atepc_dataset_items = {
90
+ dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()
91
+ }
92
  atepc_dataset_dict = {
93
  dataset.name: get_atepc_example(dataset.name)
94
  for dataset in ATEPC.ATEPCDatasetList()
 
105
 
106
  aste_dataset_items = {dataset.name: dataset for dataset in ASTE.ASTEDatasetList()}
107
  aste_dataset_dict = {
108
+ dataset.name: get_aste_example(dataset.name)
109
+ for dataset in ASTE.ASTEDatasetList()[:-1]
110
  }
111
  triplet_extractor = ASTE.AspectSentimentTripletExtractor(checkpoint="multilingual")
112
  except Exception as e:
 
118
  try:
119
  from pyabsa import ABSAInstruction
120
 
121
+ acos_dataset_items = {
122
+ dataset.name: dataset for dataset in ABSAInstruction.ACOSDatasetList()
123
+ }
124
  acos_dataset_dict = {
125
+ dataset.name: get_acos_example(dataset.name)
126
+ for dataset in ABSAInstruction.ACOSDatasetList()
127
  }
128
  quadruple_extractor = ABSAInstruction.ABSAGenerator("multilingual")
129
  except Exception as e:
 
163
 
164
  pred_triplets = pd.DataFrame(result["Triplets"])
165
  true_triplets = pd.DataFrame(result["True Triplets"])
166
+ return pred_triplets, true_triplets, "{}".format(text.split("####")[0])
167
 
168
 
169
  def perform_acos_inference(text, dataset):
 
172
  random.randint(0, len(acos_dataset_dict[dataset]) - 1)
173
  ]
174
 
175
+ raw_output = quadruple_extractor.predict(text.split("####")[0], max_length=128)
176
 
177
+ result = raw_output["Quadruples"]
178
  result = pd.DataFrame(result)
179
  return result, text
180
 
181
 
182
+ def run_demo(text, dataset, task):
183
+ try:
184
+ data = {
185
+ "text": text,
186
+ "dataset": dataset,
187
+ "task": task,
188
+ }
189
+ response = requests.post("https://pyabsa.pagekite.me/api/inference", json=data)
190
+ result = response.json()
191
+ print(response.json())
192
+ if task == "ATEPC":
193
+ return (
194
+ pd.DataFrame(
195
+ {
196
+ "aspect": result["aspect"],
197
+ "sentiment": result["sentiment"],
198
+ # 'probability': result[0]['probs'],
199
+ "confidence": [round(x, 4) for x in result["confidence"]],
200
+ "position": result["position"],
201
+ }
202
+ ),
203
+ result["text"],
204
+ )
205
+ elif task == "ASTE":
206
+ return (
207
+ pd.DataFrame(result["pred_triplets"]),
208
+ pd.DataFrame(result["true_triplets"]),
209
+ result["text"],
210
+ )
211
+ elif task == "ACOS":
212
+ return pd.DataFrame(result["Quadruples"]), result["text"]
213
 
214
+ except Exception as e:
215
+ print(e)
216
+ print("Failed to connect to the server, running locally...")
217
+ return inference(text, dataset, task)
218
 
 
 
 
 
219
 
220
+ def inference(text, dataset, task):
221
+ if task == "ATEPC":
222
+ return perform_atepc_inference(text, dataset)
223
+ elif task == "ASTE":
224
+ return perform_aste_inference(text, dataset)
225
+ elif task == "ACOS":
226
+ return perform_acos_inference(text, dataset)
227
+ else:
228
+ raise Exception("No such task: {}".format(task))
 
229
 
 
 
230
 
231
+ if __name__ == "__main__":
232
+ demo = gr.Blocks()
 
 
 
 
 
 
 
233
 
234
+ with demo:
235
+ with gr.Row():
236
+ if quadruple_extractor:
237
  with gr.Row():
238
  with gr.Column():
 
 
 
 
239
  gr.Markdown(
240
+ "# <p align='center'> ABSA Quadruple Extraction (Experimental) </p>"
 
 
 
 
 
241
  )
 
242
 
243
+ acos_input_sentence = gr.Textbox(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  placeholder="Leave this box blank and choose a dataset will give you a random example...",
245
  label="Example:",
246
  )
247
+ acos_dataset_ids = gr.Radio(
248
+ choices=[
249
+ dataset.name
250
+ for dataset in ABSAInstruction.ACOSDatasetList()
251
+ ],
252
  value="Laptop14",
253
  label="Datasets",
254
  )
255
+ acos_inference_button = gr.Button("Let's go!")
256
+
257
+ acos_output_text = gr.TextArea(label="Example:")
258
+ acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
259
+
260
+ acos_inference_button.click(
261
+ fn=run_demo,
262
+ inputs=[
263
+ acos_input_sentence,
264
+ acos_dataset_ids,
265
+ gr.Text("ACOS", visible=False),
266
+ ],
267
+ outputs=[acos_output_pred_df, acos_output_text],
268
  )
269
+ with gr.Row():
270
+ if triplet_extractor:
271
+ with gr.Column():
272
+ gr.Markdown(
273
+ "# <p align='center'>Aspect Sentiment Triplet Extraction !</p>"
274
+ )
275
 
276
+ with gr.Row():
277
+ with gr.Column():
278
+ aste_input_sentence = gr.Textbox(
279
+ placeholder="Leave this box blank and choose a dataset will give you a random example...",
280
+ label="Example:",
281
+ )
282
+ gr.Markdown(
283
+ "You can find code and dataset at [ASTE examples](https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)"
284
+ )
285
+ aste_dataset_ids = gr.Radio(
286
+ choices=[
287
+ dataset.name
288
+ for dataset in ASTE.ASTEDatasetList()[:-1]
289
+ ],
290
+ value="Restaurant14",
291
+ label="Datasets",
292
+ )
293
+ aste_inference_button = gr.Button("Let's go!")
294
+
295
+ aste_output_text = gr.TextArea(label="Example:")
296
+ aste_output_pred_df = gr.DataFrame(
297
+ label="Predicted Triplets:"
298
+ )
299
+ aste_output_true_df = gr.DataFrame(
300
+ label="Original Triplets:"
301
+ )
302
+
303
+ aste_inference_button.click(
304
+ fn=run_demo,
305
+ inputs=[
306
+ aste_input_sentence,
307
+ aste_dataset_ids,
308
+ gr.Text("ASTE", visible=False),
309
+ ],
310
+ outputs=[
311
+ aste_output_pred_df,
312
+ aste_output_true_df,
313
+ aste_output_text,
314
+ ],
315
+ )
316
+ if aspect_extractor:
317
+ with gr.Column():
318
+ gr.Markdown(
319
+ "# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>"
320
+ )
321
+ with gr.Row():
322
+ with gr.Column():
323
+ atepc_input_sentence = gr.Textbox(
324
+ placeholder="Leave this box blank and choose a dataset will give you a random example...",
325
+ label="Example:",
326
+ )
327
+ gr.Markdown(
328
+ "You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)"
329
+ )
330
+ atepc_dataset_ids = gr.Radio(
331
+ choices=[
332
+ dataset.name
333
+ for dataset in ATEPC.ATEPCDatasetList()[:-1]
334
+ ],
335
+ value="Laptop14",
336
+ label="Datasets",
337
+ )
338
+ atepc_inference_button = gr.Button("Let's go!")
339
+
340
+ atepc_output_text = gr.TextArea(label="Example:")
341
+ atepc_output_df = gr.DataFrame(label="Prediction Results:")
342
+
343
+ atepc_inference_button.click(
344
+ fn=run_demo,
345
+ inputs=[
346
+ atepc_input_sentence,
347
+ atepc_dataset_ids,
348
+ gr.Text("ATEPC", visible=False),
349
+ ],
350
+ outputs=[atepc_output_df, atepc_output_text],
351
+ )
352
+
353
+ gr.Markdown(
354
+ """### GitHub Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
355
+ ### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
356
+ [![Downloads](https://pepy.tech/badge/pyabsa)](https://pepy.tech/project/pyabsa)
357
+ [![Downloads](https://pepy.tech/badge/pyabsa/month)](https://pepy.tech/project/pyabsa)
358
+ """
359
+ )
360
+
361
+ demo.launch()