forked from xai-org/xai-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_understanding.py
More file actions
72 lines (53 loc) · 1.96 KB
/
image_understanding.py
File metadata and controls
72 lines (53 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import base64
from typing import Sequence
import requests
from absl import app, flags
from xai_sdk import Client
from xai_sdk.chat import image, user
FORMAT = flags.DEFINE_enum("format", "url", ["url", "base64"], "Image format used when providing the image.")
def image_understanding(client: Client) -> None:
"""Image understanding with multiple images."""
chat = client.chat.create(model="grok-2-vision")
# We can easily interleave text and images in a single user conversation turn.
chat.append(
user(
"What do these images have in common?",
image(
"https://www.nasa.gov/wp-content/uploads/2025/04/51d-9092large.jpg",
detail="high",
),
image(
"https://www.nasa.gov/wp-content/uploads/2025/04/0101247orig-1.jpg",
detail="high",
),
)
)
response = chat.sample()
print(response.content)
print(response.usage.prompt_image_tokens)
def image_understanding_b64(client: Client) -> None:
"""Image understanding with an image encoded as base64."""
chat = client.chat.create(model="grok-2-vision")
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_data = requests.get(image_url, timeout=5).content
image_data = base64.b64encode(image_data).decode("utf-8")
chat.append(
user(
"What kind of ant is this?",
image(f"data:image/jpeg;base64,{image_data}"),
)
)
response = chat.sample()
print(response.content)
print(response.usage.prompt_image_tokens)
def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
raise app.UsageError("Unexpected command line arguments.")
client = Client()
match FORMAT.value:
case "base64":
image_understanding_b64(client)
case "url":
image_understanding(client)
if __name__ == "__main__":
app.run(main)