""" Gradio demo for TypoRef YOLOv8 Historical Document Detector. This script defines a simple Gradio interface that allows a user to upload an image of a historical document page. The interface loads a YOLOv8 object detection model and applies it to the input image, overlaying bounding boxes around detected ornaments, typography and other decorative elements. The resulting annotated image is returned for display in the browser. By default the demo uses a small pretrained YOLOv8 model from the ``ultralytics`` repository (``yolov8n``) so that it can run without any custom weights. If you have uploaded your own fine‑tuned weights to Hugging Face (e.g. ``martinbadrous/TypoRef-YOLOv8-Historical-Document-Detection``) you can replace the ``model_path`` value below with the repository ID for your model. The ``ultralytics`` package will automatically load the model from Hugging Face when given a repo ID. To launch the demo locally run ``python app.py``. When running as a Hugging Face Space this file will be executed automatically. """ import gradio as gr from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image import numpy as np from PIL import Image, ImageDraw # Load your trained Keras model model = load_model("model.h5") def detect_objects(img): # Preprocess img_resized = img.resize((640, 640)) x = np.array(img_resized) / 255.0 x = np.expand_dims(x, axis=0) # Predict preds = model.predict(x) conf = float(np.max(preds)) # Draw a placeholder box (you can replace with your actual detection logic) draw = ImageDraw.Draw(img) draw.rectangle([100, 280, 540, 580], outline="red", width=3) draw.text((110, 260), f"Prediction conf {conf:.2f}", fill="red") return img demo = gr.Interface( fn=detect_objects, inputs=gr.Image(type="pil", label="Upload a historical document page"), outputs=gr.Image(label="Predicted ornaments / layout regions"), title="TypoRef: Historical Document Ornament Detection", description="Fine-tuned model (.h5) for detecting typographical ornaments in historical prints." ) if __name__ == "__main__": demo.launch()