Image to Text API Inference - Input Error

Hello everyone!
I would like to create a Python script in which I send a POST request via the Hugging Face API Inference for an Image to Text model. The model is: nlpconnect/vit-gpt2-image-captioning link.
I’m having issues with sending the image, as the POST request is returning a 400 error.
The Python script is as follows:

# Call the function with your image path, model, and API key
# caption = image_to_text("your_image.jpg", "nlpconnect/vit-gpt2-image-captioning", "your_api_key")

import requests

def image_to_text(image_path, model, api_key):
url = f"{model}"
    headers = {
        "Authorization": f"Bearer {api_key}",
    with open(image_path, 'rb') as image:
        files = {
            "file": image,
            response =, headers=headers, files=files)

            result = response.json()
            caption = result["result"][0]["caption"]
            return caption

        except Exception as e:
            print("Error during the API request:", str(e))
            return None

I’m struggling to identify the source of my error. Could someone please offer assistance? Thank you!