Problems with Render and Flask

Hello, I am trying to run a model I trained with fastAI (classifying churros and samosas) on render. It all works fine locally but not on render.

I have built a simple python server using flask. When running locally it spins up index.html and there I can easily upload an image and let it get classified.

from flask import Flask, request
from flask_cors import CORS
from flask import render_template
from import *

#Labeling function required for load_learner to work
def GetLabel(fileName):
  return fileName.split('_')[0]

learn = load_learner(Path('./export.pkl')) #Import Model
app = Flask(__name__)
cors = CORS(app) #Request will get blocked otherwise on Localhost

def index():
    return render_template("index.html")

@app.route('/predict', methods=['GET', 'POST'])
def predict():
    img = PILImage.create(request.files['file'])
    label,_,probs = learn.predict(img)
    return f'{label} ({torch.max(probs).item()*100:.0f}%)'

if __name__=='__main__':"", port=5001)

url_for('static', filename='style.css')
url_for('static', filename='main.js')

I have uploaded this whole thing to render via a Dockerfile that looks like this:

FROM python:3.6-slim-buster

RUN apt-get update && apt-get install -y git python3-dev gcc \
&& rm -rf /var/lib/apt/lists/*

COPY requirements.txt .

RUN pip install --upgrade -r requirements.txt

COPY ./static .
COPY ./templates .

COPY export.pkl .

RUN python


The thing is that I don’t get any Errors on Render it prints out like it does locally:

Nov 27 05:47:32 PM  #13 [9/9] RUN python
Nov 27 05:47:34 PM  #13 2.753  * Serving Flask app "main" (lazy loading)
Nov 27 05:47:34 PM  #13 2.753  * Environment: production
Nov 27 05:47:34 PM  #13 2.753    WARNING: This is a development server. Do not use it in a production deployment.
Nov 27 05:47:34 PM  #13 2.753    Use a production WSGI server instead.
Nov 27 05:47:34 PM  #13 2.753  * Debug mode: off
Nov 27 05:47:34 PM  #13 2.754 /usr/local/lib/python3.6/site-packages/torch/cuda/ UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
Nov 27 05:47:34 PM  #13 2.754   return torch._C._cuda_getDeviceCount() > 0
Nov 27 05:47:34 PM  #13 2.754  * Running on (Press CTRL+C to quit)

But if I try to reach the URL: it doesn’t work. Not in the browser or any other way of sending HTTP. In the render tutorial they used Gunicorn, but that doesn’t work for me. I have tried any WSGI there is but all for some reason throw this error:

AttributeError: Can't get attribute 'GetLabel' on <module '__main__'

refering to this line

learn = load_learner(Path('./export.pkl')) #Import Model

I have to GetLabel function included in the script and from my understanding it’s necessary as I used it to generate the labels while training. Is it somehow impossible to use a WSGI Framework with a model that has a label function. All examples I could find somehow don’t use one. I am really stuck. Everything works so well locally but putting it on some cloud server always messes something up. I am really confused as how that can be. If I remove the load_learner part from my script, everything works again with Gunicorn or Uvicorn or uwsgi.

Does flask by itself not work on Render? If so why?
Do models created with an explicit label function not work with Gunicorn? If so why?

1 Like