⤵️ Quick shortcut: Select the module you’re currently studying ⤵️
Courses Module 1 Module 2 Module 3 Module 4 Module 5 Module 6
Lesson 1:
Key principles of API design for AI applications.
Structuring API endpoints and handling errors.
Lesson 2:
Benefits of containerization in application deployment.
Best practices for writing Dockerfiles.
Lesson 3:
Tools and platforms for deploying AI applications.
Ensuring scalability and reliability in production environments.
Objective
Build, containerize, and deploy the Jarvid API pipeline as a production-grade application. Ensure the API is scalable and documented with clear instructions for setup and deployment.
1.1 Application Structure
Directory Structure:
plaintext
Copy code
jarvid-api/
├── app/
│ ├── main.py
├── requirements.txt
├── Dockerfile
├── serverless.yml
├── README.md
1.2 API Code
File: app/main.py
python
Copy code
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Welcome to the Jarvid API"}
@app.post("/generate-embedding")
def generate_embedding(text: str):
if not text:
raise HTTPException(status_code=400, detail="Text input is required")
# Placeholder for embedding logic
return {"embedding": [0.1, 0.2, 0.3]}
@app.post("/search-similarity")
def search_similarity(embedding: list, top_k: int = 5):
if not embedding:
raise HTTPException(status_code=400, detail="Embedding input is required")
# Placeholder for similarity search logic
return {"results": [{"id": 1, "name": "Alice", "similarity": 0.95}]}
File: requirements.txt
plaintext
Copy code
fastapi
uvicorn
2.1 Dockerfile
File: Dockerfile
dockerfile
Copy code
# Use an official Python runtime as the base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy application files
COPY requirements.txt .
COPY app/ ./app
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose the application port
EXPOSE 8000
# Define the entry point
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
2.2 Build and Test Docker Image
Build the Docker image:
bash
Copy code
docker build -t jarvid-api .
Run the Docker container:
bash
Copy code
docker run -p 8000:8000 jarvid-api
Test the API:
Access http://localhost:8000/docs for interactive API documentation.
Test endpoints with curl or Postman.
3.1 Configure Serverless Framework
File: serverless.yml
yaml
Copy code
service: jarvid-api
provider:
name: aws
runtime: python3.9
region: us-east-1
functions:
api:
image:
name: jarvid-api
uri: <your_ecr_repository_uri>
3.2 Push Docker Image to AWS ECR
Tag the Docker image:
bash
Copy code
docker tag jarvid-api:latest <your_ecr_repository_uri>:latest
Push the Docker image:
bash
Copy code
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <your_ecr_repository_uri>
docker push <your_ecr_repository_uri>:latest
3.3 Deploy the Application
Deploy with Serverless Framework:
bash
Copy code
serverless deploy
Retrieve API Gateway URL:
Use the output from serverless deploy to find the API endpoint.
File: README.md
markdown
Copy code
# Jarvid API Deployment Guide
## Overview
This guide describes the steps to build, containerize, and deploy the Jarvid API.
### API Endpoints
- `GET /`: Returns a welcome message.
- `POST /generate-embedding`: Generates an embedding for a given text.
- `POST /search-similarity`: Searches for similar items based on an embedding.
---
## Docker Setup
1. Build the Docker image:
```bash
docker build -t jarvid-api .
Run the Docker container:
bash
Copy code
docker run -p 8000:8000 jarvid-api
Tag the Docker image:
bash
Copy code
docker tag jarvid-api:latest <your_ecr_repository_uri>:latest
Push the image to ECR:
bash
Copy code
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <your_ecr_repository_uri>
docker push <your_ecr_repository_uri>:latest
Deploy using Serverless Framework:
bash
Copy code
serverless deploy
Test the deployed API:
Root endpoint:
bash
Copy code
curl <api_gateway_url>/
Health check endpoint:
bash
Copy code
curl <api_gateway_url>/health
Use AWS CloudWatch for monitoring and debugging.
View logs:
bash
Copy code
serverless logs -f api
markdown
Copy code
---
### **Expected Outcomes**
1. **Functional API:**
- The Jarvid API is containerized and includes endpoints for embedding generation and similarity search.
2. **Deployed and Scalable:**
- The API is deployed to AWS Lambda using the Serverless Framework, ensuring scalability.
3. **Comprehensive Documentation:**
- Clear setup and deployment instructions are included in the `README.md`.
Objective
By the end of Module 5, you will have built and deployed the entire Jarvid pipeline as a production-grade API, ensuring it is containerized and scalable.
Expected Outcomes
A Flask/FastAPI application with endpoints for the Jarvid pipeline.
A Dockerized version of the application, ready for deployment.
A deployed API running on AWS Lambda or a similar platform.
Documentation detailing the API structure, Docker setup, and deployment steps.
Learning Outcomes
After completing this module, you will:
Understand the principles of API design for AI applications.
Gain hands-on experience containerizing applications using Docker.
Learn to deploy and scale applications in a cloud environment.
By completing Module 5, you'll have the skills to deploy AI applications in real-world settings, ensuring they are scalable, reliable, and easy to maintain. This module sets the foundation for building robust, production-ready AI solutions