Loading a pickle in flask

Hello!! I am trying to load a pickle file into my flask app (python) because I am trying to make predictions with my model. When executing this code local it woks perfect and loads the file. When deploying it to render it does not load the file: it returns “Error”.
The pickle file is in GitHub, the path is correct, because it doesn’t return a FileNotFoundError. I am not really sure if the problem is that render cannot load a pickle file? When loading a txt the same way it works.
I hope someone could help me, thank you!!

import pandas as pd
import numpy as np
from flask import Flask, request, jsonify, abort, make_response
from flask_cors import CORS
from dotenv import load_dotenv
import os
import pickle
from datetime import date

app = Flask(name)
app.config[‘DEBUG’] = True
CORS(app, support_credentials=True)

def load_object(filename):
with open(filename ,‘rb’) as f:
loaded = pickle.load(f)
return loaded

@app.route(‘/retrain’, methods=[‘GET’])
def prueba():
try:
model=load_object(“/opt/render/project/src/api/JP_12_06_VotingRegressor.pickle”)
return “Done”
except FileNotFoundError:
return “File not found”
except:
return “Error”

if name == ‘main’:
app.run(host=‘0.0.0.0’, port=5000)

from mysql.connector import pooling
import pandas as pd
import numpy as np
from flask import Flask, request, jsonify, abort, make_response
from flask_cors import CORS
from dotenv import load_dotenv
import os
import pickle
from datetime import date

app = Flask(__name__)
app.config['DEBUG'] = True
CORS(app, support_credentials=True)


def load_object(filename):
    with open(filename ,'rb') as f:
        loaded = pickle.load(f)
        return loaded


@app.route('/retrain', methods=['GET'])
def prueba():
    try:
        model=load_object("/opt/render/project/src/api/JP_12_06_VotingRegressor.pickle")
        return "Done"
    except FileNotFoundError:
        return "File not found"
    except:
        return "Error"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Hi there,

The first thing to check regarding these kind of questions is the service and deploy logs that can be found in the Dashboard. Make sure all application errors are resolved. If the problem persists after that, always feel free to reach out to us directly from the Dashboard about a specific service.

Regards,
Mike


Render Support Engineer, MT (UTC-6)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.