Hey guys, I'm trying to upload images to

Hey guys, I'm trying to upload images to Workers AI REST API with Python, but I'm getting error messages. Any tips?
2 Replies
Danny (Takodan)
Danny (Takodan)OP3mo ago
def read_image_binary(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")

def read_image_array(image_path):
with Image.open(image_path) as image_file:
image = image_file.convert('L')
image_array = np.array(image)
image_array_flat = image_array.flatten().tolist()
return image_array_flat

def cat_prompt_only(model, image_data):
print(type(image_data))
# post
response = requests.post(
f"{API_BASE_URL}{model}",
headers = {"Authorization": f"Bearer {AUTH_TOKEN}"},
json = {
"prompt": "What's the animal in the picture?",
"image": image_data,
}
)

if response.status_code == 200:
result = response.json()
return result
else:
print(f"Error: {response.status_code}, Message: {response.text}")
with open("output.txt", "a") as f:
f.write(response.text)
return "Error!"

im = read_image_array(r"cat.png")
output = cat_prompt_only("@cf/meta/llama-3.2-11b-vision-instruct", im)
print(output)
def read_image_binary(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")

def read_image_array(image_path):
with Image.open(image_path) as image_file:
image = image_file.convert('L')
image_array = np.array(image)
image_array_flat = image_array.flatten().tolist()
return image_array_flat

def cat_prompt_only(model, image_data):
print(type(image_data))
# post
response = requests.post(
f"{API_BASE_URL}{model}",
headers = {"Authorization": f"Bearer {AUTH_TOKEN}"},
json = {
"prompt": "What's the animal in the picture?",
"image": image_data,
}
)

if response.status_code == 200:
result = response.json()
return result
else:
print(f"Error: {response.status_code}, Message: {response.text}")
with open("output.txt", "a") as f:
f.write(response.text)
return "Error!"

im = read_image_array(r"cat.png")
output = cat_prompt_only("@cf/meta/llama-3.2-11b-vision-instruct", im)
print(output)
I got this when I use read_image_binary()
Error: 400, Message: {"errors":[{"message":"AiError: AiError: Tensor error: failed to build tensor image: Tensor error: Unknown internal error: failed to decode u8","code":3016}],"success":false,"result":{},"messages":[]}
Error: 400, Message: {"errors":[{"message":"AiError: AiError: Tensor error: failed to build tensor image: Tensor error: Unknown internal error: failed to decode u8","code":3016}],"success":false,"result":{},"messages":[]}
I got this when I use read_image_array()
Error: 500, Message: {"errors":[{"message":"AiError: AiError: Unknown internal error","code":3028}],"success":false,"result":{},"messages":[]}
Error: 500, Message: {"errors":[{"message":"AiError: AiError: Unknown internal error","code":3028}],"success":false,"result":{},"messages":[]}
Pulkitvyas
Pulkitvyas2w ago
Hi, did you ever figure this out?
Want results from more Discord servers?
Add your server