srt_example_llava.py 810 Bytes
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
2
3
"""
Usage: python3 srt_example_llava.py
"""
Lianmin Zheng's avatar
Lianmin Zheng committed
4
5
6
7
8
9
10
11
12
import sglang as sgl


@sgl.function
def image_qa(s, image_path, question):
    s += sgl.user(sgl.image(image_path) + question)
    s += sgl.assistant(sgl.gen("answer"))


13
14
runtime = sgl.Runtime(model_path="liuhaotian/llava-v1.5-7b",
                      tokenizer_path="llava-hf/llava-1.5-7b-hf")
Lianmin Zheng's avatar
Lianmin Zheng committed
15
16
17
sgl.set_default_backend(runtime)


Lianmin Zheng's avatar
Lianmin Zheng committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Single
state = image_qa.run(
    image_path="images/cat.jpeg",
    question="What is this?",
    max_new_tokens=64)
print(state["answer"], "\n")


# Batch
states = image_qa.run_batch(
    [
        {"image_path": "images/cat.jpeg", "question":"What is this?"},
        {"image_path": "images/dog.jpeg", "question":"What is this?"},
    ],
    max_new_tokens=64,
)
for s in states:
    print(s["answer"], "\n")

Lianmin Zheng's avatar
Lianmin Zheng committed
37
38

runtime.shutdown()