App 5: Ship It (Deploy Your AI)
Put your AI model on the internet. Build a web app with Gradio and deploy it for free.
Ship It
You have built an engine, a GPT, a classifier, and a text generator. They all live on your laptop. Nobody can use them.
Time to fix that. We are putting your AI on the internet.

The Tool: Gradio
Gradio is the laziest way to turn a Python function into a web app. Install it, wrap your function, done.
# pip install gradio
import gradio as grExample 1: Deploy the Image Classifier
Remember the classifier from App 3? Let's make it a web app:
import torch
import torch.nn as nn
import torch.nn.functional as F
from PIL import Image
from torchvision import transforms
# Define the model (same as App 3)
class ImageClassifier(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.dropout = nn.Dropout(0.25)
self.fc1 = nn.Linear(128 * 4 * 4, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = self.dropout(x)
x = x.view(-1, 128 * 4 * 4)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
# Load trained model
classes = ('airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
model = ImageClassifier()
model.load_state_dict(torch.load('classifier.pth', map_location='cpu'))
model.eval()
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
def classify(image):
img_tensor = transform(image).unsqueeze(0)
with torch.no_grad():
output = model(img_tensor)
probs = F.softmax(output, dim=1)[0]
return {classes[i]: float(probs[i]) for i in range(10)}
# Launch!
demo = gr.Interface(
fn=classify,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
title="Is It a Cat?",
description="Upload an image. The AI will guess what it is.",
)
demo.launch()Run that. A browser tab opens. Drag in a photo. See the prediction.
Example 2: Deploy the Text Generator
from transformers import GPT2LMHeadModel, GPT2Tokenizer
model_name = "./pirate-gpt" # your fine-tuned model from App 4
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
def generate(prompt, temperature=0.8, max_length=150):
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(
inputs,
max_length=max_length,
temperature=temperature,
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
demo = gr.Interface(
fn=generate,
inputs=[
gr.Textbox(label="Prompt", placeholder="The captain looked at..."),
gr.Slider(0.1, 2.0, value=0.8, label="Temperature (creativity)"),
gr.Slider(50, 300, value=150, step=10, label="Max length"),
],
outputs=gr.Textbox(label="Generated Text"),
title="Pirate GPT",
description="A GPT-2 fine-tuned on pirate speak. Enter a prompt and see what happens.",
)
demo.launch()Deploy to the Internet (Free)
Gradio apps can be shared instantly:
Option 1: Temporary Link (5 seconds)
demo.launch(share=True)
# Prints: Running on public URL: https://xxxxx.gradio.liveThis gives you a public URL that works for 72 hours. Share it with friends immediately.
Option 2: HuggingFace Spaces (Permanent, Free)
- Create an account at huggingface.co
- Create a new Space (choose Gradio as the SDK)
- Push your code:
# Install the CLI
pip install huggingface_hub
# Login
huggingface-cli login
# Create your space
huggingface-cli repo create my-ai-app --type space --space-sdk gradio
# Clone and push
git clone https://huggingface.co/spaces/YOUR_USERNAME/my-ai-app
cd my-ai-app
# Add your files:
# - app.py (your Gradio code)
# - requirements.txt (torch, gradio, transformers, etc.)
# - classifier.pth (your model weights, if using the classifier)
git add .
git commit -m "Deploy my AI app"
git pushYour app is now live at https://huggingface.co/spaces/YOUR_USERNAME/my-ai-app. Forever. For free.
The Full Stack
Here is everything you built in this book, from bottom to top:
┌─────────────────────────────────────────┐
│ Web App (Gradio) │ ← App 5: You are here
├─────────────────────────────────────────┤
│ Text Generator │ Image Classifier │ ← App 3 & 4
├─────────────────────────────────────────┤
│ GPT / Transformer │ ← App 2
├─────────────────────────────────────────┤
│ Autograd / Backpropagation │ ← App 1
├─────────────────────────────────────────┤
│ 13 ML Algorithms (Part 2) │ ← ML 1-13
├─────────────────────────────────────────┤
│ Python (Part 1) │ ← Python 1.1-1.4
└─────────────────────────────────────────┘
You went from x = 5 to deploying a GPT on the internet.
Not bad for an idiot.