def __init__(self, model, servername, host: str = '127.0.0.1', http_port: int = 8080, max_buffer_size: int = 104857600, workers: int = 1): super(InferenceServer, self).__init__(servername=servername, host=host, http_port=http_port, workers=workers) self.model = model self.max_buffer_size = max_buffer_size self.app = FastAPI( routes=[ APIRoute( f"/{servername}", self.model_info, response_model=ServeModelInfoResult, response_class=JSONResponse, methods=["GET"], ), APIRoute( f"/{servername}/predict", self.predict, response_model=ServePredictResult, response_class=JSONResponse, methods=["POST"], ), ], log_level="trace", timeout=600, )
def __init__(self, pipeline: Pipeline, host: str, port: int, workers: int): self._pipeline = pipeline self.host = host self.port = port self.workers = workers if not _serve_dependencies_installed: raise RuntimeError( "Using serve command requires FastAPI and unicorn. " 'Please install transformers with [serving]: pip install "transformers[serving]".' "Or install FastAPI and unicorn separately.") else: logger.info("Serving model over {}:{}".format(host, port)) self._app = FastAPI( routes=[ APIRoute( "/", self.model_info, response_model=ServeModelInfoResult, response_class=JSONResponse, methods=["GET"], ), APIRoute( "/tokenize", self.tokenize, response_model=ServeTokenizeResult, response_class=JSONResponse, methods=["POST"], ), APIRoute( "/detokenize", self.detokenize, response_model=ServeDeTokenizeResult, response_class=JSONResponse, methods=["POST"], ), APIRoute( "/forward", self.forward, response_model=ServeForwardResult, response_class=JSONResponse, methods=["POST"], ), APIRoute( "/sentiment", self.sentiment, response_model=ServeSentimentResult, response_class=JSONResponse, methods=["POST"], ), ], timeout=600, )
def __init__(self, config_manager): module_path = os.path.dirname(os.path.realpath(__file__)) super().__init__(routes=[ APIRoute('/', root.home), APIRoute('/config', root.config, methods=['GET', 'POST']), Mount('/static', StaticFiles(directory=os.path.join(module_path, 'static')), name='static'), ]) self.templates = Jinja2Templates( directory=os.path.join(module_path, 'templates')) self.config_manager = config_manager
def load(self) -> None: from .api import ( async_create_example, create_example, example, examples, homepage, other, ) from .schema import ExampleSchema self.registry.declare_routes({ "home": Route("/", homepage, methods=["GET"]), "GET/examples/": APIRoute( "/examples/", examples, methods=[ "GET", ], response_model=List[ExampleSchema], response_class=JSONResponse, ), "POST/examples/": APIRoute( "/examples/", create_example, methods=["POST"], response_model=ExampleSchema, response_class=JSONResponse, ), "POST/examples-async/": APIRoute( "/examples-async/", async_create_example, methods=["POST"], response_model=ExampleSchema, response_class=JSONResponse, ), "GET/examples/{id}": APIRoute( "/examples/{id}", example, methods=["GET"], response_model=ExampleSchema, response_class=JSONResponse, ), "other": APIRoute("/other", other, response_class=JSONResponse), })
def __init__(self, data: Dict, ip: str = '127.0.0.1', port: int = 9000, use_ssl: str = '', log_level: str = 'error') -> None: self.ip_addr = ip self.port = port self.data = data if use_ssl and use_ssl not in ['valid', 'self-signed']: raise ValueError( "Argument 'use_ssl' can be only 'valid' or 'self-signed'") self.use_ssl = use_ssl if not use_ssl: self.protocol = 'http' else: self.protocol = 'https' self.log_level = log_level self._valid_tokens = ['MY-TOKEN'] errors_file = Path(_ERRORS_PATH) if not errors_file.is_file(): raise RuntimeError(f"No errors file at {_ERRORS_PATH}") with open(errors_file, 'r') as f: self._errors = yaml.safe_load(f.read()) self.app = FastAPI(routes=[ APIRoute("/api/dcim/devices/", self.get_devices, status_code=200, methods=["GET"]) ])
def __init__(self, host: str, http_port: int = 8080, workers: int = 1, save_dir=""): servername = "knowledgebase" super(KBServer, self).__init__(servername=servername, host=host, http_port=http_port, workers=workers) self.save_dir = FileOps.clean_folder([save_dir], clean=False)[0] self.url = f"{self.url}/{servername}" self.kb_index = KBResourceConstant.KB_INDEX_NAME.value self.app = FastAPI( routes=[ APIRoute( f"/{servername}/update", self.update, methods=["POST"], ), APIRoute( f"/{servername}/update/status", self.update_status, methods=["POST"], ), APIRoute( f"/{servername}/query", self.query, response_model=TaskItem, response_class=JSONResponse, methods=["POST"], ), APIRoute( f"/{servername}/file/download", self.file_download, methods=["GET"], ), APIRoute( f"/{servername}/file/upload", self.file_upload, methods=["POST"], ), ], log_level="trace", timeout=600, )
def make_routes(spec: Dict[str, Any], include_in_schema: bool = True) -> Iterable[APIRoute]: responses = { '4xx': { "model": ErrorModel, "description": "Bad request", 'content': { 'application/json': {} }, } } for tag, routes in spec.items(): for route in routes: path, method, func, *params = route name = params[0] if params else func.__name__ status_code = params[1] if len(params) > 1 else 200 response_model = getattr(func, 'response_model', None) response_class = getattr(func, 'response_class', None) response_status_code = getattr(func, 'response_status_code', status_code) deprecated = 'deprecated' in name yield APIRoute( path, func, name=name, methods=[method], tags=[tag], operation_id=name, response_model=response_model, responses=responses, # type: ignore status_code=response_status_code, response_class=response_class, deprecated=deprecated, include_in_schema=include_in_schema, )
def __init__( self, aggregation: str, host: str = None, http_port: int = None, exit_round: int = 1, participants_count: int = 1, ws_size: int = 10 * 1024 * 1024): if not host: host = Context.get_parameters("AGG_BIND_IP", get_host_ip()) if not http_port: http_port = int(Context.get_parameters("AGG_BIND_PORT", 7363)) super( AggregationServer, self).__init__( servername=aggregation, host=host, http_port=http_port, ws_size=ws_size) self.aggregation = aggregation self.participants_count = participants_count self.exit_round = max(int(exit_round), 1) self.app = FastAPI( routes=[ APIRoute( f"/{aggregation}", self.client_info, response_class=JSONResponse, ), WebSocketRoute( f"/{aggregation}", BroadcastWs ) ], ) self.app.shutdown = False
def load(self) -> None: from .api import ( device_fuel_gauge_state, device_relay_desired_state, device_relay_state, device_thermometer_state, device_weather_station_state, get_mode, get_thermostat_range, save_device_fuel_gauge_state, save_device_relay_desired_state, save_device_relay_state, save_device_thermometer_state, save_device_weather_station_state, save_device_weather_station_aprs_packet, set_mode, set_thermostat_range, ) from .schemas.devices import ( FuelGaugeState, RelayState, ThermometerState, ThermostatMode, ThermostatRange, WeatherStationState, ) self.registry.declare_routes({ "GET/api/mode": APIRoute( "/api/mode", get_mode, methods=["GET"], response_class=JSONResponse, response_model=ThermostatMode, ), "POST/api/mode": APIRoute( "/api/mode", set_mode, methods=["POST"], response_class=JSONResponse, response_model=ThermostatMode, ), "GET/api/device/relay/{code}/state": APIRoute( "/api/device/relay/{code}/state", device_relay_state, methods=["GET"], response_class=JSONResponse, response_model=RelayState, ), "POST/api/device/relay/{code}/state": APIRoute( "/api/device/relay/{code}/state", save_device_relay_state, methods=["POST"], response_class=JSONResponse, response_model=RelayState, ), "GET/api/device/relay/{code}/desired/state": APIRoute( "/api/device/relay/{code}/desired/state", device_relay_desired_state, methods=["GET"], response_class=JSONResponse, response_model=RelayState, ), "POST/api/device/relay/{code}/desired/state": APIRoute( "/api/device/relay/{code}/desired/state", save_device_relay_desired_state, methods=["POST"], response_class=JSONResponse, response_model=RelayState, ), "GET/api/device/fuel-gauge/{code}/state": APIRoute( "/api/device/fuel-gauge/{code}/state", device_fuel_gauge_state, methods=["GET"], response_class=JSONResponse, response_model=FuelGaugeState, ), "POST/api/device/fuel_gauge/{code}/state": APIRoute( "/api/device/fuel_gauge/{code}/state", save_device_fuel_gauge_state, methods=["POST"], response_class=JSONResponse, response_model=FuelGaugeState, ), "GET/api/device/thermometer/{code}/state": APIRoute( "/api/device/thermometer/{code}/state", device_thermometer_state, methods=["GET"], response_class=JSONResponse, response_model=ThermometerState, ), "POST/api/device/thermometer/{code}/state": APIRoute( "/api/device/thermometer/{code}/state", save_device_thermometer_state, methods=["POST"], response_class=JSONResponse, response_model=ThermometerState, ), "GET/api/thermostat/range/{code}": APIRoute( "/api/thermostat/range/{code}", get_thermostat_range, methods=["GET"], response_class=JSONResponse, response_model=ThermostatRange, ), "POST/api/thermostat/range/{code}": APIRoute( "/api/thermostat/range/{code}", set_thermostat_range, methods=["POST"], response_class=JSONResponse, response_model=ThermostatRange, ), "GET/api/device/weather-station/{code}/state": APIRoute( "/api/device/weather-station/{code}/state", device_weather_station_state, methods=["GET"], response_class=JSONResponse, response_model=WeatherStationState, ), "POST/api/device/weather-station/{code}/state": APIRoute( "/api/device/weather-station/{code}/state", save_device_weather_station_state, methods=["POST"], response_class=JSONResponse, response_model=WeatherStationState, ), "POST/api/device/weather-station/aprs-packet": APIRoute( "/api/device/weather-station/aprs-packet", save_device_weather_station_aprs_packet, methods=["POST"], response_class=JSONResponse, response_model=WeatherStationState, ), })
# Argument (name) for the Field and returns data for the query Response # Type hints: For instance methods, omit type for "self" # JS/Prisma equivalent: (obj/parent, args, context, info) async def resolve_hello(self, info, name): # We can make asynchronous network calls here return f"Hello {name}" # Explicity define schema so can use in conftest.py. There may be a better way. schema = graphene.Schema(query=Query) # Define our routes following Starlette's example routes = [ # We're using 'executor_class=AsyncioExecutor' here. # Route("/", GraphQLApp(schema=schema, executor_class=AsyncioExecutor)) APIRoute("/", GraphQLApp(schema=schema, executor_class=AsyncioExecutor)) ] app = FastAPI(routes=routes) # app.add_route("/", GraphQLApp(schema=schema)) # app.add_route( # "/", GraphQLApp(schema=schema, executor_class=AsyncioExecutor), # ) # from fastapi import FastAPI # app = FastAPI() # @app.get("/") # def read_root(): # return {"Hello": "World"}
"""""" background_tasks.add_task(write_notification, message) return {'message': 'Notification sent in the background'} async def startup(): print('Ready to go') await database.connect() async def shutdown(): print('Close') routes = [ APIRoute('/', read_root, response_class=JSONResponse), APIRoute('/items/{item_id}', read_item, response_class=JSONResponse), APIRoute( '/items/add', add_item, response_class=JSONResponse, response_model=ItemAnswer, methods=['POST'] ), APIRoute( '/send-notification/{message}', send_notification, response_class=JSONResponse, methods=['POST'] ), APIRoute(
import uvicorn from fastapi import FastAPI from fastapi.routing import APIRoute from starlette.responses import HTMLResponse from starlette.routing import Mount from starlette.staticfiles import StaticFiles from src import views from src.database import prepare_database from src.logs import setup_logging from src.settings import Settings THIS_DIR = Path(__file__).parent.resolve() routes = [ APIRoute('/', views.index, name='index', response_class=HTMLResponse), APIRoute('/list/', views.games_list, name='index', response_class=HTMLResponse), Mount('/static', StaticFiles(directory=THIS_DIR / 'static'), name='static'), ] settings = Settings() engine = prepare_database(delete_existing=False, settings=settings) app = FastAPI(debug=settings.debug, routes=routes) setup_logging() # For running locally