fastapi-ai-template

Enrique Catala
2 min readOct 25, 2022

--

FastAPI is a great choice in order to deploy RESTApi with python. Its fast, easy and powerful, and something to check if you work deploying AI models for real-time inferencing in kubernetes.

I made a “production ready” template for FastAPI that you can use to deploy your own AI models.

Whats included?

With this template you will find a very easy way to create a full template that covers the following:

  • Telemetry: ApplicationInsights ready
  • Security: Security through API key
  • Webserver: gunicorn configuration to fine tune threads and memory
  • Docker support: Dockerfile and docker-compose setup
  • Debug: visual studio code debug configuration setted up

Cookiecutter template

You don´t need to fork this project. This project is a template for cookiecutter that can be used directly from your command line without forking it. The output of this project is a fully configured FastAPI application ready for you to start coding and deploy.

Setup

Since this is a template for cookiecutter, you need first to install cookiecutter.

Once installed, you can run the following command to create a new project:

# go to the directory where you want to create the project
cd ~
mkdir your-project
cd your-project
# create your project using this template
cookiecutter https://github.com/enriquecatala/fastapi-ai-template.git \
-v \
--directory="base-template"

This will create a base ApiREST project ready for you to test and deploy. It will include a test method that you can use to test your code.

docker compose build
docker compose up

Now navigate to http://127.0.0.1:5000/docs to see the documentation.

Configuration

your-project/
├── fastapi-ai-template/ <--------- Project template
└── .vs-code/ <--------- visual studio debug config
├── launch.json
└── tasks.json
└── app/ <--------- Project folder
├── api/ <--------- api routing
├── core/ <--------- event handling and security
├── models/ <--------- Model classes definition
├── services/ <--------- API main methods (this is where you have the main logic)
└── main.py
├── Dockerfile
├── docker-compose.yml
├── gunicorn_conf.py <--------- web server config
├── requirements.txt <--------- default libraries
└── ...

Help!

Please if you think this worth the effort, consider ❤️ Sponsor this project and help me increase functionality

--

--

Enrique Catala

Computer Engineer, Cloud Data Ninja, Microsoft Data Platform MVP, Certified Kubernetes Administrator, Azure Data Scientist, Azure Data Engineer