Visual Question Answering
Transformers
English
videollama2_mistral
text-generation
multimodal large language model
large video-language model
Inference Endpoints
Sicong commited on
Commit
e823ebd
1 Parent(s): 512de2c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +14 -70
README.md CHANGED
@@ -57,86 +57,30 @@ tags:
57
 
58
  ## 🤖 Inference with VideoLLaMA2
59
  ```python
60
- import torch
61
- import transformers
62
-
63
  import sys
64
  sys.path.append('./')
65
- from videollama2.conversation import conv_templates, SeparatorStyle
66
- from videollama2.constants import DEFAULT_MMODAL_TOKEN, MMODAL_TOKEN_INDEX
67
- from videollama2.mm_utils import get_model_name_from_path, tokenizer_MMODAL_token, KeywordsStoppingCriteria, process_video, process_image
68
- from videollama2.model.builder import load_pretrained_model
69
 
70
 
71
  def inference():
72
- # Video Inference
73
- paths = ['assets/cat_and_chicken.mp4']
74
- questions = ['What animals are in the video, what are they doing, and how does the video feel?']
75
- # Reply:
76
- # The video features a kitten and a baby chick playing together. The kitten is seen laying on the floor while the baby chick hops around. The two animals interact playfully with each other, and the video has a cute and heartwarming feel to it.
77
- modal_list = ['video']
78
 
79
  # Video Inference
80
- paths = ['assets/sora.mp4']
81
- questions = ['Please describe this video.']
82
- # Reply:
83
- # The video features a series of colorful kites flying in the sky. The kites are first seen flying over trees, and then they are shown flying in the sky. The kites come in various shapes and colors, including red, green, blue, and yellow. The video captures the kites soaring gracefully through the air, with some kites flying higher than others. The sky is clear and blue, and the trees below are lush and green. The kites are the main focus of the video, and their vibrant colors and intricate designs are highlighted against the backdrop of the sky and trees. Overall, the video showcases the beauty and artistry of kite-flying, and it is a delight to watch the kites dance and glide through the air.
84
- modal_list = ['video']
85
-
86
  # Image Inference
87
- paths = ['assets/sora.png']
88
- questions = ['What is the woman wearing, what is she doing, and how does the image feel?']
89
- # Reply:
90
- # The woman in the image is wearing a black coat and sunglasses, and she is walking down a rain-soaked city street. The image feels vibrant and lively, with the bright city lights reflecting off the wet pavement, creating a visually appealing atmosphere. The woman's presence adds a sense of style and confidence to the scene, as she navigates the bustling urban environment.
91
- modal_list = ['image']
92
-
93
- # 1. Initialize the model.
94
  model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B-Base'
95
- model_name = get_model_name_from_path(model_path)
96
- tokenizer, model, processor, context_len = load_pretrained_model(model_path, None, model_name)
97
- model = model.to('cuda:0')
98
- conv_mode = 'llama_2'
99
-
100
- # 2. Visual preprocess (load & transform image or video).
101
- if modal_list[0] == 'video':
102
- tensor = process_video(paths[0], processor, model.config.image_aspect_ratio).to(dtype=torch.float16, device='cuda', non_blocking=True)
103
- default_mm_token = DEFAULT_MMODAL_TOKEN["VIDEO"]
104
- modal_token_index = MMODAL_TOKEN_INDEX["VIDEO"]
105
- else:
106
- tensor = process_image(paths[0], processor, model.config.image_aspect_ratio)[0].to(dtype=torch.float16, device='cuda', non_blocking=True)
107
- default_mm_token = DEFAULT_MMODAL_TOKEN["IMAGE"]
108
- modal_token_index = MMODAL_TOKEN_INDEX["IMAGE"]
109
- tensor = [tensor]
110
-
111
- # 3. Text preprocess (tag process & generate prompt).
112
- question = default_mm_token + "\n" + questions[0]
113
- conv = conv_templates[conv_mode].copy()
114
- conv.append_message(conv.roles[0], question)
115
- conv.append_message(conv.roles[1], None)
116
- prompt = conv.get_prompt()
117
- input_ids = tokenizer_MMODAL_token(prompt, tokenizer, modal_token_index, return_tensors='pt').unsqueeze(0).to('cuda:0')
118
-
119
- # 4. Generate a response according to visual signals and prompts.
120
- stop_str = conv.sep if conv.sep_style in [SeparatorStyle.SINGLE] else conv.sep2
121
- # keywords = ["<s>", "</s>"]
122
- keywords = [stop_str]
123
- stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
124
-
125
- with torch.inference_mode():
126
- output_ids = model.generate(
127
- input_ids,
128
- images_or_videos=tensor,
129
- modal_list=modal_list,
130
- do_sample=True,
131
- temperature=0.2,
132
- max_new_tokens=1024,
133
- use_cache=True,
134
- stopping_criteria=[stopping_criteria],
135
- )
136
-
137
- outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
138
- print(outputs[0])
139
 
 
140
 
141
  if __name__ == "__main__":
142
  inference()
 
57
 
58
  ## 🤖 Inference with VideoLLaMA2
59
  ```python
 
 
 
60
  import sys
61
  sys.path.append('./')
62
+ from videollama2 import model_init, mm_infer
63
+ from videollama2.utils import disable_torch_init
 
 
64
 
65
 
66
  def inference():
67
+ disable_torch_init()
 
 
 
 
 
68
 
69
  # Video Inference
70
+ modal = 'video'
71
+ modal_path = 'assets/cat_and_chicken.mp4'
72
+ instruct = 'What animals are in the video, what are they doing, and how does the video feel?'
73
+
 
 
74
  # Image Inference
75
+ modal = 'image'
76
+ modal_path = 'assets/sora.png'
77
+ instruct = 'What is the woman wearing, what is she doing, and how does the image feel?'
78
+
 
 
 
79
  model_path = 'DAMO-NLP-SG/VideoLLaMA2-7B-Base'
80
+ model, processor, tokenizer = model_init(model_path)
81
+ output = mm_infer(processor[modal](modal_path), instruct, model=model, tokenizer=tokenizer, do_sample=False, modal=modal)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ print(output)
84
 
85
  if __name__ == "__main__":
86
  inference()