Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 APCCheckpointManager
|
| 12 |
+
|
| 13 |
+
sentiment_classifier = APCCheckpointManager.get_sentiment_classifier(checkpoint='multilingual',
|
| 14 |
+
auto_device=True # False means load model on CPU
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def inference(text):
|
| 19 |
+
result = sentiment_classifier.infer(text=text,
|
| 20 |
+
print_result=True,
|
| 21 |
+
clear_input_samples=True)
|
| 22 |
+
|
| 23 |
+
result = pd.DataFrame({
|
| 24 |
+
'aspect': result[0]['aspect'],
|
| 25 |
+
'sentiment': result[0]['sentiment'],
|
| 26 |
+
'confidence': round(result[0]['confidence'], 3),
|
| 27 |
+
'ref_sentiment': result[0]['ref_sentiment'],
|
| 28 |
+
'is_correct': result[0]['ref_check'],
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
return result
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if __name__ == '__main__':
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=inference,
|
| 37 |
+
inputs=["text"],
|
| 38 |
+
examples=[
|
| 39 |
+
['The [ASP]battery-life[ASP], and this [ASP]screen[ASP] is ok'],
|
| 40 |
+
['The [ASP] battery-life [ASP] is bad'],
|
| 41 |
+
['The [ASP] battery-life [ASP] is good'],
|
| 42 |
+
['Strong build though which really adds to its [ASP]durability[ASP] .'], # !sent! Positive
|
| 43 |
+
['Strong [ASP]build[ASP] though which really adds to its durability . !sent! Positive'],
|
| 44 |
+
['The [ASP]battery life[ASP] is excellent - 6-7 hours without charging . !sent! Positive'],
|
| 45 |
+
['I have had my computer for 2 weeks already and it [ASP]works[ASP] perfectly . !sent! Positive'],
|
| 46 |
+
['And I may be the only one but I am really liking [ASP]Windows 8[ASP] . !sent! Positive'],
|
| 47 |
+
['This demo is trained on the laptop and restaurant and other review datasets from [ASP]ABSADatasets[ASP] (https://github.com/yangheng95/ABSADatasets)'],
|
| 48 |
+
['To fit on your data, please train the model on your own data, see the [ASP]PyABSA[ASP] (https://github.com/yangheng95/PyABSA)'],
|
| 49 |
+
],
|
| 50 |
+
outputs="dataframe",
|
| 51 |
+
title='Aspect Term Extraction for Short Texts (powered by PyABSA)'
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch(share=True)
|