Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import re
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import tempfile
|
| 11 |
import numpy as np
|
|
|
|
| 12 |
|
| 13 |
# --- 1. CONFIGURATION & SECRETS ---
|
| 14 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
@@ -197,33 +198,40 @@ def process_pipeline(image, conf_threshold):
|
|
| 197 |
except Exception as e:
|
| 198 |
return None, f"System Failure: {str(e)}", "", None, str(e), None, []
|
| 199 |
|
| 200 |
-
# --- 6. MCP API FUNCTIONS (Granular) ---
|
| 201 |
-
# These
|
| 202 |
|
| 203 |
-
def api_detect_only(image, conf):
|
| 204 |
"""API: Returns JSON detection data and annotated image."""
|
| 205 |
ann_img, dets, _ = core_detect(image, conf)
|
| 206 |
return ann_img, {"count": len(dets), "detections": dets}
|
| 207 |
|
| 208 |
-
def api_translate_only(keywords_text):
|
| 209 |
"""API: Translates a comma-separated string of keywords."""
|
| 210 |
-
# Convert string input "sun, life" to list ["sun", "life"]
|
| 211 |
if isinstance(keywords_text, str):
|
| 212 |
keywords = [k.strip() for k in keywords_text.split(',')]
|
| 213 |
else:
|
| 214 |
-
keywords =
|
| 215 |
mystical, academic = core_translate(keywords)
|
| 216 |
-
|
|
|
|
|
|
|
| 217 |
|
| 218 |
-
def api_get_supported_codes():
|
| 219 |
"""API: Returns the full Gardiner list."""
|
| 220 |
return gardiner_data
|
| 221 |
|
| 222 |
-
def api_get_analytics(json_data):
|
| 223 |
-
"""API: Generates
|
| 224 |
-
#
|
| 225 |
dets = json_data.get("detections", [])
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# --- 7. HTML GENERATORS ---
|
| 229 |
|
|
@@ -401,28 +409,50 @@ with gr.Blocks(title="Rosetta Decoder Ultimate") as demo:
|
|
| 401 |
|
| 402 |
# --- Hidden API Buttons for MCP (Granular Access) ---
|
| 403 |
# These buttons are not visible in the UI but expose the API endpoints for Claude
|
|
|
|
| 404 |
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
# --- Visible UI ---
|
| 428 |
with gr.Row(elem_classes="header-row"):
|
|
@@ -466,7 +496,8 @@ with gr.Blocks(title="Rosetta Decoder Ultimate") as demo:
|
|
| 466 |
# TAB 3: SETUP
|
| 467 |
with gr.TabItem("π€ SYSTEM SETUP"):
|
| 468 |
gr.HTML(guide_html)
|
| 469 |
-
|
|
|
|
| 470 |
|
| 471 |
# TAB 4: GARDINER CODES
|
| 472 |
with gr.TabItem("π GARDINER CODES"):
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import tempfile
|
| 11 |
import numpy as np
|
| 12 |
+
from typing import List, Tuple, Dict, Any # REQUIRED FOR MCP
|
| 13 |
|
| 14 |
# --- 1. CONFIGURATION & SECRETS ---
|
| 15 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
return None, f"System Failure: {str(e)}", "", None, str(e), None, []
|
| 200 |
|
| 201 |
+
# --- 6. MCP API FUNCTIONS (Granular & Typed) ---
|
| 202 |
+
# IMPORTANT: These functions use Type Hints so Gradio knows how to define the MCP Tool.
|
| 203 |
|
| 204 |
+
def api_detect_only(image: Image.Image, conf: float = 0.25) -> Tuple[Image.Image, Any]:
|
| 205 |
"""API: Returns JSON detection data and annotated image."""
|
| 206 |
ann_img, dets, _ = core_detect(image, conf)
|
| 207 |
return ann_img, {"count": len(dets), "detections": dets}
|
| 208 |
|
| 209 |
+
def api_translate_only(keywords_text: str) -> Tuple[str, str]:
|
| 210 |
"""API: Translates a comma-separated string of keywords."""
|
|
|
|
| 211 |
if isinstance(keywords_text, str):
|
| 212 |
keywords = [k.strip() for k in keywords_text.split(',')]
|
| 213 |
else:
|
| 214 |
+
keywords = ["Unknown"]
|
| 215 |
mystical, academic = core_translate(keywords)
|
| 216 |
+
# Strip HTML tags for clean API text
|
| 217 |
+
clean_mystical = re.sub('<[^<]+?>', '', mystical)
|
| 218 |
+
return clean_mystical, academic
|
| 219 |
|
| 220 |
+
def api_get_supported_codes() -> Dict[str, Any]:
|
| 221 |
"""API: Returns the full Gardiner list."""
|
| 222 |
return gardiner_data
|
| 223 |
|
| 224 |
+
def api_get_analytics(json_data: Dict[str, Any]) -> Image.Image:
|
| 225 |
+
"""API: Generates a plot Image from JSON detection data."""
|
| 226 |
+
# MCP cannot display interactive plots, so we convert to Image
|
| 227 |
dets = json_data.get("detections", [])
|
| 228 |
+
fig = core_analytics(dets, 1000, 1000)
|
| 229 |
+
|
| 230 |
+
# Save figure to buffer and reload as PIL Image
|
| 231 |
+
buf = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 232 |
+
fig.savefig(buf.name, format='png', facecolor='#0f0f23')
|
| 233 |
+
plt.close(fig)
|
| 234 |
+
return Image.open(buf.name)
|
| 235 |
|
| 236 |
# --- 7. HTML GENERATORS ---
|
| 237 |
|
|
|
|
| 409 |
|
| 410 |
# --- Hidden API Buttons for MCP (Granular Access) ---
|
| 411 |
# These buttons are not visible in the UI but expose the API endpoints for Claude
|
| 412 |
+
# We call the TYPED functions here so Gradio registers the tool correctly.
|
| 413 |
|
| 414 |
+
with gr.Column(visible=False):
|
| 415 |
+
# 1. Detect Only
|
| 416 |
+
btn_mcp_detect = gr.Button("MCP Detect")
|
| 417 |
+
api_detect_out_img = gr.Image()
|
| 418 |
+
api_detect_out_json = gr.JSON()
|
| 419 |
+
btn_mcp_detect.click(
|
| 420 |
+
fn=api_detect_only,
|
| 421 |
+
inputs=[gr.Image(label="Input Image"), gr.Number(value=0.25, label="Conf")],
|
| 422 |
+
outputs=[api_detect_out_img, api_detect_out_json],
|
| 423 |
+
api_name="detect_hieroglyphs"
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
# 2. Translate Only
|
| 427 |
+
btn_mcp_trans = gr.Button("MCP Translate")
|
| 428 |
+
api_trans_out_mystical = gr.Textbox()
|
| 429 |
+
api_trans_out_academic = gr.Textbox()
|
| 430 |
+
btn_mcp_trans.click(
|
| 431 |
+
fn=api_translate_only,
|
| 432 |
+
inputs=[gr.Textbox(label="Keywords")],
|
| 433 |
+
outputs=[api_trans_out_mystical, api_trans_out_academic],
|
| 434 |
+
api_name="translate_hieroglyphs"
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
# 3. Analytics Only (Returns IMAGE for MCP)
|
| 438 |
+
btn_mcp_analytics = gr.Button("MCP Analytics")
|
| 439 |
+
api_analytics_out = gr.Image() # Changed to Image for MCP
|
| 440 |
+
btn_mcp_analytics.click(
|
| 441 |
+
fn=api_get_analytics,
|
| 442 |
+
inputs=[gr.JSON(label="Data")],
|
| 443 |
+
outputs=[api_analytics_out],
|
| 444 |
+
api_name="get_analytics"
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
# 4. Get List
|
| 448 |
+
btn_mcp_list = gr.Button("MCP List")
|
| 449 |
+
api_list_out = gr.JSON()
|
| 450 |
+
btn_mcp_list.click(
|
| 451 |
+
fn=api_get_supported_codes,
|
| 452 |
+
inputs=[],
|
| 453 |
+
outputs=[api_list_out],
|
| 454 |
+
api_name="get_supported_codes"
|
| 455 |
+
)
|
| 456 |
|
| 457 |
# --- Visible UI ---
|
| 458 |
with gr.Row(elem_classes="header-row"):
|
|
|
|
| 496 |
# TAB 3: SETUP
|
| 497 |
with gr.TabItem("π€ SYSTEM SETUP"):
|
| 498 |
gr.HTML(guide_html)
|
| 499 |
+
# Expanded code box as requested
|
| 500 |
+
gr.Code(value=claude_json_content, language="json", label="claude_desktop_config.json", interactive=False, lines=30)
|
| 501 |
|
| 502 |
# TAB 4: GARDINER CODES
|
| 503 |
with gr.TabItem("π GARDINER CODES"):
|