youkii-xr commited on
Commit
36bcab9
Β·
verified Β·
1 Parent(s): 9030808

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -34
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 wrappers are exposed as API endpoints for Claude/MCP to use independently.
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 = keywords_text
215
  mystical, academic = core_translate(keywords)
216
- return mystical, academic
 
 
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 plots from JSON detection data."""
224
- # Mock image size if not provided, mostly for heatmap relative positions
225
  dets = json_data.get("detections", [])
226
- return core_analytics(dets, 1000, 1000)
 
 
 
 
 
 
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
- # 1. Detect Only
406
- btn_mcp_detect = gr.Button(visible=False)
407
- api_detect_out_img = gr.Image(visible=False)
408
- api_detect_out_json = gr.JSON(visible=False)
409
- btn_mcp_detect.click(fn=api_detect_only, inputs=[gr.Image(visible=False), gr.Number(visible=False)], outputs=[api_detect_out_img, api_detect_out_json], api_name="detect_hieroglyphs")
410
-
411
- # 2. Translate Only
412
- btn_mcp_trans = gr.Button(visible=False)
413
- api_trans_out_mystical = gr.Textbox(visible=False)
414
- api_trans_out_academic = gr.Textbox(visible=False)
415
- btn_mcp_trans.click(fn=api_translate_only, inputs=[gr.Textbox(visible=False)], outputs=[api_trans_out_mystical, api_trans_out_academic], api_name="translate_hieroglyphs")
416
-
417
- # 3. Analytics Only
418
- btn_mcp_analytics = gr.Button(visible=False)
419
- api_analytics_out = gr.Plot(visible=False)
420
- btn_mcp_analytics.click(fn=api_get_analytics, inputs=[gr.JSON(visible=False)], outputs=[api_analytics_out], api_name="get_analytics")
421
-
422
- # 4. Get List
423
- btn_mcp_list = gr.Button(visible=False)
424
- api_list_out = gr.JSON(visible=False)
425
- btn_mcp_list.click(fn=api_get_supported_codes, inputs=[], outputs=[api_list_out], api_name="get_supported_codes")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- gr.Code(value=claude_json_content, language="json", label="claude_desktop_config.json", interactive=False, lines=25, container=True)
 
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"):