Improved extract method to work properly (sync)

This commit is contained in:
Kikimanox
2022-10-18 19:08:41 +02:00
parent b483e1643f
commit 673a83691b
9 changed files with 289 additions and 120 deletions
@@ -1,114 +1,83 @@
import codecs
import os
import connexion
import json
from pathlib import Path
from swagger_server.models.izlusci_body import IzlusciBody # noqa: E501
from swagger_server.models.izlusci_async_body import IzlusciAsyncBody # noqa: E501
from swagger_server.models.izlusci_sync_body import IzlusciSyncBody # noqa: E501
from swagger_server.utils import cl_utils
from swagger_server.util import get_random_filename, create_random_file_in_tmp_folder
import requests
from werkzeug.utils import secure_filename
# ATEapi_endpoint = "http://localhost:5000/predict"
ATEapi_endpoint = "http://localhost:5000/predict"
ATEapi_endpoint = "http://ate-api:5000/predict"
# ATEapi_endpoint = "http://ate-api:5000/predict"
def get_candidates(body): # noqa: E501
"""Izlusci terminološke kandidate iz seznama besedil v conllu obliki
def get_candidates_async(body): # noqa: E501
"""Izlusci terminološke kandidate iz seznama besedil v conllu obliki [asinhrono, ustvari novi job]
# noqa: E501
:param body:
:param body:
:type body: dict | bytes
:rtype: str
"""
if connexion.request.is_json:
body = IzlusciAsyncBody.from_dict(connexion.request.get_json()) # noqa: E501
return 'do some magic!'
def do_izlusci(conllus, prepovedane_besede):
tmp_file_path = ""
try:
big_conllu = cl_utils.multipla_conllus_to_one_from_conllus_arr(conllus)
tmp_file_path = create_random_file_in_tmp_folder(big_conllu, ".conllu")
fp = open(tmp_file_path, 'rb')
try:
files = [
('file', ('temp_1.conllu', fp, 'application/octet-stream'))
]
res = requests.post(ATEapi_endpoint, files=files)
data = json.loads(res.text)
finally:
fp.close()
os.remove(tmp_file_path)
ret = {'terminoloski_kandidati': [
{
'POSoznake': tk['msd'],
'kandidat': tk['terms'], # more to bit lemma al terms?
'kanonicnaoblika': tk['canonical'],
'ranking': tk['ranking'],
'podporneutezi': [
0.0, # ????????
0.0 # ??????
],
'pogostostpojavljanja': [0, 0] # ???????
}
for tk in data if tk['terms'] not in prepovedane_besede
]}
return ret, 200
except Exception as e:
return str(e), 500
def get_candidates_sync(body): # noqa: E501
"""Izlusci terminološke kandidate iz seznama besedil v conllu obliki [sihrono, rezultat v sami zahtevi]
# noqa: E501
:param body:
:type body: dict | bytes
:rtype: List[TerminoloskiKandidat]
"""
if connexion.request.is_json:
body = IzlusciBody.from_dict(connexion.request.get_json()) # noqa: E501
body = IzlusciSyncBody.from_dict(connexion.request.get_json()) # noqa: E501
# Todo: What is "conllus" in body input anyway??
# Todo: Kaj je s prepovedanimi besedami?
# file_ids = [10000, 10001, 10002, 10003]
# big_conllu = cl_utils.multipla_conllus_to_one_from_file_ids(file_ids)
# txt = Path('../ATEapi/temp_1.conllu').read_text('utf-8') # LOCAL ONLY
# # Todo: does this close the file after the request?
# files = [
# ('file', ('temp_1.conllu', open('../ATEapi/temp_1.conllu', 'rb'),
# 'application/octet-stream'))
# ]
# res = requests.post(ATEapi_endpoint, files=files)
# # res.status_code
# # res.text
#
# return res.text, res.status_code
#
# example_data = ""
# with open("C:\\Users\\Kiki\\Desktop\\Untitled-2.json", "r", encoding='utf-8') as f:
# example_data = json.loads(f.read())
#
# d = 0
#
# ret = {'terminoloski_kandidati': [
# {
# 'POSoznake': tk['msd'],
# 'kandidat': tk['terms'], # more to bit lemma al terms?
# 'kanonicnaoblika': tk['canonical'],
# 'ranking': tk['ranking'],
# 'podporneutezi': [
# 6.0274563, # ????????
# 6.0274563 # ??????
# ],
# 'pogostostpojavljanja': [101, 71] # ???????
# }
# for tk in example_data
# ]}
data = {
"terminoloski_kandidati": [
{
"POSoznake": "Ncmsn",
"kandidat": "vpliv",
"kanonicnaoblika": "vpliv",
"nosilnautez": 0.8008282,
"podporneutezi": [
6.0274563,
6.0274563
],
"pogostostpojavljanja": [101, 71]
},
{
"POSoznake": "Agpnsg Ncnsg",
"kandidat": "bivalen okolje",
"kanonicnaoblika": "bivalno okolje",
"nosilnautez": 0.60254,
"podporneutezi": [
3.263,
2.134
],
"pogostostpojavljanja": [27, 11]
},
{
"POSoznake": "Agpmsny Ncmsn Ncmpg",
"kandidat": "motorični status otrok",
"kanonicnaoblika": "motorični status otrok",
"nosilnautez": 0.3324,
"podporneutezi": [
1.221,
3.323
],
"pogostostpojavljanja": [31, 51]
},
{
"POSoznake": "Agpnsn Ncnsn",
"kandidat": "diplomsko delo",
"kanonicnaoblika": "diplomsko delo",
"nosilnautez": 0.2008282,
"podporneutezi": [
0.883,
1.02
],
"pogostostpojavljanja": [1241, 111]
}
]
}
return data
return do_izlusci(body.conllus, body.prepovedane_besede)