Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ import re
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import tempfile
|
| 11 |
import numpy as np
|
| 12 |
-
# IMPORTANT: Importing necessary types for MCP definitions
|
| 13 |
from typing import List, Tuple, Dict, Any
|
| 14 |
|
| 15 |
# --- 1. CONFIGURATION & SECRETS ---
|
|
@@ -190,11 +189,10 @@ def process_pipeline(image, conf_threshold):
|
|
| 190 |
return None, f"System Failure: {str(e)}", "", None, str(e), None, []
|
| 191 |
|
| 192 |
# --- 6. MCP API FUNCTIONS (Optimized for Claude) ---
|
| 193 |
-
# These functions
|
| 194 |
-
# They use TYPE HINTS so Claude knows what arguments to provide.
|
| 195 |
# They return FILE PATHS (strings) for images, not base64 objects.
|
| 196 |
|
| 197 |
-
def
|
| 198 |
"""
|
| 199 |
Scans an image for Egyptian hieroglyphs.
|
| 200 |
Returns a tuple containing:
|
|
@@ -211,7 +209,7 @@ def detect_hieroglyphs(image: Image.Image, conf: float = 0.25) -> Tuple[str, Dic
|
|
| 211 |
|
| 212 |
return path, {"count": len(dets), "detections": dets}
|
| 213 |
|
| 214 |
-
def
|
| 215 |
"""
|
| 216 |
Takes a comma-separated string of Gardiner codes (e.g., 'G43, X1, N5').
|
| 217 |
Returns two translations: a mystical interpretation and an academic translation.
|
|
@@ -225,10 +223,10 @@ def translate_codes(keywords_text: str) -> Tuple[str, str]:
|
|
| 225 |
clean_mystical = re.sub('<[^<]+?>', '', mystical)
|
| 226 |
return clean_mystical, academic
|
| 227 |
|
| 228 |
-
def
|
| 229 |
"""
|
| 230 |
Generates statistical charts based on detection data.
|
| 231 |
-
Input: The JSON output from the '
|
| 232 |
Returns: The file path to the generated chart image.
|
| 233 |
"""
|
| 234 |
dets = json_data.get("detections", [])
|
|
@@ -245,7 +243,7 @@ def get_analytics_chart(json_data: Dict[str, Any]) -> str:
|
|
| 245 |
plt.close(fig)
|
| 246 |
return path
|
| 247 |
|
| 248 |
-
def
|
| 249 |
"""
|
| 250 |
Returns the complete database of supported Gardiner codes and their descriptions.
|
| 251 |
Useful for looking up specific symbol meanings.
|
|
@@ -449,7 +447,47 @@ with gr.Blocks(title="Rosetta Decoder Ultimate") as demo:
|
|
| 449 |
gr.HTML(f"<style>{custom_css}</style>")
|
| 450 |
gr.HTML(trail_script)
|
| 451 |
|
| 452 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
# --- Visible UI ---
|
| 455 |
with gr.Row(elem_classes="header-row"):
|
|
@@ -509,12 +547,9 @@ with gr.Blocks(title="Rosetta Decoder Ultimate") as demo:
|
|
| 509 |
btn_cam.click(fn=process_pipeline, inputs=[img_cam, slider_conf_cam], outputs=outputs)
|
| 510 |
|
| 511 |
if __name__ == "__main__":
|
| 512 |
-
#
|
| 513 |
-
# By passing the functions directly to mcp_tools, Gradio uses the function name as the tool name.
|
| 514 |
-
# The /tmp/gradio_results path ensures Claude can read the generated image files.
|
| 515 |
demo.launch(
|
| 516 |
mcp_server=True,
|
| 517 |
-
mcp_tools=[detect_hieroglyphs, translate_codes, get_analytics_chart, list_all_codes],
|
| 518 |
ssr_mode=False,
|
| 519 |
allowed_paths=["/tmp", "/tmp/gradio_results", "."]
|
| 520 |
)
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import tempfile
|
| 11 |
import numpy as np
|
|
|
|
| 12 |
from typing import List, Tuple, Dict, Any
|
| 13 |
|
| 14 |
# --- 1. CONFIGURATION & SECRETS ---
|
|
|
|
| 189 |
return None, f"System Failure: {str(e)}", "", None, str(e), None, []
|
| 190 |
|
| 191 |
# --- 6. MCP API FUNCTIONS (Optimized for Claude) ---
|
| 192 |
+
# These functions use TYPE HINTS so Claude knows what arguments to provide.
|
|
|
|
| 193 |
# They return FILE PATHS (strings) for images, not base64 objects.
|
| 194 |
|
| 195 |
+
def detect_hieroglyphs_api(image: Image.Image, conf: float = 0.25) -> Tuple[str, Dict[str, Any]]:
|
| 196 |
"""
|
| 197 |
Scans an image for Egyptian hieroglyphs.
|
| 198 |
Returns a tuple containing:
|
|
|
|
| 209 |
|
| 210 |
return path, {"count": len(dets), "detections": dets}
|
| 211 |
|
| 212 |
+
def translate_codes_api(keywords_text: str) -> Tuple[str, str]:
|
| 213 |
"""
|
| 214 |
Takes a comma-separated string of Gardiner codes (e.g., 'G43, X1, N5').
|
| 215 |
Returns two translations: a mystical interpretation and an academic translation.
|
|
|
|
| 223 |
clean_mystical = re.sub('<[^<]+?>', '', mystical)
|
| 224 |
return clean_mystical, academic
|
| 225 |
|
| 226 |
+
def get_analytics_chart_api(json_data: Dict[str, Any]) -> str:
|
| 227 |
"""
|
| 228 |
Generates statistical charts based on detection data.
|
| 229 |
+
Input: The JSON output from the 'detect' tool.
|
| 230 |
Returns: The file path to the generated chart image.
|
| 231 |
"""
|
| 232 |
dets = json_data.get("detections", [])
|
|
|
|
| 243 |
plt.close(fig)
|
| 244 |
return path
|
| 245 |
|
| 246 |
+
def list_all_codes_api() -> Dict[str, Any]:
|
| 247 |
"""
|
| 248 |
Returns the complete database of supported Gardiner codes and their descriptions.
|
| 249 |
Useful for looking up specific symbol meanings.
|
|
|
|
| 447 |
gr.HTML(f"<style>{custom_css}</style>")
|
| 448 |
gr.HTML(trail_script)
|
| 449 |
|
| 450 |
+
# --- MCP TOOL REGISTRATION LAYER (Hidden from UI) ---
|
| 451 |
+
# This is the correct, standard way to register tools in Gradio for MCP.
|
| 452 |
+
# We use hidden buttons connected to our specific API functions.
|
| 453 |
+
# The 'api_name' argument defines the short name you see in Claude.
|
| 454 |
+
|
| 455 |
+
with gr.Row(visible=False):
|
| 456 |
+
# 1. Detect Tool (Short name: "detect")
|
| 457 |
+
btn_detect = gr.Button("Detect")
|
| 458 |
+
btn_detect.click(
|
| 459 |
+
fn=detect_hieroglyphs_api,
|
| 460 |
+
inputs=[gr.Image(label="img"), gr.Number(label="conf")],
|
| 461 |
+
outputs=[gr.Textbox(label="path"), gr.JSON(label="json")],
|
| 462 |
+
api_name="detect" # << SHORT NAME HERE
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
# 2. Translate Tool (Short name: "translate")
|
| 466 |
+
btn_trans = gr.Button("Translate")
|
| 467 |
+
btn_trans.click(
|
| 468 |
+
fn=translate_codes_api,
|
| 469 |
+
inputs=[gr.Textbox(label="text")],
|
| 470 |
+
outputs=[gr.Textbox(label="mystic"), gr.Textbox(label="academic")],
|
| 471 |
+
api_name="translate" # << SHORT NAME HERE
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
# 3. Analytics Tool (Short name: "analytics")
|
| 475 |
+
btn_anal = gr.Button("Analytics")
|
| 476 |
+
btn_anal.click(
|
| 477 |
+
fn=get_analytics_chart_api,
|
| 478 |
+
inputs=[gr.JSON(label="data")],
|
| 479 |
+
outputs=[gr.Textbox(label="chart_path")],
|
| 480 |
+
api_name="analytics" # << SHORT NAME HERE
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
# 4. List Codes Tool (Short name: "list_codes")
|
| 484 |
+
btn_list = gr.Button("List")
|
| 485 |
+
btn_list.click(
|
| 486 |
+
fn=list_all_codes_api,
|
| 487 |
+
inputs=[],
|
| 488 |
+
outputs=[gr.JSON(label="data")],
|
| 489 |
+
api_name="list_codes" # << SHORT NAME HERE
|
| 490 |
+
)
|
| 491 |
|
| 492 |
# --- Visible UI ---
|
| 493 |
with gr.Row(elem_classes="header-row"):
|
|
|
|
| 547 |
btn_cam.click(fn=process_pipeline, inputs=[img_cam, slider_conf_cam], outputs=outputs)
|
| 548 |
|
| 549 |
if __name__ == "__main__":
|
| 550 |
+
# Standard launch. Tools are registered via the hidden buttons above.
|
|
|
|
|
|
|
| 551 |
demo.launch(
|
| 552 |
mcp_server=True,
|
|
|
|
| 553 |
ssr_mode=False,
|
| 554 |
allowed_paths=["/tmp", "/tmp/gradio_results", "."]
|
| 555 |
)
|