File size: 1,636 Bytes
bc7df4b
 
 
 
72c9149
 
 
 
 
 
 
bc7df4b
 
 
 
72c9149
 
 
 
 
 
 
 
 
 
 
 
 
bc7df4b
 
 
 
 
 
 
 
 
 
 
 
 
 
72c9149
bc7df4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72c9149
 
 
 
 
bc7df4b
72c9149
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
frameworks:
- Pytorch
license: other
license_name: glm-4
license_link: LICENSE
pipeline_tag: image-text-to-text
tags:
  - glm
  - edge
inference: false
---

# GLM-Edge-V-2B

中文阅读, 点击[这里](README_zh.md)

## Inference with Transformers

### Installation

Install the transformers library from the source code:

```shell
pip install git+https://github.com/huggingface/transformers.git
```

### Inference

```python
import torch
from PIL import Image
from transformers import (
    AutoTokenizer,
    AutoImageProcessor,
    AutoModelForCausalLM,
)

url = "img.png"
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "describe this image"}]}]
image = Image.open(url)

model_dir = "THUDM/glm-edge-v-5b"

processor = AutoImageProcessor.from_pretrained(model_dir, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
)

inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_dict=True, tokenize=True, return_tensors="pt"
).to(next(model.parameters()).device)

generate_kwargs = {
    **inputs,
    "pixel_values": torch.tensor(processor(image).pixel_values).to(next(model.parameters()).device),
}
output = model.generate(**generate_kwargs, max_new_tokens=100)
print(tokenizer.decode(output[0][len(inputs["input_ids"][0]):], skip_special_tokens=True))

```

## License

The usage of this model’s weights is subject to the terms outlined in the [LICENSE](LICENSE).