Last active
February 15, 2022 13:01
-
-
Save SETIADEEPANSHU/8d1f68ea3fff59148794de204044eec5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Run file -> python api.py | |
from fastapi import FastAPI # pip install fastapi | |
from haystack.document_store.elasticsearch import ElasticsearchDocumentStore | |
from haystack.retriever.dense import DensePassageRetriever | |
from haystack.reader.farm import FARMReader | |
from haystack.pipeline import ExtractiveQAPipeline | |
# initialize doc store, retriever and reader components | |
DOC_STORE = ElasticsearchDocumentStore( | |
host='localhost', username='', password='', index='aurelius' | |
) | |
RETRIEVER = DensePassageRetriever(DOC_STORE) # take code from retriever file | |
READER = FARMReader(model_name_or_path='deepset/bert-base-cased-squad2', | |
context_window_size=1500, | |
use_gpu=False) | |
# initialize pipeline | |
PIPELINE = ExtractiveQAPipeline(reader=READER, retriever=RETRIEVER) | |
# initialize API | |
APP = FastAPI() | |
@APP.get('/query') | |
async def get_query(q: str, retriever_limit: int = 10, reader_limit: int = 5): | |
"""Makes query to doc store via Haystack pipeline. | |
:param q: Query string representing the question being asked. | |
:type q: str | |
""" | |
# get answers | |
return PIPELINE.run(query=q, | |
top_k_retriever=retriever_limit, | |
top_k_reader=reader_limit) | |
from fastapi import FastAPI #flask | |
from haystack.document_store.elasticsearch import ElasticsearchDocumentStore | |
from haystack.retriever.dense import DensePassageRetriever | |
from haystack.reader.farm import FARMReader | |
from haystack.pipeline import ExtractiveQAPipeline | |
# initialize doc store, retriever and reader components | |
DOC_STORE = ElasticsearchDocumentStore( | |
host='localhost', username='', password='', index='aurelius' | |
) | |
RETRIEVER = DensePassageRetriever(DOC_STORE) | |
READER = FARMReader(model_name_or_path='deepset/bert-base-cased-squad2', | |
context_window_size=1500, | |
use_gpu=True) | |
# initialize pipeline | |
PIPELINE = ExtractiveQAPipeline(reader=READER, retriever=RETRIEVER) | |
# initialize API | |
APP = FastAPI() | |
@APP.get('/query') | |
async def get_query(q: str, retriever_limit: int = 15, reader_limit: int = 10): | |
"""Makes query to doc store via Haystack pipeline. | |
:param q: Query string representing the question being asked. | |
:type q: str | |
""" | |
# get answers | |
return PIPELINE.run(query=q, | |
top_k_retriever=retriever_limit, | |
top_k_reader=reader_limit) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment