Skip to content

image inference guide#8

Open
philschmid wants to merge 1 commit intomainfrom
multimodal
Open

image inference guide#8
philschmid wants to merge 1 commit intomainfrom
multimodal

Conversation

@philschmid
Copy link
Contributor

No description provided.

@HuggingFaceDocBuilderDev
Copy link
Collaborator

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.


## Best Practices

1. **Image Size**: While there's no strict limit on image dimensions, it's recommended to resize large images before sending them to reduce bandwidth usage and processing time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a recommended range of image dimensions that makes sense for most multimodal models?

Copy link
Member

@alvarobartt alvarobartt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment on lines +28 to +57
* Using a URL
```python
from huggingface_hub import InferenceClient
import base64

client = InferenceClient(base_url="http://localhost:8080", api_key="-")

# Using a URL
image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in detail.",
},
{
"type": "image_url",
"image_url": {"url": image_url},
},
],
},
],
temperature=0.7,
max_tokens=128,
)
print(chat_completion.choices[0].message.content)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Using a URL
```python
from huggingface_hub import InferenceClient
import base64
client = InferenceClient(base_url="http://localhost:8080", api_key="-")
# Using a URL
image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in detail.",
},
{
"type": "image_url",
"image_url": {"url": image_url},
},
],
},
],
temperature=0.7,
max_tokens=128,
)
print(chat_completion.choices[0].message.content)
```
##### Using a URL
```python
from huggingface_hub import InferenceClient
import base64
client = InferenceClient(base_url="http://localhost:8080", api_key="-")
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in detail.",
},
{
"type": "image_url",
"image_url": {"url": image_url},
},
],
},
],
temperature=0.7,
max_tokens=128,
)
print(chat_completion.choices[0].message.content)

client = OpenAI(base_url="http://localhost:8080/v1/", api_key="-")

# Using a URL or base64-encoded image
image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" # or your base64 data URL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, to use a Hugging Face image instead (in case the other link is eventually broken / not working):

Suggested change
image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" # or your base64 data URL
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png" # or your base64 data URL

},
{
"type":"image_url",
"image_url":{"url":"https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

Suggested change
"image_url":{"url":"https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"}
"image_url":{"url":"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"}

print(chat_completion.choices[0].message.content)
```

* Using a local image (base64 encoded)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Using a local image (base64 encoded)
##### Using a local image (base64 encoded)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants