import gradio as gr from ultralytics import YOLO from PIL import Image import os from huggingface_hub import hf_hub_download import numpy as np # --- 1. SETUP & MODEL LOADING --- MODEL_REPO = "youkii-xr/hieroglyphic-detection" MODEL_FILENAME = "best.pt" # Fix for the Ultralytics config warning in containers os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics" try: print("System: Downloading model weights...") model_path = hf_hub_download( repo_id=MODEL_REPO, filename=MODEL_FILENAME, token=os.environ.get("HF_TOKEN") ) model = YOLO(model_path) print("System: Model loaded successfully.") except Exception as e: print(f"Error: {e}") model = None # --- 2. DETECTION LOGIC --- def detect_hieroglyphs(image: Image.Image, conf_threshold: float = 0.25): if image is None: return None, None if model is None: return None, {"error": "Model failed to load"} try: results = model.predict(source=image, conf=conf_threshold, iou=0.45, imgsz=640, verbose=False, device='cpu', max_det=300) # Visual annotated_array = results[0].plot() annotated_image = Image.fromarray(annotated_array[..., ::-1]) # Data detections = [] gardiner_counts = {} for box in results[0].boxes: if box.cls.numel() > 0: cls_id = int(box.cls[0]) if 0 <= cls_id < len(model.names): code = model.names[cls_id] conf = float(box.conf[0]) if code not in gardiner_counts: gardiner_counts[code] = 0 gardiner_counts[code] += 1 detections.append({"code": code, "confidence": round(conf, 2)}) summary = { "status": "success", "total_found": len(detections), "counts": gardiner_counts } return annotated_image, summary except Exception as e: return None, {"error": str(e)} # --- 3. UI/UX: THE EGYPTIAN NIGHT STYLE --- custom_css = """ @import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&display=swap'); :root { --body-background-fill: #050510 !important; --background-fill-primary: #0a0f1e !important; --border-color-primary: #d4af37 !important; /* Gold */ --text-body: #e0e7ff !important; --gold-glow: 0 0 15px rgba(212, 175, 55, 0.3); } body, .gradio-container { background: radial-gradient(circle at 50% 0%, #1a1f35 0%, #050510 100%) !important; font-family: 'Cinzel', serif !important; } /* --- THE GLASS CARDS --- */ .card { background: rgba(10, 15, 30, 0.6) !important; border: 1px solid rgba(212, 175, 55, 0.3) !important; border-radius: 20px; padding: 24px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4); backdrop-filter: blur(10px); } .card-title { font-size: 18px; font-weight: 700; color: #d4af37; text-transform: uppercase; letter-spacing: 2px; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid rgba(212, 175, 55, 0.1); padding-bottom: 10px; } /* --- THE TERMINAL WINDOW (Quick Start) --- */ .terminal-window { background: #0f0f15; border: 1px solid #333; border-radius: 12px; overflow: hidden; font-family: 'Courier New', monospace; box-shadow: 0 10px 30px rgba(0,0,0,0.8); } .terminal-header { background: #1a1a20; padding: 10px 15px; display: flex; gap: 8px; border-bottom: 1px solid #333; } .dot { width: 12px; height: 12px; border-radius: 50%; } .red { background: #ff5f56; } .yellow { background: #ffbd2e; } .green { background: #27c93f; } .terminal-body { padding: 20px; color: #a9b1d6; font-size: 13px; line-height: 1.6; } .json-key { color: #7aa2f7; } .json-string { color: #e0af68; } /* --- BUTTONS --- */ button.primary-btn { background: linear-gradient(135deg, #b8860b 0%, #d4af37 100%) !important; border: 1px solid #ffd700 !important; color: #000 !important; font-weight: bold !important; font-family: 'Cinzel', serif !important; text-transform: uppercase; letter-spacing: 1px; transition: all 0.3s ease; box-shadow: var(--gold-glow); } button.primary-btn:hover { transform: translateY(-2px); box-shadow: 0 0 25px rgba(212, 175, 55, 0.6); } /* --- ELEMENT VISIBILITY --- */ .gradio-image, .gradio-json { background: transparent !important; border: none !important; } """ # HTML for the Header (Floating Style) header_html = """
HIEROGLYPHIC INTELLIGENCE SYSTEM
This system utilizes the YOLOv8 architecture to detect and classify Gardiner codes.
> Model: YOLOv8 Custom
> Security: MCP Standard