chenkq commited on
Commit
986550c
1 Parent(s): 4c1034a

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +34 -0
README.md ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Quickstart
2
+
3
+ ```python
4
+ import torch
5
+ import requests
6
+ from PIL import Image
7
+ from transformers import AutoModelForCausalLM, LlamaTokenizer
8
+
9
+ tokenizer = LlamaTokenizer.from_pretrained('lmsys/vicuna-7b-v1.5')
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ 'THUDM/cogvlm-chat-hf',
12
+ torch_dtype=torch.bfloat16,
13
+ low_cpu_mem_usage=True,
14
+ trust_remote_code=True
15
+ ).to('cuda').eval()
16
+
17
+ query = 'Describe this image'
18
+ image = Image.open(requests.get('https://github.com/THUDM/CogVLM/blob/main/examples/1.png?raw=true', stream=True).raw).convert('RGB')
19
+ inputs = model.build_conversation_input_ids(tokenizer, query=query, history=[], images=[image])
20
+ inputs = {
21
+ 'input_ids': inputs['input_ids'].unsqueeze(0).to('cuda'),
22
+ 'token_type_ids': inputs['token_type_ids'].unsqueeze(0).to('cuda'),
23
+ 'attention_mask': inputs['attention_mask'].unsqueeze(0).to('cuda'),
24
+ 'images': [[inputs['images'][0].to('cuda').to(torch.bfloat16)]],
25
+ }
26
+ gen_kwargs = {"max_length": 2048, "do_sample": False}
27
+
28
+ with torch.no_grad():
29
+ outputs = model.generate(**inputs, **gen_kwargs)
30
+ outputs = outputs[:, inputs['input_ids'].shape[1]:]
31
+ print(tokenizer.decode(outputs[0]))
32
+
33
+ ```
34
+