-
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
Changes from 4 commits
1efb326
7cf2959
b6771b3
5977ab7
05b8858
b94abfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <!--- | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not expected (i.e. having So I see 3 independent things to correct here:
cc @osanseviero as well for viz' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to add, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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 | ||
|
|
||
| For the API specification of conversational image-text-to-text models, please refer to the [Chat Completion API documentation](https://huggingface.co/docs/api-inference/tasks/chat-completion#api-specification). | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.