Skip to content

Commit

Permalink
Merge pull request #126 from bothub-it/staging
Browse files Browse the repository at this point in the history
Refactor question answering to extract information from torchserve API
  • Loading branch information
johncordeiro authored May 20, 2022
2 parents d1416e9 + e2d8d97 commit 606755a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
26 changes: 14 additions & 12 deletions bothub_nlp_api/handlers/question_answering.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import threading
import json
import requests

from bothub_nlp_celery.app import celery_app
from bothub_nlp_celery.tasks import TASK_NLU_QUESTION_ANSWERING
Expand All @@ -10,10 +11,10 @@
EmptyInputException,
EmptyBaseException,
)
from bothub_nlp_api.utils import backend, repository_authorization_validation, language_validation
from bothub_nlp_api.utils import backend, repository_authorization_validation, language_validation, language_to_qa_model
from bothub_nlp_api.exceptions.celery_exceptions import CeleryTimeoutException
from celery.exceptions import TimeLimitExceeded
from bothub_nlp_api.settings import BOTHUB_NLP_API_QA_TEXT_LIMIT, BOTHUB_NLP_API_QA_QUESTION_LIMIT
from bothub_nlp_api.settings import BOTHUB_NLP_API_QA_TEXT_LIMIT, BOTHUB_NLP_API_QA_QUESTION_LIMIT, BOTHUB_TORCHSERVE_URL


def qa_handler(
Expand All @@ -40,16 +41,7 @@ def qa_handler(
elif len(text) > BOTHUB_NLP_API_QA_TEXT_LIMIT:
raise LargeContextException(len(text), limit=BOTHUB_NLP_API_QA_TEXT_LIMIT)

try:
answer_task = celery_app.send_task(
TASK_NLU_QUESTION_ANSWERING,
args=[text, question, language],
queue=queue_name(language, ACTION_QUESTION_ANSWERING, "QA"),
)
answer_task.wait()
result = answer_task.result
except TimeLimitExceeded:
raise CeleryTimeoutException()
result = request_torchserve(text, question, language)

if len(result["answers"]) > 0:
answer_object = result["answers"][0]
Expand Down Expand Up @@ -79,3 +71,13 @@ def qa_handler(
log.start()

return result

def request_torchserve(text, question, language):
model = language_to_qa_model.get(language, "multilang")
url = f"{BOTHUB_TORCHSERVE_URL}/predictions/{model}"

payload = json.dumps(dict(question=question, context=text))
headers = {"Content-type": "application/json"}

response = requests.request("POST", url, headers=headers, data=payload)
return response.json()
3 changes: 2 additions & 1 deletion bothub_nlp_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def cast_supported_languages(i):
BOTHUB_NLP_SENTRY=(str, None),
SUPPORTED_LANGUAGES=(cast_supported_languages, "en|pt"),
BOTHUB_ENGINE_URL=(str, "https://api.bothub.it"),
BOTHUB_TORCHSERVE_URL=(str, "https://torchserve.weni.ai"),
BOTHUB_GOOGLE_PROJECT_ID=(str, None),
BOTHUB_GOOGLE_CREDENTIALS_REFRESH_TOKEN=(str, None),
BOTHUB_GOOGLE_CREDENTIALS_TOKEN_URI=(str, None),
Expand Down Expand Up @@ -67,7 +68,7 @@ def cast_supported_languages(i):
)

BOTHUB_ENGINE_URL = env.str("BOTHUB_ENGINE_URL")

BOTHUB_TORCHSERVE_URL = env.str("BOTHUB_TORCHSERVE_URL")

BOTHUB_SERVICE_TRAIN = env.str("BOTHUB_SERVICE_TRAIN", default="celery")

Expand Down
6 changes: 6 additions & 0 deletions bothub_nlp_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,9 @@ def get_language_model(update):
model = "SPACY"

return model

language_to_qa_model = {
'en': 'en',
'pt_br': 'pt_br',
'pt': 'pt_br',
}

0 comments on commit 606755a

Please sign in to comment.