import torchfrom PIL import Imagefrom peft import PeftModelfrom transformers import AutoModelForImageTextToText, AutoProcessor REPO_ID = "StrivingBapan/lm-eels-qwen3.8-27b-mars-lora"BASE_MODEL = "Qwen/Qwen3.8-27B"VLM_IMAGE_SIZE = (384, 288)SYSTEM_PROMPT = "You control the simulated LM-EELS snake manipulator using ONLY wrist camera images and end-effector pose telemetry (`measured` = [x_mm, y_mm, z_mm, r00, r01, r02, r10, r11, r12] in world frame).\nGoal: pick up the test tube from the ground, place it upright inside the stationary sample holder mounted on the Mars helicopter mockup, release the gripper, withdraw clearly away from the tube, and call finish once the tube is stably seated upright in the holder.\n\nOutput ONLY a single valid JSON object (no markdown fences, no extra prose) matching one of:\n1. {\"move_mm\": [dx, dy, dz], \"rotate_deg\": [axis, deg] | null, \"gripper\": g | null}\n - move_mm: world-frame EEF translation in mm (norm <= 60 mm; use 30-60 mm in clear space, 3-25 mm near grasp/rim/insertion).\n - rotate_deg: null or [axis, deg] where axis is \"x\"|\"y\"|\"z\" or unit [ax, ay, az] and |deg| <= 45.\n - gripper: 0.0 (closed) to 1.0 (open), or null to hold current opening (~0.32 for grasp approach, 0.0 to grip, 0.55 to release).\n2. {\"finish\": true} (only after releasing the tube inside the holder bore and withdrawing clear).\n\nExecution rules:\n- Orient gripper downward above the tube before descending to grasp; close gripper (0.0) to grip.\n- Lift vertically clear of the ground/mockup before rotating the tube upright.\n- Align the tube bottom over the holder mouth rim before descending into the bore; if near-rim misalignment or slip occurs, lift/retreat and recover.\n- If an error occurs (e.g., IK/joint limit/settle miss), adjust with a smaller or retreating move rather than repeating the same blocked move." processor = AutoProcessor.from_pretrained(REPO_ID)if processor.tokenizer.pad_token is None: processor.tokenizer.pad_token = processor.tokenizer.eos_tokenprocessor.tokenizer.padding_side = "left" base_model = AutoModelForImageTextToText.from_pretrained( BASE_MODEL, dtype=torch.bfloat16, device_map="auto",)model = PeftModel.from_pretrained(base_model, REPO_ID)model.eval() imgs = [ Image.open("prev_wrist.png").convert("RGB").resize(VLM_IMAGE_SIZE, Image.Resampling.BILINEAR), Image.open("curr_wrist.png").convert("RGB").resize(VLM_IMAGE_SIZE, Image.Resampling.BILINEAR),] turn_text = ( "Recent history: none (initial observation).\n" "Current observation:\n" "measured=[...]\n" "error=null\n" "Wrist images attached in chronological order: [previous, current].\n" "Respond with the next JSON step command only.") messages = [ {"role": "system", "content": SYSTEM_PROMPT}, { "role": "user", "content": [ {"type": "image", "image": imgs[0]}, {"type": "image", "image": imgs[1]}, {"type": "text", "text": turn_text}, ], },] prompt = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=False,)inputs = processor(text=[prompt], images=[imgs], padding=True, return_tensors="pt").to(model.device) with torch.no_grad(): out_ids = model.generate(**inputs, max_new_tokens=64, do_sample=False) in_len = inputs["input_ids"].shape[1]print(processor.batch_decode(out_ids[:, in_len:], skip_special_tokens=False)[0])