-
Notifications
You must be signed in to change notification settings - Fork 374
[Inference API] Add image-text-to-text task and fix generate script
#1440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1efb326
Add image-text-to-text task and fix generate script
hanouticelina 7cf2959
Run generate script
hanouticelina b6771b3
fix chat-completion and image-text-to-text docs
hanouticelina 5977ab7
fix typo
hanouticelina 05b8858
Fix chat completion package reference links
hanouticelina b94abfa
regenerate inference api docs
hanouticelina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| <!--- | ||
| This markdown file has been generated from a script. Please do not edit it directly. | ||
| For more details, check out: | ||
| - the `generate.ts` script: https://github.com/huggingface/hub-docs/blob/main/scripts/api-inference/scripts/generate.ts | ||
| - 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 | ||
| - 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 | ||
| - 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 | ||
| - the snippets used to generate the example: | ||
| - curl: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/curl.ts | ||
| - python: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/python.ts | ||
| - javascript: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/snippets/js.ts | ||
| - the "tasks" content for recommended models: https://huggingface.co/api/tasks | ||
| ---> | ||
|
|
||
| ## Image-Text to Text | ||
|
|
||
| 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. | ||
|
|
||
| <Tip> | ||
|
|
||
| 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. | ||
|
|
||
| </Tip> | ||
|
|
||
| ### Recommended models | ||
|
|
||
| - [HuggingFaceM4/idefics2-8b-chatty](https://huggingface.co/HuggingFaceM4/idefics2-8b-chatty): Cutting-edge conversational vision language model that can take multiple image inputs. | ||
| - [microsoft/Phi-3.5-vision-instruct](https://huggingface.co/microsoft/Phi-3.5-vision-instruct): Strong image-text-to-text model. | ||
|
|
||
| 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). | ||
|
|
||
| ### Using the API | ||
|
|
||
|
|
||
| <inferencesnippet> | ||
|
|
||
| <curl> | ||
| ```bash | ||
| curl https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty \ | ||
| -X POST \ | ||
| -d '{"inputs": No input example has been defined for this model task.}' \ | ||
| -H 'Content-Type: application/json' \ | ||
| -H "Authorization: Bearer hf_***" | ||
| ``` | ||
| </curl> | ||
|
|
||
| <python> | ||
| ```py | ||
| import requests | ||
|
|
||
| API_URL = "https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty" | ||
| headers = {"Authorization": "Bearer hf_***"} | ||
|
|
||
| from huggingface_hub import InferenceClient | ||
|
|
||
| client = InferenceClient(api_key="hf_***") | ||
|
|
||
| image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" | ||
|
|
||
| for message in client.chat_completion( | ||
| model="HuggingFaceM4/idefics2-8b-chatty", | ||
| messages=[ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "image_url", "image_url": {"url": image_url}}, | ||
| {"type": "text", "text": "Describe this image in one sentence."}, | ||
| ], | ||
| } | ||
| ], | ||
| max_tokens=500, | ||
| stream=True, | ||
| ): | ||
| print(message.choices[0].delta.content, end="") | ||
| ``` | ||
|
|
||
| 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). | ||
| </python> | ||
|
|
||
| <js> | ||
| ```js | ||
| async function query(data) { | ||
| const response = await fetch( | ||
| "https://api-inference.huggingface.co/models/HuggingFaceM4/idefics2-8b-chatty", | ||
| { | ||
| headers: { | ||
| Authorization: "Bearer hf_***" | ||
| "Content-Type": "application/json", | ||
| }, | ||
| method: "POST", | ||
| body: JSON.stringify(data), | ||
| } | ||
| ); | ||
| const result = await response.json(); | ||
| return result; | ||
| } | ||
|
|
||
| query({"inputs": No input example has been defined for this model task.}).then((response) => { | ||
hanouticelina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(JSON.stringify(response)); | ||
| }); | ||
| ``` | ||
|
|
||
| To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#imagetext-to-text). | ||
| </js> | ||
|
|
||
| </inferencesnippet> | ||
|
|
||
|
|
||
|
|
||
| ### API specification | ||
|
|
||
| #### Request | ||
|
|
||
|
|
||
hanouticelina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Some options can be configured by passing headers to the Inference API. Here are the available headers: | ||
|
|
||
| | Headers | | | | ||
| | :--- | :--- | :--- | | ||
| | **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). | | ||
| | **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]). | | ||
| | **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]). | | ||
|
|
||
| For more information about Inference API headers, check out the parameters [guide](../parameters). | ||
|
|
||
| #### Response | ||
|
|
||
|
|
||
hanouticelina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not expected (i.e. having
import requests ...beforefrom huggingface_hub import InferenceClient). I realized that https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct?inference_api=true has a problem. Model doesn't have a chat template and therefore is not tagged as"conversational"which creates this weird side effect.So I see 3 independent things to correct here:
meta-llama/Llama-3.2-11B-Vision-Instructfirst on the image-text-to-image task page (to update here)"conversational"detection. At the moment, it's based only on the presence of a chat template. However for idefics chatty 8b it seems it's using "use_default_system_prompt": true instead. @Rocketknight1 is it safe to assume that a model with no chat template but this parameter set to True is in fact a conversational model? And if not, which parameter could we check?image-text-to-text(does that even exist?), we should fix the snippet generator so that only therequests-based snippet is displayed instead of this weird combination.cc @osanseviero as well for viz'
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to add,
HuggingFaceM4/idefics2-8b-chattyhas thechat_templatedefined in the processor_config.json. Thetokenizer.chat_templateattribute is supposed to be saved intokenizer_config.jsonfile. I guess the template was set usingtransformers.ProcessorMixininstead.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the 3rd point, pinging @mishig25 since it's related to huggingface.js/pull/938. do you think it's okay to map image-text-to-text to
snippetBasicinstead and define the task input here ?