In this guide, we’ll deploy a FastAPI service for solving the Iris classification problem. Iris flower has 3 species: Iris setosa, Iris versicolor, and Iris virginica. The problem involves predicting the species of an iris flower based on its sepal length, sepal width, petal length, and petal width.

The model has 4 inputs: sepal length, sepal width, petal length, and petal width and ouputs the confidence scores for each species in the following format:

{
  "predictions": [
    {
      "label": "setosa",
      "score": 2.1184377821359003e-16
    },
    {
      "label": "versicolor",
      "score": 3.264647319023382e-9
    },
    {
      "label": "virginica",
      "score": 0.9999999967353526
    }
  ]
}

We’ve already created a FastAPI service for the Iris classification problem, and you can find the code in our GitHub Repository.

Please visit the repository to familiarize yourself with the code you’ll be deploying. The project files are organized as follows:

Directory Structure
.
├── app.py - Contains FastAPI code for inference.
├── iris_classifier.joblib - The model file.
└── requirements.txt - Lists dependencies.

Getting Started With Deployment

To deploy a service, you’ll need a workspace. If you don’t have one, you can create it using this guide: Creating a Workspace or seek assistance from your cluster administrator in case you don’t have permission to create a workspace.

In TrueFoundry, you can either deploy code from your Github repository or from your local machine in case the code is not pushed to a Github repository.

In the above walkthrough, we did the following steps:

  1. Select a workspace to deploy the service. This basically decides which cluster and environment the service will be deployed to.
  2. Select the Service option since this is a FastAPI service and exposed a REST API.
  3. We chose the Github option since the code is already pushed to a Github repository.

The key fields that we need to fill up in the deployment form are:

  1. Repo Url: This is the URL of the Github repository that contains the code for the service. For this example, the repo url is https://github.com/truefoundry/getting-started-examples
  2. Path to build context: This is the path to the directory in the Github repository that contains the code for the service. For this example, the path to the build context is ./deploy-model-with-fastapi/
  3. Command: This is the command to run the service. For this example, the command is uvicorn app:app --host 0.0.0.0 --port 8000
  4. Port: This is the port on which the service will be exposed. Since we have mentioned port 8000 in the command above, so we need to specify the port here as 8000.

On filling up the form, we can press the Submit button to deploy the service.

View your deployed service

Congratulations! You’ve successfully deployed your FastAPI service.

Once you click Submit, your deployment will be successful in a few seconds, and your service will be displayed as active (green), indicating that it’s up and running.

You can view all the information about your service following the steps below:

To make a request to the Service, you can copy the endpoint URL as shown on the UI or from what you provided in the Ports section. The endpoint will be an internal cluster URL or an external URL based on whether you chose Expose in the Ports configuration. You can read about it more in the Ports section. You can call the service using curl or Postman or using the Python code as shown below:

import json  
from urllib.parse import urljoin
import requests

ENDPOINT_URL = "\<YOUR_ENDPOINT_URL>"  # e.g., https://your-service-endpoint.com

response = requests.post(  
    urljoin(ENDPOINT_URL, 'predict'),  
    params={  
        "sepal_length": 7.0,  
        "sepal_width": 3.2,  
        "petal_length": 4.7,  
        "petal_width": 1.4,  
    }  
)
result = response.json()  
print("Predicted Classes:", result["prediction"])

FAQ