Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,15 @@ import gradio as gr
|
|
4 |
import pandas as pd
|
5 |
import requests
|
6 |
|
7 |
-
from pyabsa import
|
8 |
-
from pyabsa.
|
9 |
|
10 |
-
|
11 |
|
12 |
-
dataset_items = {dataset.name: dataset for dataset in
|
13 |
-
|
14 |
-
URL = 'https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fyangheng%2Fpyabsa_inference&label=Inference%20Count&labelColor=%2337d67a&countColor=%23f47373&style=flat&labelStyle=none'
|
15 |
|
16 |
def get_example(dataset):
|
17 |
-
task =
|
18 |
dataset_file = detect_infer_dataset(dataset_items[dataset], task)
|
19 |
|
20 |
for fname in dataset_file:
|
@@ -28,27 +26,27 @@ def get_example(dataset):
|
|
28 |
lines.extend(fin.readlines())
|
29 |
fin.close()
|
30 |
for i in range(len(lines)):
|
31 |
-
lines[i] = lines[i][:lines[i].find('
|
32 |
return sorted(set(lines), key=lines.index)
|
33 |
|
34 |
|
35 |
-
dataset_dict = {dataset.name: get_example(dataset.name) for dataset in
|
36 |
-
aspect_extractor =
|
37 |
|
38 |
|
39 |
def perform_inference(text, dataset):
|
40 |
if not text:
|
41 |
text = dataset_dict[dataset][random.randint(0, len(dataset_dict[dataset]) - 1)]
|
42 |
|
43 |
-
result = aspect_extractor.
|
44 |
-
|
45 |
|
46 |
result = pd.DataFrame({
|
47 |
-
'aspect': result[
|
48 |
-
'sentiment': result[
|
49 |
# 'probability': result[0]['probs'],
|
50 |
-
'confidence': [round(x, 4) for x in result[
|
51 |
-
'position': result[
|
52 |
})
|
53 |
return result, '{}'.format(text)
|
54 |
|
@@ -57,22 +55,20 @@ demo = gr.Blocks()
|
|
57 |
|
58 |
with demo:
|
59 |
gr.Markdown("# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>")
|
60 |
-
gr.Markdown("""### Repo: [PyABSA](https://github.com/yangheng95/PyABSA)
|
61 |
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
62 |
[](https://pepy.tech/project/pyabsa)
|
63 |
[](https://pepy.tech/project/pyabsa)
|
64 |
"""
|
65 |
)
|
66 |
-
gr.Markdown("Your input text should be no more than 80 words, that's the longest text we used in
|
67 |
-
gr.Markdown("**You don't need to split each Chinese (Korean, etc.) token as the provided
|
68 |
-
gr.Markdown("请确保输入的文本长度不超过200词,这是训练时的最大文本长度,过长将不会获得结果")
|
69 |
-
gr.Markdown("**提供的中文等其他非拉丁语系数据集采用了空格分字,这是早期数据集的遗留问题,预测时不用对中文等语言进行空格分字**")
|
70 |
output_dfs = []
|
71 |
with gr.Row():
|
72 |
with gr.Column():
|
73 |
input_sentence = gr.Textbox(placeholder='Leave this box blank and choose a dataset will give you a random example...', label="Example:")
|
74 |
gr.Markdown("You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)")
|
75 |
-
dataset_ids = gr.Radio(choices=[dataset.name for dataset in
|
76 |
inference_button = gr.Button("Let's go!")
|
77 |
gr.Markdown("There is a [demo](https://huggingface.co/spaces/yangheng/PyABSA-ATEPC-Chinese) specialized for the Chinese langauge")
|
78 |
gr.Markdown("This demo support many other language as well, you can try and explore the results of other languages by yourself.")
|
@@ -84,10 +80,6 @@ with demo:
|
|
84 |
|
85 |
inference_button.click(fn=perform_inference,
|
86 |
inputs=[input_sentence, dataset_ids],
|
87 |
-
outputs=[output_df, output_text]
|
88 |
-
api_name='inference')
|
89 |
-
|
90 |
-
gr.Markdown("")
|
91 |
-
gr.Markdown("".format(URL))
|
92 |
|
93 |
demo.launch()
|
|
|
4 |
import pandas as pd
|
5 |
import requests
|
6 |
|
7 |
+
from pyabsa import download_all_available_datasets, AspectTermExtraction as ATEPC, TaskCodeOption
|
8 |
+
from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
|
9 |
|
10 |
+
download_all_available_datasets()
|
11 |
|
12 |
+
dataset_items = {dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()}
|
|
|
|
|
13 |
|
14 |
def get_example(dataset):
|
15 |
+
task = TaskCodeOption.Aspect_Polarity_Classification
|
16 |
dataset_file = detect_infer_dataset(dataset_items[dataset], task)
|
17 |
|
18 |
for fname in dataset_file:
|
|
|
26 |
lines.extend(fin.readlines())
|
27 |
fin.close()
|
28 |
for i in range(len(lines)):
|
29 |
+
lines[i] = lines[i][:lines[i].find('$LABEL$')].replace('[B-ASP]', '').replace('[E-ASP]', '').strip()
|
30 |
return sorted(set(lines), key=lines.index)
|
31 |
|
32 |
|
33 |
+
dataset_dict = {dataset.name: get_example(dataset.name) for dataset in ATEPC.ATEPCDatasetList()}
|
34 |
+
aspect_extractor = ATEPC.AspectExtractor(checkpoint='multilingual')
|
35 |
|
36 |
|
37 |
def perform_inference(text, dataset):
|
38 |
if not text:
|
39 |
text = dataset_dict[dataset][random.randint(0, len(dataset_dict[dataset]) - 1)]
|
40 |
|
41 |
+
result = aspect_extractor.predict(text,
|
42 |
+
pred_sentiment=True)
|
43 |
|
44 |
result = pd.DataFrame({
|
45 |
+
'aspect': result['aspect'],
|
46 |
+
'sentiment': result['sentiment'],
|
47 |
# 'probability': result[0]['probs'],
|
48 |
+
'confidence': [round(x, 4) for x in result['confidence']],
|
49 |
+
'position': result['position']
|
50 |
})
|
51 |
return result, '{}'.format(text)
|
52 |
|
|
|
55 |
|
56 |
with demo:
|
57 |
gr.Markdown("# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>")
|
58 |
+
gr.Markdown("""### Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
|
59 |
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
60 |
[](https://pepy.tech/project/pyabsa)
|
61 |
[](https://pepy.tech/project/pyabsa)
|
62 |
"""
|
63 |
)
|
64 |
+
gr.Markdown("Your input text should be no more than 80 words, that's the longest text we used in trainer. However, you can try longer text in self-trainer ")
|
65 |
+
gr.Markdown("**You don't need to split each Chinese (Korean, etc.) token as the provided, just input the natural language text.**")
|
|
|
|
|
66 |
output_dfs = []
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
input_sentence = gr.Textbox(placeholder='Leave this box blank and choose a dataset will give you a random example...', 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 ATEPC.ATEPCDatasetList()[:-1]], value='Laptop14', label="Datasets")
|
72 |
inference_button = gr.Button("Let's go!")
|
73 |
gr.Markdown("There is a [demo](https://huggingface.co/spaces/yangheng/PyABSA-ATEPC-Chinese) specialized for the Chinese langauge")
|
74 |
gr.Markdown("This demo support many other language as well, you can try and explore the results of other languages by yourself.")
|
|
|
80 |
|
81 |
inference_button.click(fn=perform_inference,
|
82 |
inputs=[input_sentence, dataset_ids],
|
83 |
+
outputs=[output_df, output_text])
|
|
|
|
|
|
|
|
|
84 |
|
85 |
demo.launch()
|