Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,45 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# time: 2021/10/10
|
| 4 |
-
# author: yangheng <yangheng@m.scnu.edu.cn>
|
| 5 |
-
# github: https://github.com/yangheng95
|
| 6 |
-
# Copyright (C) 2021. All Rights Reserved.
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
import pandas as pd
|
|
|
|
| 10 |
|
| 11 |
from pyabsa import ATEPCCheckpointManager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
aspect_extractor = ATEPCCheckpointManager.get_aspect_extractor(checkpoint='multilingual')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def inference(text):
|
| 17 |
result = aspect_extractor.extract_aspect(inference_source=[text],
|
| 18 |
pred_sentiment=True)
|
| 19 |
|
|
@@ -23,38 +49,37 @@ def inference(text):
|
|
| 23 |
'position': result[0]['position']
|
| 24 |
})
|
| 25 |
|
| 26 |
-
return result
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
iface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
+
from findfile import find_files
|
| 6 |
|
| 7 |
from pyabsa import ATEPCCheckpointManager
|
| 8 |
+
from pyabsa.functional.dataset.dataset_manager import download_datasets_from_github, ABSADatasetList
|
| 9 |
+
|
| 10 |
+
download_datasets_from_github(os.getcwd())
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_example(dataset):
|
| 14 |
+
filter_key_words = ['.py', '.md', 'readme', 'log', 'result', 'zip', '.state_dict', '.model', '.png', 'acc_', 'f1_', '.origin', '.adv', '.csv']
|
| 15 |
+
dataset_file = {'train': [], 'test': [], 'valid': []}
|
| 16 |
+
search_path = './'
|
| 17 |
+
task = 'apc_datasets'
|
| 18 |
+
dataset_file['test'] += find_files(search_path, [dataset, 'test', task, '.inference'], exclude_key=['.adv', '.org', '.defense', 'train.'] + filter_key_words)
|
| 19 |
+
|
| 20 |
+
for fname in dataset_file['test']:
|
| 21 |
+
lines = []
|
| 22 |
+
if isinstance(fname, str):
|
| 23 |
+
fname = [fname]
|
| 24 |
+
|
| 25 |
+
for f in fname:
|
| 26 |
+
print('loading: {}'.format(f))
|
| 27 |
+
fin = open(f, 'r', encoding='utf-8')
|
| 28 |
+
lines.extend(fin.readlines())
|
| 29 |
+
fin.close()
|
| 30 |
+
for i in range(len(lines)):
|
| 31 |
+
lines[i] = lines[i][:lines[i].find('!sent!')].replace('[ASP]', '')
|
| 32 |
+
return sorted(set(lines), key=lines.index)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
dataset_dict = {dataset.name: get_example(dataset.name) for dataset in ABSADatasetList()}
|
| 36 |
+
aspect_extractor = ATEPCCheckpointManager.get_aspect_extractor(checkpoint='english')
|
| 37 |
|
|
|
|
| 38 |
|
| 39 |
+
def perform_inference(text, dataset):
|
| 40 |
+
if not text:
|
| 41 |
+
text = dataset_dict[dataset][random.randint(0, len(dataset_dict[dataset]))]
|
| 42 |
|
|
|
|
| 43 |
result = aspect_extractor.extract_aspect(inference_source=[text],
|
| 44 |
pred_sentiment=True)
|
| 45 |
|
|
|
|
| 49 |
'position': result[0]['position']
|
| 50 |
})
|
| 51 |
|
| 52 |
+
return result, text
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
demo = gr.Blocks()
|
| 56 |
+
|
| 57 |
+
with demo:
|
| 58 |
+
gr.Markdown("# Multilingual Aspect-based Sentiment Analysis!")
|
| 59 |
+
gr.Markdown("### Repo: [PyABSA](https://github.com/yangheng95/PyABSA)")
|
| 60 |
+
gr.Markdown("""### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
| 61 |
+
[](https://pepy.tech/project/pyabsa)
|
| 62 |
+
[](https://pepy.tech/project/pyabsa)
|
| 63 |
+
"""
|
| 64 |
+
)
|
| 65 |
+
gr.Markdown("Your input text should be no more than 80 words, that's the longest text we used in training. However, you can try longer text in self-training ")
|
| 66 |
+
output_dfs = []
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
input_sentence = gr.Textbox(placeholder='Leave blank to give you a random result...', label="Example:")
|
| 70 |
+
gr.Markdown("You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)")
|
| 71 |
+
dataset_ids = gr.Radio(choices=[dataset.name for dataset in ABSADatasetList()[:-1]], value='Laptop14', label="Datasets")
|
| 72 |
+
inference_button = gr.Button("Let's go!")
|
| 73 |
+
gr.Markdown("This demo support many other language as well, you can try and explore the results of other languages by yourself.")
|
| 74 |
+
|
| 75 |
+
with gr.Column():
|
| 76 |
+
output_df = gr.DataFrame(label="Prediction Results:")
|
| 77 |
+
output_dfs.append(output_df)
|
| 78 |
+
|
| 79 |
+
inference_button.click(fn=perform_inference,
|
| 80 |
+
inputs=[input_sentence, dataset_ids],
|
| 81 |
+
outputs=[output_df, input_sentence])
|
| 82 |
+
|
| 83 |
+
gr.Markdown("")
|
| 84 |
+
|
| 85 |
+
demo.launch(share=True)
|
|
|