Week 3 Practical: Deploy Your Deep Learning Models

Today's Goal: Take your trained models and deploy them as web applications that anyone can use!

⚠️ Known Issues & Options

There are version compatibility issues between Hugging Face and Kaggle/Colab. If you encounter errors:

Option 1 (Recommended): Follow tasks below using Hugging Face Spaces
Option 2: Run everything on Kaggle (see example: dmitrykonovalov/fastai22-lesson2-prac3-app-250605)
Option 3: Install Gradio locally on your laptop (ask ChatGPT: "how to install and run gradio locally on windows-11")

Note: Hugging Face may be blocked on JCU network. Use personal hotspot if needed.

TASK 1: Your First Gradio App (Hello World)
1 Create Hugging Face Account
2 Create New Space
3 Create app.py File
import gradio as gr

def greet(name):
    return "Hello " + name + "!!"

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
4 Test Your App
✓ SUBMIT: URL of your Hugging Face Space (or screenshot if running locally)
TASK 2: Deploy a Cat vs Dog Classifier

Part A: Train and Export Model

1 Open Training Notebook
2 Add Export Code
learn.export('model.pkl')  # Save the trained model
3 Download model.pkl

Part B: Deploy the Model

4 Duplicate Example Space
5 Upload Your Model
6 Fix app.py
learn = load_learner('my_model.pkl')  # This will give error!
learn = load_learner('model.pkl')  # Use your uploaded model
7 Test Your Classifier
✓ SUBMIT:
  1. URL of your Kaggle notebook (with model.pkl in outputs)
  2. URL of your Hugging Face Space
TASK 3: Deploy Your Three-Bear Classifier
Remember Week 2? You trained a model to recognize grizzly, black, and teddy bears. Now deploy it!
1 Export Your Bear Model
2 Duplicate Your Task 2 Space
3 Replace the Model
4 Update app.py
demo = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(),
    outputs=gr.Label(num_top_classes=3),
    title="🐻 Bear Classifier",
    description="Upload a bear image to classify: Grizzly, Black, or Teddy Bear"
)
5 Test with Bear Images
✓ SUBMIT: URL of your Bear Classifier Space

Common Errors & Solutions

Error Solution
FileNotFoundError: model.pkl Make sure model.pkl is uploaded AND filename in app.py matches exactly
ModuleNotFoundError: No module named 'fastai' Create requirements.txt with:
fastai
gradio
Build keeps failing Check Logs tab for specific error. Try Option 2 (Kaggle) or Option 3 (local)
Space shows blank/loading forever Check if HF is blocked on network. Try personal hotspot or home internet
Version mismatch errors Pin versions in requirements.txt:
fastai==2.7.14
gradio==4.0.0
✓ Final Checklist

Before You Leave:

Bonus Challenges (Optional):

Need Help? Ask your instructor or classmates. Check the Logs tab in your Space for error details. Remember: everyone encounters errors when deploying - it's part of learning!