1import torch
2from transformers import AutoProcessor, AutoModel
3
4model_name = "Dream-org/Dream-VL-7B"
5
6model = AutoModel.from_pretrained(
7 model_name,
8 torch_dtype=torch.bfloat16,
9 trust_remote_code=True,
10).to('cuda')
11
12processor = AutoProcessor.from_pretrained(
13 model_name,
14 trust_remote_code=True
15)
16
17####### Method 1
18from PIL import Image
19import requests
20url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
21image = Image.open(requests.get(url, stream=True).raw)
22messages = [
23 {
24 "role": "user","content": [{"type": "image"}, {"type": "text", "text": "Describe this image"}]
25 }
26]
27text = processor.apply_chat_template(
28 messages, tokenize=False, add_generation_prompt=True
29)
30print(text)
31inputs = processor(
32 text=[text], images=[image], padding=True, return_tensors="pt"
33)
34
35####### Method 2: use qwen_vl_utils
36# messages = [
37# {
38# "role": "user",
39# "content": [
40# {
41# "type": "image",
42# "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
43# },
44# {"type": "text", "text": "Describe this image."},
45# ],
46# }
47# ]
48# text = processor.apply_chat_template(
49# messages, tokenize=False, add_generation_prompt=True
50# )
51# from qwen_vl_utils import process_vision_info
52# image_inputs, video_inputs = process_vision_info(messages)
53# inputs = processor(
54# text=[text],
55# images=image_inputs,
56# videos=video_inputs,
57# padding=True,
58# return_tensors="pt",
59# )
60
61inputs = inputs.to("cuda")
62input_ids = inputs.pop("input_ids")
63output = model.diffusion_generate(
64 input_ids,
65 max_new_tokens=128,
66 output_history=True,
67 return_dict_in_generate=True,
68 steps=128,
69 temperature=0.1,
70 top_p=1,
71 alg="maskgit_plus",
72 alg_temp=0,
73 use_cache=False,
74 **inputs
75)
76
77generations = [
78 processor.tokenizer.decode(g[len(p):].cpu().tolist())
79 for p, g in zip(input_ids, output.sequences)
80]
81
82for j in range(len(messages)):
83 print("output:", j, generations[j].split(processor.tokenizer.eos_token)[0])
84
85
86# output: The image depicts a serene beach scene featuring a young woman and a golden retriever.
87# The woman, dressed in a plaid shirt and dark pants, is seated on the sandy shore, smiling warmly at the camera.
88# The golden retriever, adorned with a colorful harness, sits attentively beside her, its gaze fixed on the woman.
89# The background reveals the vast expanse of the ocean, with waves gently kissing the shore. The sky above is a clear blue, suggesting a sunny day.
90# The overall atmosphere exudes a sense of peace and companionship between the woman and her dog.
91