# Image to Text API Inference - Input Error

**URL:** <https://discuss.huggingface.co/t/image-to-text-api-inference-input-error/60416>\
**Category:** Inference Endpoints on the Hub\
**Created:** [October 30, 2023, 10:50am UTC](https://discuss.huggingface.co/t/image-to-text-api-inference-input-error/60416 "2023-10-30T10:50:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michele96](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michele96](https://discuss.huggingface.co/u/Michele96)\
**Post date:** [October 30, 2023, 10:50am UTC](https://discuss.huggingface.co/t/image-to-text-api-inference-input-error/60416/1 "2023-10-30T10:50:46Z")

</div>

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](https://huggingface.co/segmind/SSD-1B).  
I’m having issues with sending the image, as the POST request is returning a 400 error.  
The Python script is as follows:

```auto
# 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!
