Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,10 +19,11 @@ from pyabsa import (
|
|
19 |
)
|
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
|
28 |
dataset_file = detect_infer_dataset(atepc_dataset_items[dataset], task)
|
@@ -46,6 +47,7 @@ def get_atepc_example(dataset):
|
|
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,6 +64,7 @@ def get_aste_example(dataset):
|
|
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)
|
@@ -79,6 +82,7 @@ def get_acos_example(dataset):
|
|
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 |
|
@@ -99,16 +103,12 @@ except Exception as e:
|
|
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 = {}
|
@@ -179,6 +179,44 @@ def perform_acos_inference(text, dataset):
|
|
179 |
return result, text
|
180 |
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
def inference(text, dataset, task):
|
183 |
if task == "ATEPC":
|
184 |
return perform_atepc_inference(text, dataset)
|
@@ -220,8 +258,12 @@ if __name__ == "__main__":
|
|
220 |
acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
221 |
|
222 |
acos_inference_button.click(
|
223 |
-
fn=
|
224 |
-
inputs=[
|
|
|
|
|
|
|
|
|
225 |
outputs=[acos_output_pred_df, acos_output_text],
|
226 |
)
|
227 |
with gr.Row():
|
@@ -259,8 +301,12 @@ if __name__ == "__main__":
|
|
259 |
)
|
260 |
|
261 |
aste_inference_button.click(
|
262 |
-
fn=
|
263 |
-
inputs=[
|
|
|
|
|
|
|
|
|
264 |
outputs=[
|
265 |
aste_output_pred_df,
|
266 |
aste_output_true_df,
|
@@ -295,11 +341,11 @@ if __name__ == "__main__":
|
|
295 |
atepc_output_df = gr.DataFrame(label="Prediction Results:")
|
296 |
|
297 |
atepc_inference_button.click(
|
298 |
-
fn=
|
299 |
inputs=[
|
300 |
atepc_input_sentence,
|
301 |
atepc_dataset_ids,
|
302 |
-
gr.Text("ATEPC"),
|
303 |
],
|
304 |
outputs=[atepc_output_df, atepc_output_text],
|
305 |
)
|
|
|
19 |
)
|
20 |
from pyabsa import ABSAInstruction
|
21 |
from pyabsa.utils.data_utils.dataset_manager import detect_infer_dataset
|
22 |
+
import requests
|
23 |
|
24 |
download_all_available_datasets()
|
25 |
|
26 |
+
|
27 |
def get_atepc_example(dataset):
|
28 |
task = TaskCodeOption.Aspect_Polarity_Classification
|
29 |
dataset_file = detect_infer_dataset(atepc_dataset_items[dataset], task)
|
|
|
47 |
)
|
48 |
return sorted(set(lines), key=lines.index)
|
49 |
|
50 |
+
|
51 |
def get_aste_example(dataset):
|
52 |
task = TaskCodeOption.Aspect_Sentiment_Triplet_Extraction
|
53 |
dataset_file = detect_infer_dataset(aste_dataset_items[dataset], task)
|
|
|
64 |
fin.close()
|
65 |
return sorted(set(lines), key=lines.index)
|
66 |
|
67 |
+
|
68 |
def get_acos_example(dataset):
|
69 |
task = "ACOS"
|
70 |
dataset_file = detect_infer_dataset(acos_dataset_items[dataset], task)
|
|
|
82 |
lines = [line.split("####")[0] for line in lines]
|
83 |
return sorted(set(lines), key=lines.index)
|
84 |
|
85 |
+
|
86 |
try:
|
87 |
from pyabsa import AspectTermExtraction as ATEPC
|
88 |
|
|
|
103 |
try:
|
104 |
from pyabsa import AspectSentimentTripletExtraction as ASTE
|
105 |
|
106 |
+
aste_dataset_items = {dataset.name: dataset for dataset in ASTE.ASTEDatasetList()}
|
|
|
|
|
107 |
aste_dataset_dict = {
|
108 |
dataset.name: get_aste_example(dataset.name)
|
109 |
for dataset in ASTE.ASTEDatasetList()[:-1]
|
110 |
}
|
111 |
+
triplet_extractor = ASTE.AspectSentimentTripletExtractor(checkpoint="multilingual")
|
|
|
|
|
112 |
except Exception as e:
|
113 |
print(e)
|
114 |
aste_dataset_items = {}
|
|
|
179 |
return result, text
|
180 |
|
181 |
|
182 |
+
def run_demo(text, dataset, task):
|
183 |
+
try:
|
184 |
+
data = {
|
185 |
+
"text": text,
|
186 |
+
"dataset": dataset,
|
187 |
+
"task": task,
|
188 |
+
}
|
189 |
+
response = requests.post("https://pyabsa.pagekite.me/api/inference", json=data)
|
190 |
+
result = response.json()
|
191 |
+
print(response.json())
|
192 |
+
if task == "ATEPC":
|
193 |
+
return (
|
194 |
+
pd.DataFrame(
|
195 |
+
{
|
196 |
+
"aspect": result["aspect"],
|
197 |
+
"sentiment": result["sentiment"],
|
198 |
+
# 'probability': result[0]['probs'],
|
199 |
+
"confidence": [round(x, 4) for x in result["confidence"]],
|
200 |
+
"position": result["position"],
|
201 |
+
}
|
202 |
+
),
|
203 |
+
result["text"],
|
204 |
+
)
|
205 |
+
elif task == "ASTE":
|
206 |
+
return (
|
207 |
+
pd.DataFrame(result["pred_triplets"]),
|
208 |
+
pd.DataFrame(result["true_triplets"]),
|
209 |
+
result["text"],
|
210 |
+
)
|
211 |
+
elif task == "ACOS":
|
212 |
+
return pd.DataFrame(result["Quadruples"]), result["text"]
|
213 |
+
|
214 |
+
except Exception as e:
|
215 |
+
print(e)
|
216 |
+
print("Failed to connect to the server, running locally...")
|
217 |
+
return inference(text, dataset, task)
|
218 |
+
|
219 |
+
|
220 |
def inference(text, dataset, task):
|
221 |
if task == "ATEPC":
|
222 |
return perform_atepc_inference(text, dataset)
|
|
|
258 |
acos_output_pred_df = gr.DataFrame(label="Predicted Triplets:")
|
259 |
|
260 |
acos_inference_button.click(
|
261 |
+
fn=run_demo,
|
262 |
+
inputs=[
|
263 |
+
acos_input_sentence,
|
264 |
+
acos_dataset_ids,
|
265 |
+
gr.Text("ACOS", visible=False),
|
266 |
+
],
|
267 |
outputs=[acos_output_pred_df, acos_output_text],
|
268 |
)
|
269 |
with gr.Row():
|
|
|
301 |
)
|
302 |
|
303 |
aste_inference_button.click(
|
304 |
+
fn=run_demo,
|
305 |
+
inputs=[
|
306 |
+
aste_input_sentence,
|
307 |
+
aste_dataset_ids,
|
308 |
+
gr.Text("ASTE", visible=False),
|
309 |
+
],
|
310 |
outputs=[
|
311 |
aste_output_pred_df,
|
312 |
aste_output_true_df,
|
|
|
341 |
atepc_output_df = gr.DataFrame(label="Prediction Results:")
|
342 |
|
343 |
atepc_inference_button.click(
|
344 |
+
fn=run_demo,
|
345 |
inputs=[
|
346 |
atepc_input_sentence,
|
347 |
atepc_dataset_ids,
|
348 |
+
gr.Text("ATEPC", visible=False),
|
349 |
],
|
350 |
outputs=[atepc_output_df, atepc_output_text],
|
351 |
)
|