Skip to content

Commit 1efb326

Browse files
committed
Add image-text-to-text task and fix generate script
1 parent 0383e4e commit 1efb326

22 files changed

+239
-76
lines changed

docs/api-inference/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
title: Image Segmentation
3131
- local: tasks/image-to-image
3232
title: Image to Image
33+
- local: tasks/image-text-to-text
34+
title: Image-Text to Text
3335
- local: tasks/object-detection
3436
title: Object Detection
3537
- local: tasks/question-answering

docs/api-inference/tasks/audio-classification.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ curl https://api-inference.huggingface.co/models/<REPO_ID> \
4343
-X POST \
4444
--data-binary '@sample1.flac' \
4545
-H "Authorization: Bearer hf_***"
46-
4746
```
4847
</curl>
4948

docs/api-inference/tasks/automatic-speech-recognition.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ For more details about the `automatic-speech-recognition` task, check out its [d
3030
### Recommended models
3131

3232
- [openai/whisper-large-v3](https://huggingface.co/openai/whisper-large-v3): A powerful ASR model by OpenAI.
33+
- [facebook/seamless-m4t-v2-large](https://huggingface.co/facebook/seamless-m4t-v2-large): An end-to-end model that performs ASR and Speech Translation by MetaAI.
3334
- [pyannote/speaker-diarization-3.1](https://huggingface.co/pyannote/speaker-diarization-3.1): Powerful speaker diarization model.
3435

3536
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=automatic-speech-recognition&sort=trending).
@@ -45,7 +46,6 @@ curl https://api-inference.huggingface.co/models/openai/whisper-large-v3 \
4546
-X POST \
4647
--data-binary '@sample1.flac' \
4748
-H "Authorization: Bearer hf_***"
48-
4949
```
5050
</curl>
5151

@@ -108,7 +108,7 @@ To use the JavaScript client, see `huggingface.js`'s [package reference](https:/
108108
| **inputs*** | _string_ | The input audio data as a base64-encoded string. If no `parameters` are provided, you can also provide the audio data as a raw bytes payload. |
109109
| **parameters** | _object_ | Additional inference parameters for Automatic Speech Recognition |
110110
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return_timestamps** | _boolean_ | Whether to output corresponding timestamps with the generated text |
111-
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generate** | _object_ | Ad-hoc parametrization of the text generation process |
111+
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generation_parameters** | _object_ | Ad-hoc parametrization of the text generation process |
112112
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temperature** | _number_ | The value used to modulate the next token probabilities. |
113113
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top_k** | _integer_ | The number of highest probability vocabulary tokens to keep for top-k-filtering. |
114114
| **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;top_p** | _number_ | If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to top_p or higher are kept for generation. |

docs/api-inference/tasks/chat-completion.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@ curl 'https://api-inference.huggingface.co/models/google/gemma-2-2b-it/v1/chat/c
5959
```py
6060
from huggingface_hub import InferenceClient
6161

62-
client = InferenceClient(
63-
"google/gemma-2-2b-it",
64-
token="hf_***",
65-
)
62+
client = InferenceClient(api_key="hf_***")
6663

6764
for message in client.chat_completion(
65+
model="google/gemma-2-2b-it",
6866
messages=[{"role": "user", "content": "What is the capital of France?"}],
6967
max_tokens=500,
7068
stream=True,
7169
):
7270
print(message.choices[0].delta.content, end="")
73-
7471
```
7572

7673
To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion).
@@ -89,7 +86,6 @@ for await (const chunk of inference.chatCompletionStream({
8986
})) {
9087
process.stdout.write(chunk.choices[0]?.delta?.content || "");
9188
}
92-
9389
```
9490
9591
To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#chatcompletion).

docs/api-inference/tasks/feature-extraction.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ curl https://api-inference.huggingface.co/models/thenlper/gte-large \
4545
-d '{"inputs": "Today is a sunny day and I will get some ice cream."}' \
4646
-H 'Content-Type: application/json' \
4747
-H "Authorization: Bearer hf_***"
48-
4948
```
5049
</curl>
5150

docs/api-inference/tasks/fill-mask.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ curl https://api-inference.huggingface.co/models/google-bert/bert-base-uncased \
4141
-d '{"inputs": "The answer to the universe is [MASK]."}' \
4242
-H 'Content-Type: application/json' \
4343
-H "Authorization: Bearer hf_***"
44-
4544
```
4645
</curl>
4746

docs/api-inference/tasks/image-classification.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ curl https://api-inference.huggingface.co/models/google/vit-base-patch16-224 \
3939
-X POST \
4040
--data-binary '@cats.jpg' \
4141
-H "Authorization: Bearer hf_***"
42-
4342
```
4443
</curl>
4544

docs/api-inference/tasks/image-segmentation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ curl https://api-inference.huggingface.co/models/nvidia/segformer-b0-finetuned-a
3939
-X POST \
4040
--data-binary '@cats.jpg' \
4141
-H "Authorization: Bearer hf_***"
42-
4342
```
4443
</curl>
4544

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+

docs/api-inference/tasks/object-detection.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ curl https://api-inference.huggingface.co/models/facebook/detr-resnet-50 \
4040
-X POST \
4141
--data-binary '@cats.jpg' \
4242
-H "Authorization: Bearer hf_***"
43-
4443
```
4544
</curl>
4645

0 commit comments

Comments
 (0)