import gradio as gr from transformers import pipeline from PIL import Image import numpy as np # Load the model detector = pipeline("object-detection", model="mdefrance/yolos-tiny-signature-detection") # Fix: Convert NumPy to PIL image def detect_signature(image): if image is None: return {"error": "❌ Please draw or upload a signature before submitting."} # Normalize input structure if isinstance(image, dict): image = image.get("image", None) if image is None: return {"error": "❌ No image data received."} pil_image = Image.fromarray(np.array(image).astype("uint8"), "RGB") results = detector(pil_image) valid = [r for r in results if r["score"] > 0.3] if not valid: return {"error": "❌ No valid signature detected. Try again."} return {"message": "✅ Signature detected.", "detections": valid} # Interface gr.Interface( fn=detect_signature, inputs=gr.Sketchpad(label="✍️ Draw Your Signature", canvas_size=(512, 256)), outputs=gr.JSON(label="Detection Result"), title="Signature Validator", description="Draw your signature using your mouse or finger. Model will detect and validate it." ).launch()