File tree Expand file tree Collapse file tree 2 files changed +16
-17
lines changed
examples/runtime/token_in_token_out Expand file tree Collapse file tree 2 files changed +16
-17
lines changed Original file line number Diff line number Diff line change 1
1
import argparse
2
2
import dataclasses
3
- from io import BytesIO
4
3
from typing import Tuple
5
4
6
- import requests
7
- from PIL import Image
8
5
from transformers import AutoProcessor
9
6
10
7
from sglang import Engine
@@ -19,20 +16,22 @@ def get_input_ids(
19
16
) -> Tuple [list [int ], list ]:
20
17
chat_template = get_chat_template_by_model_path (model_config .model_path )
21
18
text = f"{ chat_template .image_token } What is in this picture?"
22
- images = [Image .open (BytesIO (requests .get (DEFAULT_IMAGE_URL ).content ))]
23
19
image_data = [DEFAULT_IMAGE_URL ]
24
20
25
21
processor = AutoProcessor .from_pretrained (
26
22
model_config .model_path , trust_remote_code = server_args .trust_remote_code
27
23
)
28
24
29
- inputs = processor (
30
- text = [text ],
31
- images = images ,
32
- return_tensors = "pt" ,
25
+ input_ids = (
26
+ processor .tokenizer (
27
+ text = [text ],
28
+ return_tensors = "pt" ,
29
+ )
30
+ .input_ids [0 ]
31
+ .tolist ()
33
32
)
34
33
35
- return inputs . input_ids [ 0 ]. tolist () , image_data
34
+ return input_ids , image_data
36
35
37
36
38
37
def token_in_out_example (
Original file line number Diff line number Diff line change 5
5
6
6
"""
7
7
8
- from io import BytesIO
9
8
from typing import Tuple
10
9
11
10
import requests
12
- from PIL import Image
13
11
from transformers import AutoProcessor
14
12
15
13
from sglang .lang .chat_template import get_chat_template_by_model_path
28
26
def get_input_ids () -> Tuple [list [int ], list ]:
29
27
chat_template = get_chat_template_by_model_path (MODEL_PATH )
30
28
text = f"{ chat_template .image_token } What is in this picture?"
31
- images = [Image .open (BytesIO (requests .get (DEFAULT_IMAGE_URL ).content ))]
32
29
image_data = [DEFAULT_IMAGE_URL ]
33
30
34
31
processor = AutoProcessor .from_pretrained (MODEL_PATH )
35
32
36
- inputs = processor (
37
- text = [text ],
38
- images = images ,
39
- return_tensors = "pt" ,
33
+ input_ids = (
34
+ processor .tokenizer (
35
+ text = [text ],
36
+ return_tensors = "pt" ,
37
+ )
38
+ .input_ids [0 ]
39
+ .tolist ()
40
40
)
41
41
42
- return inputs . input_ids [ 0 ]. tolist () , image_data
42
+ return input_ids , image_data
43
43
44
44
45
45
def main ():
You can’t perform that action at this time.
0 commit comments