Beginner’s Guide to Deploying Machine Learning APIs with FastApi

In this guide, you will learn how to deploy machine learning models as APIs using FastAPI. We will create an API that predicts penguin species based on its bill length and curvature length.
Prerequisites
- Basics of Python
- Python is installed on your system (preferably version 3.7 or higher)
- Familiar with machine learning concepts (optional)
Step 1: Set up your environment
- Create a project directory
Open a terminal and create a new directory for your project: - Set up a virtual environment
Create and activate a virtual environment: - Use on Windows: venvscriptSactivate
- Install the required packages
Install Fastapi, Uvicorn (for service applications) and other necessary libraries:
Step 2: Prepare the machine learning model
- Download the dataset
In this example, we will use the Palmer Penguins dataset. You can download it from here. - Create a Python script for the model
Create a file named model.py in your project directory:
Step 3: Create a FastAPI application
- Create the main application file
Create a file named main.py:
Step 4: Run your fastapi application
- Run the application
In your terminal, run the following command:
- Access API
Opening your web browser and navigating to this will open the Swagger UI where you can test the API.
Step 5: Test your API
- Using Swagger UI
In the Swagger UI, find/predict the endpoint, click it, and click “Try it”. Enter the values of bill_length and flipper_length and click Execute. You should see a response to the predicted penguin species!
in conclusion
Congratulations! You have successfully deployed the machine learning API using FastAPI. This guide covers:
- Set up your environment.
- Prepare machine learning models.
- Create a FastAPI application.
- Run and test your API.
Next step
- Explore more advanced features of FastApi, such as authentication and database integration.
- Try different machine learning models and datasets.
- Consider containerizing your application containers with Docker containers for easier deployment.
If you have any questions or need further assistance, feel free to contact us!

Nikhil is an intern consultant at Marktechpost. He is studying for a comprehensive material degree in integrated materials at the Haragpur Indian Technical College. Nikhil is an AI/ML enthusiast and has been studying applications in fields such as biomaterials and biomedical sciences. He has a strong background in materials science, and he is exploring new advancements and creating opportunities for contribution.