My pathon script can't analysing Cervical Spine MRI Image with lingshu-medical-mllm/Lingshu-7B

#7
by ak9393 - opened

import torch
from PIL import Image
from transformers import AutoTokenizer, Qwen2_5_VLForConditionalGeneration

Step 1: 加载模型

model_dir = "./Lingshu-7B"
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
model_dir,
trust_remote_code=True,
torch_dtype=torch.float16,
local_files_only=True
)

tokenizer = AutoTokenizer.from_pretrained(model_dir)

Step 2: 准备输入

image_path = "1.jpg"
question = "这是一张颈椎MRI影像,请分析判断该影像显示的脊髓病变,是否需要手术?"

prompt = f"{image_path}\n{question}"

Step 3: 编码

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
image = Image.open(image_path).convert("RGB")

Step 4: 生成(不传 images 参数)

with torch.inference_mode():
output = model.generate(
**inputs,
max_new_tokens=512,
do_sample=False
)

Step 5: 输出

generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("✅ 生成结果:")
print(generated_text)

Sign up or log in to comment