Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,9 @@ import httpx
|
|
| 3 |
import json
|
| 4 |
from huggingface_hub import HfApi
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
-
def find_endpoints(openapi_spec_url, api_base_url, methods):
|
| 8 |
print(f"Finding endpoints for {openapi_spec_url} with methods {methods}")
|
| 9 |
if openapi_spec_url.startswith(("http://", "https://")):
|
| 10 |
response = httpx.get(openapi_spec_url)
|
|
@@ -27,16 +28,25 @@ def find_endpoints(openapi_spec_url, api_base_url, methods):
|
|
| 27 |
for method, operation in path_item.items():
|
| 28 |
if methods and method.lower() not in [m.lower() for m in methods]:
|
| 29 |
continue
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
return gr.JSON(valid_api_paths, label=f"π {len(valid_api_paths)} endpoints found")
|
| 36 |
|
| 37 |
def update_bottom(oauth_token: gr.OAuthToken | None):
|
| 38 |
if oauth_token:
|
| 39 |
-
return "Click the π
|
| 40 |
else:
|
| 41 |
return gr.skip()
|
| 42 |
|
|
@@ -46,16 +56,18 @@ import gradio as gr
|
|
| 46 |
gr.load_openapi(
|
| 47 |
openapi_spec=\"{}\",
|
| 48 |
base_url=\"{}\",
|
|
|
|
| 49 |
methods={},
|
| 50 |
).launch(mcp_server=True)
|
| 51 |
"""
|
| 52 |
|
| 53 |
-
def create_hf_space(token, app_code):
|
| 54 |
"""
|
| 55 |
Create a new Hugging Face Space with optional app.py file
|
| 56 |
|
| 57 |
Args:
|
| 58 |
token (str): Your Hugging Face API token
|
|
|
|
| 59 |
app_code (str): String content for the app.py file
|
| 60 |
|
| 61 |
Returns:
|
|
@@ -65,42 +77,41 @@ def create_hf_space(token, app_code):
|
|
| 65 |
api = HfApi(token=token)
|
| 66 |
user_info = api.whoami()
|
| 67 |
username = user_info["name"]
|
| 68 |
-
space_name = f"
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
-
gr.Info(f"Creating space {
|
| 72 |
space_info = api.create_repo(
|
| 73 |
-
repo_id=
|
| 74 |
repo_type="space",
|
| 75 |
private=False,
|
| 76 |
space_sdk="gradio"
|
| 77 |
)
|
| 78 |
-
print(f"Space created: {space_info}")
|
| 79 |
-
|
| 80 |
api.upload_file(
|
| 81 |
path_or_fileobj=app_code.encode('utf-8'),
|
| 82 |
path_in_repo="app.py",
|
| 83 |
-
repo_id=
|
| 84 |
repo_type="space",
|
| 85 |
commit_message="Add app.py"
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
space_url = f"https://huggingface.co/spaces/{space_name}"
|
| 90 |
-
gr.Success(f"π Your space is available at: <a href='{space_url}' target='_blank'>{space_url}</a>", duration=None)
|
| 91 |
return space_info
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
gr.Error(f"β Error creating space: {str(e)}")
|
| 95 |
|
| 96 |
|
| 97 |
-
def launch_mcp_server(openapi_spec_url, api_base_url, methods, oauth_token: gr.OAuthToken | None):
|
| 98 |
if oauth_token:
|
| 99 |
create_hf_space(
|
| 100 |
oauth_token.token,
|
|
|
|
| 101 |
gradio_app_code.format(
|
| 102 |
openapi_spec_url,
|
| 103 |
api_base_url,
|
|
|
|
| 104 |
methods
|
| 105 |
)
|
| 106 |
)
|
|
@@ -116,19 +127,21 @@ with gr.Blocks(theme="ocean") as demo:
|
|
| 116 |
with gr.Column():
|
| 117 |
openapi_spec_url = gr.Textbox(label="OpenAPI Spec URL", value="https://petstore3.swagger.io/api/v3/openapi.json")
|
| 118 |
api_base_url = gr.Textbox(label="API Base URL", value="https://petstore3.swagger.io/api/v3/")
|
|
|
|
| 119 |
methods = gr.CheckboxGroup(label="Methods", choices=["GET", "POST", "PUT", "DELETE"], value=["GET", "POST", "PUT", "DELETE"])
|
| 120 |
find_endpoints_button = gr.Button("π Find Endpoints")
|
| 121 |
with gr.Column():
|
| 122 |
endpoints = gr.JSON(label="π endpoints found", value=[], max_height=400)
|
| 123 |
-
message = gr.Markdown("_Note:_ you must be signed in through your Hugging Face account to
|
|
|
|
| 124 |
with gr.Row():
|
| 125 |
login_button = gr.LoginButton()
|
| 126 |
-
launch_button = gr.Button("π
|
| 127 |
|
| 128 |
gr.on(
|
| 129 |
[demo.load, find_endpoints_button.click],
|
| 130 |
find_endpoints,
|
| 131 |
-
inputs=[openapi_spec_url, api_base_url, methods],
|
| 132 |
outputs=endpoints,
|
| 133 |
)
|
| 134 |
|
|
@@ -142,7 +155,7 @@ with gr.Blocks(theme="ocean") as demo:
|
|
| 142 |
gr.on(
|
| 143 |
[launch_button.click],
|
| 144 |
launch_mcp_server,
|
| 145 |
-
inputs=[openapi_spec_url, api_base_url, methods],
|
| 146 |
outputs=None
|
| 147 |
)
|
| 148 |
|
|
|
|
| 3 |
import json
|
| 4 |
from huggingface_hub import HfApi
|
| 5 |
import random
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
+
def find_endpoints(openapi_spec_url, api_base_url, paths, methods):
|
| 9 |
print(f"Finding endpoints for {openapi_spec_url} with methods {methods}")
|
| 10 |
if openapi_spec_url.startswith(("http://", "https://")):
|
| 11 |
response = httpx.get(openapi_spec_url)
|
|
|
|
| 28 |
for method, operation in path_item.items():
|
| 29 |
if methods and method.lower() not in [m.lower() for m in methods]:
|
| 30 |
continue
|
| 31 |
+
if not paths:
|
| 32 |
+
valid_api_paths.append({
|
| 33 |
+
"path": path,
|
| 34 |
+
"method": method.upper(),
|
| 35 |
+
})
|
| 36 |
+
else:
|
| 37 |
+
for path_regex in paths.split(","):
|
| 38 |
+
if re.match(path_regex, path):
|
| 39 |
+
valid_api_paths.append({
|
| 40 |
+
"path": path,
|
| 41 |
+
"method": method.upper(),
|
| 42 |
+
})
|
| 43 |
+
break
|
| 44 |
|
| 45 |
return gr.JSON(valid_api_paths, label=f"π {len(valid_api_paths)} endpoints found")
|
| 46 |
|
| 47 |
def update_bottom(oauth_token: gr.OAuthToken | None):
|
| 48 |
if oauth_token:
|
| 49 |
+
return "Click the π Create button to create a new MCP Space", gr.Button(interactive=True)
|
| 50 |
else:
|
| 51 |
return gr.skip()
|
| 52 |
|
|
|
|
| 56 |
gr.load_openapi(
|
| 57 |
openapi_spec=\"{}\",
|
| 58 |
base_url=\"{}\",
|
| 59 |
+
paths={},
|
| 60 |
methods={},
|
| 61 |
).launch(mcp_server=True)
|
| 62 |
"""
|
| 63 |
|
| 64 |
+
def create_hf_space(token, space_name, app_code):
|
| 65 |
"""
|
| 66 |
Create a new Hugging Face Space with optional app.py file
|
| 67 |
|
| 68 |
Args:
|
| 69 |
token (str): Your Hugging Face API token
|
| 70 |
+
space_name (str): The name of the space to create
|
| 71 |
app_code (str): String content for the app.py file
|
| 72 |
|
| 73 |
Returns:
|
|
|
|
| 77 |
api = HfApi(token=token)
|
| 78 |
user_info = api.whoami()
|
| 79 |
username = user_info["name"]
|
| 80 |
+
space_name = space_name or f"my-mcp-space-{random.randint(100000, 999999)}"
|
| 81 |
+
space_id = f"{username}/{space_name}"
|
| 82 |
|
| 83 |
try:
|
| 84 |
+
gr.Info(f"Creating space {space_id}...")
|
| 85 |
space_info = api.create_repo(
|
| 86 |
+
repo_id=space_id,
|
| 87 |
repo_type="space",
|
| 88 |
private=False,
|
| 89 |
space_sdk="gradio"
|
| 90 |
)
|
|
|
|
|
|
|
| 91 |
api.upload_file(
|
| 92 |
path_or_fileobj=app_code.encode('utf-8'),
|
| 93 |
path_in_repo="app.py",
|
| 94 |
+
repo_id=space_id,
|
| 95 |
repo_type="space",
|
| 96 |
commit_message="Add app.py"
|
| 97 |
)
|
| 98 |
+
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 99 |
+
gr.Success(f"π Your space is available at: <a href='{space_url}' target='_blank'>{space_url} ‴</a>", duration=None)
|
|
|
|
|
|
|
| 100 |
return space_info
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
gr.Error(f"β Error creating space: {str(e)}")
|
| 104 |
|
| 105 |
|
| 106 |
+
def launch_mcp_server(openapi_spec_url, api_base_url, paths, methods, space_name_box, oauth_token: gr.OAuthToken | None):
|
| 107 |
if oauth_token:
|
| 108 |
create_hf_space(
|
| 109 |
oauth_token.token,
|
| 110 |
+
space_name_box,
|
| 111 |
gradio_app_code.format(
|
| 112 |
openapi_spec_url,
|
| 113 |
api_base_url,
|
| 114 |
+
paths or "None",
|
| 115 |
methods
|
| 116 |
)
|
| 117 |
)
|
|
|
|
| 127 |
with gr.Column():
|
| 128 |
openapi_spec_url = gr.Textbox(label="OpenAPI Spec URL", value="https://petstore3.swagger.io/api/v3/openapi.json")
|
| 129 |
api_base_url = gr.Textbox(label="API Base URL", value="https://petstore3.swagger.io/api/v3/")
|
| 130 |
+
paths = gr.Textbox(label="Optional paths to filter by (comma separated regexes)", placeholder=".*user.*")
|
| 131 |
methods = gr.CheckboxGroup(label="Methods", choices=["GET", "POST", "PUT", "DELETE"], value=["GET", "POST", "PUT", "DELETE"])
|
| 132 |
find_endpoints_button = gr.Button("π Find Endpoints")
|
| 133 |
with gr.Column():
|
| 134 |
endpoints = gr.JSON(label="π endpoints found", value=[], max_height=400)
|
| 135 |
+
message = gr.Markdown("_Note:_ you must be signed in through your Hugging Face account to create the MCP Space")
|
| 136 |
+
space_name_box = gr.Textbox(show_label=False, placeholder="Optional space name (e.g. my-mcp-space)")
|
| 137 |
with gr.Row():
|
| 138 |
login_button = gr.LoginButton()
|
| 139 |
+
launch_button = gr.Button("π Create MCP Space", variant="primary", interactive=False)
|
| 140 |
|
| 141 |
gr.on(
|
| 142 |
[demo.load, find_endpoints_button.click],
|
| 143 |
find_endpoints,
|
| 144 |
+
inputs=[openapi_spec_url, api_base_url, paths, methods],
|
| 145 |
outputs=endpoints,
|
| 146 |
)
|
| 147 |
|
|
|
|
| 155 |
gr.on(
|
| 156 |
[launch_button.click],
|
| 157 |
launch_mcp_server,
|
| 158 |
+
inputs=[openapi_spec_url, api_base_url, paths, methods, space_name_box],
|
| 159 |
outputs=None
|
| 160 |
)
|
| 161 |
|