Views
No views yet
ATMEGA328P-PU)7.62MM-3P)+5V, GND)1import torch
2from transformers import AutoProcessor, AutoModelForVision2Seq
3from PIL import Image
4
5MODEL_ID = "kingabzpro/qwen3vl-open-schematics-lora" # change me
6
7processor = AutoProcessor.from_pretrained(MODEL_ID)
8model = AutoModelForVision2Seq.from_pretrained(
9 MODEL_ID,
10 torch_dtype=torch.bfloat16,
11 device_map="auto",
12).eval()
13
14def build_prompt(example):
15 name = example.get("name") or "Unknown project"
16 ftype = example.get("type") or "unknown format"
17 return (
18 f"Project: {name}\nFormat: {ftype}\n"
19 "From the schematic image, extract all component labels and identifiers exactly as shown "
20 "(part numbers, values, footprints, net labels like +5V/GND).\n"
21 "Output only a comma-separated list. Do not generalize or add extra text."
22 )
23
24def run_inference(model_, example, max_new_tokens=256):
25 prompt = build_prompt(example)
26 messages = [{
27 "role": "user",
28 "content": [
29 {"type": "image", "image": example["image"]},
30 {"type": "text", "text": prompt},
31 ],
32 }]
33
34 inputs = processor.apply_chat_template(
35 messages,
36 tokenize=True,
37 add_generation_prompt=True,
38 return_dict=True,
39 return_tensors="pt",
40 ).to(model_.device)
41
42 with torch.inference_mode():
43 out = model_.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False)
44
45 gen = out[0][inputs["input_ids"].shape[1]:]
46 return processor.decode(gen, skip_special_tokens=True)
47
48# ---- Small usage example ----
49example = {
50 "name": "Arduino-like Board",
51 "type": "kicad",
52 "image": Image.open("schematic.png").convert("RGB"),
53}
54
55print(run_inference(model, example))
R1,10kΩ,PC6_RESET#,PC6_ADC0,PC6_ADC1,...,PB7_XTAL2,VCC,AVCC,AREF,GND,+5V,
C1,22pF,C2,100nF,C3,22pF,X1,16MHz,U1,ATMEGA328P-PU,...
ATMEGA328P-PU, +5V, GND, R, C, C16MHz,
SERVO_A, SERVO_B, SERVO_C, SERVO_D, SERVO_E, SERVO_F
+5V, 7.62MM-3P, 7.62MM-3P_1, ..., ATMEGA328P-PU, ATMEGA328P-PU_1,
GND, MBB02070C1002FCT00, ..., Y5P102K2KV16CC0224_2Even with a small dataset and a single training epoch, the fine-tuned model already shows improved semantic filtering toward schematic-level components, forming a strong base for further refinement with more data and stricter target alignment.