Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
```py
|
| 7 |
+
from transformers import AutoConfig, AutoModel, logging
|
| 8 |
+
from transformers import AutoModel, AutoTokenizer
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
logging.set_verbosity_error() # silence HF info spam
|
| 15 |
+
|
| 16 |
+
MODEL_ID = "openbmb/MiniCPM-o-2_6"
|
| 17 |
+
|
| 18 |
+
device = "cpu"
|
| 19 |
+
cfg = AutoConfig.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 20 |
+
|
| 21 |
+
cfg.hidden_size = 28 * 6
|
| 22 |
+
cfg.num_heads = 2
|
| 23 |
+
cfg.num_hidden_layers = 2
|
| 24 |
+
cfg.intermediate_size = 32
|
| 25 |
+
|
| 26 |
+
cfg.vision_config.hidden_size = 32
|
| 27 |
+
cfg.vision_config.num_hidden_layers = 1
|
| 28 |
+
cfg.vision_config.num_attention_heads = 2
|
| 29 |
+
cfg.vision_config.intermediate_size = 32
|
| 30 |
+
cfg.vision_config.image_size = 224
|
| 31 |
+
|
| 32 |
+
cfg.audio_config.encoder_layers = 2
|
| 33 |
+
|
| 34 |
+
cfg.tts_config.llm_dim = 32
|
| 35 |
+
cfg.tts_config.hidden_size = 24
|
| 36 |
+
|
| 37 |
+
model = AutoModel.from_config(cfg, trust_remote_code=True)
|
| 38 |
+
|
| 39 |
+
print("Built tiny MiniCPM-o model on", device)
|
| 40 |
+
print("Config summary:", {k: getattr(cfg, k) for k in ["hidden_size", "num_hidden_layers", "num_attention_heads", "vocab_size"] if hasattr(cfg, k)})
|
| 41 |
+
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 43 |
+
|
| 44 |
+
image = Image.open('./image.jpg').convert('RGB')
|
| 45 |
+
question = 'What is in the image?'
|
| 46 |
+
msgs = [{'role': 'user', 'content': [image, question]}]
|
| 47 |
+
|
| 48 |
+
output_dir = "./tiny-random-minicpmo-new-version"
|
| 49 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 50 |
+
model.save_pretrained(output_dir)
|
| 51 |
+
tokenizer.save_pretrained(output_dir)
|
| 52 |
+
model.processor.save_pretrained(output_dir)
|
| 53 |
+
|
| 54 |
+
print("Inference starts here")
|
| 55 |
+
res = model.chat(
|
| 56 |
+
image=None,
|
| 57 |
+
msgs=msgs,
|
| 58 |
+
tokenizer=tokenizer
|
| 59 |
+
)
|
| 60 |
+
print(res)
|
| 61 |
+
```
|