abidlabs HF Staff commited on
Commit
807c861
Β·
verified Β·
1 Parent(s): 3754af6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -51,6 +51,7 @@ def update_bottom(oauth_token: gr.OAuthToken | None):
51
  return gr.skip()
52
 
53
  gradio_app_code = """
 
54
  import gradio as gr
55
 
56
  gr.load_openapi(
@@ -58,7 +59,7 @@ gr.load_openapi(
58
  base_url=\"{}\",
59
  paths={},
60
  methods={},
61
- auth_token={}
62
  ).launch(mcp_server=True)
63
  """
64
 
@@ -80,15 +81,17 @@ requirements_code = """
80
  https://gradio-pypi-previews.s3.amazonaws.com/86331cf36187d17a6d3ce26b01f28ea8dd5cbe35/gradio-5.38.2-py3-none-any.whl
81
  """
82
 
83
- def create_hf_space(token, space_name, app_code):
84
  """
85
  Create a new Hugging Face Space with optional app.py file
86
 
87
  Args:
88
  token (str): Your Hugging Face API token
89
  space_name (str): The name of the space to create
 
90
  app_code (str): String content for the app.py file
91
-
 
92
  Returns:
93
  SpaceInfo: Information about the created space
94
  """
@@ -104,7 +107,7 @@ def create_hf_space(token, space_name, app_code):
104
  space_info = api.create_repo(
105
  repo_id=space_id,
106
  repo_type="space",
107
- private=False,
108
  space_sdk="gradio"
109
  )
110
  api.upload_file(
@@ -128,6 +131,12 @@ def create_hf_space(token, space_name, app_code):
128
  repo_type="space",
129
  commit_message="Add requirements.txt"
130
  )
 
 
 
 
 
 
131
 
132
  space_url = f"https://huggingface.co/spaces/{space_id}"
133
  gr.Success(f"πŸš€ Your space will be available at: <a href='{space_url}' target='_blank'>{space_url} ‴</a>", duration=None)
@@ -137,7 +146,7 @@ def create_hf_space(token, space_name, app_code):
137
  gr.Error(f"❌ Error creating space: {str(e)}")
138
 
139
 
140
- def launch_mcp_server(openapi_spec_url, api_base_url, paths, methods, auth_token, space_name_box, oauth_token: gr.OAuthToken | None):
141
  if oauth_token:
142
  if not paths:
143
  paths = None
