Are you the author?
pip install transformers
pip install qwen_vl_utils1from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4model_path = "OpenGVLab/VideoChat-R1_7B"
5# default: Load the model on the available device(s)
6model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
7 model_path, torch_dtype="auto", device_map="auto",
8 attn_implementation="flash_attention_2"
9)
10
11# default processer
12processor = AutoProcessor.from_pretrained(model_path)
13
14video_path = "your_video.mp4"
15question = "Where is the final cup containing the object?"
16
17messages = [
18 {
19 "role": "user",
20 "content": [
21 {
22 "type": "video",
23 "video": video_path,
24 "max_pixels": 460800,
25 "nframes": 32
26 },
27 {"type": "text", "text": f"""{question}
28 Provide your final answer within the <answer> </answer> tags.
29 """},
30 ],
31 }
32]
33
34
35
36#In Qwen 2.5 VL, frame rate information is also input into the model to align with absolute time.
37# Preparation for inference
38text = processor.apply_chat_template(
39 messages, tokenize=False, add_generation_prompt=True
40)
41image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)
42inputs = processor(
43 text=[text],
44 images=image_inputs,
45 videos=video_inputs,
46 padding=True,
47 return_tensors="pt",
48 **video_kwargs,
49)
50inputs = inputs.to("cuda")
51
52# Inference
53generated_ids = model.generate(**inputs, max_new_tokens=512)
54generated_ids_trimmed = [
55 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
56]
57output_text = processor.batch_decode(
58 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
59)
60print(output_text)1
2@article{li2025videochatr1,
3 title={VideoChat-R1: Enhancing Spatio-Temporal Perception via Reinforcement Fine-Tuning},
4 author={Li, Xinhao and Yan, Ziang and Meng, Desen and Dong, Lu and Zeng, Xiangyu and He, Yinan and Wang, Yali and Qiao, Yu and Wang, Yi and Wang, Limin},
5 journal={arXiv preprint arXiv:2504.06958},
6 year={2025}
7}