Repoaner commited on
Commit
458a86b
·
verified ·
1 Parent(s): 982f7e0

Upload LLaVA-Next-3D/data_precessing/cogvlm.py with huggingface_hub

Browse files
LLaVA-Next-3D/data_precessing/cogvlm.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from transformers import AutoModelForCausalLM, LlamaTokenizer
4
+
5
+ tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
6
+ model = AutoModelForCausalLM.from_pretrained(
7
+ 'THUDM/cogvlm-grounding-generalist-hf',
8
+ torch_dtype=torch.bfloat16,
9
+ low_cpu_mem_usage=True,
10
+ trust_remote_code=True,
11
+
12
+ ).to('cuda').eval()
13
+
14
+ query = 'Can you provide a description of the image and include the coordinates [[x0,y0,x1,y1]] for each mentioned object?'
15
+ image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/4.jpg?raw=true', stream=True).raw).convert('RGB')
16
+ inputs = model.build_conversation_input_ids(tokenizer, query=query, images=[image])
17
+ inputs = {
18
+ 'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
19
+ 'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
20
+ 'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
21
+ 'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
22
+ }
23
+ gen_kwargs = {"max_length": 2048, "do_sample": False}
24
+
25
+ with torch.no_grad():
26
+ outputs = model.generate(**inputs, **gen_kwargs)
27
+ outputs = outputs[:, inputs['input_ids'].shape[1]:]
28
+ print(tokenizer.decode(outputs[0]))