/fast-api-ml-template

An Boilerplate using FastAPI to server ML models/predictions

Primary LanguagePythonMIT LicenseMIT

FASTAPI - Template

Backend Requirements

  • FastAPI Github Repo -- Alembic Docs : For SQL Database Migrations -- Celery for Tasks Docs

Backend local development

  • Start the virtualenv with pipenv into the root folder: Instructions to install here
pipenv shell
  • Now install the dependecies on Pipfile:
pipenv install
  • Everthing is set up now apply the migrations on the database:
alembic upgrade head
  • And run locally:
uvicorn app.main:app --reload

** For more flags visit here

  • Now you can open your browser and interact with these URLs:

Backend, JSON based web API based on OpenAPI: http://localhost/api/

Automatic interactive documentation with Swagger UI (from the OpenAPI backend): http://localhost/docs

Alternative automatic documentation with ReDoc (from the OpenAPI backend): http://localhost/redoc

Backend local development, additional details

General workflow

Open your editor at ./app/ . That way, your editor will be able to find all the imports, etc.

Modify or add SQLAlchemy models in ./app/app/db_models/, Pydantic models in ./app/app/models/, API endpoints in ./app/app/api/, CRUD (Create, Read, Update, Delete) utils in ./app/app/crud/. The easiest might be to copy the ones for Items (models, endpoints, and CRUD utils) and update them to your needs.

Backend tests

The tests run with Pytest, modify and add tests to ./backend/app/app/tests/.

If you need to install any additional package for the tests, add it to the file ./app/Pipfile.

Migrations

Make sure you create a "revision" of your models and that you "upgrade" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors.

  • If you created a new model in ./app/app/db_models/, make sure to import it in ./app/app/db/base.py, that Python module (base.py) that imports all the models will be used by Alembic.
  • After changing a model (for example, adding a column), inside the container, create a revision, e.g.:
alembic revision --autogenerate -m "Add column last_name to User model"
  • Commit to the git repository the files generated in the alembic directory.
  • After creating the revision, run the migration in the database (this is what will actually change the database):
alembic upgrade head

If you don't want to use migrations at all, uncomment the line in the file at ./backend/app/app/db/init_db.py with:

Base.metadata.create_all(bind=engine)

and comment the line in the file prestart.sh that contains:

alembic upgrade head

If you don't want to start with the default models and want to remove them / modify them, from the beginning, without having any previous revision, you can remove the revision files (.py Python files) under ./backend/app/alembic/versions/. And then create a first migration as described above.

TODO

  • PGAdmin, PostgreSQL web administration

  • Flower, administration of Celery tasks: http://localhost:5555

  • NoSQL Integration with MotorDriver (Async MongoDB Driver)

  • Dockernize applications: -- Celery -- Pgadmin -- Flower

  • Create a template using cookie-cutter TODO

  • Tests the tests 👌

URLs

These are the URLs that will be used and generated by the project.

DEPLOY - WIP

  • deployment.yaml (for kubernetes configurations)

Project generation and updating, or re-generating - WIP

Create a template using cookie-cutter TODO