π€ Huggingface + β‘ FastAPI = β€οΈ Awesomeness. How to structure Deep Learning model serving REST API with FastAPI
How to server Hugging face models with FastAPI, the Python's fastest REST API framework.
Project structure for development and production.
Installation and setup instructions to run the development mode model and serve a local RESTful API endpoint.
Files related to application are in the huggingfastapi
or tests
directories.
Application parts are:
huggingfastapi
βββ api - Main API.
β βββ routes - Web routes.
βββ core - Application configuration, startup events, logging.
βββ models - Pydantic models for api.
βββ services - NLP logics.
βββ main.py - FastAPI application creation and configuration.
β
tests - Codes without tests is an illusion
Python 3.6+, [Make and Docker]
Install the required packages in your local environment (ideally virtualenv, conda, etc.).
python -m venv venv
source venv/bin/activate
make install
make run
make deploy
make test
-
Duplicate the
.env.example
file and rename it to.env
-
In the
.env
file configure theAPI_KEY
entry. The key is used for authenticating our API.
Execute script to generate .env, and replaceexample_key
with the UUID generated:
make generate_dot_env
python -c "import uuid;print(str(uuid.uuid4()))"
- Start your app with:
PYTHONPATH=./huggingfastapi uvicorn main:app --reload
-
Go to http://localhost:8000/docs or http://localhost:8000/redoc for alternative swagger
-
Click
Authorize
and enter the API key as created in the Setup step.
Intall testing libraries and run tox
:
pip install tox pytest flake8 coverage bandit
tox
This runs tests and coverage for Python 3.8 and Flake8, Bandit.
- Change make to invoke
- Add endpoint for uploading text file and questions