Skip to content

Commit

Permalink
Added image-text-to-text pipeline to task guide (#34783)
Browse files Browse the repository at this point in the history
* Added image-text-to-text pipeline to task guide

* Update docs/source/en/tasks/image_text_to_text.md

Co-authored-by: Steven Liu <[email protected]>

* Update docs/source/en/tasks/image_text_to_text.md

Co-authored-by: Steven Liu <[email protected]>

* Update docs/source/en/tasks/image_text_to_text.md

Co-authored-by: Steven Liu <[email protected]>

* Update docs/source/en/tasks/image_text_to_text.md

Co-authored-by: Steven Liu <[email protected]>

* Merge codeblocks

---------

Co-authored-by: Steven Liu <[email protected]>
  • Loading branch information
merveenoyan and stevhliu authored Nov 19, 2024
1 parent 469eddb commit befbbf2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/source/en/tasks/image_text_to_text.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,46 @@ print(generated_texts)
## ['User: What do we see in this image? \nAssistant: In this image we can see two cats on the nets. \nUser: And how about this image? \nAssistant: In this image we can see flowers, plants and insect.']
```

## Pipeline

The fastest way to get started is to use the [`Pipeline`] API. Specify the `"image-text-to-text"` task and the model you want to use.

```python
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="llava-hf/llava-interleave-qwen-0.5b-hf")
```

The example below uses chat templates to format the text inputs.

```python
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",
},
{"type": "text", "text": "Describe this image."},
],
},
{
"role": "assistant",
"content": [
{"type": "text", "text": "There's a pink flower"},
],
},
]
```

Pass the chat template formatted text and image to [`Pipeline`] and set `return_full_text=False` to remove the input from the generated output.

```python
outputs = pipe(text=messages, max_new_tokens=20, return_full_text=False)
outputs[0]["generated_text"]
# with a yellow center in the foreground. The flower is surrounded by red and white flowers with green stems
```

## Streaming

We can use [text streaming](./generation_strategies#streaming) for a better generation experience. Transformers supports streaming with the [`TextStreamer`] or [`TextIteratorStreamer`] classes. We will use the [`TextIteratorStreamer`] with IDEFICS-8B.
Expand Down

0 comments on commit befbbf2

Please sign in to comment.