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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""
|
| 3 |
+
Gradio demo UI for the BIDSifierAgent.
|
| 4 |
+
|
| 5 |
+
This wraps the existing CLI-style step-wise logic (prompts.py + agent.py)
|
| 6 |
+
into an interactive Gradio interface.
|
| 7 |
+
|
| 8 |
+
Requirements
|
| 9 |
+
------------
|
| 10 |
+
pip install gradio bids_validator python-dotenv dspy-ai
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import time
|
| 16 |
+
from bids_validator import BIDSValidator
|
| 17 |
+
import subprocess
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 20 |
+
|
| 21 |
+
import gradio as gr
|
| 22 |
+
|
| 23 |
+
from agent import BIDSifierAgent # your existing agent
|
| 24 |
+
from cli import parse_commands_from_markdown # reuse the CLI helper if available
|
| 25 |
+
|
| 26 |
+
# Step mapping: UI label -> agent step id
|
| 27 |
+
|
| 28 |
+
BIDSIFIER_STEPS: Dict[str, str] = {
|
| 29 |
+
"1. Summarize dataset": "summary",
|
| 30 |
+
"2. Propose metadata commands": "create_metadata",
|
| 31 |
+
"3. Propose structure commands": "create_structure",
|
| 32 |
+
"4. Propose rename/move commands": "rename_move",
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
STEP_LABELS = list(BIDSIFIER_STEPS.keys())
|
| 36 |
+
NUM_STEPS = len(STEP_LABELS)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Helpers
|
| 40 |
+
|
| 41 |
+
def split_shell_commands(text: str) -> List[str]:
|
| 42 |
+
"""
|
| 43 |
+
Split a multi-line shell script into individual commands.
|
| 44 |
+
|
| 45 |
+
Each non-empty line is treated as a separate command, except when a line
|
| 46 |
+
ends with a backslash (\\), in which case it is joined with the following
|
| 47 |
+
line(s) to form a single logical command.
|
| 48 |
+
|
| 49 |
+
Parameters
|
| 50 |
+
----------
|
| 51 |
+
text : str
|
| 52 |
+
Multi-line string containing shell commands.
|
| 53 |
+
|
| 54 |
+
Returns
|
| 55 |
+
-------
|
| 56 |
+
list of str
|
| 57 |
+
The list of shell commands to execute.
|
| 58 |
+
"""
|
| 59 |
+
commands: List[str] = []
|
| 60 |
+
current: str = ""
|
| 61 |
+
|
| 62 |
+
for raw_line in text.splitlines():
|
| 63 |
+
line = raw_line.strip()
|
| 64 |
+
if not line:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
if current:
|
| 68 |
+
# Continue an ongoing command
|
| 69 |
+
if line.endswith("\\"):
|
| 70 |
+
current += " " + line[:-1].rstrip()
|
| 71 |
+
else:
|
| 72 |
+
current += " " + line
|
| 73 |
+
commands.append(current)
|
| 74 |
+
current = ""
|
| 75 |
+
else:
|
| 76 |
+
# Start a new command
|
| 77 |
+
if line.endswith("\\"):
|
| 78 |
+
current = line[:-1].rstrip()
|
| 79 |
+
else:
|
| 80 |
+
commands.append(line)
|
| 81 |
+
|
| 82 |
+
if current:
|
| 83 |
+
commands.append(current)
|
| 84 |
+
|
| 85 |
+
return commands
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def build_context(
|
| 89 |
+
dataset_xml: str,
|
| 90 |
+
readme_text: str,
|
| 91 |
+
publication_text: str,
|
| 92 |
+
output_root: str,
|
| 93 |
+
) -> Dict[str, Any]:
|
| 94 |
+
"""
|
| 95 |
+
Build the context dictionary expected by BIDSifierAgent.
|
| 96 |
+
|
| 97 |
+
Parameters
|
| 98 |
+
----------
|
| 99 |
+
dataset_xml : str
|
| 100 |
+
Dataset XML content (or empty string).
|
| 101 |
+
readme_text : str
|
| 102 |
+
README text content (or empty string).
|
| 103 |
+
publication_text : str
|
| 104 |
+
Publication/notes content (or empty string).
|
| 105 |
+
output_root : str
|
| 106 |
+
Target BIDS root directory.
|
| 107 |
+
|
| 108 |
+
Returns
|
| 109 |
+
-------
|
| 110 |
+
dict
|
| 111 |
+
Context dictionary.
|
| 112 |
+
"""
|
| 113 |
+
return {
|
| 114 |
+
"dataset_xml": dataset_xml or None,
|
| 115 |
+
"readme_text": readme_text or None,
|
| 116 |
+
"publication_text": publication_text or None,
|
| 117 |
+
"output_root": output_root or "./bids_output",
|
| 118 |
+
"user_feedback": "",
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# Core callbacks
|
| 123 |
+
|
| 124 |
+
def call_bidsifier_step(
|
| 125 |
+
dataset_xml: str,
|
| 126 |
+
readme_text: str,
|
| 127 |
+
publication_text: str,
|
| 128 |
+
output_root: str,
|
| 129 |
+
provider: str,
|
| 130 |
+
model: str,
|
| 131 |
+
step_label: str,
|
| 132 |
+
manual_prompt: str,
|
| 133 |
+
) -> Tuple[str, str, Dict[str, Any], int]:
|
| 134 |
+
"""
|
| 135 |
+
Call BIDSifierAgent for a given step and return raw output + parsed commands.
|
| 136 |
+
|
| 137 |
+
Parameters
|
| 138 |
+
----------
|
| 139 |
+
dataset_xml : str
|
| 140 |
+
Dataset XML content.
|
| 141 |
+
readme_text : str
|
| 142 |
+
README content.
|
| 143 |
+
publication_text : str
|
| 144 |
+
Publication/notes content.
|
| 145 |
+
output_root : str
|
| 146 |
+
Target BIDS root directory.
|
| 147 |
+
provider : str
|
| 148 |
+
LLM provider (e.g. "openai").
|
| 149 |
+
model : str
|
| 150 |
+
LLM model name (e.g. "gpt-5" or "gpt-4o-mini").
|
| 151 |
+
step_label : str
|
| 152 |
+
UI label of the selected step.
|
| 153 |
+
manual_prompt : str
|
| 154 |
+
Optional free-form user override; if non-empty we call `run_query`
|
| 155 |
+
instead of the structured `run_step`.
|
| 156 |
+
|
| 157 |
+
Returns
|
| 158 |
+
-------
|
| 159 |
+
llm_output : str
|
| 160 |
+
Raw text returned by the LLM.
|
| 161 |
+
commands_str : str
|
| 162 |
+
Commands extracted from the first fenced bash/sh code block.
|
| 163 |
+
state : dict
|
| 164 |
+
State capturing last call inputs, for potential reuse (e.g. retry).
|
| 165 |
+
step_index : int
|
| 166 |
+
Index of the current step (for progress updates).
|
| 167 |
+
"""
|
| 168 |
+
if not output_root.strip():
|
| 169 |
+
return (
|
| 170 |
+
"⚠️ Please provide an output root before calling BIDSifier.",
|
| 171 |
+
"",
|
| 172 |
+
{},
|
| 173 |
+
0,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
if step_label not in BIDSIFIER_STEPS:
|
| 177 |
+
return (
|
| 178 |
+
"⚠️ Please select a valid BIDSifier step.",
|
| 179 |
+
"",
|
| 180 |
+
{},
|
| 181 |
+
0,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
step_id = BIDSIFIER_STEPS[step_label]
|
| 185 |
+
context = build_context(dataset_xml, readme_text, publication_text, output_root)
|
| 186 |
+
|
| 187 |
+
agent = BIDSifierAgent(provider=provider, model=model)
|
| 188 |
+
|
| 189 |
+
# Decide whether to use the structured step prompt or a free-form query:
|
| 190 |
+
if manual_prompt.strip():
|
| 191 |
+
llm_output = agent.run_query(manual_prompt)
|
| 192 |
+
else:
|
| 193 |
+
llm_output = agent.run_step(step_id, context)
|
| 194 |
+
|
| 195 |
+
# Extract bash commands from fenced block
|
| 196 |
+
commands = parse_commands_from_markdown(llm_output)
|
| 197 |
+
commands_str = "\n".join(commands) if commands else ""
|
| 198 |
+
|
| 199 |
+
# Step index for progress bar
|
| 200 |
+
try:
|
| 201 |
+
step_index = STEP_LABELS.index(step_label) + 1
|
| 202 |
+
except ValueError:
|
| 203 |
+
step_index = 0
|
| 204 |
+
|
| 205 |
+
state = {
|
| 206 |
+
"dataset_xml": dataset_xml,
|
| 207 |
+
"readme_text": readme_text,
|
| 208 |
+
"publication_text": publication_text,
|
| 209 |
+
"output_root": output_root,
|
| 210 |
+
"provider": provider,
|
| 211 |
+
"model": model,
|
| 212 |
+
"step_label": step_label,
|
| 213 |
+
"step_id": step_id,
|
| 214 |
+
"llm_output": llm_output,
|
| 215 |
+
"commands": commands,
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
return llm_output, commands_str, state, step_index
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def confirm_commands(
|
| 222 |
+
last_state: Optional[Dict[str, Any]],
|
| 223 |
+
progress_value: int,
|
| 224 |
+
) -> Tuple[str, int]:
|
| 225 |
+
"""
|
| 226 |
+
Confirm and execute the commands proposed in the last LLM response.
|
| 227 |
+
|
| 228 |
+
Parameters
|
| 229 |
+
----------
|
| 230 |
+
last_state : dict or None
|
| 231 |
+
State returned by `call_bidsifier_step`, containing `output_root` and
|
| 232 |
+
list of `commands`. If None, nothing can be executed.
|
| 233 |
+
progress_value : int
|
| 234 |
+
Current progress through steps.
|
| 235 |
+
|
| 236 |
+
Returns
|
| 237 |
+
-------
|
| 238 |
+
status_message : str
|
| 239 |
+
Combined stdout/stderr/exit codes of executed commands.
|
| 240 |
+
new_progress : int
|
| 241 |
+
Updated progress value.
|
| 242 |
+
"""
|
| 243 |
+
if not last_state:
|
| 244 |
+
return "⚠️ No previous BIDSifier step to confirm.", progress_value
|
| 245 |
+
|
| 246 |
+
output_root = last_state.get("output_root", "").strip()
|
| 247 |
+
commands: List[str] = last_state.get("commands", [])
|
| 248 |
+
|
| 249 |
+
if not output_root:
|
| 250 |
+
return "⚠️ Output root is empty; cannot execute commands.", progress_value
|
| 251 |
+
|
| 252 |
+
if not commands:
|
| 253 |
+
return "⚠️ No commands detected in the last BIDSifier output.", progress_value
|
| 254 |
+
|
| 255 |
+
root = Path(output_root)
|
| 256 |
+
root.mkdir(parents=True, exist_ok=True)
|
| 257 |
+
|
| 258 |
+
all_details: List[str] = []
|
| 259 |
+
|
| 260 |
+
for raw_cmd in commands:
|
| 261 |
+
# Support multi-line shell with backslash continuations *within* a line,
|
| 262 |
+
# but commands already come one-per-line from `parse_commands_from_markdown`.
|
| 263 |
+
for cmd in split_shell_commands(raw_cmd):
|
| 264 |
+
proc = subprocess.run(
|
| 265 |
+
cmd,
|
| 266 |
+
shell=True,
|
| 267 |
+
cwd=str(root),
|
| 268 |
+
capture_output=True,
|
| 269 |
+
text=True,
|
| 270 |
+
)
|
| 271 |
+
all_details.append(
|
| 272 |
+
f"Executed: {cmd}\n"
|
| 273 |
+
f"Exit code: {proc.returncode}\n"
|
| 274 |
+
f"Stdout:\n{proc.stdout}\n"
|
| 275 |
+
f"Stderr:\n{proc.stderr}\n" + "-" * 40
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
status = "### Command execution log\n\n" + "\n\n".join(all_details)
|
| 279 |
+
|
| 280 |
+
# Heuristic: if we executed commands for this step, bump progress to max
|
| 281 |
+
# of current and the step index.
|
| 282 |
+
step_label = last_state.get("step_label")
|
| 283 |
+
try:
|
| 284 |
+
idx = STEP_LABELS.index(step_label)
|
| 285 |
+
new_progress = max(progress_value, idx + 1)
|
| 286 |
+
except (ValueError, TypeError):
|
| 287 |
+
new_progress = progress_value
|
| 288 |
+
|
| 289 |
+
return status, new_progress
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def run_bids_validation(output_root: str) -> Tuple[str, str]:
|
| 293 |
+
"""
|
| 294 |
+
Run the BIDS filename validator on all files under `output_root`.
|
| 295 |
+
|
| 296 |
+
Parameters
|
| 297 |
+
----------
|
| 298 |
+
output_root : str
|
| 299 |
+
Root directory of the BIDS dataset.
|
| 300 |
+
|
| 301 |
+
Returns
|
| 302 |
+
-------
|
| 303 |
+
report : str
|
| 304 |
+
A Markdown report summarizing which files are BIDS-like and which are not.
|
| 305 |
+
status_token : str
|
| 306 |
+
"pass:<timestamp>" if all files are BIDS-compliant (at least one file),
|
| 307 |
+
otherwise "fail:<timestamp>". The timestamp ensures Gradio's .change
|
| 308 |
+
event fires every time.
|
| 309 |
+
"""
|
| 310 |
+
if not output_root.strip():
|
| 311 |
+
return (
|
| 312 |
+
"⚠️ Please provide an output root before running the BIDS validator.",
|
| 313 |
+
f"fail:{time.time()}",
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
root = Path(output_root)
|
| 317 |
+
if not root.exists():
|
| 318 |
+
return (
|
| 319 |
+
f"⚠️ Output root `{output_root}` does not exist. Nothing to validate.",
|
| 320 |
+
f"fail:{time.time()}",
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
validator = BIDSValidator()
|
| 324 |
+
|
| 325 |
+
lines = []
|
| 326 |
+
valid_count = 0
|
| 327 |
+
invalid_count = 0
|
| 328 |
+
|
| 329 |
+
for path in sorted(root.rglob("*")):
|
| 330 |
+
if not path.is_file():
|
| 331 |
+
continue
|
| 332 |
+
rel = path.relative_to(root)
|
| 333 |
+
rel_str = "/" + rel.as_posix()
|
| 334 |
+
is_valid = validator.is_bids(rel_str)
|
| 335 |
+
if is_valid:
|
| 336 |
+
valid_count += 1
|
| 337 |
+
status = "OK"
|
| 338 |
+
else:
|
| 339 |
+
invalid_count += 1
|
| 340 |
+
status = "NOT BIDS"
|
| 341 |
+
lines.append(f"{rel_str}: {status}")
|
| 342 |
+
|
| 343 |
+
if not lines:
|
| 344 |
+
return (
|
| 345 |
+
f"Note: No files found under `{output_root}` to validate.",
|
| 346 |
+
f"fail:{time.time()}",
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
summary = (
|
| 350 |
+
f"Validated {valid_count + invalid_count} files: "
|
| 351 |
+
f"{valid_count} OK, {invalid_count} NOT BIDS."
|
| 352 |
+
)
|
| 353 |
+
bullet_lines = "\n".join(f"- `{line}`" for line in lines)
|
| 354 |
+
|
| 355 |
+
report = f"### BIDS Validator report\n\n{bullet_lines}\n\n**Summary:** {summary}"
|
| 356 |
+
|
| 357 |
+
status_flag = "pass" if invalid_count == 0 and valid_count > 0 else "fail"
|
| 358 |
+
status_token = f"{status_flag}:{time.time()}"
|
| 359 |
+
return report, status_token
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
# Gradio UI
|
| 363 |
+
|
| 364 |
+
with gr.Blocks(
|
| 365 |
+
title="BIDSifier Agent Interface",
|
| 366 |
+
theme=gr.themes.Citrus(),
|
| 367 |
+
head="""
|
| 368 |
+
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.2/dist/confetti.browser.min.js"></script>
|
| 369 |
+
""",
|
| 370 |
+
) as demo:
|
| 371 |
+
gr.Image(
|
| 372 |
+
value="images/bh_logo.png",
|
| 373 |
+
show_label=False,
|
| 374 |
+
height=80,
|
| 375 |
+
elem_id="bh_logo",
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
+
gr.Markdown(
|
| 379 |
+
"""
|
| 380 |
+
# BIDSifier Agent Demo
|
| 381 |
+
|
| 382 |
+
Interactive UI wrapping the **BIDSifierAgent** (CLI logic) to propose
|
| 383 |
+
shell commands for BIDS conversion, step by step.
|
| 384 |
+
Commands are extracted from fenced ```bash```/```sh``` blocks.
|
| 385 |
+
"""
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
with gr.Row():
|
| 389 |
+
dataset_xml_input = gr.Textbox(
|
| 390 |
+
label="Dataset XML",
|
| 391 |
+
placeholder="Paste dataset_structure.xml content here (optional)",
|
| 392 |
+
lines=8,
|
| 393 |
+
)
|
| 394 |
+
readme_input = gr.Textbox(
|
| 395 |
+
label="README",
|
| 396 |
+
placeholder="Paste README.md content here (optional)",
|
| 397 |
+
lines=8,
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
publication_input = gr.Textbox(
|
| 401 |
+
label="Publication / Notes",
|
| 402 |
+
placeholder="Paste relevant publication snippets or notes here (optional)",
|
| 403 |
+
lines=6,
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
with gr.Accordion("LLM settings (advanced)", open=False):
|
| 407 |
+
provider_input = gr.Dropdown(
|
| 408 |
+
label="Provider",
|
| 409 |
+
choices=["openai"],
|
| 410 |
+
value="openai",
|
| 411 |
+
)
|
| 412 |
+
model_input = gr.Textbox(
|
| 413 |
+
label="Model",
|
| 414 |
+
value="gpt-4o-mini",
|
| 415 |
+
placeholder="e.g., gpt-4o-mini, gpt-5",
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
output_root_input = gr.Textbox(
|
| 419 |
+
label="Output root",
|
| 420 |
+
placeholder="brainmets-bids",
|
| 421 |
+
lines=1,
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
step_dropdown = gr.Dropdown(
|
| 425 |
+
label="BIDSifier step",
|
| 426 |
+
choices=STEP_LABELS,
|
| 427 |
+
value=STEP_LABELS[0],
|
| 428 |
+
info="Select the current logical step in the BIDSifier workflow.",
|
| 429 |
+
)
|
| 430 |
+
|
| 431 |
+
progress_bar = gr.Slider(
|
| 432 |
+
label="Progress through BIDSifier steps",
|
| 433 |
+
minimum=0,
|
| 434 |
+
maximum=NUM_STEPS,
|
| 435 |
+
step=1,
|
| 436 |
+
value=0,
|
| 437 |
+
interactive=False,
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
manual_prompt_input = gr.Textbox(
|
| 441 |
+
label="Override prompt / free-form query (optional)",
|
| 442 |
+
placeholder=(
|
| 443 |
+
"If non-empty, this free-form query will be sent to the agent instead "
|
| 444 |
+
"of the structured step prompt."
|
| 445 |
+
),
|
| 446 |
+
lines=3,
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
call_button = gr.Button("Call BIDSifier", variant="primary")
|
| 450 |
+
|
| 451 |
+
llm_output_box = gr.Textbox(
|
| 452 |
+
label="Raw BIDSifier output",
|
| 453 |
+
lines=10,
|
| 454 |
+
interactive=True,
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
commands_box = gr.Textbox(
|
| 458 |
+
label="Parsed shell commands (from fenced bash block)",
|
| 459 |
+
lines=10,
|
| 460 |
+
interactive=True,
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
confirm_button = gr.Button("Confirm / Run commands", variant="primary")
|
| 464 |
+
bids_validator_button = gr.Button("Run BIDS Validator", variant="primary")
|
| 465 |
+
|
| 466 |
+
status_msg = gr.Markdown(label="Status / execution log")
|
| 467 |
+
validation_status = gr.Textbox(visible=False)
|
| 468 |
+
|
| 469 |
+
# State to store last agent call for Confirm
|
| 470 |
+
last_state = gr.State(value=None)
|
| 471 |
+
|
| 472 |
+
# Wiring
|
| 473 |
+
|
| 474 |
+
call_button.click(
|
| 475 |
+
fn=call_bidsifier_step,
|
| 476 |
+
inputs=[
|
| 477 |
+
dataset_xml_input,
|
| 478 |
+
readme_input,
|
| 479 |
+
publication_input,
|
| 480 |
+
output_root_input,
|
| 481 |
+
provider_input,
|
| 482 |
+
model_input,
|
| 483 |
+
step_dropdown,
|
| 484 |
+
manual_prompt_input,
|
| 485 |
+
],
|
| 486 |
+
outputs=[llm_output_box, commands_box, last_state, progress_bar],
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
confirm_button.click(
|
| 490 |
+
fn=confirm_commands,
|
| 491 |
+
inputs=[last_state, progress_bar],
|
| 492 |
+
outputs=[status_msg, progress_bar],
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
bids_validator_button.click(
|
| 496 |
+
fn=run_bids_validation,
|
| 497 |
+
inputs=[output_root_input],
|
| 498 |
+
outputs=[status_msg, validation_status],
|
| 499 |
+
)
|
| 500 |
+
|
| 501 |
+
validation_status.change(
|
| 502 |
+
fn=None,
|
| 503 |
+
inputs=[validation_status],
|
| 504 |
+
outputs=[],
|
| 505 |
+
js="""
|
| 506 |
+
(value) => {
|
| 507 |
+
if (value && value.startsWith("pass") && window.confetti) {
|
| 508 |
+
window.confetti({
|
| 509 |
+
particleCount: 240,
|
| 510 |
+
spread: 70,
|
| 511 |
+
origin: { y: 0.6 }
|
| 512 |
+
});
|
| 513 |
+
}
|
| 514 |
+
return [];
|
| 515 |
+
}
|
| 516 |
+
""",
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
if __name__ == "__main__":
|
| 521 |
+
demo.launch()
|
images/bh_logo.png
ADDED
|