OsGo commited on
Commit
936781a
·
verified ·
1 Parent(s): b22adbc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -72
app.py CHANGED
@@ -40,8 +40,7 @@ def make_txt(title: str, content: str) -> str:
40
  return str(filepath)
41
 
42
 
43
- def build_demo():
44
- iface = gr.Interface(
45
  fn=make_txt,
46
  inputs=[
47
  gr.Textbox(label="Título", placeholder="Escribe el título aquí"),
@@ -51,76 +50,8 @@ def build_demo():
51
  title="Generador de .txt",
52
  description="Genera un archivo .txt con un título y contenido."
53
  )
54
- return iface
55
 
56
 
57
  if __name__ == "__main__":
58
- demo = build_demo()
59
- # Permitir activar MCP pasando --mcp o configurando la variable de entorno ENABLE_MCP=1
60
- enable_mcp = "--mcp" in sys.argv or os.getenv("ENABLE_MCP", "").lower() in ("1", "true", "yes")
61
-
62
- def _get_server_config() -> dict:
63
- server_name = os.getenv("GRADIO_SERVER_NAME") or os.getenv("SERVER_NAME") or "127.0.0.1"
64
- try:
65
- server_port = int(os.getenv("GRADIO_SERVER_PORT") or os.getenv("SERVER_PORT") or "7860")
66
- except Exception:
67
- server_port = 7860
68
- return {"server_name": server_name, "server_port": server_port, "share": False}
69
-
70
- def launch_demo(demo_obj, enable_mcp_flag: bool = False) -> None:
71
- base_kwargs = _get_server_config()
72
- print(f"[info] Launch config: host={base_kwargs['server_name']} port={base_kwargs['server_port']} enable_mcp={enable_mcp_flag}")
73
-
74
- # If MCP requested, try several strategies to enable it across Gradio versions
75
- if enable_mcp_flag:
76
- # First, inspect signature for any known parameter names
77
- try:
78
- sig = inspect.signature(demo_obj.launch)
79
- for param in ("mcp_server", "mcp", "enable_mcp"):
80
- if param in sig.parameters:
81
- kwargs = dict(base_kwargs)
82
- kwargs[param] = True
83
- print(f"[info] Passing {param}=True based on launch signature.")
84
- try:
85
- demo_obj.launch(**kwargs)
86
- return
87
- except TypeError as e:
88
- print(f"[debug] demo.launch(**kwargs) failed with TypeError: {e}")
89
- except Exception as e:
90
- print(f"[error] demo.launch raised: {e}")
91
- except Exception as e:
92
- print(f"[debug] Could not inspect demo.launch signature: {e}")
93
-
94
- # Try passing common kwarg names as a fallback
95
- for param in ("mcp_server", "mcp", "enable_mcp"):
96
- kwargs = dict(base_kwargs)
97
- kwargs[param] = True
98
- try:
99
- print(f"[info] Trying demo.launch({param}=True) as fallback.")
100
- demo_obj.launch(**kwargs)
101
- return
102
- except TypeError as e:
103
- print(f"[debug] demo.launch with {param} failed: {e}")
104
- except Exception as e:
105
- print(f"[debug] demo.launch with {param} raised: {e}")
106
-
107
- # If we reach here, we couldn't enable MCP in obvious ways
108
- print("[warning] No compatible parameter found to enable MCP on this Gradio version.")
109
- print("[warning] Consider installing 'gradio[mcp]' or updating Gradio. Launching normally.")
110
-
111
- # Final attempt: launch with base kwargs, with safe fallbacks
112
- try:
113
- demo_obj.launch(**base_kwargs)
114
- except TypeError:
115
- try:
116
- # Older versions sometimes don't accept kwargs; call without them
117
- demo_obj.launch()
118
- except Exception as e:
119
- print(f"[error] demo.launch() failed: {e}")
120
- raise
121
- except Exception as e:
122
- print(f"[error] demo.launch(**base_kwargs) raised unexpected error: {e}")
123
- raise
124
-
125
- # Launch the demo with the robust launcher
126
- launch_demo(demo, enable_mcp)
 
40
  return str(filepath)
41
 
42
 
43
+ demo = gr.Interface(
 
44
  fn=make_txt,
45
  inputs=[
46
  gr.Textbox(label="Título", placeholder="Escribe el título aquí"),
 
50
  title="Generador de .txt",
51
  description="Genera un archivo .txt con un título y contenido."
52
  )
 
53
 
54
 
55
  if __name__ == "__main__":
56
+
57
+ demo.launch(mcp_server=True)