Hyeonseo commited on
Commit
7ecf54a
·
unverified ·
1 Parent(s): b0cec26

Refactor app.py to simplify UI launch process

Browse files
external/mcp-servers/hf-translation-docs-explorer/app.py CHANGED
@@ -1,8 +1,6 @@
1
  from __future__ import annotations
2
 
3
- import argparse
4
  import os
5
-
6
  import gradio as gr
7
 
8
  from services import get_available_projects, LANGUAGE_CHOICES
@@ -11,15 +9,17 @@ from setting import SETTINGS
11
 
12
 
13
  def ensure_mcp_support() -> None:
14
- """Verify that ``gradio[mcp]`` is installed and enable the MCP server flag."""
15
  try:
16
  import gradio.mcp # noqa: F401
17
- except ImportError as exc: # pragma: no cover - runtime guard
18
  raise RuntimeError("Install gradio[mcp] before launching this module.") from exc
 
19
  os.environ.setdefault("GRADIO_MCP_SERVER", "true")
 
20
 
21
 
22
- def build_demo() -> gr.Blocks:
23
  """Create a lightweight Gradio Blocks UI for exercising the MCP tools."""
24
  projects = get_available_projects()
25
  languages = LANGUAGE_CHOICES[:]
@@ -32,12 +32,12 @@ def build_demo() -> gr.Blocks:
32
  catalog_output = gr.JSON(label="catalog")
33
  gr.Button("Fetch").click(
34
  fn=list_projects,
35
- inputs=[], # 인자 없음
36
  outputs=catalog_output,
37
  api_name="translation_project_catalog",
38
  )
39
 
40
- # --- 2) File search (report + candidates) ---
41
  with gr.Tab("File search"):
42
  project_input = gr.Dropdown(
43
  choices=projects,
@@ -96,57 +96,14 @@ def build_demo() -> gr.Blocks:
96
  return demo
97
 
98
 
99
- def _parse_args(argv=None) -> argparse.Namespace:
100
- """Parse CLI arguments used for local or Space deployments."""
101
- parser = argparse.ArgumentParser(description="Launch the translation MCP demo.")
102
-
103
- parser.add_argument(
104
- "--as-space",
105
- action="store_true",
106
- help="Use Hugging Face Space defaults.",
107
- )
108
- parser.add_argument(
109
- "--share",
110
- action="store_true",
111
- help="Create a public share link.",
112
- )
113
- parser.add_argument(
114
- "--no-queue",
115
- dest="queue",
116
- action="store_false",
117
- help="Disable the request queue.",
118
- )
119
- parser.set_defaults(queue=True)
120
-
121
- return parser.parse_args(argv)
122
-
123
-
124
- def main(argv=None) -> None:
125
- """Launch the Gradio app with MCP server support enabled."""
126
- args = _parse_args(argv)
127
-
128
- ensure_mcp_support()
129
-
130
- launch_kwargs = {"mcp_server": True}
131
-
132
- if args.as_space or os.environ.get("SPACE_ID"):
133
- launch_kwargs.update(
134
- {
135
- "server_name": "0.0.0.0",
136
- "server_port": int(os.environ.get("PORT", "7860")),
137
- "show_api": False,
138
- }
139
- )
140
- else:
141
- launch_kwargs["show_api"] = True
142
-
143
- if args.share:
144
- launch_kwargs["share"] = True
145
-
146
- demo = build_demo()
147
- app = demo.queue() if args.queue else demo
148
- app.launch(**launch_kwargs)
149
-
150
-
151
- if __name__ == "__main__": # pragma: no cover - manual execution helper
152
- main()
 
1
  from __future__ import annotations
2
 
 
3
  import os
 
4
  import gradio as gr
5
 
6
  from services import get_available_projects, LANGUAGE_CHOICES
 
9
 
10
 
11
  def ensure_mcp_support() -> None:
12
+ """Verify that `gradio[mcp]` is installed and enable the MCP server flag."""
13
  try:
14
  import gradio.mcp # noqa: F401
15
+ except ImportError as exc:
16
  raise RuntimeError("Install gradio[mcp] before launching this module.") from exc
17
+
18
  os.environ.setdefault("GRADIO_MCP_SERVER", "true")
19
+ os.environ.setdefault("GRADIO_SHOW_API", "true")
20
 
21
 
22
+ def build_ui() -> gr.Blocks:
23
  """Create a lightweight Gradio Blocks UI for exercising the MCP tools."""
24
  projects = get_available_projects()
25
  languages = LANGUAGE_CHOICES[:]
 
32
  catalog_output = gr.JSON(label="catalog")
33
  gr.Button("Fetch").click(
34
  fn=list_projects,
35
+ inputs=[],
36
  outputs=catalog_output,
37
  api_name="translation_project_catalog",
38
  )
39
 
40
+ # --- 2) File search ---
41
  with gr.Tab("File search"):
42
  project_input = gr.Dropdown(
43
  choices=projects,
 
96
  return demo
97
 
98
 
99
+ ensure_mcp_support()
100
+
101
+ ui = build_ui()
102
+
103
+ ui.launch(
104
+ server_name="0.0.0.0",
105
+ server_port=int(os.environ.get("PORT", "7860")),
106
+ share=False,
107
+ show_api=True,
108
+ mcp_server=True
109
+ )