Skip to main content

Command Palette

Search for a command to run...

πŸš€ Getting Started with FastAPI: Installation, File Structure & FastAPI for AI & MLOps Engineers

Published
β€’5 min read
πŸš€ Getting Started with FastAPI: Installation, File Structure & FastAPI for AI & MLOps Engineers
B

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! π—Ÿπ—²π˜'π˜€ π—–π—Όπ—»π—»π—²π—°π˜ I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.

🌐 Introduction

In the world of AI Engineering and MLOps, speed and scalability are everything. Whether you're serving a machine learning model or building APIs for AI-driven services, you need a framework that’s fast, reliable, and developer-friendly.

That’s where FastAPI shines β€” it’s one of the fastest Python web frameworks, built specifically for modern APIs and microservices.

In this blog, we’ll cover:

  • 🧩 What is FastAPI

  • βš™οΈ Installation & setup

  • πŸ“‚ Project structure

  • 🧠 Creating your first API endpoint

  • πŸ§ͺ Running & testing the app


βš™οΈ What is FastAPI?

FastAPI is a modern, high-performance, web framework for building APIs with Python 3.7+ based on standard type hints. It’s built on top of Starlette for async support and Pydantic for data validation.

πŸš€ Key Features:

  • βœ… Fast (as the name says β€” built on ASGI & async I/O)

  • βœ… Automatic docs (Swagger UI & ReDoc)

  • βœ… Data validation using Pydantic

  • βœ… Great for production ML and AI systems

  • βœ… Easy to integrate with Docker, Kubernetes, and CI/CD pipelines


🧰 Installation Guide

Let’s start by setting up FastAPI in your environment.

πŸͺœ Step 1: Create a Virtual Environment

python -m venv venv

Activate it:

  • Windows:

      venv\Scripts\activate
    
  • Mac/Linux:

      source venv/bin/activate
    

πŸͺœ Step 2: Install FastAPI and Uvicorn

pip install fastapi uvicorn

βœ… FastAPI β†’ Main framework
βœ… Uvicorn β†’ ASGI web server for running FastAPI apps


A clean folder structure makes scaling and deploying AI/ML APIs much easier β€” especially for MLOps pipelines.

Here’s a recommended project layout:

fastapi-mlops-app/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── predict.py
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── model.pkl
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   └── config.py
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   └── preprocess.py
β”‚   └── schemas/
β”‚       └── prediction_schema.py
β”‚
β”œβ”€β”€ requirements.txt
└── README.md

πŸ“ Folder Explanation:

  • app/main.py β†’ Entry point of the FastAPI app

  • app/routes/ β†’ Contains different route (API endpoint) files

  • app/models/ β†’ Pre-trained ML/DL model files

  • app/schemas/ β†’ Input/output data validation models (Pydantic)

  • app/core/ β†’ Config files, constants, and environment settings

  • app/utils/ β†’ Helper scripts (data preprocessing, feature extraction, etc.)


🧠 Create Your First FastAPI App

Now, let’s write your first API endpoint using FastAPI.

app/main.py

from fastapi import FastAPI
from pydantic import BaseModel

# Initialize the app
app = FastAPI(title="FastAPI for AI & MLOps")

# Define input data schema
class InputData(BaseModel):
    name: str
    age: int
    country: str

# Root endpoint
@app.get("/")
def read_root():
    return {"message": "Welcome to FastAPI for AI & MLOps Engineers πŸš€"}

# Example POST endpoint
@app.post("/predict")
def predict(data: InputData):
    # Dummy logic - you can replace it with your ML model
    if data.age > 30:
        result = f"{data.name} is likely experienced in AI!"
    else:
        result = f"{data.name} is just getting started in MLOps!"
    return {"result": result}

▢️ Run Your Application

Use Uvicorn to start your FastAPI server:

uvicorn app.main:app --reload
  • app.main:app β†’ path to the FastAPI instance

  • --reload β†’ enables auto-reload on code change (great for development)


🌍 Test Your API

Once the server is running, open your browser and visit:

  • Swagger UI (Interactive API Docs):
    πŸ‘‰ http://127.0.0.1:8000/docs

  • ReDoc (Alternative API Docs):
    πŸ‘‰ http://127.0.0.1:8000/redoc

You’ll see your endpoints automatically documented β€” no extra code needed!


🧠 FastAPI in MLOps Use Cases

FastAPI isn’t just for CRUD APIs β€” it’s a powerful layer for serving and managing machine learning models in production.

Use CaseDescription
🧩 Model DeploymentServe trained ML/DL models as APIs
πŸ“ˆ Model MonitoringIntegrate with Prometheus or Grafana
🧰 CI/CD IntegrationAutomate deployments using Docker, Jenkins, or GitHub Actions
πŸ” Vector SearchBuild semantic search APIs for LLMs
🧠 AIOps/MLOps PipelinesOrchestrate end-to-end pipelines using FastAPI microservices

🧱 Example: Serving an ML Model with FastAPI

Let’s say you have a model file model.pkl. You can serve it easily:

from fastapi import FastAPI
import joblib
from pydantic import BaseModel

app = FastAPI()

model = joblib.load("app/models/model.pkl")

class InputData(BaseModel):
    feature1: float
    feature2: float

@app.post("/predict")
def predict(data: InputData):
    prediction = model.predict([[data.feature1, data.feature2]])
    return {"prediction": float(prediction[0])}

Deploy this API with Docker or Kubernetes β€” and congratulations, you’ve built a production-ready MLOps inference service 🎯


🧩 Next Steps for AI & MLOps Engineers

  1. Containerize your FastAPI app using Docker.

  2. Deploy it on cloud (AWS, Azure, or GCP).

  3. Add monitoring using Prometheus + Grafana.

  4. Integrate CI/CD pipelines for automated deployment.

  5. Version your models using MLflow or DVC.


🏁 Conclusion

FastAPI is not just another Python framework β€” it’s the foundation for AI-driven microservices.
Its performance, simplicity, and automatic documentation make it the perfect choice for AI Engineers, MLOps Engineers, and Data Scientists who want to build scalable APIs fast.

With just a few lines of code, you can go from a Jupyter Notebook model to a fully-deployed production service β€” that’s the real power of FastAPI.


πŸ’¬ Final Thought

β€œIn the MLOps world, FastAPI is the bridge between model experimentation and real-world impact.”

Follow me on LinkedIn

Follow me on GitHub

Keep Learning……