File size: 5,640 Bytes
af7647d
 
 
 
 
 
 
11743be
db4e1b2
af7647d
 
db4e1b2
 
af7647d
 
 
 
 
db4e1b2
af7647d
db4e1b2
af7647d
 
db4e1b2
af7647d
 
11743be
af7647d
 
 
 
 
db4e1b2
af7647d
 
 
 
 
 
 
 
db4e1b2
af7647d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4e1b2
af7647d
 
 
 
 
 
 
db4e1b2
af7647d
 
f6fedbb
 
 
 
 
11743be
f6fedbb
11743be
f6fedbb
 
11743be
f6fedbb
11743be
f6fedbb
 
 
 
 
 
 
 
 
 
11743be
f6fedbb
11743be
f6fedbb
 
 
 
 
 
 
 
 
 
 
 
 
 
11743be
f6fedbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11743be
 
f6fedbb
 
 
 
 
11743be
f6fedbb
 
 
 
 
 
 
 
 
 
11743be
 
f6fedbb
 
 
 
 
 
 
 
 
 
 
 
 
11743be
f6fedbb
 
af7647d
db4e1b2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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"

print(f"Server Status: Public MCP Endpoint Active")
print(f"Security: Model weights are protected (private repo)")

try:
    model_path = hf_hub_download(
        repo_id=MODEL_REPO,
        filename=MODEL_FILENAME,
        token=os.environ.get("HF_TOKEN")
    )
    print(f"System: Model loaded successfully from private storage.")
    model = YOLO(model_path)
except Exception as e:
    print(f"CRITICAL ERROR: Could not load model. Check HF_TOKEN in Settings. {e}")
    model = None

# --- 2. DETECTION LOGIC ---
def detect_hieroglyphs(image: Image.Image, conf_threshold: float = 0.25):
    if image is None:
        return None, {"error": "No image provided"}
    
    if model is None:
        return None, {"error": "Server Error: Model not loaded."}

    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])
        
        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),
                        "box": [round(x, 1) for x in box.xyxy[0].cpu().numpy().tolist()]
                    })

        summary = {
            "status": "success",
            "total_found": len(detections),
            "unique_symbols": list(gardiner_counts.keys()),
            "counts": gardiner_counts
        }
        
        return annotated_image, summary

    except Exception as e:
        print(f"Inference Error: {e}")
        return None, {"error": str(e)}

# --- 3. UI/UX CONFIGURATION ---

custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&display=swap');

/* Force background color via CSS since Theme is disabled */
body, .gradio-container {
    background-color: #fdf6e3 !important; 
}

h1, h2, h3, span {
    font-family: 'Cinzel', serif !important;
    color: #8b4513 !important; 
}

/* The Magic Button Animation */
@keyframes goldenPulse {
    0% { box-shadow: 0 0 0 0 rgba(212, 175, 55, 0.7); transform: scale(1); }
    50% { box-shadow: 0 0 0 10px rgba(212, 175, 55, 0); transform: scale(1.02); }
    100% { box-shadow: 0 0 0 0 rgba(212, 175, 55, 0); transform: scale(1); }
}

#magic-btn {
    background: linear-gradient(135deg, #b8860b 0%, #d4af37 100%);
    border: 1px solid #8b4513;
    color: white !important;
    font-family: 'Cinzel', serif;
    font-weight: bold;
    font-size: 1.2em;
    animation: goldenPulse 2s infinite;
    transition: all 0.3s ease;
}

#magic-btn:hover {
    animation: none;
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(139, 69, 19, 0.4);
}

.json-output {
    background-color: #fff8dc;
    border: 1px solid #d4af37;
}
"""

claude_config_content = """
{
  "mcpServers": {
    "hieroglyph-detector": {
      "command": "uv",
      "args": [
        "python",
        "client.py" 
      ],
      "env": {
        "GRADIO_SERVER_URL": "YOUR_SPACE_URL_HERE"
      }
    }
  }
}
"""

# --- 4. BUILD THE APP WITH BLOCKS ---
# FIX: Removed 'theme' argument to prevent TypeError on older Gradio versions
with gr.Blocks(css=custom_css, title="Horus Vision") as demo:
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("""
            # ๐Ÿ‘๏ธ Horus Vision
            ### AI Hieroglyphic Decoder
            """)
        with gr.Column(scale=3):
            gr.Markdown("""
            > *"The eye sees all."* Upload an image of Egyptian text. 
            """)

    with gr.Tabs():
        with gr.TabItem("๐Ÿ” Decoder"):
            with gr.Row():
                with gr.Column():
                    img_input = gr.Image(type="pil", label="Upload Papyrus", sources=["upload", "clipboard"])
                    conf_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.25, label="Confidence")
                    analyze_btn = gr.Button("๐Ÿ”ฎ Decipher Symbols", elem_id="magic-btn", variant="primary")
                with gr.Column():
                    img_output = gr.Image(label="Annotated Result", interactive=False)
                    json_output = gr.JSON(label="Glyph Data", elem_classes="json-output")
            
            analyze_btn.click(
                fn=detect_hieroglyphs,
                inputs=[img_input, conf_slider],
                outputs=[img_output, json_output]
            )

        with gr.TabItem("๐Ÿค– Connect to Claude"):
            gr.Markdown("### MCP Server Configuration")
            gr.Code(value=claude_config_content, language="json", label="claude_desktop_config.json", interactive=False)

# --- 5. LAUNCH ---
if __name__ == "__main__":
    demo.launch(
        mcp_server=True, 
        ssr_mode=False, 
        allowed_paths=["/tmp"]
    )