CP3501 Deep Learning | Week 3 Practical Workshop
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.
1
Create Hugging Face Account
- Go to
huggingface.co/spaces
- Sign up for free account
- Log in
2
Create New Space
- Click "Create new Space"
- Name:
fastai-lesson2-hello (or your choice)
- Select: Gradio as SDK
- License: Apache 2.0
- Visibility: Public
- Click "Create Space"
3
Create app.py File
- Click "Files" tab
- Click "+ Add file" → "Create a new file"
- Filename:
app.py
- Copy this code:
import gradio as gr
def greet(name):
return "Hello " + name + "!!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
- Click "Commit new file to main"
4
Test Your App
- Wait 1-2 minutes for build to complete
- Your app will appear in the Space
- Type your name in the text box
- Click Submit - you should see "Hello [your name]!!"
✓ SUBMIT: URL of your Hugging Face Space (or screenshot if running locally)
Part A: Train and Export Model
1
Open Training Notebook
- Go to Kaggle
- Copy this notebook:
dmitrykonovalov/fastai-lesson2-part2b-train-save-model-v250528
- Or use your Week 2 cat/dog classifier
2
Add Export Code
- At the end of your training code, add:
learn.export('model.pkl') # Save the trained model
- Run all cells
- Save the notebook
3
Download model.pkl
- Click "Output" tab in Kaggle
- Find
model.pkl
- Download it to your computer
Part B: Deploy the Model
4
Duplicate Example Space
- Go to:
huggingface.co/spaces/dmitryakonovalov/fastai-lesson2-part2-cats-dogs-v2
- Click "⋮" (three dots) in top-right
- Select "Duplicate this Space"
- Give it a new name:
your-username/cat-dog-classifier
5
Upload Your Model
- In your duplicated Space, click "Files"
- Click "+ Add file" → "Upload files"
- Upload your
model.pkl
- Commit changes
6
Fix app.py
- Click on
app.py to edit it
- Find this line:
learn = load_learner('my_model.pkl') # This will give error!
learn = load_learner('model.pkl') # Use your uploaded model
7
Test Your Classifier
- Wait for build to complete (2-3 minutes)
- Upload a cat or dog image
- Check if predictions are reasonable
- Try multiple images
✓ SUBMIT:
- URL of your Kaggle notebook (with model.pkl in outputs)
- URL of your Hugging Face Space
Remember Week 2? You trained a model to recognize grizzly, black, and teddy bears. Now deploy it!
1
Export Your Bear Model
- Open your Week 2 bear classifier notebook
- Add at the end:
learn.export('bear_model.pkl')
- Run and download
bear_model.pkl
2
Duplicate Your Task 2 Space
- Go to your cat-dog classifier Space from Task 2
- Click "⋮" → "Duplicate this Space"
- Name it:
your-username/bear-classifier
3
Replace the Model
- Delete old
model.pkl
- Upload your
bear_model.pkl
- Rename it to
model.pkl
4
Update app.py
- Edit
app.py
- Change title from "Cat Dog Classifier" to "Bear Classifier"
- Change description to mention the three bear types
- Example:
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
- Search Google Images for bears
- Test: grizzly bear, black bear, teddy bear
- Check if predictions match
✓ 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 |
Before You Leave:
- ☐ Task 1: Hello World app working
- ☐ Task 2: Cat/Dog classifier deployed
- ☐ Task 3: Bear classifier deployed
- ☐ All Spaces are set to "Public"
- ☐ Tested each app with multiple images
- ☐ Submitted all URLs to LearnJCU
Bonus Challenges (Optional):
- Add example images to your classifier
- Customize the theme and colors
- Add a README.md with project description
- Deploy a classifier for YOUR own custom dataset
- Share your Space on social media!
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!