Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,35 +179,36 @@ def build_json_example(body_schema: dict, full_schema: dict) -> str:
|
|
| 179 |
return json.dumps(example, indent=2)
|
| 180 |
|
| 181 |
|
| 182 |
-
def build_field_component(f: dict):
|
| 183 |
"""Pick the widget type the way Swagger UI would, based on the field's schema."""
|
| 184 |
label = f["name"] + (" *required" if f["required"] else "")
|
| 185 |
default = f["example"] if f["example"] is not None else f["default"]
|
| 186 |
|
| 187 |
if f["is_file"]:
|
| 188 |
-
return gr.File(label=label)
|
| 189 |
if f["enum"]:
|
| 190 |
return gr.Dropdown(label=label, choices=[str(v) for v in f["enum"]],
|
| 191 |
value=str(default) if default is not None else None,
|
| 192 |
-
info=f["description"], interactive=True)
|
| 193 |
if f["type"] == "boolean":
|
| 194 |
return gr.Checkbox(label=label, value=bool(default) if default is not None else False,
|
| 195 |
-
info=f["description"], interactive=True)
|
| 196 |
if f["type"] in ("integer", "number"):
|
| 197 |
-
return gr.Number(label=label, value=default, info=f["description"], interactive=True)
|
| 198 |
if f["type"] == "array":
|
| 199 |
item_enum = f["items"].get("enum") if isinstance(f["items"], dict) else None
|
| 200 |
if item_enum:
|
| 201 |
return gr.Dropdown(label=label, choices=[str(v) for v in item_enum], multiselect=True,
|
| 202 |
-
info=f["description"], interactive=True)
|
| 203 |
return gr.Textbox(label=label, value=", ".join(default) if isinstance(default, list) else "",
|
| 204 |
placeholder="Comma-separated values", info=f["description"] or "List β separate items with commas",
|
| 205 |
-
interactive=True)
|
| 206 |
|
| 207 |
placeholder = f["description"] or f["name"]
|
| 208 |
if f["pattern"] and default is None:
|
| 209 |
placeholder = f"Pattern: {f['pattern']}"
|
| 210 |
-
return gr.Textbox(label=label, value=str(default) if default is not None else "", placeholder=placeholder,
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
def coerce_field_value(f: dict, val):
|
|
@@ -252,7 +253,7 @@ with gr.Blocks(title="SOG-X Modular Workspace") as demo:
|
|
| 252 |
gr.Markdown("### π₯ Input")
|
| 253 |
|
| 254 |
param_fields = extract_param_fields(mod, full_schema)
|
| 255 |
-
param_components = {f["name"]: build_field_component(f) for f in param_fields}
|
| 256 |
|
| 257 |
content_type, body_schema = get_request_body(mod, full_schema)
|
| 258 |
body_mode, body_fields, body_components, json_editor = None, [], {}, None
|
|
@@ -260,24 +261,24 @@ with gr.Blocks(title="SOG-X Modular Workspace") as demo:
|
|
| 260 |
if content_type == "multipart/form-data" and body_schema:
|
| 261 |
body_mode = "form"
|
| 262 |
body_fields = extract_multipart_fields(body_schema, full_schema)
|
| 263 |
-
body_components = {f["name"]: build_field_component(f) for f in body_fields}
|
| 264 |
elif content_type == "application/json" and body_schema:
|
| 265 |
body_mode = "json"
|
| 266 |
example_str = build_json_example(body_schema, full_schema)
|
| 267 |
json_editor = gr.Code(label="Request Body (JSON) β edit before running",
|
| 268 |
-
language="json", value=example_str, lines=10, interactive=True)
|
| 269 |
|
| 270 |
if not param_fields and not body_mode:
|
| 271 |
gr.Markdown("_This endpoint takes no parameters β just click Run._")
|
| 272 |
|
| 273 |
-
run_btn = gr.Button("π Run", variant="primary", size="lg")
|
| 274 |
gr.Markdown("---")
|
| 275 |
gr.Markdown("### π€ Output")
|
| 276 |
-
out_json = gr.JSON(label="Response Payload", visible=True)
|
| 277 |
-
out_image = gr.Image(label="Response Image", visible=False)
|
| 278 |
-
out_audio = gr.Audio(label="Response Audio", visible=False)
|
| 279 |
-
out_video = gr.Video(label="Response Video", visible=False)
|
| 280 |
-
out_file = gr.File(label="Response File", visible=False)
|
| 281 |
outputs = [out_json, out_image, out_audio, out_video, out_file]
|
| 282 |
|
| 283 |
def _hide_all_but(target, value):
|
|
|
|
| 179 |
return json.dumps(example, indent=2)
|
| 180 |
|
| 181 |
|
| 182 |
+
def build_field_component(f: dict, key: str):
|
| 183 |
"""Pick the widget type the way Swagger UI would, based on the field's schema."""
|
| 184 |
label = f["name"] + (" *required" if f["required"] else "")
|
| 185 |
default = f["example"] if f["example"] is not None else f["default"]
|
| 186 |
|
| 187 |
if f["is_file"]:
|
| 188 |
+
return gr.File(label=label, key=key)
|
| 189 |
if f["enum"]:
|
| 190 |
return gr.Dropdown(label=label, choices=[str(v) for v in f["enum"]],
|
| 191 |
value=str(default) if default is not None else None,
|
| 192 |
+
info=f["description"], interactive=True, key=key)
|
| 193 |
if f["type"] == "boolean":
|
| 194 |
return gr.Checkbox(label=label, value=bool(default) if default is not None else False,
|
| 195 |
+
info=f["description"], interactive=True, key=key)
|
| 196 |
if f["type"] in ("integer", "number"):
|
| 197 |
+
return gr.Number(label=label, value=default, info=f["description"], interactive=True, key=key)
|
| 198 |
if f["type"] == "array":
|
| 199 |
item_enum = f["items"].get("enum") if isinstance(f["items"], dict) else None
|
| 200 |
if item_enum:
|
| 201 |
return gr.Dropdown(label=label, choices=[str(v) for v in item_enum], multiselect=True,
|
| 202 |
+
info=f["description"], interactive=True, key=key)
|
| 203 |
return gr.Textbox(label=label, value=", ".join(default) if isinstance(default, list) else "",
|
| 204 |
placeholder="Comma-separated values", info=f["description"] or "List β separate items with commas",
|
| 205 |
+
interactive=True, key=key)
|
| 206 |
|
| 207 |
placeholder = f["description"] or f["name"]
|
| 208 |
if f["pattern"] and default is None:
|
| 209 |
placeholder = f"Pattern: {f['pattern']}"
|
| 210 |
+
return gr.Textbox(label=label, value=str(default) if default is not None else "", placeholder=placeholder,
|
| 211 |
+
interactive=True, key=key)
|
| 212 |
|
| 213 |
|
| 214 |
def coerce_field_value(f: dict, val):
|
|
|
|
| 253 |
gr.Markdown("### π₯ Input")
|
| 254 |
|
| 255 |
param_fields = extract_param_fields(mod, full_schema)
|
| 256 |
+
param_components = {f["name"]: build_field_component(f, key=f"param::{f['name']}") for f in param_fields}
|
| 257 |
|
| 258 |
content_type, body_schema = get_request_body(mod, full_schema)
|
| 259 |
body_mode, body_fields, body_components, json_editor = None, [], {}, None
|
|
|
|
| 261 |
if content_type == "multipart/form-data" and body_schema:
|
| 262 |
body_mode = "form"
|
| 263 |
body_fields = extract_multipart_fields(body_schema, full_schema)
|
| 264 |
+
body_components = {f["name"]: build_field_component(f, key=f"body::{f['name']}") for f in body_fields}
|
| 265 |
elif content_type == "application/json" and body_schema:
|
| 266 |
body_mode = "json"
|
| 267 |
example_str = build_json_example(body_schema, full_schema)
|
| 268 |
json_editor = gr.Code(label="Request Body (JSON) β edit before running",
|
| 269 |
+
language="json", value=example_str, lines=10, interactive=True, key="json_editor")
|
| 270 |
|
| 271 |
if not param_fields and not body_mode:
|
| 272 |
gr.Markdown("_This endpoint takes no parameters β just click Run._")
|
| 273 |
|
| 274 |
+
run_btn = gr.Button("π Run", variant="primary", size="lg", key="run_btn")
|
| 275 |
gr.Markdown("---")
|
| 276 |
gr.Markdown("### π€ Output")
|
| 277 |
+
out_json = gr.JSON(label="Response Payload", visible=True, key="out_json")
|
| 278 |
+
out_image = gr.Image(label="Response Image", visible=False, key="out_image")
|
| 279 |
+
out_audio = gr.Audio(label="Response Audio", visible=False, key="out_audio")
|
| 280 |
+
out_video = gr.Video(label="Response Video", visible=False, key="out_video")
|
| 281 |
+
out_file = gr.File(label="Response File", visible=False, key="out_file")
|
| 282 |
outputs = [out_json, out_image, out_audio, out_video, out_file]
|
| 283 |
|
| 284 |
def _hide_all_but(target, value):
|