Deploying Machine Learning Model in Flask to Render

Hello, I’m having difficulty in deploying my Flask script in Render. this is the code.

model.py
from flask import Flask, request, jsonify
import joblib
import numpy as np

app = Flask(name)

Load the trained model

model = joblib.load(‘SVR_1.joblib’)

@app.route(‘/predict’, methods=[‘POST’])
def predict():
try:
# Get input data from the request
data = request.get_json(force=True)
input_data = np.array(data[‘features’])

    # Reshape the 1D array to a 2D array
    input_data_2d = input_data.reshape(1, -1)

    # Make predictions using the loaded model
    prediction = model.predict(input_data_2d)

    # Return the prediction as JSON
    return jsonify({"prediction": prediction.tolist()})

except Exception as e:
    return jsonify({"error": str(e)})

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

build.sh:
pip install --upgrade pip
pip install -r requirements.txt

requirements.txt:
Flask
Gunicorn
Numpy
Joblib
sklearn

build command:
./build.sh

start command:
python model.py

i keep having errors like this:

Traceback (most recent call last):

File “/opt/render/project/src/model.py”, line 8, in
model = joblib.load(‘SVR_1.joblib’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/render/project/src/.venv/lib/python3.11/site-packages/joblib/numpy_pickle.py”, line 658, in load
obj = _unpickle(fobj, filename, mmap_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/render/project/src/.venv/lib/python3.11/site-packages/joblib/numpy_pickle.py”, line 577, in _unpickle
obj = unpickler.load()
^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/pickle.py”, line 1213, in load
dispatchkey[0]

File “/usr/local/lib/python3.11/pickle.py”, line 1538, in load_stack_global

self.append(self.find_class(module, name))

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/pickle.py”, line 1580, in find_class
import(module, level=0)
ModuleNotFoundError: No module named ‘sklearn’

thanks in advance!

Hi,

The main error appears to be:

ModuleNotFoundError: No module named ‘sklearn’

I’m not a Python expert, but searching for that error seems to bring up plenty of resources with the same issue and suggested resolutions.

Maybe you’ve using the old package? https://pypi.org/project/sklearn/

Alan

1 Like

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