Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,8 +20,8 @@ from pyabsa import (
|
|
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
|
@@ -46,7 +46,6 @@ def get_atepc_example(dataset):
|
|
46 |
)
|
47 |
return sorted(set(lines), key=lines.index)
|
48 |
|
49 |
-
|
50 |
def get_aste_example(dataset):
|
51 |
task = TaskCodeOption.Aspect_Sentiment_Triplet_Extraction
|
52 |
dataset_file = detect_infer_dataset(aste_dataset_items[dataset], task)
|
@@ -63,9 +62,8 @@ def get_aste_example(dataset):
|
|
63 |
fin.close()
|
64 |
return sorted(set(lines), key=lines.index)
|
65 |
|
66 |
-
|
67 |
def get_acos_example(dataset):
|
68 |
-
task =
|
69 |
dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
|
70 |
|
71 |
for fname in dataset_file:
|
@@ -78,14 +76,15 @@ def get_acos_example(dataset):
|
|
78 |
fin = open(f, "r", encoding="utf-8")
|
79 |
lines.extend(fin.readlines())
|
80 |
fin.close()
|
81 |
-
lines = [line.split(
|
82 |
return sorted(set(lines), key=lines.index)
|
83 |
|
84 |
-
|
85 |
try:
|
86 |
from pyabsa import AspectTermExtraction as ATEPC
|
87 |
|
88 |
-
atepc_dataset_items = {
|
|
|
|
|
89 |
atepc_dataset_dict = {
|
90 |
dataset.name: get_atepc_example(dataset.name)
|
91 |
for dataset in ATEPC.ATEPCDatasetList()
|
@@ -100,11 +99,16 @@ except Exception as e:
|
|
100 |
try:
|
101 |
from pyabsa import AspectSentimentTripletExtraction as ASTE
|
102 |
|
103 |
-
aste_dataset_items = {
|
|
|
|
|
104 |
aste_dataset_dict = {
|
105 |
-
dataset.name: get_aste_example(dataset.name)
|
|
|
106 |
}
|
107 |
-
triplet_extractor = ASTE.AspectSentimentTripletExtractor(
|
|
|
|
|
108 |
except Exception as e:
|
109 |
print(e)
|
110 |
aste_dataset_items = {}
|
@@ -114,9 +118,12 @@ except Exception as e:
|
|
114 |
try:
|
115 |
from pyabsa import ABSAInstruction
|
116 |
|
117 |
-
acos_dataset_items = {
|
|
|
|
|
118 |
acos_dataset_dict = {
|
119 |
-
dataset.name: get_acos_example(dataset.name)
|
|
|
120 |
}
|
121 |
quadruple_extractor = ABSAInstruction.ABSAGenerator("multilingual")
|
122 |
except Exception as e:
|
@@ -156,7 +163,7 @@ def perform_aste_inference(text, dataset):
|
|
156 |
|
157 |
pred_triplets = pd.DataFrame(result["Triplets"])
|
158 |
true_triplets = pd.DataFrame(result["True Triplets"])
|
159 |
-
return pred_triplets, true_triplets, "{}".format(text.split(
|
160 |
|
161 |
|
162 |
def perform_acos_inference(text, dataset):
|
@@ -165,108 +172,144 @@ def perform_acos_inference(text, dataset):
|
|
165 |
random.randint(0, len(acos_dataset_dict[dataset]) - 1)
|
166 |
]
|
167 |
|
168 |
-
raw_output = quadruple_extractor.predict(text.split(
|
169 |
|
170 |
-
result = raw_output[
|
171 |
result = pd.DataFrame(result)
|
172 |
return result, text
|
173 |
|
174 |
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
-
with demo:
|
178 |
-
with gr.Row():
|
179 |
|
180 |
-
|
181 |
-
|
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="Laptop14",
|
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 |
-
"
|
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 |
-
|
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.
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
value="Laptop14",
|
251 |
label="Datasets",
|
252 |
)
|
253 |
-
|
254 |
|
255 |
-
|
256 |
-
|
257 |
|
258 |
-
|
259 |
-
fn=
|
260 |
-
inputs=[
|
261 |
-
outputs=[
|
262 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
from pyabsa import ABSAInstruction
|
21 |
from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
|
22 |
|
|
|
23 |
|
24 |
+
download_all_available_datasets()
|
25 |
|
26 |
def get_atepc_example(dataset):
|
27 |
task = TaskCodeOption.Aspect_Polarity_Classification
|
|
|
46 |
)
|
47 |
return sorted(set(lines), key=lines.index)
|
48 |
|
|
|
49 |
def get_aste_example(dataset):
|
50 |
task = TaskCodeOption.Aspect_Sentiment_Triplet_Extraction
|
51 |
dataset_file = detect_infer_dataset(aste_dataset_items[dataset], task)
|
|
|
62 |
fin.close()
|
63 |
return sorted(set(lines), key=lines.index)
|
64 |
|
|
|
65 |
def get_acos_example(dataset):
|
66 |
+
task = "ACOS"
|
67 |
dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
|
68 |
|
69 |
for fname in dataset_file:
|
|
|
76 |
fin = open(f, "r", encoding="utf-8")
|
77 |
lines.extend(fin.readlines())
|
78 |
fin.close()
|
79 |
+
lines = [line.split("####")[0] for line in lines]
|
80 |
return sorted(set(lines), key=lines.index)
|
81 |
|
|
|
82 |
try:
|
83 |
from pyabsa import AspectTermExtraction as ATEPC
|
84 |
|
85 |
+
atepc_dataset_items = {
|
86 |
+
dataset.name: dataset for dataset in ATEPC.ATEPCDatasetList()
|
87 |
+
}
|
88 |
atepc_dataset_dict = {
|
89 |
dataset.name: get_atepc_example(dataset.name)
|
90 |
for dataset in ATEPC.ATEPCDatasetList()
|
|
|
99 |
try:
|
100 |
from pyabsa import AspectSentimentTripletExtraction as ASTE
|
101 |
|
102 |
+
aste_dataset_items = {
|
103 |
+
dataset.name: dataset for dataset in ASTE.ASTEDatasetList()
|
104 |
+
}
|
105 |
aste_dataset_dict = {
|
106 |
+
dataset.name: get_aste_example(dataset.name)
|
107 |
+
for dataset in ASTE.ASTEDatasetList()[:-1]
|
108 |
}
|
109 |
+
triplet_extractor = ASTE.AspectSentimentTripletExtractor(
|
110 |
+
checkpoint="multilingual"
|
111 |
+
)
|
112 |
except Exception as e:
|
113 |
print(e)
|
114 |
aste_dataset_items = {}
|
|
|
118 |
try:
|
119 |
from pyabsa import ABSAInstruction
|
120 |
|
121 |
+
acos_dataset_items = {
|
122 |
+
dataset.name: dataset for dataset in ABSAInstruction.ACOSDatasetList()
|
123 |
+
}
|
124 |
acos_dataset_dict = {
|
125 |
+
dataset.name: get_acos_example(dataset.name)
|
126 |
+
for dataset in ABSAInstruction.ACOSDatasetList()
|
127 |
}
|
128 |
quadruple_extractor = ABSAInstruction.ABSAGenerator("multilingual")
|
129 |
except Exception as e:
|
|
|
163 |
|
164 |
pred_triplets = pd.DataFrame(result["Triplets"])
|
165 |
true_triplets = pd.DataFrame(result["True Triplets"])
|
166 |
+
return pred_triplets, true_triplets, "{}".format(text.split("####")[0])
|
167 |
|
168 |
|
169 |
def perform_acos_inference(text, dataset):
|
|
|
172 |
random.randint(0, len(acos_dataset_dict[dataset]) - 1)
|
173 |
]
|
174 |
|
175 |
+
raw_output = quadruple_extractor.predict(text.split("####")[0], max_length=128)
|
176 |
|
177 |
+
result = raw_output["Quadruples"]
|
178 |
result = pd.DataFrame(result)
|
179 |
return result, text
|
180 |
|
181 |
|
182 |
+
def inference(text, dataset, task):
|
183 |
+
if task == "ATEPC":
|
184 |
+
return perform_atepc_inference(text, dataset)
|
185 |
+
elif task == "ASTE":
|
186 |
+
return perform_aste_inference(text, dataset)
|
187 |
+
elif task == "ACOS":
|
188 |
+
return perform_acos_inference(text, dataset)
|
189 |
+
else:
|
190 |
+
raise Exception("No such task: {}".format(task))
|
191 |
|
|
|
|
|
192 |
|
193 |
+
if __name__ == "__main__":
|
194 |
+
demo = gr.Blocks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
+
with demo:
|
197 |
+
with gr.Row():
|
198 |
+
if quadruple_extractor:
|
199 |
with gr.Row():
|
200 |
with gr.Column():
|
|
|
|
|
|
|
|
|
201 |
gr.Markdown(
|
202 |
+
"# <p align='center'> ABSA Quadruple Extraction (Experimental) </p>"
|
|
|
|
|
|
|
|
|
|
|
203 |
)
|
|
|
204 |
|
205 |
+
acos_input_sentence = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
207 |
label="Example:",
|
208 |
)
|
209 |
+
acos_dataset_ids = gr.Radio(
|
210 |
+
choices=[
|
211 |
+
dataset.name
|
212 |
+
for dataset in ABSAInstruction.ACOSDatasetList()
|
213 |
+
],
|
214 |
value="Laptop14",
|
215 |
label="Datasets",
|
216 |
)
|
217 |
+
acos_inference_button = gr.Button("Let's go!")
|
218 |
|
219 |
+
acos_output_text = gr.TextArea(label="Example:")
|
220 |
+
acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
221 |
|
222 |
+
acos_inference_button.click(
|
223 |
+
fn=inference,
|
224 |
+
inputs=[acos_input_sentence, acos_dataset_ids, gr.Text("ACOS")],
|
225 |
+
outputs=[acos_output_pred_df, acos_output_text],
|
226 |
)
|
227 |
+
with gr.Row():
|
228 |
+
if triplet_extractor:
|
229 |
+
with gr.Column():
|
230 |
+
gr.Markdown(
|
231 |
+
"# <p align='center'>Aspect Sentiment Triplet Extraction !</p>"
|
232 |
+
)
|
233 |
|
234 |
+
with gr.Row():
|
235 |
+
with gr.Column():
|
236 |
+
aste_input_sentence = gr.Textbox(
|
237 |
+
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
238 |
+
label="Example:",
|
239 |
+
)
|
240 |
+
gr.Markdown(
|
241 |
+
"You can find code and dataset at [ASTE examples](https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)"
|
242 |
+
)
|
243 |
+
aste_dataset_ids = gr.Radio(
|
244 |
+
choices=[
|
245 |
+
dataset.name
|
246 |
+
for dataset in ASTE.ASTEDatasetList()[:-1]
|
247 |
+
],
|
248 |
+
value="Restaurant14",
|
249 |
+
label="Datasets",
|
250 |
+
)
|
251 |
+
aste_inference_button = gr.Button("Let's go!")
|
252 |
+
|
253 |
+
aste_output_text = gr.TextArea(label="Example:")
|
254 |
+
aste_output_pred_df = gr.DataFrame(
|
255 |
+
label="Predicted Triplets:"
|
256 |
+
)
|
257 |
+
aste_output_true_df = gr.DataFrame(
|
258 |
+
label="Original Triplets:"
|
259 |
+
)
|
260 |
+
|
261 |
+
aste_inference_button.click(
|
262 |
+
fn=inference,
|
263 |
+
inputs=[aste_input_sentence, aste_dataset_ids, gr.Text("ASTE")],
|
264 |
+
outputs=[
|
265 |
+
aste_output_pred_df,
|
266 |
+
aste_output_true_df,
|
267 |
+
aste_output_text,
|
268 |
+
],
|
269 |
+
)
|
270 |
+
if aspect_extractor:
|
271 |
+
with gr.Column():
|
272 |
+
gr.Markdown(
|
273 |
+
"# <p align='center'>Multilingual Aspect-based Sentiment Analysis !</p>"
|
274 |
+
)
|
275 |
+
with gr.Row():
|
276 |
+
with gr.Column():
|
277 |
+
atepc_input_sentence = gr.Textbox(
|
278 |
+
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
279 |
+
label="Example:",
|
280 |
+
)
|
281 |
+
gr.Markdown(
|
282 |
+
"You can find the datasets at [github.com/yangheng95/ABSADatasets](https://github.com/yangheng95/ABSADatasets/tree/v1.2/datasets/text_classification)"
|
283 |
+
)
|
284 |
+
atepc_dataset_ids = gr.Radio(
|
285 |
+
choices=[
|
286 |
+
dataset.name
|
287 |
+
for dataset in ATEPC.ATEPCDatasetList()[:-1]
|
288 |
+
],
|
289 |
+
value="Laptop14",
|
290 |
+
label="Datasets",
|
291 |
+
)
|
292 |
+
atepc_inference_button = gr.Button("Let's go!")
|
293 |
+
|
294 |
+
atepc_output_text = gr.TextArea(label="Example:")
|
295 |
+
atepc_output_df = gr.DataFrame(label="Prediction Results:")
|
296 |
+
|
297 |
+
atepc_inference_button.click(
|
298 |
+
fn=inference,
|
299 |
+
inputs=[
|
300 |
+
atepc_input_sentence,
|
301 |
+
atepc_dataset_ids,
|
302 |
+
gr.Text("ATEPC"),
|
303 |
+
],
|
304 |
+
outputs=[atepc_output_df, atepc_output_text],
|
305 |
+
)
|
306 |
+
|
307 |
+
gr.Markdown(
|
308 |
+
"""### GitHub Repo: [PyABSA V2](https://github.com/yangheng95/PyABSA)
|
309 |
+
### Author: [Heng Yang](https://github.com/yangheng95) (杨恒)
|
310 |
+
[](https://pepy.tech/project/pyabsa)
|
311 |
+
[](https://pepy.tech/project/pyabsa)
|
312 |
+
"""
|
313 |
+
)
|
314 |
+
|
315 |
+
demo.launch()
|