File size: 3,878 Bytes
af7647d
 
 
 
 
 
 
 
db4e1b2
 
af7647d
 
db4e1b2
 
af7647d
 
db4e1b2
 
 
af7647d
 
 
db4e1b2
af7647d
db4e1b2
af7647d
 
db4e1b2
af7647d
 
 
 
 
db4e1b2
af7647d
 
db4e1b2
 
af7647d
 
db4e1b2
af7647d
 
 
 
 
db4e1b2
af7647d
 
 
 
 
 
 
 
 
db4e1b2
af7647d
 
 
db4e1b2
af7647d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db4e1b2
af7647d
 
 
 
 
 
 
db4e1b2
af7647d
 
db4e1b2
af7647d
 
 
 
db4e1b2
af7647d
 
 
 
 
 
db4e1b2
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
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 ---
# Replace with your actual private repo ID
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:
    # 🔒 SECURE DOWNLOAD: 
    # This uses the 'HF_TOKEN' Secret from Space Settings to authenticate.
    # Users of the Space CANNOT see this token or the downloaded file.
    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):
    """
    Analyzes an image to find Egyptian hieroglyphs.
    
    Args:
        image: The image to analyze.
        conf_threshold: Confidence level (0.1 to 1.0). Default is 0.25.
    
    Returns:
        A tuple containing the annotated image and a JSON summary of findings.
    """
    if image is None:
        return None, {"error": "No image provided"}
    
    if model is None:
        return None, {"error": "Server Error: Model not loaded."}

    try:
        # Run Inference
        results = model.predict(
            source=image,
            conf=conf_threshold,
            iou=0.45,
            imgsz=640,
            verbose=False,
            device='cpu',
            max_det=300
        )
        
        # 1. Generate Visual Output (RGB Image)
        annotated_array = results[0].plot() 
        annotated_image = Image.fromarray(annotated_array[..., ::-1])
        
        # 2. Generate Data Output (JSON)
        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. INTERFACE ---
demo = gr.Interface(
    fn=detect_hieroglyphs,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Number(value=0.25, label="Confidence")
    ],
    outputs=[
        gr.Image(label="Annotated Result"),
        gr.JSON(label="Detection Data")
    ],
    title="Egyptian Hieroglyph MCP Server",
    description="Public MCP Endpoint for Hieroglyph Detection. (Model Weights are Private)"
)

if __name__ == "__main__":
    # Settings to ensure Public access works without 403 errors:
    # ssr_mode=False: Disables Server-Side Rendering (helps with API proxies)
    # allowed_paths: Grants permission to read temp files uploaded by MCP
    demo.launch(
        mcp_server=True, 
        ssr_mode=False, 
        allowed_paths=["/tmp"]
    )