Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker available for TransformsReader Class #180

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY models /home/user/models

# copy sqlite db if needed for testing
#COPY qa.db /home/user/
COPY data /home/user/data

EXPOSE 8000

Expand Down
28 changes: 28 additions & 0 deletions docker-compose-custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3'
services:
haystack-api:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
environment:
# see rest_api/config.py for additional variables to configure.
- READER_MODEL_PATH=distilbert-base-uncased-distilled-squad
# - READER_MODEL_PATH=fmikaelian/camembert-base-fquad
# - READER_MODEL_PATH=deepset/roberta-base-squad2
# - READER_MODEL_PATH=/home/user/models/roberta-base-squad2
- READER_TOKENIZER=distilbert-base-uncased
- READER_USE_TRANSFORMERS=True
- DB_HOST=elasticsearch
- USE_GPU=False
restart: always
depends_on:
- elasticsearch

elasticsearch:
image: "elasticsearch:7.6.1"
ports:
- 9200:9200
environment:
- discovery.type=single-node
2 changes: 1 addition & 1 deletion haystack/reader/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
:param tokenizer: name of the tokenizer (usually the same as model)
:param context_window_size: num of chars (before and after the answer) to return as "context" for each answer.
The context usually helps users to understand if the answer really makes sense.
:param use_gpu: < 1 -> use cpu
:param use_gpu: < 0 -> use cpu
>= 0 -> ordinal of the gpu to use
"""
self.model = pipeline("question-answering", model=model, tokenizer=tokenizer, device=use_gpu)
Expand Down
3 changes: 3 additions & 0 deletions rest_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Resources / Computation
USE_GPU = os.getenv("USE_GPU", "True").lower() == "true"
GPU_NUMBER = int(os.getenv("GPU_NUMBER", 1))
MAX_PROCESSES = int(os.getenv("MAX_PROCESSES", 4))
BATCHSIZE = int(os.getenv("BATCHSIZE", 50))
CONCURRENT_REQUEST_PER_WORKER = int(os.getenv("CONCURRENT_REQUEST_PER_WORKER", 4))
Expand All @@ -26,6 +27,8 @@

# Reader
READER_MODEL_PATH = os.getenv("READER_MODEL_PATH", None)
READER_USE_TRANSFORMERS = os.getenv("READER_USE_TRANSFORMERS", "True").lower() == "true"
READER_TOKENIZER = os.getenv("READER_TOKENIZER", None)
CONTEXT_WINDOW_SIZE = int(os.getenv("CONTEXT_WINDOW_SIZE", 500))
DEFAULT_TOP_K_READER = int(os.getenv("DEFAULT_TOP_K_READER", 5))
TOP_K_PER_CANDIDATE = int(os.getenv("TOP_K_PER_CANDIDATE", 3))
Expand Down
34 changes: 22 additions & 12 deletions rest_api/controller/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
EMBEDDING_DIM, EMBEDDING_FIELD_NAME, EXCLUDE_META_DATA_FIELDS, EMBEDDING_MODEL_PATH, USE_GPU, READER_MODEL_PATH, \
BATCHSIZE, CONTEXT_WINDOW_SIZE, TOP_K_PER_CANDIDATE, NO_ANS_BOOST, MAX_PROCESSES, MAX_SEQ_LEN, DOC_STRIDE, \
DEFAULT_TOP_K_READER, DEFAULT_TOP_K_RETRIEVER, CONCURRENT_REQUEST_PER_WORKER, FAQ_QUESTION_FIELD_NAME, \
EMBEDDING_MODEL_FORMAT
EMBEDDING_MODEL_FORMAT, READER_USE_TRANSFORMERS, READER_TOKENIZER, GPU_NUMBER
from rest_api.controller.utils import RequestLimiter
from haystack.database.elasticsearch import ElasticsearchDocumentStore
from haystack.reader.farm import FARMReader
from haystack.reader.transformers import TransformersReader
from haystack.retriever.base import BaseRetriever
from haystack.retriever.elasticsearch import ElasticsearchRetriever, EmbeddingRetriever

Expand Down Expand Up @@ -52,17 +53,26 @@
retriever = ElasticsearchRetriever(document_store=document_store)

if READER_MODEL_PATH: # for extractive doc-qa
reader = FARMReader(
model_name_or_path=str(READER_MODEL_PATH),
batch_size=BATCHSIZE,
use_gpu=USE_GPU,
context_window_size=CONTEXT_WINDOW_SIZE,
top_k_per_candidate=TOP_K_PER_CANDIDATE,
no_ans_boost=NO_ANS_BOOST,
num_processes=MAX_PROCESSES,
max_seq_len=MAX_SEQ_LEN,
doc_stride=DOC_STRIDE,
) # type: Optional[FARMReader]
if READER_USE_TRANSFORMERS:
use_gpu = -1 if not USE_GPU else GPU_NUMBER
reader = TransformersReader(
model=str(READER_MODEL_PATH),
use_gpu=use_gpu,
context_window_size=CONTEXT_WINDOW_SIZE,
tokenizer=str(READER_TOKENIZER)
) # type: Optional[FARMReader]
else:
reader = FARMReader(
model_name_or_path=str(READER_MODEL_PATH),
batch_size=BATCHSIZE,
use_gpu=USE_GPU,
context_window_size=CONTEXT_WINDOW_SIZE,
top_k_per_candidate=TOP_K_PER_CANDIDATE,
no_ans_boost=NO_ANS_BOOST,
num_processes=MAX_PROCESSES,
max_seq_len=MAX_SEQ_LEN,
doc_stride=DOC_STRIDE,
) # type: Optional[FARMReader]
else:
reader = None # don't need one for pure FAQ matching

Expand Down