@@ -150,13 +159,14 @@ def launch_mcp_server(openapi_spec_url, api_base_url, paths, methods, auth_token
150
  create_hf_space(
151
  oauth_token.token,
152
  space_name_box,
 
153
  gradio_app_code.format(
154
  openapi_spec_url,
155
  api_base_url,
156
  paths,
157
  methods,
158
- auth_token
159
- )
160
  )
161
  else:
162
  pass
@@ -173,12 +183,13 @@ with gr.Blocks(theme="ocean") as demo:
173
  methods = gr.CheckboxGroup(label="Methods", choices=["GET", "POST", "PUT", "DELETE"], value=["GET", "POST", "PUT", "DELETE"])
174
  with gr.Accordion("Optional Settings", open=False):
175
  paths = gr.Textbox(label="Regex to filter paths by", placeholder=".*user.*", info="Only include API endpoints that match this regex")
176
- auth_token = gr.Textbox(label="Auth token to include in API requests", info="This will be sent to the API endpoints as a Bearer token")
177
  find_endpoints_button = gr.Button("πŸ” Find Endpoints")
178
  with gr.Column():
179
  endpoints = gr.JSON(label="πŸ” endpoints found", value=[], max_height=400)
180
  message = gr.Markdown("_Note:_ you must be signed in through your Hugging Face account to create the MCP Space")
181
  space_name_box = gr.Textbox(show_label=False, placeholder="Optional space name (e.g. my-mcp-space)")
 
182
  with gr.Row():
183
  login_button = gr.LoginButton()
184
  launch_button = gr.Button("πŸš€ Create MCP Space", variant="primary", interactive=False)
@@ -200,7 +211,7 @@ with gr.Blocks(theme="ocean") as demo:
200
  gr.on(
201
  [launch_button.click],
202
  launch_mcp_server,
203
- inputs=[openapi_spec_url, api_base_url, paths, methods, auth_token, space_name_box],
204
  outputs=None
205
  )
206
 
 
51
  return gr.skip()
52
 
53
  gradio_app_code = """
54
+ import os
55
  import gradio as gr
56
 
57
  gr.load_openapi(
 
59
  base_url=\"{}\",
60
  paths={},
61
  methods={},
62
+ auth_token=os.getenv("OPENAPI_AUTH_TOKEN")
63
  ).launch(mcp_server=True)
64
  """
65
 
 
81
  https://gradio-pypi-previews.s3.amazonaws.com/86331cf36187d17a6d3ce26b01f28ea8dd5cbe35/gradio-5.38.2-py3-none-any.whl
82
  """
83
 
84
+ def create_hf_space(token, space_name, space_public, app_code, auth_token):
85
  """
86
  Create a new Hugging Face Space with optional app.py file
87
 
88
  Args:
89
  token (str): Your Hugging Face API token
90
  space_name (str): The name of the space to create
91
+ space_public (bool): Whether the space should be public
92
  app_code (str): String content for the app.py file
93
+ auth_token (str | None): The auth token to include in the API requests
94
+
95
  Returns:
96
  SpaceInfo: Information about the created space
97
  """
 
107
  space_info = api.create_repo(
108
  repo_id=space_id,
109
  repo_type="space",
110
+ private=not space_public,
111
  space_sdk="gradio"
112
  )
113
  api.upload_file(
 
131
  repo_type="space",
132
  commit_message="Add requirements.txt"
133
  )
134
+ if auth_token:
135
+ api.add_space_secret(
136
+ repo_id=space_id,
137
+ key="OPENAPI_AUTH_TOKEN",
138
+ value=auth_token
139
+ )
140
 
141
  space_url = f"https://huggingface.co/spaces/{space_id}"
142
  gr.Success(f"πŸš€ Your space will be available at: <a href='{space_url}' target='_blank'>{space_url} ‴</a>", duration=None)
 
146
  gr.Error(f"❌ Error creating space: {str(e)}")
147
 
148
 
149
+ def launch_mcp_server(openapi_spec_url, api_base_url, paths, methods, auth_token, space_name_box, space_public_box, oauth_token: gr.OAuthToken | None):
150
  if oauth_token:
151
  if not paths:
152
  paths = None
 
159
  create_hf_space(
160
  oauth_token.token,
161
  space_name_box,
162
+ space_public_box,
163
  gradio_app_code.format(
164
  openapi_spec_url,
165
  api_base_url,
166
  paths,
167
  methods,
168
+ ),
169
+ auth_token
170
  )
171
  else:
172
  pass
 
183
  methods = gr.CheckboxGroup(label="Methods", choices=["GET", "POST", "PUT", "DELETE"], value=["GET", "POST", "PUT", "DELETE"])
184
  with gr.Accordion("Optional Settings", open=False):
185
  paths = gr.Textbox(label="Regex to filter paths by", placeholder=".*user.*", info="Only include API endpoints that match this regex")
186
+ auth_token = gr.Textbox(label="Auth token to include in API requests", info="This will be sent to the API endpoints as a Bearer token", type="password")
187
  find_endpoints_button = gr.Button("πŸ” Find Endpoints")
188
  with gr.Column():
189
  endpoints = gr.JSON(label="πŸ” endpoints found", value=[], max_height=400)
190
  message = gr.Markdown("_Note:_ you must be signed in through your Hugging Face account to create the MCP Space")
191
  space_name_box = gr.Textbox(show_label=False, placeholder="Optional space name (e.g. my-mcp-space)")
192
+ space_public_box = gr.Checkbox(label="Make Space public", value=True)
193
  with gr.Row():
194
  login_button = gr.LoginButton()
195
  launch_button = gr.Button("πŸš€ Create MCP Space", variant="primary", interactive=False)
 
211
  gr.on(
212
  [launch_button.click],
213
  launch_mcp_server,
214
+ inputs=[openapi_spec_url, api_base_url, paths, methods, auth_token, space_name_box, space_public_box],
215
  outputs=None
216
  )
217