1import os
2import torch
3from vllm import LLM, SamplingParams
4from transformers import AutoProcessor, AutoTokenizer
5from qwen_vl_utils import process_vision_info
6
7# Set model path
8model_path = "Video-R1/Video-R1-7B"
9
10# Set video path and question
11video_path = "./src/example_video/video1.mp4"
12question = "Which move motion in the video lose the system energy?"
13
14# Choose the question type from 'multiple choice', 'numerical', 'OCR', 'free-form', 'regression'
15problem_type = 'free-form'
16
17# Initialize the LLM
18llm = LLM(
19 model=model_path,
20 tensor_parallel_size=1,
21 max_model_len=81920,
22 gpu_memory_utilization=0.8,
23 limit_mm_per_prompt={"video": 1, "image": 1},
24)
25
26sampling_params = SamplingParams(
27 temperature=0.1,
28 top_p=0.001,
29 max_tokens=1024,
30)
31
32# Load processor and tokenizer
33processor = AutoProcessor.from_pretrained(model_path)
34tokenizer = AutoTokenizer.from_pretrained(model_path)
35tokenizer.padding_side = "left"
36processor.tokenizer = tokenizer
37
38# Prompt template
39QUESTION_TEMPLATE = (
40 "{Question}\n"
41 "Please think about this question as if you were a human pondering deeply. "
42 "Engage in an internal dialogue using expressions such as 'let me think', 'wait', 'Hmm', 'oh, I see', 'let's break it down', etc, or other natural language thought expressions "
43 "It's encouraged to include self-reflection or verification in the reasoning process. "
44 "Provide your detailed reasoning between the <think> and </think> tags, and then give your final answer between the <answer> and </answer> tags."
45)
46
47# Question type
48TYPE_TEMPLATE = {
49 "multiple choice": " Please provide only the single option letter (e.g., A, B, C, D, etc.) within the <answer> </answer> tags.",
50 "numerical": " Please provide the numerical value (e.g., 42 or 3.14) within the <answer> </answer> tags.",
51 "OCR": " Please transcribe text from the image/video clearly and provide your text answer within the <answer> </answer> tags.",
52 "free-form": " Please provide your text answer within the <answer> </answer> tags.",
53 "regression": " Please provide the numerical value (e.g., 42 or 3.14) within the <answer> </answer> tags."
54}
55
56# Construct multimodal message
57messages = [
58 {
59 "role": "user",
60 "content": [
61 {
62 "type": "video",
63 "video": video_path,
64 "max_pixels": 200704, # max pixels for each frame
65 "nframes": 32 # max frame number
66 },
67 {
68 "type": "text",
69 "text": QUESTION_TEMPLATE.format(Question=question) + TYPE_TEMPLATE[problem_type]
70 },
71 ],
72 }
73]
74
75# Convert to prompt string
76prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
77
78# Process video input
79image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)
80
81# Prepare vllm input
82llm_inputs = [{
83 "prompt": prompt,
84 "multi_modal_data": {"video": video_inputs[0]},
85 "mm_processor_kwargs": {key: val[0] for key, val in video_kwargs.items()},
86}]
87
88# Run inference
89outputs = llm.generate(llm_inputs, sampling_params=sampling_params)
90output_text = outputs[0].outputs[0].text
91
92print(output_text)