|
| 1 | +<!--- |
| 2 | +This markdown file has been generated from a script. Please do not edit it directly. |
| 3 | +For more details, check out: |
| 4 | +- the `generate.ts` script: https://github.com/huggingface/hub-docs/blob/main/scripts/api-inference/scripts/generate.ts |
| 5 | +- the task template defining the sections in the page: https://github.com/huggingface/hub-docs/tree/main/scripts/api-inference/templates/task/image-text-to-text.handlebars |
| 6 | +- the input jsonschema specifications used to generate the input markdown table: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-text/spec/input.json |
| 7 | +- the output jsonschema specifications used to generate the output markdown table: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/tasks/image-text-to-text/spec/output.json |
| 8 | +- the snippets used to generate the example: |
| 9 | + - curl: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/curl.ts |
| 10 | + - python: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/python.ts |
| 11 | + - javascript: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/js.ts |
| 12 | +- the "tasks" content for recommended models: https://huggingface.co/api/tasks |
| 13 | +---> |
| 14 | + |
| 15 | +## Image-Text to Text |
| 16 | + |
| 17 | +Image-text-to-text models take in an image and text prompt and output text. These models are also called vision-language models, or VLMs. The difference from image-to-text models is that these models take an additional text input, not restricting the model to certain use cases like image captioning, and may also be trained to accept a conversation as input. |
| 18 | + |
| 19 | +<Tip> |
| 20 | + |
| 21 | +For more details about the `image-text-to-text` task, check out its [dedicated page](https://huggingface.co/tasks/image-text-to-text)! You will find examples and related materials. |
| 22 | + |
| 23 | +</Tip> |
| 24 | + |
| 25 | +### Recommended models |
| 26 | + |
| 27 | +- [HuggingFaceM4/idefics2-8b-chatty](https://huggingface.co/HuggingFaceM4/idefics2-8b-chatty): Cutting-edge conversational vision language model that can take multiple image inputs. |
| 28 | +- [microsoft/Phi-3.5-vision-instruct](https://huggingface.co/microsoft/Phi-3.5-vision-instruct): Strong image-text-to-text model. |
| 29 | + |
| 30 | +This is only a subset of the supported models. Find the model that suits you best [here](https://huggingface.co/models?inference=warm&pipeline_tag=image-text-to-text&sort=trending). |
| 31 | + |
| 32 | +### Using the API |
| 33 | + |
| 34 | + |
| 35 | +<inferencesnippet> |
| 36 | + |
| 37 | +<curl> |
| 38 | +```bash |
| 39 | +curl https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty \ |
| 40 | + -X POST \ |
| 41 | + -d '{"inputs": No input example has been defined for this model task.}' \ |
| 42 | + -H 'Content-Type: application/json' \ |
| 43 | + -H "Authorization: Bearer hf_***" |
| 44 | +``` |
| 45 | +</curl> |
| 46 | + |
| 47 | +<python> |
| 48 | +```py |
| 49 | +import requests |
| 50 | + |
| 51 | +API_URL = "https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty" |
| 52 | +headers = {"Authorization": "Bearer hf_***"} |
| 53 | + |
| 54 | +from huggingface_hub import InferenceClient |
| 55 | + |
| 56 | +client = InferenceClient(api_key="hf_***") |
| 57 | + |
| 58 | +image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" |
| 59 | + |
| 60 | +for message in client.chat_completion( |
| 61 | + model="HuggingFaceM4/idefics2-8b-chatty", |
| 62 | + messages=[ |
| 63 | + { |
| 64 | + "role": "user", |
| 65 | + "content": [ |
| 66 | + {"type": "image_url", "image_url": {"url": image_url}}, |
| 67 | + {"type": "text", "text": "Describe this image in one sentence."}, |
| 68 | + ], |
| 69 | + } |
| 70 | + ], |
| 71 | + max_tokens=500, |
| 72 | + stream=True, |
| 73 | +): |
| 74 | + print(message.choices[0].delta.content, end="") |
| 75 | +``` |
| 76 | + |
| 77 | +To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.image_text-to-text). |
| 78 | +</python> |
| 79 | + |
| 80 | +<js> |
| 81 | +```js |
| 82 | +async function query(data) { |
| 83 | + const response = await fetch( |
| 84 | + "https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty", |
| 85 | + { |
| 86 | + headers: { |
| 87 | + Authorization: "Bearer hf_***" |
| 88 | + "Content-Type": "application/json", |
| 89 | + }, |
| 90 | + method: "POST", |
| 91 | + body: JSON.stringify(data), |
| 92 | + } |
| 93 | + ); |
| 94 | + const result = await response.json(); |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +query({"inputs": No input example has been defined for this model task.}).then((response) => { |
| 99 | + console.log(JSON.stringify(response)); |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#imagetext-to-text). |
| 104 | +</js> |
| 105 | + |
| 106 | +</inferencesnippet> |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +### API specification |
| 111 | + |
| 112 | +#### Request |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +Some options can be configured by passing headers to the Inference API. Here are the available headers: |
| 117 | + |
| 118 | +| Headers | | | |
| 119 | +| :--- | :--- | :--- | |
| 120 | +| **authorization** | _string_ | Authentication header in the form `'Bearer: hf_****'` when `hf_****` is a personal user access token with Inference API permission. You can generate one from [your settings page](https://huggingface.co/settings/tokens). | |
| 121 | +| **x-use-cache** | _boolean, default to `true`_ | There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching [here](../parameters#caching]). | |
| 122 | +| **x-wait-for-model** | _boolean, default to `false`_ | If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error, as it will limit hanging in your application to known places. Read more about model availability [here](../overview#eligibility]). | |
| 123 | + |
| 124 | +For more information about Inference API headers, check out the parameters [guide](../parameters). |
| 125 | + |
| 126 | +#### Response |
| 127 | + |
| 128 | + |
| 129 | + |
0 commit comments