import base64from io import BytesIOfrom openai import OpenAIfrom PIL import Image, ImageOpsfrom pydantic import BaseModel, ConfigDict class SignatureItem(BaseModel): model_config = ConfigDict(extra="forbid") role: str bbox_2d: list[int] class SignatureResponse(BaseModel): model_config = ConfigDict(extra="forbid") answer: str is_present: bool count: int signatures: list[SignatureItem] def encode_upright_image(image_path: str) -> tuple[str, int, int]: img = ImageOps.exif_transpose(Image.open(image_path)).convert("RGB") width, height = img.size buf = BytesIO() img.save(buf, format="JPEG", quality=95) b64 = base64.b64encode(buf.getvalue()).decode("utf-8") return b64, width, height client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")b64_img, width, height = encode_upright_image("document.jpg") completion = client.chat.completions.parse( model="Vishva007/Qwen3.5-9B-Signature-W4A16", temperature=0.0, messages=[ { "role": "user", "content": [ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_img}"}}, {"type": "text", "text": "Identify and locate all signatures present on this document."}, ], } ], response_format=SignatureResponse,) result: SignatureResponse = completion.choices[0].message.parsedprint(result.model_dump_json(indent=2)) # Convert normalized [0, 1000] coordinates back to pixel coordinatesfor sig in result.signatures: x1, y1, x2, y2 = sig.bbox_2d pixel_box = [ int(x1 / 1000 * width), int(y1 / 1000 * height), int(x2 / 1000 * width), int(y2 / 1000 * height), ] print(f"Role: {sig.role} | Pixel Box: {pixel_box}")