-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
69 lines (56 loc) · 2.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from arguments import args
from fastapi import FastAPI, HTTPException, APIRouter
from fastapi.routing import APIRoute
from fastapi.openapi.utils import get_openapi
from starlette.middleware.cors import CORSMiddleware
from haystack import __version__ as haystack_version
from controller.errors.http_error import http_error_handler
from pipelines import setup_pipelines
app = None
pipelines = None
def get_app() -> FastAPI:
"""
Initializes the App object and creates the global pipelines as possible.
"""
global app # pylint: disable=global-statement
if app:
return app
from rest_api.config import ROOT_PATH
app = FastAPI(
title="Haystack REST API",
debug=True,
version=haystack_version,
root_path=ROOT_PATH,
)
# Creates the router for the API calls
from controller import file_upload, search, feedback, document, health
router = APIRouter()
router.include_router(search.router, tags=["search"])
router.include_router(feedback.router, tags=["feedback"])
router.include_router(file_upload.router, tags=["file-upload"])
router.include_router(document.router, tags=["document"])
router.include_router(health.router, tags=["health"])
# This middleware enables allow all cross-domain requests to the API from a browser. For production
# deployments, it could be made more restrictive.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_exception_handler(HTTPException, http_error_handler)
app.include_router(router)
# Simplify operation IDs so that generated API clients have simpler function
# names (see https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#using-the-path-operation-function-name-as-the-operationid).
# The operation IDs will be the same as the route names (i.e. the python method names of the endpoints)
# Should be called only after all routes have been added.
for route in app.routes:
if isinstance(route, APIRoute):
route.operation_id = route.name
return app
def get_pipelines():
global pipelines # pylint: disable=global-statement
if not pipelines:
pipelines = setup_pipelines(args)
return pipelines