爱玩科技网
您的当前位置:首页【大模型】试用glm4v-9b

【大模型】试用glm4v-9b

来源:爱玩科技网
import torch
from PIL import Image
from transformers import AutoModelForCausalLM, AutoTokenizer
import os
os.environ['CUDA_VISIBLE_DEVICES']='1'

device = "cuda:0"
tokenizer = AutoTokenizer.from_pretrained("../../glm-4v-9b", trust_remote_code=True)

query = '请识别提取出图片中的文本'
image = Image.open("./stamp2.JPG").convert('RGB')
inputs = tokenizer.apply_chat_template([{"role": "user", "image": image, "content": query}],
                                       add_generation_prompt=True, tokenize=True, return_tensors="pt",
                                       return_dict=True)  # chat mode

inputs = inputs.to(device)
model = AutoModelForCausalLM.from_pretrained(
    "../../glm-4v-9b",
    device_map=device,
    torch_dtype=torch.bfloat16,
    trust_remote_code=True
)

gen_kwargs = {"max_length": 1500, "do_sample": True, "top_k": 1}
with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0]))

效果不错

因篇幅问题不能全部显示,请点此查看更多更全内容