Slide 1

Slide 1 text

@weskambale | kambale.dev Serving Machine Learning Models in Django with FastAPI Wesley Kambale

Slide 2

Slide 2 text

• Machine Learning Engineer • Community Builder • Explore ML Facilitator with Crowdsource by Google • Consultant at The Innovation Village • Google Dev Library Contributor Profile Interests Experience • Research in TinyML, TTS & LLM

Slide 3

Slide 3 text

The agenda to agend… ● Introduction & Overview ● Setting Up the Django App ● Building the FastAPI API ● Integrating Django and FastAPI ● Deploying Django and FastAPI ● Q&A

Slide 4

Slide 4 text

The repo…

Slide 5

Slide 5 text

The model…

Slide 6

Slide 6 text

Why? What is the story?

Slide 7

Slide 7 text

Introduction to ML Model Serving What is Model Serving? ● Model serving is how machine learning models are deployed to make predictions in production. ● We’ll use Django for the app framework and FastAPI for serving the model via APIs. Goal: Build an app that detects mango damage using a TensorFlow model with Django and FastAPI.

Slide 8

Slide 8 text

Tools and Frameworks What tools and frameworks can we use? Django: A high-level Python web framework. FastAPI: A fast web framework to serve APIs, especially for machine learning models. TensorFlow: Our deep learning framework for building and serving the model. Uvicorn: ASGI server to run FastAPI apps.

Slide 9

Slide 9 text

├── mango_app 
 │ ├── __init__.py
 │ ├── views.py
 ├── fastapi_app
 │ ├── __init__.py
 │ ├── api.py
 ├── model 
 │ ├── mango_model.h5
 ├── main/templates 
 │ ├── index.html
 └── manage.py
 Project Directory Layout

Slide 10

Slide 10 text

Setting Up Django Web App How do I setup a Django application? Initialize a Django project and create an app. Set up views and templates to handle user interaction.

Slide 11

Slide 11 text

# views.py
 from django.shortcuts import render
 def index(request):
 return render(request, 'index.html') 
 
 # index.html
 
 
 Detect 
 
 


Slide 12

Slide 12 text

FastAPI API Setup How do I setup a FastAPI? Set up FastAPI to load the model and serve the predictions via an endpoint. FastAPI handles asynchronous requests well, making it ideal for serving models.

Slide 13

Slide 13 text

from fastapi import FastAPI, File, UploadFile
 import tensorflow as tf
 from PIL import Image
 import numpy as np
 
 app = FastAPI()
 
 model = tf.keras.models.load_model('model/mango_model.h5') 
 
 @app.post("/predict/") 
 async def predict(file: UploadFile = File(...)):
 image = Image.open(file.file)
 img_array = np.array(image.resize((224, 224))) / 255.0
 predictions = model.predict(np.expand_dims(img_array, 0))
 return {"predictions": predictions.tolist()}


Slide 14

Slide 14 text

Integrating Django with FastAPI How do I integrate FastAPI and Django? Django handles the web interface, while FastAPI serves the model. Use Uvicorn to run the FastAPI server and route requests from Django. # Bash
 uvicorn fastapi_app.api:app --reload --port 8001
 
 


Slide 15

Slide 15 text

# Call FastAPI
 
 import requests
 from django.shortcuts import render
 
 def detect(request): 
 if request.method == 'POST':
 image = request.FILES['mango_image']
 response = requests.post('http://127.0.0.1:8001/predict/', files={'file': image})
 predictions = response.json()['predictions'] 
 return render(request, 'index.html', {'predictions': predictions})
 
 


Slide 16

Slide 16 text

The product…

Slide 17

Slide 17 text

Read more… Code: https://github.com/wkambale/mango_app Model: https://bit.ly/pyconmodelh5 Notebook: https://bit.ly/vgg19-model

Slide 18

Slide 18 text

Wesley Kambale @weskambale kambale.dev Me da wo ase! Any questions?