Views
No views yet
1from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
2import torch
3model_path = "Ssdaizi/Qwen3-VL-2B-Sono"
4
5# Load the model
6model = Qwen3VLForConditionalGeneration.from_pretrained(
7 model_path,
8 dtype=torch.bfloat16,
9 device_map="auto",
10)
11
12processor = AutoProcessor.from_pretrained(model_path)
13
14messages = [
15 {
16 "role": "user",
17 "content": [
18 {
19 "type": "image",
20 "image": "test1.png",
21 },
22 {
23 "type": "text",
24 "text": "Is this a benign lesion or a malignant lesion?",
25 }
26 ],
27 }
28]
29
30# Preparation for inference
31inputs = processor.apply_chat_template(
32 messages,
33 tokenize=True,
34 add_generation_prompt=True,
35 return_dict=True,
36 return_tensors="pt",
37)
38inputs = inputs.to(model.device)
39
40# Generate response
41generated_ids = model.generate(
42 **inputs,
43 max_new_tokens=512,
44 top_p=0.8,
45 top_k=20,
46 temperature=0.7,
47 repetition_penalty=1.0
48)
49
50# Remove input tokens from generated output
51generated_ids_trimmed = [
52 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
53]
54
55output_text = processor.batch_decode(
56 generated_ids_trimmed,
57 skip_special_tokens=True,
58 clean_up_tokenization_spaces=False,
59)
60
61print(output_text[0])