Beginner: Using Colab or Kaggle ✅

@mike.moloch, thanks, this looks like a useful approach, looking into it.

@kurianbenoy, thats awesomesauce! even better than I was looking for.

[How to] deploy Kaggle trained model on Hugging Face

For my own later reference, and maybe help others unfamiliar with git,
here is my action log…

  1. Created a new space at hugging space

  2. Cloned that space to my local machine.

$ git clone https://huggingface.co/spaces/bencoman/cats-v-dogs
$ cd cats-v-dogs
  1. Added Git Large File Storage.
# This needs to do this once per machine
$ curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
$ sudo apt-get install git-lfs

# This needs to be done once per repo
$ git lfs install
$ git lfs track "*.pkl"
$ git add .gitattributes
$ git commit -m "added git large file storage"
  1. Copied random test pics dog.jpg and cat.jpg files into this folder

  2. Edited the application code app.py as follows…

from fastai.vision.all import *
import gradio as gr

def is_cat(x): return x[0].isupper()

def classify_image(img):
    pred,idx,probs = learn.predict(img)
    return( dict(zip(categories, map(float,probs)))

learn = load_learner('model.pkl')
categories = ('Dog', 'Cat')
image = gr.inputs.Image(shape=(192,192))
label = gr.outputs.Label()
examples = [ 'dog.jpg', 'cat.jpg' ]
iface = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
iface.launch(inline=False)
  1. Confirmed the trained model.pkl file was available here…
    https://www.kaggle.com/code/bencoman/cats-v-dogs-saving-a-basic-fastai-model/data. Note: it took me a while to realise this file was disappearing because I was doing a Quick Save rather than Save And Run All.

  2. Downloaded and installed API key in kaggle.json file created per API Credentials section at https://github.com/Kaggle/kaggle-api#api-credentials

  3. Reviewed kaggle list and output examples, then downloaded model…

$ pip install kaggle

# Downloaded API key to ~/.kaggle/kaggle.json

$ kaggle list kernels --mine | grep -i cat
code/bencoman/cats-v-dogs-inference-gradio                      Cats v Dogs - inference gradio                     Ben Coman  2022-05-08 13:39:35           0
code/bencoman/cats-v-dogs-saving-a-basic-fastai-model           Cats v Dogs - saving a basic fastai model          Ben Coman  2022-05-08 16:02:11           0

$ ls
README.md  app.py  cat.jpg   dog.jpg

$ kaggle kernels output bencoman/cats-v-dogs-saving-a-basic-fastai-model --wp

$ ls
README.md  app.py  cat.jpg   dog.jpg  
cats-v-dogs-saving-a-basic-fastai-model.log  model.pkl
  1. Pushed to hugging-space
$ git add -A
$ git status
        new file:   app.py
        new file:   cat.jpg
        new file:   cats-v-dogs-saving-a-basic-fastai-model.log
        new file:   dog.jpg
        new file:   model.pkl
$ git commit -m "First app & model"
$ git push
Username for 'https://huggingface.co': bencoman
Password for 'https://bencoman@huggingface.co': ******
Uploading LFS objects:   0% (0/1), 22 MB | 113 KB/s                                                                                 
Uploading LFS objects: 100% (1/1), 47 MB | 114 KB/s, done.
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 12 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 65.79 KiB | 21.93 MiB/s, done.
Total 10 (delta 2), reused 0 (delta 0)
remote: Enforcing permissions...
remote: Allowed refs: all
To https://huggingface.co/spaces/bencoman/cats-v-dogs
   ae86db9..813dfce  main -> main  
  1. Tested application by clicking…
    https://huggingface.co/spaces/bencoman/cats-v-dogs

Note: After completing this local jupyter from scratch setup,
the app can be run locally by creating a new notebook applocal.ipynb
next to app.py and copying the contents of app.py into the notebook.

9 Likes