def qanary_service(): """the POST endpoint required for a Qanary service""" triplestore_endpoint = request.json["values"]["urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"]["urn:qanary#outGraph"] logging.info( "endpoint: %s, inGraph: %s, outGraph: %s" % (triplestore_endpoint, triplestore_ingraph, triplestore_outgraph)) text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] logging.info(f'Question Text: {text}') lang, prob = langid.classify(text) batch = tokenizers[lang]([text], return_tensors="pt", padding=True) # Make sure that the tokenized text does not exceed the maximum # allowed size of 512 batch["input_ids"] = batch["input_ids"][:, :512] batch["attention_mask"] = batch["attention_mask"][:, :512] # Perform the translation and decode the output translation = models[lang].generate(**batch) result = tokenizers[lang].batch_decode(translation, skip_special_tokens=True)[0] # building SPARQL query SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> INSERT {{ GRAPH <{uuid}> {{ ?a a qa:AnnotationOfQuestionLanguage . ?a qa:translationResult "{result}" . ?a qa:sourceLanguage "{src_lang}" . ?a oa:annotatedBy <urn:qanary:{app_name}> . ?a oa:annotatedAt ?time . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, result=result, src_lang=lang, app_name="{0}:Python".format(SERVICE_NAME_COMPONENT)) logging.info(f'SPARQL: {SPARQLquery}') # inserting new data to the triplestore insert_into_triplestore(triplestore_endpoint, SPARQLquery) return jsonify(request.get_json())
def qanaryService(): """the POST endpoint required for a Qanary service""" triplestore_endpoint = request.json["values"]["urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"]["urn:qanary#outGraph"] logging.info( "endpoint: %s, inGraph: %s, outGraph: %s" % (triplestore_endpoint, triplestore_ingraph, triplestore_outgraph)) text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] logging.info(f'Question Text: {text}') data = {'questions': [text]} # creating params dict for the service #TODO: move URL to config file json_response = requests.post(configuration.classificationendpoint, data=data) # making a request to the service predicted_answer_type = json.loads(json_response.text)['predictions'][0] #building SPARQL query SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> PREFIX dbo: <http://dbpedia.org/ontology/> INSERT {{ GRAPH <{uuid}> {{ ?a a qa:AnnotationOfAnswerTypeClassifier . ?a qa:hasAnswerType dbo:{answer_type} . ?a oa:annotatedBy <urn:qanary:{app_name}> . ?a oa:annotatedAt ?time . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, answer_type=predicted_answer_type, app_name="{0}:{1}:Python".format( configuration.servicename, configuration.serviceversion)) logging.info(f'SPARQL: {SPARQLquery}') insert_into_triplestore( triplestore_endpoint, triplestore_ingraph, SPARQLquery) #inserting new data to the triplestore return jsonify(request.get_json())
def qanary_service(): """the POST endpoint required for a Qanary service""" triplestore_endpoint = request.json["values"][ "urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"][ "urn:qanary#outGraph"] #logging.info( # "endpoint: %s, inGraph: %s, outGraph: %s" % (triplestore_endpoint, triplestore_ingraph, triplestore_outgraph)) text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] #logging.info(f'Question Text: {text}') res = ask.get_final_result(self.asks, triplestore_endpoint, triplestore_ingraph) text = self.function(text, res) #logging.info(f'Answer Text: {text}') SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> PREFIX dbo: <http://dbpedia.org/ontology/> INSERT {{ GRAPH <{uuid}> {{ ?a oa:{data_type} <{data_uri}> . ?a oa:annotatedBy <urn:qanary:{app_name}> . ?a oa:annotatedAt ?time . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, app_name="{0}:{1}:Python".format( configuration.servicename, configuration.serviceversion), data_uri=text, data_type=data_type) # building SPARQL query #logging.info(f'SPARQL: {SPARQLquery}') insert_into_triplestore( triplestore_endpoint, triplestore_ingraph, SPARQLquery) # inserting new data to the triplestore return jsonify(request.get_json())
def qanary_service(): """the POST endpoint required for a Qanary service""" triplestore_endpoint = request.json["values"]["urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"]["urn:qanary#outGraph"] logging.info( "endpoint: %s, inGraph: %s, outGraph: %s" % (triplestore_endpoint, triplestore_ingraph, triplestore_outgraph)) text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] logging.info(f'Question Text: {text}') pred = classifier.predict(vectorizer.transform([text])) logging.info("PREDICTION!!!!!", pred) SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> PREFIX dbo: <http://dbpedia.org/ontology/> INSERT {{ GRAPH <{uuid}> {{ ?a oa:relation <{relation_uri}> . ?a oa:annotatedBy <urn:qanary:{app_name}> . ?a oa:annotatedAt ?time . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, app_name="{0}:{1}:Python".format( configuration.servicename, configuration.serviceversion), relation_uri=pred[0]) # building SPARQL query logging.info(f'SPARQL: {SPARQLquery}') insert_into_triplestore( triplestore_endpoint, triplestore_ingraph, SPARQLquery) # inserting new data to the triplestore return jsonify(request.get_json())
def qanary_service(): triplestore_endpoint = request.json["values"]["urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"]["urn:qanary#outGraph"] text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] out = make(text) SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> PREFIX dbo: <http://dbpedia.org/ontology/> INSERT {{ GRAPH <{uuid}> {{ ?a oa:object <{object_uri}> . ?a oa:annotatedBy <urn:qanary:{app_name}> . ?a oa:annotatedAt ?time . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, app_name="{0}:{1}:Python".format( configuration.servicename, configuration.serviceversion), object_uri=out) insert_into_triplestore(triplestore_endpoint, triplestore_ingraph, SPARQLquery) return jsonify(request.get_json())
def qanary_service(): """the POST endpoint required for a Qanary service""" triplestore_endpoint = request.json["values"]["urn:qanary#endpoint"] triplestore_ingraph = request.json["values"]["urn:qanary#inGraph"] triplestore_outgraph = request.json["values"]["urn:qanary#outGraph"] logging.info( "endpoint: %s, inGraph: %s, outGraph: %s" % (triplestore_endpoint, triplestore_ingraph, triplestore_outgraph)) # check if translation was involved SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> SELECT ?val FROM <{uuid}> {{ ?s a qa:AnnotationOfQuestionLanguage ; qa:translationResult ?val . }} """.format(uuid=triplestore_ingraph) translation = select_from_triplestore(triplestore_endpoint, SPARQLquery) logging.info(f" {translation}") if 'results' in translation.keys() and len( translation['results']['bindings']) > 0: text = list(translation['results']['bindings'][0].values())[0]['value'] else: text = get_text_question_in_graph( triplestore_endpoint=triplestore_endpoint, graph=triplestore_ingraph)[0]['text'] logging.info(f'Question Text: {text}') QA_SYSTEM_PARAMS['question'] = text json_response = requests.get(QA_SYSTEM_URL.format( **QA_SYSTEM_PARAMS)).json() # making a request to a service if len(json_response['answer'] ) > 0 and 'http' in json_response['answer'][0]: results = ', '.join('<' + uri + '>' for uri in json_response['answer']) elif len(json_response['answer'] ) > 0 and 'http' not in json_response['answer'][0]: results = ', '.join('"' + uri + '"' for uri in json_response['answer']) else: results = '"No information available"' # building SPARQL query SPARQLquery = """ PREFIX qa: <http://www.wdaqua.eu/qa#> PREFIX oa: <http://www.w3.org/ns/openannotation/core/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> INSERT {{ GRAPH <{uuid}> {{ ?a a qa:AnnotationOfQaInterface ; oa:annotatedBy <urn:qanary:{app_name}:{qa_system_name}> ; oa:annotatedAt ?time ; oa:answerResult {results} . }} }} WHERE {{ BIND (IRI(str(RAND())) AS ?a) . BIND (now() as ?time) }} """.format(uuid=triplestore_ingraph, qa_system_name=QA_SYSTEM_NAME, qa_system_url=QA_SYSTEM_URL.format(**QA_SYSTEM_PARAMS), results=results, app_name="{0}:Python".format(SERVICE_NAME_COMPONENT)) logging.info(f'SPARQL: {SPARQLquery}') # inserting new data to the triplestore insert_into_triplestore(triplestore_endpoint, SPARQLquery) return jsonify(request.get_json())