Spaces:
Paused
Paused
update_ckpt
Browse files- app.py +92 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 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 |
+
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 |
+
|
| 17 |
+
def get_example(dataset):
|
| 18 |
+
task = TaskCodeOption.Aspect_Polarity_Classification
|
| 19 |
+
dataset_file = detect_infer_dataset(dataset_items[dataset], task)
|
| 20 |
+
|
| 21 |
+
for fname in dataset_file:
|
| 22 |
+
lines = []
|
| 23 |
+
if isinstance(fname, str):
|
| 24 |
+
fname = [fname]
|
| 25 |
+
|
| 26 |
+
for f in fname:
|
| 27 |
+
print('loading: {}'.format(f))
|
| 28 |
+
fin = open(f, 'r', encoding='utf-8')
|
| 29 |
+
lines.extend(fin.readlines())
|
| 30 |
+
fin.close()
|
| 31 |
+
for i in range(len(lines)):
|
| 32 |
+
lines[i] = lines[i][:lines[i].find('$LABEL$')].replace('[B-ASP]', '').replace('[E-ASP]', '').strip()
|
| 33 |
+
return sorted(set(lines), key=lines.index)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
dataset_dict = {dataset.name: get_example(dataset.name) for dataset in ATEPC.ATEPCDatasetList()}
|
| 37 |
+
aspect_extractor = ATEPC.AspectExtractor(checkpoint='multilingual')
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def perform_inference(text, dataset):
|
| 41 |
+
if not text:
|
| 42 |
+
text = dataset_dict[dataset][random.randint(0, len(dataset_dict[dataset]) - 1)]
|
| 43 |
+
|
| 44 |
+
result = aspect_extractor.predict(example=text,
|
| 45 |
+
pred_sentiment=True)
|
| 46 |
+
|
| 47 |
+
result = pd.DataFrame({
|
| 48 |
+
'aspect': result['aspect'],
|
| 49 |
+
'sentiment': result['sentiment'],
|
| 50 |
+
# 'probability': result[0]['probs'],
|
| 51 |
+
'confidence': [round(x, 4) for x in result['confidence']],
|
| 52 |
+
'position': result['position']
|
| 53 |
+
})
|
| 54 |
+
requests.get(URL)
|
| 55 |
+
return result, '{}'.format(text)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
demo = gr.Blocks()
|
| 59 |
+
|
| 60 |
+
with demo:
|
| 61 |
+
gr.Markdown("# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>")
|
| 62 |
+
gr.Markdown("""### Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
|
| 63 |
+
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
| 64 |
+
[](https://pepy.tech/project/pyabsa)
|
| 65 |
+
[](https://pepy.tech/project/pyabsa)
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
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 ")
|
| 69 |
+
gr.Markdown("**You don't need to split each Chinese (Korean, etc.) token as the provided, just input the natural language text.**")
|
| 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 ATEPC.ATEPCDatasetList()[:-1]], value='Laptop14', label="Datasets")
|
| 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.")
|
| 79 |
+
|
| 80 |
+
with gr.Column():
|
| 81 |
+
output_text = gr.TextArea(label="Example:")
|
| 82 |
+
output_df = gr.DataFrame(label="Prediction Results:")
|
| 83 |
+
output_dfs.append(output_df)
|
| 84 |
+
|
| 85 |
+
inference_button.click(fn=perform_inference,
|
| 86 |
+
inputs=[input_sentence, dataset_ids],
|
| 87 |
+
outputs=[output_df, output_text])
|
| 88 |
+
|
| 89 |
+
gr.Markdown("")
|
| 90 |
+
gr.Markdown("".format(URL))
|
| 91 |
+
|
| 92 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pyabsa>=2.0.0b0
|