generated from iai-group/template-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from iai-group/feature/54-Create-endpoint-for…
…-NL-query Implement endpoint /nl_processing
- Loading branch information
Showing
11 changed files
with
304 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Define server configuration.""" | ||
|
||
from pkg_api.nl_to_pkg.annotators.three_step_annotator import ( | ||
_DEFAULT_CONFIG_PATH as DEFAULT_3_STEP_CONFIG_PATH, | ||
) | ||
from pkg_api.nl_to_pkg.annotators.three_step_annotator import ( | ||
_DEFAULT_PROMPT_PATHS, | ||
) | ||
from pkg_api.nl_to_pkg.entity_linking.rel_entity_linking import ( | ||
_DEFAULT_API_URL, | ||
) | ||
from pkg_api.pkg import DEFAULT_VISUALIZATION_PATH | ||
|
||
|
||
class BaseConfig: | ||
"""Base configuration for the server.""" | ||
|
||
TESTING = False | ||
|
||
# Three step annotator configuration | ||
TS_ANNOTATOR_CONFIG_PATH = DEFAULT_3_STEP_CONFIG_PATH | ||
TS_ANNOTATOR_PROMPT_PATHS = _DEFAULT_PROMPT_PATHS | ||
|
||
# Entity linker configuration. Use REL by default. | ||
ENTITY_LINKER_CONFIG = { | ||
"class_path": "pkg_api.nl_to_pkg.entity_linking.rel_entity_linking." | ||
"RELEntityLinker", | ||
"kwargs": {"api_url": _DEFAULT_API_URL}, | ||
} | ||
|
||
|
||
class DevelopmentConfig(BaseConfig): | ||
"""Development configuration for the server.""" | ||
|
||
DEBUG = True | ||
SQLALCHEMY_DATABASE_URI = "sqlite:///db.sqlite" | ||
STORE_PATH = "data" | ||
VISUALIZATION_PATH = DEFAULT_VISUALIZATION_PATH | ||
|
||
|
||
class TestingConfig(BaseConfig): | ||
"""Testing configuration for the server.""" | ||
|
||
TESTING = True | ||
SQLALCHEMY_DATABASE_URI = "sqlite:///test.sqlite" | ||
STORE_PATH = "tests/data/RDFStore" | ||
VISUALIZATION_PATH = "tests/data/pkg_visualizations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,73 @@ | ||
"""API Resource receiving NL input.""" | ||
|
||
from typing import Dict, Tuple | ||
from typing import Any, Dict, Tuple | ||
|
||
from flask import request | ||
from flask_restful import Resource | ||
|
||
from pkg_api.core.intents import Intent | ||
from pkg_api.nl_to_pkg.nl_to_pkg import NLtoPKG | ||
from pkg_api.server.utils import open_pkg | ||
|
||
|
||
class NLResource(Resource): | ||
def post(self) -> Tuple[Dict[str, str], int]: | ||
def __init__(self, nl_to_pkg: NLtoPKG) -> None: | ||
"""Initializes the NL resource. | ||
Args: | ||
nl_to_pkg: NLtoPKG object. | ||
""" | ||
self.nl_to_pkg = nl_to_pkg | ||
|
||
def post(self) -> Tuple[Dict[str, Any], int]: | ||
"""Processes the NL input to update the PKG. | ||
Note that the returned dictionary may contain additional fields based | ||
on the frontend's needs. | ||
Raises: | ||
KeyError: if there is missing information to open the user's PKG. | ||
Returns: | ||
A tuple with a dictionary containing a message, and the status code. | ||
""" | ||
# TODO: Implement this method following the approved pipeline. | ||
# See issue: https://github.com/iai-group/pkg-api/issues/78 | ||
return {"message": "Not implemented."}, 400 | ||
data = request.json | ||
|
||
try: | ||
pkg = open_pkg(data) | ||
except KeyError as e: | ||
return {"message": e.args[0]}, 400 | ||
|
||
query = data.get("query", None) | ||
if not query: | ||
return {"message": "Missing query."}, 400 | ||
|
||
intent, statement_data = self.nl_to_pkg.annotate(query) | ||
if intent == Intent.ADD: | ||
pkg.add_statement(statement_data) | ||
pkg.close() | ||
return { | ||
"message": "Statement added to your PKG.", | ||
"annotation": statement_data.as_dict(), | ||
}, 200 | ||
elif intent == Intent.GET: | ||
statements = pkg.get_statements( | ||
statement_data, triple_conditioned=True | ||
) | ||
return { | ||
"message": "Statements retrieved from your PKG.", | ||
"data": [s.as_dict() for s in statements], | ||
"annotation": statement_data.as_dict(), | ||
}, 200 | ||
elif intent == Intent.DELETE: | ||
pkg.remove_statement(statement_data) | ||
pkg.close() | ||
return { | ||
"message": "Statement was deleted if present.", | ||
"annotation": statement_data.as_dict(), | ||
}, 200 | ||
|
||
return { | ||
"message": "The operation could not be performed. Please try to" | ||
" rephrase your query." | ||
}, 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.