Deployment Platform: Render ✅

Hey guys! I’m currently prepping a deployment workshop/teaching for my Fast.AI study group at my University, and I’m revisiting the Render methodology as a baseline to utilize Starlette. While looking over the source code, I’m noticing that classes is never utilized in ‘serve.py’. Is it being used elsewhere? Or never utilized at all? Thanks!

One other question, if we want to upload and analyze a CSV document, how do I set it up so it’s readable? For example, my customer sends a CSV full of URLs and I want to download them all, so my analyze function looks like so:

@app.route('/analyze', methods=['POST'])
async def analyze(request):
    data = await request.form()
    content = data['content']
    mkdir('Downloaded_Images')
    download_images(content, 'Downloaded_Images')
    path2 = Path('Downloaded_Images')
    data = ImageList.from_folder(path)
    learn = load_learner(path, export_file_name, test=data)
    y, _ = learn.get_preds(DatasetType.Test)
    y = torch.argmax(y, dim=1)
    preds = [learn.data.classes[int(x)] for x in y]
    resultsFile = open('results.csv', 'wb')
    wr = csv.writer(resultsFile)
    wr.writerows([preds])
    return FileResponse('results.csv')

I know that currently it does not work, as I get a ‘KeyError: ‘content’’ but I am unsure where to go from here. Any advice? Thanks :slight_smile:

I’d log the contents of your XMLHttprequest to the console in the browser to check what’s being sent to the server and perhaps use the debugger on the server side to inspect what ‘data’ looks like also

1 Like

@OllieG, thank you for the reply :slight_smile: I’m very new to web-development so I’m a little bit lost there. I tried looking at the console and it says there was an error in the client.js (something I didn’t think to change)

var el = x => document.getElementById(x);

function showPicker() {
  el("file-input").click();
}

function showPicked(input) {
  el("upload-label").innerHTML = input.files[0].name;
  var reader = new FileReader();
  reader.onload = function(e) {
    el("image-picked").src = e.target.result;
    el("image-picked").className = "";
  };
  reader.readAsDataURL(input.files[0]);
}

function analyze() {
  var uploadFiles = el("file-input").files;
  if (uploadFiles.length !== 1) alert("Please select a file to analyze!");

  el("analyze-button").innerHTML = "Analyzing...";
  var xhr = new XMLHttpRequest();
  var loc = window.location;
  xhr.open("POST", `${loc.protocol}//${loc.hostname}:${loc.port}/analyze`,
    true);
  xhr.onerror = function() {
    alert(xhr.responseText);
  };
  xhr.onload = function(e) {
    if (this.readyState === 4) {
      var response = JSON.parse(e.target.responseText);
      el("result-label").innerHTML = `Result = ${response["result"]}`;
    }
    el("analyze-button").innerHTML = "Analyze";
  };

  var fileData = new FormData();
  fileData.append("file", uploadFiles[0]);
  xhr.send(fileData);
}

I think I need to adjust how fileData is being made? Or what do I need to adjust from here? Thanks :slight_smile:

Edit: I changed fileData to accept a CSS,

fileData = new CommaSeparatedStrings()

but I’m unsure where to go from here.

Edit x2: Got it working! For anyone wanting to read csv documents to do whatever (my long-term goal was to read a csv of customer data for a tabular model) you need to do the fileData declaration, and in the analyze function (or prepData if you want) do the following:

data = await request.form()
content = await (data['file'].read())
s = str(content, 'utf-8')
data = StringIO(s)
content = pd.read_csv(data)

Hope this can help others :slight_smile:

2 Likes

Thank You @mrfabulous1


i am unable to deploy my app.it is saying gunicorn command not found.i tried keeping gunicorn in requirements.txt after that also its popping up the same error

Hi harshasatyavardhan
Have you tried adding

RUN pip install gunicorn

After the requirements line in the file called “Dockerfile” located in the repository.

Cheers mrfabulous1 :smiley::smiley:

Worked well thanks

Hi Zachery I have also looked and the classes list and it is definitely not a requirement for the app to work.
It is my understanding that it is there for the following reasons.

  1. To enable class names to be used in any app you develop more easily.
  2. As a reference to make it easier to remember what classes were used in your app when you look back at the software in a years time.

If it were not there you would have to extract them from the .pkl file.

Cheers mrfabulous1 :smiley::smiley:

1 Like

Thanks mrfabulous! Yeah I realized I think it’s being used for the website (I saw it referenced in the javascript I believe). For anyone interested, at my campus I gave that talk on deployment and went over a few different models. Here’s the notebook: (The front end is awful, I am not a front-end dev so just focus on how the back-end is operating)
notebook

1 Like

Hi Zachery thanks for sharing your notebook!

mrfabulous1 :smiley::smiley:

No problem! Do note it’s a general guide on how to structure the inference and data collection. Don’t run the notebook :slight_smile: (I’ve just gotten addicted to writing notes this way)

It does work, but depending on the plan you select it can take a REALLY long time to process a large image due to the low ram (1GB) and number cpus (1) of the lowest plans.

If you want more speed I’d look at increasing the ram and cpu of the instance, or hosting on a service which provides a gpu

I am also having this issue. I developed my model locally on my desktop PC in Jupyter.
Anyone come up with a solution yet?

Hi Matt this problem took me ages to fix, and I didn’t get a reply to this question.

I had to install uvicord 0.7.1 however I was unable to install it using Anaconda.
So from anaconda I had to:

  1. Open the terminal
  2. pip install uvicorn

When I built my bear classifier it was version 0.7.1 its now 0.8.4

Hope this helps

mrfabulous1 :smiley::smiley:

Hi mrfabulous1,
Thanks for your update. If I am interpreting what you saying correctly, I need to:

  1. Pip install uvicorn locally on my desktop
  2. Re-run my model training after installing uvicorn
  3. Upload the new exported .pkl file
  4. Deploy on Render.

Is that the fix that worked for you?

Hi Matt that is correct if.
Once you get it working on your desktop the fun begins when you deploy it.

The render guide is good however it is likely you may have a few issues deploying it. So check the posts on this thread.

On point 3 your library versions on your laptop may be different to the repository. So you may have to change the requirements.txt when you deploy the code on render.com. See the threads which contain !pip list for an explanation.

Cheers mrfabulous1:smiley::smiley:

1 Like

If want to host you model on Google Drive and still have the problem of not being able to download the large, this is the code I´ve used on server.py, it replaces the function download_file():

Note that now instead of the url, you just pass the id of the file:

model_file_id = '19xqtsyusFddcSkdCm1hlhW3y6ljGck5L'
def download_file_from_google_drive(id, destination):
    if destination.exists():
        return
    URL = "https://docs.google.com/uc?export=download"
    session = requests.Session()
    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)
    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)
    save_response_content(response, destination)

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value
    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768
    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

async def setup_learner():
    download_file_from_google_drive(model_file_id, path/'models'/f'{model_file_name}.pth')
    data_bunch = ImageDataBunch.single_from_classes(path, classes,
        ds_tfms=get_transforms(), size=224).normalize(imagenet_stats)
    learn = cnn_learner(data_bunch, models.resnet34, pretrained=False)
    learn.load(model_file_name)
    return learn

Did Render start charging fastai students? I noticed an email telling me to add a credit card and also an unpaid bill.

Yes, Render has always charged for compute, but a few months ago we added $25 in credits to new accounts. I believe you got an email because your credits ran out.

1 Like