Views
No views yet

1import torch
2import transformers
3import gc
4from videoxlpro.videoxlpro.demo_utils import process_video, load_image_processor, generate_response
5from transformers import AutoTokenizer, AutoModelForCausalLM
6import warnings
7
8# 禁用一些警告
9transformers.logging.set_verbosity_error()
10warnings.filterwarnings('ignore')
11
12# 设置设备
13device = 'cuda' if torch.cuda.is_available() else 'cpu'
14
15# 模型路径
16model_path = "lxr2003/Video-XL-Pro-3B"
17video_path = "/path/to/your/example_video.mp4"
18
19# 使用 Auto 类加载模型
20# 使用 Auto 类加载模型
21model = AutoModelForCausalLM.from_pretrained(
22 model_path,
23 low_cpu_mem_usage=True,
24 torch_dtype=torch.float16,
25 attn_implementation="flash_attention_2",
26 device_map=device,
27 trust_remote_code=True
28)
29tokenizer = AutoTokenizer.from_pretrained(
30 model_path,
31 trust_remote_code=True
32)
33
34image_processor = load_image_processor(model, tokenizer)
35
36max_frames_num = 128
37
38# 处理视频
39video_tensor,time_embed = process_video(video_path,tokenizer, image_processor, model.device, max_frames_num)
40
41# 生成参数
42gen_kwargs = {
43 "do_sample": True,
44 "temperature": 0.01,
45 "top_p": 0.001,
46 "num_beams": 1,
47 "use_cache": True,
48 "max_new_tokens": 256
49}
50
51# 文本提示
52prompt = "Describe this video."
53
54text = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image>\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
55
56response = generate_response(model, tokenizer, text, video_tensor,time_embed, gen_kwargs)
57
58# 4. 输出结果
59print("\n===== 生成的回答 =====")
60print(response)
61'example_video.mp4' with your actual video path.