from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
from PIL import Image
import torch
model_id = "peiyugeorgia/qwen2vl-crochetbench-task-c-finetuned"
model = Qwen2VLForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_id)
image = Image.open("your_crochet_image.jpg").convert("RGB")
messages = [
{
"role": "system",
"content": (
"You are a professional crochet pattern writer. "
"Examine the image of the finished crochet product carefully. "
"Write a complete set of crochet instructions in the standard style used in published patterns. "
"Use standard abbreviations: sc (single crochet), hdc (half double crochet), dc (double crochet), "
"tr (treble), ch (chain), sl st (slip stitch), rep (repeat). "
"Organize the instructions row by row or round by round (e.g., 'Rnd 1: ...', 'Row 2: ...'). "
"Keep the instructions concise and precise, as if for experienced crocheters. "
"Output only the crochet pattern. Do not add any explanations, commentary, or extra text."
)
},
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": "Generate step-by-step crochet instructions for this image."}
]
}
]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(text=[text], images=[image], return_tensors="pt").to(model.device)
with torch.no_grad():
output_ids = model.generate(**inputs, max_new_tokens=1024)
generated = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
print(generated)