yangheng commited on
Commit
0181e0b
1 Parent(s): 1797d3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -44
app.py CHANGED
@@ -1,19 +1,45 @@
1
- # -*- coding: utf-8 -*-
2
- # file: deploy_demo.py
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
- if __name__ == '__main__':
30
- iface = gr.Interface(
31
- fn=inference,
32
- inputs=["text"],
33
- examples=[
34
- ['Even though it is running Snow Leopard, 2.4 GHz C2D is a bit of an antiquated CPU and thus the occasional spinning '
35
- 'wheel would appear when running Office Mac applications such as Word or Excel .'],
36
- ['从这门课程的内容丰富程度还有老师的授课及讨论区的答疑上来说,我都是很喜欢的。但是吧,我觉得每个章的内容太多了,三个学分的量就分在了上个章节三次小测'],
37
- ['Als je geen steak liefhebber bent is er een visalternatief, eend en lam aan aanvaardbare prijzen.'],
38
- ['سأوصي بالتأكيد بموقع المدينة القديمة إلا إنه عليك الحذر من الأسعار السياحية الأكثر ارتفاعاً'],
39
- ['Nous avons bien aimé l\'ambiance, sur la promenade principale de Narbonne-Plage, et la qualité du service.'],
40
- ['la calidad del producto, el servicio, el entorno todo fue excelente'],
41
- ['По поводу интерьера: место спокойное, шумных компаний нет (не было, по крайней мере, в момент нашего посещения), очень приятная и уютная атмосфера, все в лучших традициях.'],
42
- ['Yemekler iyi hos, lezzetler iyi ama heyecan verici bi taraflari yok, iyi bir baligi iyi bir sekilde izgara yapmak artik atla deve bi olay degil.'],
43
- ['このレストランのサービスはまあまあで,待ち時間は長かったが,料理はまずまずのものだった'],
44
- ['Die wartezeit war recht mittelmäßig, aber das Essen war befriedigend'],
45
- ['O serviço é médio, o tempo de espera é longo, mas os pratos são razoavelmente satisfatórios'],
46
- ['Dịch vụ của nhà hàng này rất trung bình và thời gian chờ đợi rất dài, nhưng món ăn thì khá là thỏa mãn'],
47
- ['Pelayanan di restoran biasa dan penantian yang lama, tetapi hasilnya cukup memuaskan'],
48
- ['This demo is trained on the public and community shared datasets from ABSADatasets (https://github.com/yangheng95/ABSADatasets),'
49
- ' please feel free to share your data to improve this work'],
50
- ['To fit on your data, please train our ATEPC models on your own data, see the PyABSA (https://github.com/yangheng95/PyABSA/tree/release/demos/aspect_term_extraction)'],
51
- ],
52
- outputs="dataframe",
53
- description='This demo is trained on the public and community shared datasets from ABSADatasets (https://github.com/yangheng95/ABSADatasets),'
54
- ' please feel free to share your data to improve this work, To fit on your data, please train our ATEPC models on your own data,'
55
- ' see the PyABSA (https://github.com/yangheng95/PyABSA/tree/release/demos/aspect_term_extraction)',
56
- title='Multilingual Aspect Term Extraction for Short Texts (powered by PyABSA)'
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
+ [![Downloads](https://pepy.tech/badge/pyabsa)](https://pepy.tech/project/pyabsa)
62
+ [![Downloads](https://pepy.tech/badge/pyabsa/month)](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("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=https://huggingface.co/spaces/yangheng/Multilingual-Aspect-Based-Sentiment-Analysis)")
84
+
85
+ demo.launch(share=True)