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 import tempfile # --- 1. SETUP & MODEL LOADING --- MODEL_REPO = "youkii-xr/hieroglyphic-detection" MODEL_FILENAME = "best.pt" os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics" try: print("System: Initializing Rosetta Decoder Core...") 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. LOGIC --- def generate_human_report(detections, counts): if not detections: return "Rosetta Decoder detects no identifiable Gardiner codes in this image." total = len(detections) sorted_counts = sorted(counts.items(), key=lambda item: item[1], reverse=True) report = f"🔓 DECODING SEQUENCE COMPLETE\n" report += f"===================================\n" report += f"Glyph Density: {total} Symbols Identified\n\n" report += "🔣 GARDINER CODE INVENTORY:\n" for code, count in sorted_counts: report += f"• Code [{code}]: {count} instance(s)\n" report += f"\n===================================\n" report += f"CONFIDENCE: High\n" report += f"STATUS: Digitized & Ready for Translation." return report def detect_hieroglyphs(image: Image.Image, conf_threshold: float = 0.25): if image is None: return None, None, "Input source required.", None if model is None: return None, {"error": "Model failed"}, "Critical Error: Weights not loaded.", None try: results = model.predict(source=image, conf=conf_threshold, iou=0.45, imgsz=640, verbose=False, device='cpu', max_det=300) annotated_array = results[0].plot() annotated_image = Image.fromarray(annotated_array[..., ::-1]) temp_dir = tempfile.gettempdir() save_path = os.path.join(temp_dir, "rosetta_decoded_result.jpg") annotated_image.save(save_path) 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_json = { "status": "success", "total_found": len(detections), "counts": gardiner_counts } text_report = generate_human_report(detections, gardiner_counts) return annotated_image, summary_json, text_report, save_path except Exception as e: return None, {"error": str(e)}, f"System Failure: {str(e)}", None # --- 3. UI STYLING --- cursor_url = "url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgdmlld0JveD0iMCAwIDMyIDMyIj4KICA8ZyBmaWxsPSJub25lIiBzdHJva2U9IiNkNGFmMzciIHN0cm9rZS13aWR0aD0iMS41Ij4KICAgIDxwYXRoIGQ9Ik0xNiw4IEM2LDIwIDI2LDIwIDE2LDggWiIgZmlsbD0icmdiYSgyMTIsIDE3NSwgNTUsIDAuMSkiLz4KICAgIDxjaXJjbGUgY3g9IjE2IiBjeT0iMTUiIHI9IjMiIGZpbGw9IiNkNGFmMzciLz4KICAgIDxwYXRoIGQ9Ik0xNiwyMiBMMTYsMjggTDEwLDI4Ii8+CiAgPC9nPgo8L3N2Zz4=')" custom_css = f""" @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@300;400;600;700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&display=swap'); /* --- 1. DEFAULT (DARK/NIGHT MODE) --- */ :root, .dark, body {{ --bg-gradient: radial-gradient(circle at 50% 0%, #0a0a2e 0%, #000000 100%); --card-bg: rgba(15, 15, 35, 0.7); --text-primary: #e0e7ff; --text-accent: #d4af37; --border-color: #d4af37; --btn-grad: linear-gradient(135deg, #b8860b 0%, #d4af37 100%); --btn-text: #000; --info-bg: rgba(212, 175, 55, 0.08); --info-border: #d4af37; --glow-color: rgba(212, 175, 55, 0.4); --input-bg: rgba(0,0,0,0.2); }} /* --- 2. LIGHT MODE OVERRIDE (PAPYRUS) --- */ body.light-mode, .gradio-container.light-mode {{ --bg-gradient: linear-gradient(135deg, #f0e6d2 0%, #e6dcc3 100%) !important; --card-bg: rgba(255, 255, 255, 0.6) !important; --text-primary: #3d342b !important; --text-accent: #8b4513 !important; --border-color: #8b4513 !important; --btn-grad: linear-gradient(135deg, #cd853f 0%, #8b4513 100%) !important; --btn-text: #fff !important; --info-bg: rgba(139, 69, 19, 0.05) !important; --info-border: #8b4513 !important; --glow-color: rgba(139, 69, 19, 0.3) !important; --input-bg: rgba(255, 255, 255, 0.7) !important; color: var(--text-primary) !important; }} /* --- GLOBAL LAYOUT --- */ body, .gradio-container {{ background: var(--bg-gradient) !important; font-family: 'Cairo', sans-serif !important; color: var(--text-primary) !important; cursor: {cursor_url} 16 16, auto !important; -webkit-font-smoothing: antialiased; transition: background 0.5s ease, color 0.5s ease; }} button, a, .cursor-pointer {{ cursor: {cursor_url} 16 16, pointer !important; }} /* --- TRAIL --- */ .gold-dust {{ position: fixed; width: 6px; height: 6px; background: var(--text-accent); border-radius: 50%; pointer-events: none; z-index: 9999; animation: fadeDust 0.6s linear forwards; box-shadow: 0 0 5px var(--text-accent); }} @keyframes fadeDust {{ 0% {{ opacity: 1; transform: scale(1); }} 100% {{ opacity: 0; transform: scale(0); }} }} /* --- CARDS & TITLES --- */ .card {{ background: var(--card-bg) !important; border: 1px solid rgba(128, 128, 128, 0.2) !important; border-radius: 12px; padding: 24px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); backdrop-filter: blur(12px); margin-bottom: 24px; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); color: var(--text-primary) !important; }} .card:hover {{ transform: translateY(-4px); border-color: var(--border-color) !important; box-shadow: 0 10px 40px rgba(0,0,0,0.2), 0 0 20px var(--glow-color); }} .card-title {{ font-family: 'Cairo', sans-serif; font-size: 20px; font-weight: 700; color: var(--text-accent) !important; text-transform: uppercase; letter-spacing: 2px; border-bottom: 1px solid rgba(128,128,128, 0.2); padding-bottom: 15px; margin-bottom: 20px; display: flex; align-items: center; justify-content: center; gap: 8px; }} /* --- BUTTONS --- */ button.primary-btn {{ background: var(--btn-grad) !important; border: 1px solid var(--border-color) !important; color: var(--btn-text) !important; font-weight: 700 !important; font-family: 'Cairo', sans-serif !important; text-transform: uppercase; letter-spacing: 1px; transition: all 0.3s ease; font-size: 16px !important; }} button.primary-btn:hover {{ transform: scale(1.02); box-shadow: 0 0 25px var(--glow-color); }} button.toggle-btn {{ background: transparent !important; border: 1px solid var(--border-color) !important; color: var(--text-accent) !important; padding: 5px 15px !important; font-family: 'Space Mono', monospace; }} button.toggle-btn:hover {{ background: var(--info-bg) !important; }} /* --- GUIDE BOXES --- */ .guide-step {{ background-color: var(--info-bg) !important; border-left: 4px solid var(--info-border) !important; padding: 16px; margin: 12px 0; border-radius: 0 8px 8px 0; font-family: 'Space Mono', monospace; font-size: 13px; line-height: 1.6; color: var(--text-primary) !important; }} .step-number {{ color: var(--text-accent) !important; font-weight: bold; text-transform: uppercase; display: block; margin-bottom: 6px; font-size: 12px; }} .path-highlight {{ background: rgba(128,128,128,0.2); padding: 3px 8px; border-radius: 4px; color: var(--text-accent) !important; font-weight: bold; border: 1px solid var(--border-color); }} /* --- DEEP LIGHT MODE FIXES --- */ /* 1. Force Image Containers to be Light/Transparent in Light Mode */ body.light-mode .gradio-image, body.light-mode .image-container, body.light-mode .gradio-image > div {{ background-color: rgba(255, 255, 255, 0.4) !important; }} /* Fix the upload area button specifically */ body.light-mode .gradio-image button {{ background-color: rgba(255, 248, 240, 0.8) !important; border: 2px dashed var(--border-color) !important; color: var(--text-primary) !important; }} /* 2. Fix Analysis Log (Textbox) Background & Text */ .report-box textarea {{ background-color: var(--input-bg) !important; /* Uses variable now */ border: 1px solid var(--border-color) !important; font-family: 'Space Mono', monospace !important; color: var(--text-primary) !important; font-size: 14px !important; }} /* 3. Fix Accordion/JSON in Light Mode */ body.light-mode .accordion {{ background-color: rgba(255, 255, 255, 0.5) !important; border: 1px solid var(--border-color) !important; }} body.light-mode .accordion span, body.light-mode .accordion label {{ color: var(--text-primary) !important; }} /* 4. Fix General Text Visibility */ body.light-mode span, .gradio-container.light-mode span, body.light-mode p, .gradio-container.light-mode p {{ color: var(--text-primary) !important; }} body.light-mode .guide-step code {{ background-color: rgba(139, 69, 19, 0.1); color: var(--text-accent) !important; }} body.light-mode .tab-nav button {{ color: var(--text-primary) !important; opacity: 0.7; }} body.light-mode .tab-nav button.selected {{ color: var(--text-accent) !important; opacity: 1; }} /* Cleanup */ .gradio-image, .gradio-json {{ background: transparent !important; border: none !important; }} .block-title {{ color: var(--text-accent) !important; }} """ header_html = """
AI HIEROGLYPHIC TRANSLATION SYSTEM
Bridging the Ancient and the Digital.
The Rosetta Decoder project utilizes advanced computer vision to identify and catalog Ancient Egyptian hieroglyphs.
By automating the detection of Gardiner codes, we are creating a digital bridge that will eventually allow for instant,
context-aware translation of Pharaonic wisdom, making the voices of the past accessible to everyone.
C:\\Python313\\python.exewhere python.%APPDATA%\\Claude\\claude_desktop_config.json"mcpServers" section.