Invoking a Canvas-generated endpoint via Python script in my desktop

0

I generated a job in Canvas via CSV file. After deploying, an endpoint was generated.

end point

end point

Now I am trying to generate predictions via a Python script on my desktop. The script is as follows:

import requests
import pandas as pd
import json
import os

# Definir o endpoint do Canvas
endpoint = "https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/canvas-new-deployment-03-12-2024-4-51-PM/invocations"

# Carregar o arquivo CSV
data = pd.read_csv("C:\\Users\\Druida\\Desktop\\dados_binance.csv")

# Converter o DataFrame para JSON
data_json = data.to_json(orient="records")

# Obter o token de autenticação
token = os.environ.get("canvas-new-deployment-03-12-2024-4-51-PM")

# Criar a requisição
headers = {
    "Content-Type": "application/json",
    "X-Amzn-SageMaker-Custom-Auth": token
}
body = data_json
response = requests.post(endpoint, headers=headers, data=body)

# Tratar a resposta
if response.status_code == 200:
    prediction = json.loads(response.content)
    print(prediction)
else:
    print(f"Erro: {response.status_code}")
    print(response.content)

However, instead of the prediction, I get the following message regarding the token:

C:\Users\Druida\PycharmProjects\pythonProject.venv\Scripts\python.exe "C:\Users\Druida\PycharmProjects\pythonProject\regressão linear.py" Erro: 403 b'{"message":"Missing Authentication Token"}'

Process finished with exit code 0

Please help me because I can't generate the predictions.

Thais
asked 5 months ago658 views
1 Answer
0

This error occurs because the authentication token is missing from the script. Here are a couple of ways to resolve this issue:

  1. Retrieve the auth token from the .aws/credentials file.
  2. Call the endpoint using the AWS SDK and run the program after logging in with the AWS CLI.

In the provided code, the token is being retrieved from the environment variable "canvas-new-deployment-03-12-2024-4-51-PM." Is this intentional, or could it be a typo?

AWS
answered 2 months ago