forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisual_language_chat.py
More file actions
executable file
·97 lines (71 loc) · 2.97 KB
/
visual_language_chat.py
File metadata and controls
executable file
·97 lines (71 loc) · 2.97 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import argparse
import numpy as np
import openvino_genai
from PIL import Image
from openvino import Tensor
from pathlib import Path
def streamer(subword: str) -> bool:
'''
Args:
subword: sub-word of the generated text.
Returns: Return flag corresponds whether generation should be stopped.
'''
print(subword, end='', flush=True)
# No value is returned as in this example we don't want to stop the generation in this method.
# "return None" will be treated the same as "return openvino_genai.StreamingStatus.RUNNING".
def read_image(path: str) -> Tensor:
'''
Args:
path: The path to the image.
Returns: the ov.Tensor containing the image.
'''
pic = Image.open(path).convert("RGB")
image_data = np.array(pic)
return Tensor(image_data)
def read_images(path: str) -> list[Tensor]:
entry = Path(path)
if entry.is_dir():
return [read_image(str(file)) for file in sorted(entry.iterdir())]
return [read_image(path)]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("model_dir", help="Path to the model directory")
parser.add_argument("image_dir", help="Image file or dir with images")
parser.add_argument("device", nargs="?", default="CPU", help="Device to run the model on (default: CPU)")
args = parser.parse_args()
rgbs = read_images(args.image_dir)
# GPU and NPU can be used as well.
# Note: If NPU is selected, only the language model will be run on the NPU.
properties = dict()
properties["prompt_lookup"] = True
if args.device == "GPU":
# Cache compiled models on disk for GPU to save time on the next run.
# It's not beneficial for CPU.
properties["CACHE_DIR"] = "vlm_cache"
pipe = openvino_genai.VLMPipeline(args.model_dir, args.device, **properties)
config = openvino_genai.GenerationConfig()
config.max_new_tokens = 100
# add parameter to enable prompt lookup decoding to generate `num_assistant_tokens` candidates per iteration
config.num_assistant_tokens = 5
# Define max_ngram_size
config.max_ngram_size = 3
history = openvino_genai.ChatHistory()
prompt = input('question:\n')
history.append({"role": "user", "content": prompt})
decoded_results = pipe.generate(history, images=rgbs, generation_config=config, streamer=streamer)
history.append({"role": "assistant", "content": decoded_results.texts[0]})
while True:
try:
prompt = input("\n----------\n"
"question:\n")
except EOFError:
break
history.append({"role": "user", "content": prompt})
# New images and videos can be passed at each turn
decoded_results = pipe.generate(history, generation_config=config, streamer=streamer)
history.append({"role": "assistant", "content": decoded_results.texts[0]})
if '__main__' == __name__:
main()