Commit
·
73085ab
1
Parent(s):
8b2c047
aaa
Browse files- .idea/.name +1 -1
- handler.py +113 -6
- inference.py +0 -415
.idea/.name
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
handler.py
|
handler.py
CHANGED
@@ -1,9 +1,116 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
class EndpointHandler:
|
4 |
-
def __init__(self,
|
5 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from typing import Dict, Any
|
6 |
+
from transformers import LlamaTokenizer, GenerationConfig
|
7 |
+
from robohusky.model.modeling_husky_embody2 import HuskyForConditionalGeneration
|
8 |
+
from robohusky.video_transformers import (
|
9 |
+
GroupNormalize, GroupScale, GroupCenterCrop,
|
10 |
+
Stack, ToTorchFormatTensor, get_index
|
11 |
+
)
|
12 |
+
from decord import VideoReader, cpu
|
13 |
+
import torchvision.transforms as T
|
14 |
+
from torchvision.transforms.functional import InterpolationMode
|
15 |
+
|
16 |
+
DEFAULT_IMG_START_TOKEN = "<img>"
|
17 |
+
DEFAULT_IMG_END_TOKEN = "</img>"
|
18 |
+
DEFAULT_VIDEO_START_TOKEN = "<vid>"
|
19 |
+
DEFAULT_VIDEO_END_TOKEN = "</vid>"
|
20 |
|
21 |
class EndpointHandler:
|
22 |
+
def __init__(self, model_path: str = "."):
|
23 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
self.tokenizer = LlamaTokenizer.from_pretrained(model_path, use_fast=False)
|
25 |
+
self.model = HuskyForConditionalGeneration.from_pretrained(
|
26 |
+
model_path, torch_dtype=torch.float16 if self.device == "cuda" else torch.float32
|
27 |
+
).to(self.device).eval()
|
28 |
+
|
29 |
+
self.gen_config = GenerationConfig(
|
30 |
+
bos_token_id=1,
|
31 |
+
do_sample=True,
|
32 |
+
temperature=0.7,
|
33 |
+
max_new_tokens=1024
|
34 |
+
)
|
35 |
+
|
36 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
37 |
+
# Hugging Face 会调用这个函数,data 是原始输入
|
38 |
+
inputs = self.preprocess(data)
|
39 |
+
prediction = self.inference(inputs)
|
40 |
+
return self.postprocess(prediction)
|
41 |
+
|
42 |
+
def preprocess(self, request: Dict[str, Any]) -> Dict[str, Any]:
|
43 |
+
prompt = request["inputs"]
|
44 |
+
image = request.get("image", None)
|
45 |
+
video = request.get("video", None)
|
46 |
+
|
47 |
+
if image:
|
48 |
+
pixel_values = self._load_image(image).unsqueeze(0).to(self.device)
|
49 |
+
prompt = prompt.replace("<image>", DEFAULT_IMG_START_TOKEN + DEFAULT_IMG_END_TOKEN)
|
50 |
+
elif video:
|
51 |
+
pixel_values = self._load_video(video).unsqueeze(0).to(self.device)
|
52 |
+
prompt = prompt.replace("<video>", DEFAULT_VIDEO_START_TOKEN + DEFAULT_VIDEO_END_TOKEN)
|
53 |
+
else:
|
54 |
+
pixel_values = None
|
55 |
+
|
56 |
+
return {
|
57 |
+
"prompt": prompt,
|
58 |
+
"pixel_values": pixel_values
|
59 |
+
}
|
60 |
+
|
61 |
+
def inference(self, inputs: Dict[str, Any]) -> str:
|
62 |
+
prompt = inputs["prompt"]
|
63 |
+
pixel_values = inputs["pixel_values"]
|
64 |
+
|
65 |
+
model_inputs = self.tokenizer([prompt], return_tensors="pt")
|
66 |
+
model_inputs.pop("token_type_ids", None)
|
67 |
+
model_inputs = {k: v.to(self.device) for k, v in model_inputs.items()}
|
68 |
+
|
69 |
+
if pixel_values is not None:
|
70 |
+
output = self.model.generate(
|
71 |
+
**model_inputs,
|
72 |
+
pixel_values=pixel_values,
|
73 |
+
generation_config=self.gen_config,
|
74 |
+
return_dict_in_generate=True,
|
75 |
+
output_scores=True
|
76 |
+
)
|
77 |
+
else:
|
78 |
+
output = self.model.language_model.generate(
|
79 |
+
**model_inputs,
|
80 |
+
generation_config=self.gen_config,
|
81 |
+
return_dict_in_generate=True,
|
82 |
+
output_scores=True
|
83 |
+
)
|
84 |
+
|
85 |
+
return self.tokenizer.decode(output.sequences[0], skip_special_tokens=True)
|
86 |
+
|
87 |
+
def postprocess(self, output: str) -> Dict[str, str]:
|
88 |
+
return {"output": output.strip()}
|
89 |
+
|
90 |
+
def _load_image(self, image_bytes: bytes) -> torch.Tensor:
|
91 |
+
image = Image.open(BytesIO(image_bytes)).convert('RGB')
|
92 |
+
crop_pct = 224 / 256
|
93 |
+
size = int(224 / crop_pct)
|
94 |
+
transform = T.Compose([
|
95 |
+
T.Resize(size, interpolation=InterpolationMode.BICUBIC),
|
96 |
+
T.CenterCrop(224),
|
97 |
+
T.ToTensor(),
|
98 |
+
T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
|
99 |
+
])
|
100 |
+
return transform(image)
|
101 |
+
|
102 |
+
def _load_video(self, video_bytes: bytes, num_segments=8) -> torch.Tensor:
|
103 |
+
with open("/tmp/temp_video.mp4", "wb") as f:
|
104 |
+
f.write(video_bytes)
|
105 |
+
vr = VideoReader("/tmp/temp_video.mp4", ctx=cpu(0))
|
106 |
+
frame_indices = get_index(len(vr), num_segments)
|
107 |
+
frames = [Image.fromarray(vr[idx].asnumpy()) for idx in frame_indices]
|
108 |
|
109 |
+
transform = T.Compose([
|
110 |
+
GroupScale(224),
|
111 |
+
GroupCenterCrop(224),
|
112 |
+
Stack(),
|
113 |
+
ToTorchFormatTensor(),
|
114 |
+
GroupNormalize([0.48145466, 0.4578275, 0.40821073], [0.26862954, 0.26130258, 0.27577711])
|
115 |
+
])
|
116 |
+
return transform(frames)
|
inference.py
DELETED
@@ -1,415 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
srun -p INTERN2 --job-name='husky_multi_test' --gres=gpu:1 --cpus-per-task=8 --quotatype="auto" python -u demo/inference_new.py
|
3 |
-
"""
|
4 |
-
|
5 |
-
import abc
|
6 |
-
from typing import Optional
|
7 |
-
|
8 |
-
import os
|
9 |
-
import requests
|
10 |
-
from PIL import Image
|
11 |
-
from io import BytesIO
|
12 |
-
|
13 |
-
import torch
|
14 |
-
import torchvision.transforms as T
|
15 |
-
from peft import PeftModel
|
16 |
-
from torchvision.transforms.functional import InterpolationMode
|
17 |
-
|
18 |
-
from transformers import (
|
19 |
-
LlamaTokenizer,
|
20 |
-
GenerationConfig,
|
21 |
-
StoppingCriteria,
|
22 |
-
StoppingCriteriaList,
|
23 |
-
)
|
24 |
-
|
25 |
-
from robohusky.model.modeling_husky_embody2 import HuskyForConditionalGeneration
|
26 |
-
|
27 |
-
from robohusky.conversation import (
|
28 |
-
conv_templates,
|
29 |
-
get_conv_template,
|
30 |
-
)
|
31 |
-
|
32 |
-
from robohusky.video_transformers import (
|
33 |
-
GroupNormalize,
|
34 |
-
GroupScale,
|
35 |
-
GroupCenterCrop,
|
36 |
-
Stack,
|
37 |
-
ToTorchFormatTensor,
|
38 |
-
get_index,
|
39 |
-
)
|
40 |
-
|
41 |
-
from robohusky.compression import compress_module
|
42 |
-
from decord import VideoReader, cpu
|
43 |
-
|
44 |
-
# import deepspeed
|
45 |
-
|
46 |
-
IGNORE_INDEX = -100
|
47 |
-
DEFAULT_UNK_TOKEN = "<unk>"
|
48 |
-
DEFAULT_IMG_START_TOKEN = "<img>"
|
49 |
-
DEFAULT_IMG_END_TOKEN = "</img>"
|
50 |
-
|
51 |
-
DEFAULT_VIDEO_START_TOKEN = "<vid>"
|
52 |
-
DEFAULT_VIDEO_END_TOKEN = "</vid>"
|
53 |
-
|
54 |
-
def get_gpu_memory(max_gpus=None):
|
55 |
-
gpu_memory = []
|
56 |
-
num_gpus = (
|
57 |
-
torch.cuda.device_count()
|
58 |
-
if max_gpus is None
|
59 |
-
else min(max_gpus, torch.cuda.device_count())
|
60 |
-
)
|
61 |
-
|
62 |
-
for gpu_id in range(num_gpus):
|
63 |
-
with torch.cuda.device(gpu_id):
|
64 |
-
device = torch.cuda.current_device()
|
65 |
-
gpu_properties = torch.cuda.get_device_properties(device)
|
66 |
-
total_memory = gpu_properties.total_memory / (1024 ** 3)
|
67 |
-
allocated_memory = torch.cuda.memory_allocated() / (1024 ** 3)
|
68 |
-
available_memory = total_memory - allocated_memory
|
69 |
-
gpu_memory.append(available_memory)
|
70 |
-
return gpu_memory
|
71 |
-
|
72 |
-
def load_model(
|
73 |
-
model_path, device, num_gpus, max_gpu_memory=None, load_8bit=False, lora_weights=None
|
74 |
-
):
|
75 |
-
if device == "cpu":
|
76 |
-
kwargs = {}
|
77 |
-
elif device == "cuda":
|
78 |
-
kwargs = {"torch_dtype": torch.float16}
|
79 |
-
if num_gpus == "auto":
|
80 |
-
kwargs["device_map"] = "auto"
|
81 |
-
else:
|
82 |
-
num_gpus = int(num_gpus)
|
83 |
-
if num_gpus != 1:
|
84 |
-
kwargs["device_map"] = "auto"
|
85 |
-
if max_gpu_memory is None:
|
86 |
-
kwargs[
|
87 |
-
"device_map"
|
88 |
-
] = "sequential" # This is important for not the same VRAM sizes
|
89 |
-
available_gpu_memory = get_gpu_memory(num_gpus)
|
90 |
-
kwargs["max_memory"] = {
|
91 |
-
i: str(int(available_gpu_memory[i] * 0.85)) + "GiB"
|
92 |
-
for i in range(num_gpus)
|
93 |
-
}
|
94 |
-
else:
|
95 |
-
kwargs["max_memory"] = {i: max_gpu_memory for i in range(num_gpus)}
|
96 |
-
else:
|
97 |
-
raise ValueError(f"Invalid device: {device}")
|
98 |
-
|
99 |
-
tokenizer = LlamaTokenizer.from_pretrained(
|
100 |
-
model_path, use_fast=False)
|
101 |
-
|
102 |
-
if lora_weights is None:
|
103 |
-
model = HuskyForConditionalGeneration.from_pretrained(
|
104 |
-
model_path, low_cpu_mem_usage=True, **kwargs
|
105 |
-
)
|
106 |
-
else:
|
107 |
-
kwargs["device_map"] = "auto"
|
108 |
-
model = HuskyForConditionalGeneration.from_pretrained(
|
109 |
-
model_path, low_cpu_mem_usage=True, **kwargs
|
110 |
-
)
|
111 |
-
model.language_model = PeftModel.from_pretrained(
|
112 |
-
model.language_model,
|
113 |
-
lora_weights,
|
114 |
-
**kwargs
|
115 |
-
)
|
116 |
-
|
117 |
-
if load_8bit:
|
118 |
-
compress_module(model, device)
|
119 |
-
|
120 |
-
if (device == "cuda" and num_gpus == 1) or device == "mps":
|
121 |
-
model.to(device)
|
122 |
-
|
123 |
-
model = model.eval()
|
124 |
-
return model, tokenizer
|
125 |
-
|
126 |
-
def load_image(image_file, input_size=224):
|
127 |
-
if image_file.startswith('http') or image_file.startswith('https'):
|
128 |
-
response = requests.get(image_file)
|
129 |
-
image = Image.open(BytesIO(response.content)).convert('RGB')
|
130 |
-
else:
|
131 |
-
image = Image.open(image_file).convert('RGB')
|
132 |
-
|
133 |
-
crop_pct = 224 / 256
|
134 |
-
size = int(input_size / crop_pct)
|
135 |
-
transform = T.Compose([
|
136 |
-
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
137 |
-
T.Resize(size, interpolation=InterpolationMode.BICUBIC),
|
138 |
-
T.CenterCrop(input_size),
|
139 |
-
T.ToTensor(),
|
140 |
-
T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
|
141 |
-
])
|
142 |
-
image = transform(image)
|
143 |
-
return image
|
144 |
-
|
145 |
-
def load_video(video_path, num_segments=8):
|
146 |
-
vr = VideoReader(video_path, ctx=cpu(0))
|
147 |
-
num_frames = len(vr)
|
148 |
-
frame_indices = get_index(num_frames, num_segments)
|
149 |
-
|
150 |
-
# transform
|
151 |
-
crop_size = 224
|
152 |
-
scale_size = 224
|
153 |
-
input_mean = [0.48145466, 0.4578275, 0.40821073]
|
154 |
-
input_std = [0.26862954, 0.26130258, 0.27577711]
|
155 |
-
|
156 |
-
transform = T.Compose([
|
157 |
-
GroupScale(int(scale_size), interpolation=InterpolationMode.BICUBIC),
|
158 |
-
GroupCenterCrop(crop_size),
|
159 |
-
Stack(),
|
160 |
-
ToTorchFormatTensor(),
|
161 |
-
GroupNormalize(input_mean, input_std)
|
162 |
-
])
|
163 |
-
|
164 |
-
images_group = list()
|
165 |
-
for frame_index in frame_indices:
|
166 |
-
img = Image.fromarray(vr[frame_index].asnumpy())
|
167 |
-
images_group.append(img)
|
168 |
-
video = transform(images_group)
|
169 |
-
return video
|
170 |
-
|
171 |
-
class StoppingCriteriaSub(StoppingCriteria):
|
172 |
-
|
173 |
-
def __init__(self, stops, encounters=1):
|
174 |
-
super().__init__()
|
175 |
-
self.stops = stops
|
176 |
-
|
177 |
-
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs):
|
178 |
-
for stop in self.stops:
|
179 |
-
if torch.all((stop == input_ids[0][-len(stop):])).item():
|
180 |
-
return True
|
181 |
-
|
182 |
-
return False
|
183 |
-
|
184 |
-
@torch.inference_mode()
|
185 |
-
def generate_stream(
|
186 |
-
model, tokenizer, image_processor, params, device
|
187 |
-
):
|
188 |
-
prompt = params["prompt"]
|
189 |
-
images = params.get("images", None)
|
190 |
-
videos = params.get("videos", None)
|
191 |
-
temperature = float(params.get("temperature", 0.7))
|
192 |
-
max_new_tokens = int(params.get("max_new_tokens", 1024))
|
193 |
-
|
194 |
-
num_queries = model.config.num_query_tokens
|
195 |
-
|
196 |
-
stop_words = ["Human: ", "Assistant: ", "###", "\n\n"]
|
197 |
-
stop_words_ids = [tokenizer(stop_word, return_tensors='pt')['input_ids'].squeeze() for stop_word in stop_words]
|
198 |
-
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
|
199 |
-
|
200 |
-
generation_config = GenerationConfig(
|
201 |
-
bos_token_id=1,
|
202 |
-
do_sample=True,
|
203 |
-
temperature=temperature,
|
204 |
-
max_new_tokens=max_new_tokens,
|
205 |
-
stopping_criteria=stopping_criteria
|
206 |
-
)
|
207 |
-
|
208 |
-
pixel_values = None
|
209 |
-
if images is not None:
|
210 |
-
pixel_values = load_image(images).to(device) # only support one image
|
211 |
-
image_query = DEFAULT_IMG_START_TOKEN + DEFAULT_IMG_END_TOKEN
|
212 |
-
prompt = prompt.replace("<image>", image_query)
|
213 |
-
|
214 |
-
elif videos is not None:
|
215 |
-
pixel_values = load_video(videos).to(device)
|
216 |
-
video_query = DEFAULT_VIDEO_START_TOKEN + DEFAULT_VIDEO_END_TOKEN
|
217 |
-
prompt = prompt.replace("<video>", video_query)
|
218 |
-
|
219 |
-
model_inputs = tokenizer([prompt], return_tensors="pt")
|
220 |
-
model_inputs.pop("token_type_ids", None)
|
221 |
-
|
222 |
-
if pixel_values is not None:
|
223 |
-
model_inputs["pixel_values"] = pixel_values
|
224 |
-
|
225 |
-
generation_output = model.generate(
|
226 |
-
**model_inputs,
|
227 |
-
generation_config=generation_config,
|
228 |
-
return_dict_in_generate=True,
|
229 |
-
output_scores=True
|
230 |
-
)
|
231 |
-
else:
|
232 |
-
generation_output = model.language_model.generate(
|
233 |
-
**model_inputs,
|
234 |
-
generation_config=generation_config,
|
235 |
-
return_dict_in_generate=True,
|
236 |
-
output_scores=True
|
237 |
-
)
|
238 |
-
|
239 |
-
preds = generation_output.sequences
|
240 |
-
outputs = tokenizer.batch_decode(preds, skip_special_tokens=True)
|
241 |
-
return outputs
|
242 |
-
|
243 |
-
class Chat:
|
244 |
-
def __init__(
|
245 |
-
self,
|
246 |
-
model_path,
|
247 |
-
device,
|
248 |
-
num_gpus=1,
|
249 |
-
load_8bit=False,
|
250 |
-
temperature=0.7,
|
251 |
-
max_new_tokens=512,
|
252 |
-
lora_path=None,
|
253 |
-
):
|
254 |
-
model, tokenizer = load_model(
|
255 |
-
model_path, device, num_gpus, load_8bit=load_8bit, lora_weights=lora_path
|
256 |
-
)
|
257 |
-
|
258 |
-
self.model = model
|
259 |
-
# self.model.language_model = deepspeed.init_inference(
|
260 |
-
# self.model.language_model, mp_size=1, dtype=torch.float16, checkpoint=None, replace_with_kernel_inject=True)
|
261 |
-
self.tokenizer = tokenizer
|
262 |
-
num_queries = model.config.num_query_tokens
|
263 |
-
|
264 |
-
self.device = device
|
265 |
-
self.dtype = model.dtype
|
266 |
-
|
267 |
-
stop_words = ["Human: ", "Assistant: ", "###", "\n\n"]
|
268 |
-
stop_words_ids = [tokenizer(stop_word, return_tensors='pt')['input_ids'].squeeze() for stop_word in stop_words]
|
269 |
-
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
|
270 |
-
|
271 |
-
self.conv = get_conv_template("husky")
|
272 |
-
|
273 |
-
self.image_query = DEFAULT_IMG_START_TOKEN + DEFAULT_IMG_END_TOKEN
|
274 |
-
self.video_query = DEFAULT_VIDEO_START_TOKEN + DEFAULT_VIDEO_END_TOKEN
|
275 |
-
|
276 |
-
self.generation_config = GenerationConfig(
|
277 |
-
bos_token_id=1,
|
278 |
-
do_sample=True,
|
279 |
-
top_k=20,
|
280 |
-
top_p=0.9,
|
281 |
-
temperature=temperature,
|
282 |
-
max_new_tokens=max_new_tokens,
|
283 |
-
stopping_criteria=stopping_criteria
|
284 |
-
)
|
285 |
-
|
286 |
-
def ask(self, text, conv, modal_type="image"):
|
287 |
-
assert modal_type in ["text", "image", "video"]
|
288 |
-
conversations = []
|
289 |
-
|
290 |
-
if len(conv.messages) > 0 or modal_type == "text":
|
291 |
-
conv.append_message(conv.roles[0], text)
|
292 |
-
elif modal_type == "image":
|
293 |
-
conv.append_message(conv.roles[0], self.image_query + "\n" + text)
|
294 |
-
else:
|
295 |
-
conv.append_message(conv.roles[0], self.video_query + "\n" + text)
|
296 |
-
|
297 |
-
conv.append_message(conv.roles[1], None)
|
298 |
-
conversations.append(conv.get_prompt())
|
299 |
-
return conversations
|
300 |
-
|
301 |
-
@torch.no_grad()
|
302 |
-
def get_image_embedding(self, image_file):
|
303 |
-
pixel_values = load_image(image_file)
|
304 |
-
pixel_values = pixel_values.unsqueeze(0).to(self.device, dtype=self.dtype)
|
305 |
-
language_model_inputs = self.model.extract_feature(pixel_values)
|
306 |
-
return language_model_inputs
|
307 |
-
|
308 |
-
@torch.no_grad()
|
309 |
-
def get_video_embedding(self, video_file):
|
310 |
-
pixel_values = load_video(video_file)
|
311 |
-
TC, H, W = pixel_values.shape
|
312 |
-
pixel_values = pixel_values.reshape(TC // 3, 3, H, W).transpose(0, 1) # [C, T, H, W]
|
313 |
-
pixel_values = pixel_values.unsqueeze(0).to(self.device, dtype=self.dtype)
|
314 |
-
assert len(pixel_values.shape) == 5
|
315 |
-
language_model_inputs = self.model.extract_feature(pixel_values)
|
316 |
-
return language_model_inputs
|
317 |
-
|
318 |
-
@torch.no_grad()
|
319 |
-
def answer(self, conversations, language_model_inputs, modal_type="image"):
|
320 |
-
model_inputs = self.tokenizer(
|
321 |
-
conversations,
|
322 |
-
return_tensors="pt",
|
323 |
-
)
|
324 |
-
model_inputs.pop("token_type_ids", None)
|
325 |
-
|
326 |
-
input_ids = model_inputs["input_ids"].to(self.device)
|
327 |
-
attention_mask = model_inputs["attention_mask"].to(self.device)
|
328 |
-
|
329 |
-
if modal_type == "text":
|
330 |
-
generation_output = self.model.language_model.generate(
|
331 |
-
input_ids=input_ids,
|
332 |
-
attention_mask=attention_mask,
|
333 |
-
generation_config=self.generation_config,
|
334 |
-
return_dict_in_generate=True,
|
335 |
-
output_scores=True
|
336 |
-
)
|
337 |
-
else:
|
338 |
-
pixel_values = model_inputs.pop("pixel_values", None)
|
339 |
-
if pixel_values is not None:
|
340 |
-
pixel_values = pixel_values.to(self.device)
|
341 |
-
|
342 |
-
generation_output = self.model.generate(
|
343 |
-
pixel_values=pixel_values,
|
344 |
-
input_ids=input_ids,
|
345 |
-
attention_mask=attention_mask,
|
346 |
-
language_model_inputs=language_model_inputs,
|
347 |
-
generation_config=self.generation_config,
|
348 |
-
return_dict_in_generate=True,
|
349 |
-
output_scores=True
|
350 |
-
)
|
351 |
-
|
352 |
-
preds = generation_output.sequences
|
353 |
-
outputs = self.tokenizer.batch_decode(preds, skip_special_tokens=True)[0]
|
354 |
-
|
355 |
-
if modal_type == "text":
|
356 |
-
skip_echo_len = len(conversations[0]) - conversations[0].count("</s>") * 3
|
357 |
-
outputs = outputs[skip_echo_len:].strip()
|
358 |
-
|
359 |
-
return outputs
|
360 |
-
|
361 |
-
if __name__ == '__main__':
|
362 |
-
# model_path = "/mnt/petrelfs/zhangqinglong/Documents/Husky/work_dirs/husky_v3/EmbodiedGPT/pretrain_0727"
|
363 |
-
model_path = "./"
|
364 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
365 |
-
chat = Chat(model_path, device=device, num_gpus=1, max_new_tokens=1024, load_8bit=False)
|
366 |
-
|
367 |
-
vision_feature = None
|
368 |
-
image_state = False
|
369 |
-
video_state = False
|
370 |
-
|
371 |
-
while True:
|
372 |
-
query = input("\n")
|
373 |
-
if query.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
|
374 |
-
if os.path.exists(query):
|
375 |
-
print("received.")
|
376 |
-
vision_feature = chat.get_image_embedding(query)
|
377 |
-
chat.conv = get_conv_template("husky").copy()
|
378 |
-
image_state = True
|
379 |
-
continue
|
380 |
-
if query.lower().endswith(('.mp4', '.mkv', '.avi', '.wmv', '.iso', ".webm")):
|
381 |
-
if os.path.exists(query):
|
382 |
-
print("received.")
|
383 |
-
vision_feature = chat.get_video_embedding(query)
|
384 |
-
chat.conv = get_conv_template("husky").copy()
|
385 |
-
video_state = True
|
386 |
-
continue
|
387 |
-
|
388 |
-
if query == "stop":
|
389 |
-
break
|
390 |
-
if query == "clear" or query == "" or query == "\n":
|
391 |
-
chat.conv = get_conv_template("husky").copy()
|
392 |
-
image_state = False
|
393 |
-
video_state = False
|
394 |
-
os.system("clear")
|
395 |
-
print("欢迎使用 husky-13b-zh 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序")
|
396 |
-
continue
|
397 |
-
|
398 |
-
if image_state:
|
399 |
-
modal_type = "image"
|
400 |
-
elif video_state:
|
401 |
-
modal_type = "video"
|
402 |
-
else:
|
403 |
-
modal_type = "text"
|
404 |
-
|
405 |
-
# image_test = "assets/husky.jpg"
|
406 |
-
# image_test = "assets/yoga.mp4"
|
407 |
-
# video_test = "assets/pretty_girl.mp4"
|
408 |
-
# video_test = "assets/stock-footage-billiards-concentrated-young-woman-playing-in-club.webm"
|
409 |
-
# video_test = "assets/stock-footage-kherson-ukraine-may-open-free-rock-music-festival-crowd-partying-at-a-rock-concert.webm"
|
410 |
-
conversations = chat.ask(text=query, conv=chat.conv, modal_type=modal_type)
|
411 |
-
outputs = chat.answer(conversations, vision_feature, modal_type=modal_type)
|
412 |
-
# NOTE: strip is important to align with the training data.
|
413 |
-
chat.conv.messages[-1][1] = outputs.strip()
|
414 |
-
|
415 |
-
print(f"Husky: \n{outputs}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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