Deploying ML Models as API using FastAPI (original) (raw)

Last Updated : 11 May, 2026

Self-training is a semi-supervised learning technique where a model is first trained on a small labeled dataset and then iteratively improves itself using its own high-confidence predictions on unlabeled data.

Before getting started, install the required libraries using the following command:

pip install fastapi uvicorn scikit-learn pydantic

Step-by-Step Implementation

Step 1: Import Required Libraries

We first import all the necessary modules like FastAPI, scikit-learn, and pydantic.

Python `

from fastapi import FastAPI from sklearn.datasets import load_iris from sklearn.naive_bayes import GaussianNB from pydantic import BaseModel

`

**Explanation:

Step 2: Define Input Data Format

Next, we define what kind of input data our API will accept. Using Pydantic’s BaseModel, we define the structure of the incoming JSON data.

Python `

class IrisFeatures(BaseModel): sepal_length: float sepal_width: float petal_length: float petal_width: float

`

**Explanation:

Step 3: Train the Machine Learning Model

We’ll now load the Iris dataset and train a simple Gaussian Naive Bayes classifier to predict the flower type.

Python `

Load Iris dataset

iris = load_iris()

Extract features and labels

X, y = iris.data, iris.target

Train the model

clf = GaussianNB() clf.fit(X, y)

`

**Explanation:

Step 4: Create the FastAPI App and Prediction Endpoint

Now, we’ll combine everything create the FastAPI app, load the model, and add a /predict endpoint that accepts JSON input and returns the predicted flower class.

Python `

Create FastAPI instance

app = FastAPI()

Define prediction endpoint

@app.post("/predict") def predict(data: IrisFeatures): test_data = [[ data.sepal_length, data.sepal_width, data.petal_length, data.petal_width ]] class_idx = clf.predict(test_data)[0] return {"class": iris.target_names[class_idx]}

`

**Explanation:

Step 5: Run and Test the API

Now, save your code (for example, as basic.py) and run the server using:

uvicorn basic:app --reload

**Explanation:

Output

Then, open your browser and go to http://127.0.0.1:8000/ You’ll see a message showing that your FastAPI app is running.

IrisPredictionAPI_Output

http://127.0.0.1:8000/

To test our API we'll be using Swagger UI now to access that you'll just need to add /docsat the end of your path. So go to http://127.0.0.1:8000/docs. And you should see the following output:

SwaggerDocs

http://127.0.0.1:8000/docs

Here, you’ll see the /predictendpoint we created along with a “Try it out” button to test the API directly. Click “Try it out” and enter the following JSON input in the text box:

{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
}

Predict_post

Responses

After entering the data, click “Execute.” You’ll see the output under the Responses section showing the predicted flower type, for example:

PredictedValue

Output

That means the model has classified the input as the Setosaspecies.