deploymentendpointautomlazure-auto-ml

I am having forbidden error (Error 403) when calling Azure AutoML endpoint from Flask


I'm working on a Flask application that utilizes an Azure AutoML model to make predictions based on user input. I've set up the necessary code and endpoints but am encountering a specific error when making a POST request to the AutoML endpoint. I have also tried using postman to test the endpoint and I am getting the same error

When I try to make a POST request to the Azure AutoML endpoint, I receive the following error:

this is the code I am used

from flask import Flask, render_template, request, jsonify
import requests
import json

app = Flask(__name__)

# Define the home route
@app.route('/')
def home():
    return render_template('index.html')

# Define the predict route (POST method)
@app.route('/predict', methods=['POST'])
def predict():
    # Collect data from the form
    data = {
        "age": int(request.form['age']),
        "job": request.form['job'],
        "marital": request.form['marital'],
        "education": request.form['education'],
        "default": request.form['default'],
        "housing": request.form['housing'],
        "loan": request.form['loan'],
        "contact": request.form['contact'],
        "month": request.form['month'],
        "day_of_week": request.form['day_of_week'],
        "duration": int(request.form['duration']),
        "campaign": int(request.form['campaign']),
        "pdays": int(request.form['pdays']),
        "previous": int(request.form['previous']),
        "poutcome": request.form['poutcome'],
        "emp.var.rate": float(request.form['emp_var_rate']),
        "cons.price.idx": float(request.form['cons_price_idx']),
        "cons.conf.idx": float(request.form['cons_conf_idx']),
        "euribor3m": float(request.form['euribor3m']),
        "nr.employed": float(request.form['nr_employed'])
    }
    
    # Format the data for the AutoML endpoint
    input_data = {
        "input_data": {
            "columns": [
                "age", "job", "marital", "education", "default", "housing", "loan", "contact",
                "month", "day_of_week", "duration", "campaign", "pdays", "previous", "poutcome",
                "emp.var.rate", "cons.price.idx", "cons.conf.idx", "euribor3m", "nr.employed"
            ],
            "index": [0],
            "data": [[
                data['age'], data['job'], data['marital'], data['education'], data['default'],
                data['housing'], data['loan'], data['contact'], data['month'], data['day_of_week'],
                data['duration'], data['campaign'], data['pdays'], data['previous'], data['poutcome'],
                data['emp.var.rate'], data['cons.price.idx'], data['cons.conf.idx'], data['euribor3m'],
                data['nr.employed']
            ]]
        }
    }
    
    # Send the request to the Azure AutoML endpoint with the Authorization header
    headers = {
        'Content-Type': 'application/json',
        'Authorization': ' xxxxxxxx'  
    }
    
    automl_endpoint_url = 'https://bankmarketing-09241401697583.eastus.inference.ml.azure.com/score'  
    response = requests.post(automl_endpoint_url, headers=headers, json=input_data)
    
    # Parse the response from the AutoML model
    if response.status_code == 200:
        prediction = response.json()
        return jsonify(prediction)
    else:
        return jsonify({"error": "Error in prediction", "status_code": response.status_code})

if __name__ == '__main__':
    app.run(debug=True)

This is the output below

{
    "error": "Error in prediction",
    "status_code": 403
}

Solution

  • You can check here how to authenticate the online endpoint.

    You need to passing authentication header as bearer token.

    headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
    

    Also, if you are using self-signed certificate in your scoring service then bypass it with below code.

    import ssl
    
    def allowSelfSignedHttps(allowed):
        if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
            ssl._create_default_https_context = ssl._create_unverified_context
    
    allowSelfSignedHttps(True)
    

    This should help you for authenticating the endpoints.