FastAI V2: Pip Install Fails in Google Cloud Functions

Hi!

Trying to put a small Tabular Model in the cloud, but the fastai installation is failing.
Typically we are able to install packages by defining the requirements.txt

requirements.txt

...
# Install pyTorch Works
https://download.pytorch.org/whl/cpu/torch-1.7.0%2Bcpu-cp37-cp37m-linux_x86_64.whl # Works
https://download.pytorch.org/whl/cpu/torchvision-0.8.0-cp37-cp37m-linux_x86_64.whl # Works
# Fails
fastai==2.0.18 
# Fails as well even if done separately
fastcore==1.2.5
...

The error thrown is:

Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                               
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: `pip_install_from_wheels` had stderr output:
/opt/python3.7/bin/python3.7: No module named pip

error: `pip_install_from_wheels` returned code: 1; Error ID: ECB5F712

Note that this ONLY occurs if fastai or fastcore is defined in the requirements.txt. Other packages work fine.


Potential Solution
There have been a couple posts saying if pip is listed as a dependancy in the package, that it will cause a failure in a Google Cloud Function.

Is pip listed as a dependency in fastai? And how could it be removed?

I attempted removing from requires.txt and repackaging, but this proved to be difficult… Is there an easy way to do this?


Other Things Tried

There are a couple resources for FastAI v1, but unfortunately falls short in V2:
[https://jianjye.medium.com/how-to-deploy-fast-ai-models-to-google-cloud-functions-for-predictions-e3d73d71546b](https://blog post)

Hello @jasonho28 , I did have similar mistakes with fastai v2 but now it’s working on Cloud Functions.

edit: better version with requests

main.py

from fastai.learner import load_learner
from fastai.vision.core import PILImage
from google.cloud import storage
import requests as r


storage_client = storage.Client()
bucket = storage_client.get_bucket("model-chef-oeuvre")
blob = bucket.blob("model.pkl")
blob.download_to_filename("/tmp/model.pkl")
learner = load_learner("/tmp/model.pkl")


def run(request):

    response = r.get(
        request.args["image"],
        headers={
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        }
    )
    
    # PILImage.create(BytesIO(response.content)

    label, label_idx, _ = learner.predict(PILImage.create(response.content))

    return str(label)

requirements.txt

https://files.pythonhosted.org/packages/e0/c7/1c91a71b413c82cd4c49fb8b6676f6135650cd2cca2745a96bd84a56166c/ipython-7.19.0-py3-none-any.whl
torch==1.7.0+cpu
torchvision==0.8.1+cpu
-f https://download.pytorch.org/whl/torch_stable.html
fastai
google-cloud-storage
requests

1 Like

Thanks! I’ll be interested to try it out.

Fingers crossed!

@aquadzn how did you find the python hosted url by the way?

Hmmm unfortunately did not prove to work :confused:

are you trying to install only fastai ?

Maybe try Python 3.8 runtime for your cloud function
If you install these exacts requirements it should works

https://files.pythonhosted.org/packages/e0/c7/1c91a71b413c82cd4c49fb8b6676f6135650cd2cca2745a96bd84a56166c/ipython-7.19.0-py3-none-any.whl
torch==1.7.0+cpu
torchvision==0.8.1+cpu
-f https://download.pytorch.org/whl/torch_stable.html
fastai