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"https://api-inference.huggingface.co/models/{model}"
    headers = {
        "Authorization": f"Bearer {api_key}",
    }
    with open(image_path, 'rb') as image:
        files = {
            "file": image,
        }
        try:
            response = requests.post(url, headers=headers, files=files)
            response.raise_for_status()

            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!

1 Like