测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A53072. Qwen2.5-VL 是多模态⼤语⾔模型(Vision-Language Model, VLM),能够同时处理图像与⽂本输 ⼊,实现图⽂问答与视觉描述等任务。以下代码演⽰了使⽤ Qwen2.5-VL-7B-Instruct 模型,对输⼊图 像进⾏分析并⽣成⽂本回答。请阅读以下代码,并根据描述完成空缺部分。import torch

编程题

题目描述

Qwen2.5-VL 是多模态⼤语⾔模型(Vision-Language Model, VLM),能够同时处理图像与⽂本输 ⼊,实现图⽂问答与视觉描述等任务。以下代码演⽰了使⽤ Qwen2.5-VL-7B-Instruct 模型,对输⼊图 像进⾏分析并⽣成⽂本回答。请阅读以下代码,并根据描述完成空缺部分。

import torch
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from PIL import Image
model_name = "Qwen/Qwen2.5-VL-7B-Instruct"
# 1) 加载处理器与模型
processor = ____[1]____
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    model_name,
    device_map="auto",
    torch_dtype=torch.bfloat16,
    trust_remote_code=True
)
    # 2) 读取输⼊图像
    image = ____[2]____
    # 3) 构建多模态输⼊提⽰
    messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": image},
            {"type": "text", "text": "请描述图⽚中的主要内容。"}
        ]
    }
]
text = ____[3]____
inputs = processor(text=[text], images=[image],
return_tensors="pt").to(model.device)
# 4) 模型推理⽣成回答
model = model.eval()
with ____[4]____:
    output_ids = model.generate(
        **inputs,
        max_new_tokens=128,
        temperature=0.7,
    )
    generated_ids = ____[5]____
    answer = processor.tokenizer.batch_decode(generated_ids,
skip_special_tokens=True)[0]
    print("模型回答:", answer)