Update app.py
Browse files
app.py
CHANGED
|
@@ -8,22 +8,20 @@
|
|
| 8 |
# Copyright (C) 2023. All Rights Reserved.
|
| 9 |
|
| 10 |
import random
|
|
|
|
|
|
|
| 11 |
import gradio as gr
|
| 12 |
import pandas as pd
|
| 13 |
from pyabsa import (
|
| 14 |
download_all_available_datasets,
|
| 15 |
-
AspectTermExtraction as ATEPC,
|
| 16 |
TaskCodeOption,
|
| 17 |
available_checkpoints,
|
| 18 |
)
|
| 19 |
-
from pyabsa import
|
| 20 |
from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
|
| 21 |
|
| 22 |
download_all_available_datasets()
|
| 23 |
|
| 24 |
-
atepc_dataset_items = {dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()}
|
| 25 |
-
aste_dataset_items = {dataset.name: dataset for dataset in ASTE.ASTEDatasetList()}
|
| 26 |
-
|
| 27 |
|
| 28 |
def get_atepc_example(dataset):
|
| 29 |
task = TaskCodeOption.Aspect_Polarity_Classification
|
|
@@ -66,18 +64,66 @@ def get_aste_example(dataset):
|
|
| 66 |
return sorted(set(lines), key=lines.index)
|
| 67 |
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
atepc_dataset_dict = {
|
| 72 |
-
dataset.name: get_atepc_example(dataset.name)
|
| 73 |
-
for dataset in ATEPC.ATEPCDatasetList()
|
| 74 |
-
}
|
| 75 |
-
aspect_extractor = ATEPC.AspectExtractor(checkpoint="multilingual")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def perform_atepc_inference(text, dataset):
|
|
@@ -113,68 +159,108 @@ def perform_aste_inference(text, dataset):
|
|
| 113 |
return pred_triplets, true_triplets, "{}".format(text)
|
| 114 |
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
with gr.Row():
|
| 125 |
-
with gr.Column():
|
| 126 |
-
aste_input_sentence = gr.Textbox(
|
| 127 |
-
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
| 128 |
-
label="Example:",
|
| 129 |
-
)
|
| 130 |
-
gr.Markdown(
|
| 131 |
-
"You can find code and dataset at [ASTE examples](https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)"
|
| 132 |
-
)
|
| 133 |
-
aste_dataset_ids = gr.Radio(
|
| 134 |
-
choices=[dataset.name for dataset in ASTE.ASTEDatasetList()[:-1]],
|
| 135 |
-
value="Restaurant14",
|
| 136 |
-
label="Datasets",
|
| 137 |
-
)
|
| 138 |
-
aste_inference_button = gr.Button("Let's go!")
|
| 139 |
|
| 140 |
-
|
| 141 |
-
aste_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
| 142 |
-
aste_output_true_df = gr.DataFrame(label="Original Triplets:")
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
inputs=[aste_input_sentence, aste_dataset_ids],
|
| 147 |
-
outputs=[aste_output_pred_df, aste_output_true_df, aste_output_text],
|
| 148 |
-
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
gr.Markdown(
|
| 152 |
-
"# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>"
|
| 153 |
-
)
|
| 154 |
with gr.Row():
|
| 155 |
with gr.Column():
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
| 158 |
label="Example:",
|
| 159 |
)
|
| 160 |
-
gr.
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
atepc_dataset_ids = gr.Radio(
|
| 164 |
-
choices=[dataset.name for dataset in ATEPC.ATEPCDatasetList()[:-1]],
|
| 165 |
-
value="Laptop14",
|
| 166 |
label="Datasets",
|
| 167 |
)
|
| 168 |
-
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
fn=
|
| 175 |
-
inputs=[
|
| 176 |
-
outputs=[
|
| 177 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
gr.Markdown(
|
| 179 |
"""### GitHub Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
|
| 180 |
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
|
@@ -183,4 +269,4 @@ with demo:
|
|
| 183 |
"""
|
| 184 |
)
|
| 185 |
|
| 186 |
-
demo.launch()
|
|
|
|
| 8 |
# Copyright (C) 2023. All Rights Reserved.
|
| 9 |
|
| 10 |
import random
|
| 11 |
+
|
| 12 |
+
import autocuda
|
| 13 |
import gradio as gr
|
| 14 |
import pandas as pd
|
| 15 |
from pyabsa import (
|
| 16 |
download_all_available_datasets,
|
|
|
|
| 17 |
TaskCodeOption,
|
| 18 |
available_checkpoints,
|
| 19 |
)
|
| 20 |
+
from pyabsa import ABSAInstruction
|
| 21 |
from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
|
| 22 |
|
| 23 |
download_all_available_datasets()
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def get_atepc_example(dataset):
|
| 27 |
task = TaskCodeOption.Aspect_Polarity_Classification
|
|
|
|
| 64 |
return sorted(set(lines), key=lines.index)
|
| 65 |
|
| 66 |
|
| 67 |
+
def get_acos_example(dataset):
|
| 68 |
+
task = 'ACOS'
|
| 69 |
+
dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
|
| 70 |
+
|
| 71 |
+
for fname in dataset_file:
|
| 72 |
+
lines = []
|
| 73 |
+
if isinstance(fname, str):
|
| 74 |
+
fname = [fname]
|
| 75 |
+
|
| 76 |
+
for f in fname:
|
| 77 |
+
print("loading: {}".format(f))
|
| 78 |
+
fin = open(f, "r", encoding="utf-8")
|
| 79 |
+
lines.extend(fin.readlines())
|
| 80 |
+
fin.close()
|
| 81 |
+
lines = [line.split('####')[0] for line in lines]
|
| 82 |
+
return sorted(set(lines), key=lines.index)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
try:
|
| 86 |
+
from pyabsa import AspectTermExtraction as ATEPC
|
| 87 |
+
|
| 88 |
+
atepc_dataset_items = {dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()}
|
| 89 |
+
atepc_dataset_dict = {
|
| 90 |
+
dataset.name: get_atepc_example(dataset.name)
|
| 91 |
+
for dataset in ATEPC.ATEPCDatasetList()
|
| 92 |
+
}
|
| 93 |
+
aspect_extractor = ATEPC.AspectExtractor(checkpoint="multilingual")
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(e)
|
| 96 |
+
atepc_dataset_items = {}
|
| 97 |
+
atepc_dataset_dict = {}
|
| 98 |
+
aspect_extractor = None
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
from pyabsa import AspectSentimentTripletExtraction as ASTE
|
| 102 |
+
|
| 103 |
+
aste_dataset_items = {dataset.name: dataset for dataset in ASTE.ASTEDatasetList()}
|
| 104 |
+
aste_dataset_dict = {
|
| 105 |
+
dataset.name: get_aste_example(dataset.name) for dataset in ASTE.ASTEDatasetList()
|
| 106 |
+
}
|
| 107 |
+
triplet_extractor = ASTE.AspectSentimentTripletExtractor(checkpoint="multilingual")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(e)
|
| 110 |
+
aste_dataset_items = {}
|
| 111 |
+
aste_dataset_dict = {}
|
| 112 |
+
triplet_extractor = None
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
from pyabsa import ABSAInstruction
|
| 116 |
+
|
| 117 |
+
acos_dataset_items = {dataset.name: dataset for dataset in ABSAInstruction.ACOSDatasetList()}
|
| 118 |
+
acos_dataset_dict = {
|
| 119 |
+
dataset.name: get_acos_example(dataset.name) for dataset in ABSAInstruction.ACOSDatasetList()
|
| 120 |
+
}
|
| 121 |
+
quadruple_extractor = ABSAInstruction.ABSAGenerator("multilingual")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(e)
|
| 124 |
+
acos_dataset_items = {}
|
| 125 |
+
acos_dataset_dict = {}
|
| 126 |
+
quadruple_extractor = None
|
| 127 |
|
| 128 |
|
| 129 |
def perform_atepc_inference(text, dataset):
|
|
|
|
| 159 |
return pred_triplets, true_triplets, "{}".format(text)
|
| 160 |
|
| 161 |
|
| 162 |
+
def perform_acos_inference(text, dataset):
|
| 163 |
+
if not text:
|
| 164 |
+
text = acos_dataset_dict[dataset][
|
| 165 |
+
random.randint(0, len(acos_dataset_dict[dataset]) - 1)
|
| 166 |
+
]
|
| 167 |
|
| 168 |
+
raw_output = quadruple_extractor.predict(text.split('####')[0])
|
|
|
|
| 169 |
|
| 170 |
+
result = raw_output['Quadruple']
|
| 171 |
+
result = pd.DataFrame(result)
|
| 172 |
+
return result, text
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
demo = gr.Blocks()
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
with demo:
|
| 178 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
if quadruple_extractor:
|
|
|
|
|
|
|
|
|
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column():
|
| 183 |
+
gr.Markdown("# <p align='center'> ABSA Quadruple Extraction (Experimental) </p>")
|
| 184 |
+
|
| 185 |
+
acos_input_sentence = gr.Textbox(
|
| 186 |
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
| 187 |
label="Example:",
|
| 188 |
)
|
| 189 |
+
acos_dataset_ids = gr.Radio(
|
| 190 |
+
choices=[dataset.name for dataset in ABSAInstruction.ACOSDatasetList()],
|
| 191 |
+
value="Restaurant16",
|
|
|
|
|
|
|
|
|
|
| 192 |
label="Datasets",
|
| 193 |
)
|
| 194 |
+
acos_inference_button = gr.Button("Let's go!")
|
| 195 |
|
| 196 |
+
acos_output_text = gr.TextArea(label="Example:")
|
| 197 |
+
acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
| 198 |
|
| 199 |
+
acos_inference_button.click(
|
| 200 |
+
fn=perform_acos_inference,
|
| 201 |
+
inputs=[acos_input_sentence, acos_dataset_ids],
|
| 202 |
+
outputs=[acos_output_pred_df, acos_output_text],
|
| 203 |
)
|
| 204 |
+
with gr.Row():
|
| 205 |
+
if triplet_extractor:
|
| 206 |
+
with gr.Column():
|
| 207 |
+
gr.Markdown("# <p align='center'>Aspect Sentiment Triplet Extraction !</p>")
|
| 208 |
+
|
| 209 |
+
with gr.Row():
|
| 210 |
+
with gr.Column():
|
| 211 |
+
aste_input_sentence = gr.Textbox(
|
| 212 |
+
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
| 213 |
+
label="Example:",
|
| 214 |
+
)
|
| 215 |
+
gr.Markdown(
|
| 216 |
+
"You can find code and dataset at [ASTE examples](https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)"
|
| 217 |
+
)
|
| 218 |
+
aste_dataset_ids = gr.Radio(
|
| 219 |
+
choices=[dataset.name for dataset in ASTE.ASTEDatasetList()[:-1]],
|
| 220 |
+
value="Restaurant14",
|
| 221 |
+
label="Datasets",
|
| 222 |
+
)
|
| 223 |
+
aste_inference_button = gr.Button("Let's go!")
|
| 224 |
+
|
| 225 |
+
aste_output_text = gr.TextArea(label="Example:")
|
| 226 |
+
aste_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
| 227 |
+
aste_output_true_df = gr.DataFrame(label="Original Triplets:")
|
| 228 |
+
|
| 229 |
+
aste_inference_button.click(
|
| 230 |
+
fn=perform_aste_inference,
|
| 231 |
+
inputs=[aste_input_sentence, aste_dataset_ids],
|
| 232 |
+
outputs=[aste_output_pred_df, aste_output_true_df, aste_output_text],
|
| 233 |
+
)
|
| 234 |
+
if aspect_extractor:
|
| 235 |
+
with gr.Column():
|
| 236 |
+
gr.Markdown(
|
| 237 |
+
"# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>"
|
| 238 |
+
)
|
| 239 |
+
with gr.Row():
|
| 240 |
+
with gr.Column():
|
| 241 |
+
atepc_input_sentence = gr.Textbox(
|
| 242 |
+
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
| 243 |
+
label="Example:",
|
| 244 |
+
)
|
| 245 |
+
gr.Markdown(
|
| 246 |
+
"You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)"
|
| 247 |
+
)
|
| 248 |
+
atepc_dataset_ids = gr.Radio(
|
| 249 |
+
choices=[dataset.name for dataset in ATEPC.ATEPCDatasetList()[:-1]],
|
| 250 |
+
value="Laptop14",
|
| 251 |
+
label="Datasets",
|
| 252 |
+
)
|
| 253 |
+
atepc_inference_button = gr.Button("Let's go!")
|
| 254 |
+
|
| 255 |
+
atepc_output_text = gr.TextArea(label="Example:")
|
| 256 |
+
atepc_output_df = gr.DataFrame(label="Prediction Results:")
|
| 257 |
+
|
| 258 |
+
atepc_inference_button.click(
|
| 259 |
+
fn=perform_atepc_inference,
|
| 260 |
+
inputs=[atepc_input_sentence, atepc_dataset_ids],
|
| 261 |
+
outputs=[atepc_output_df, atepc_output_text],
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
gr.Markdown(
|
| 265 |
"""### GitHub Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
|
| 266 |
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
|
|
|
| 269 |
"""
|
| 270 |
)
|
| 271 |
|
| 272 |
+
demo.launch()
|