2022-11-23 08:56:46 +08:00
|
|
|
import json
|
2024-02-14 01:05:19 +08:00
|
|
|
import os
|
2022-11-23 08:56:46 +08:00
|
|
|
import random
|
2022-12-28 20:40:03 +08:00
|
|
|
import subprocess
|
2024-02-14 01:05:19 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import nbformat as nbf
|
2022-11-23 08:56:46 +08:00
|
|
|
|
2024-02-14 01:05:19 +08:00
|
|
|
GRADIO_DEMO_DIR = Path.cwd() / "demo"
|
2022-11-23 08:56:46 +08:00
|
|
|
DEMOS_TO_SKIP = {"all_demos", "reset_components", "custom_path", "kitchen_sink_random"}
|
|
|
|
|
|
|
|
demos = os.listdir(GRADIO_DEMO_DIR)
|
2024-02-14 01:05:19 +08:00
|
|
|
demos = [
|
|
|
|
demo
|
|
|
|
for demo in demos
|
|
|
|
if demo not in DEMOS_TO_SKIP
|
|
|
|
and os.path.isdir(os.path.join(GRADIO_DEMO_DIR, demo))
|
|
|
|
and os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "run.py"))
|
|
|
|
]
|
|
|
|
|
2022-11-23 08:56:46 +08:00
|
|
|
|
2022-12-28 20:40:03 +08:00
|
|
|
def git_tracked(demo, file):
|
2024-02-14 01:05:19 +08:00
|
|
|
osstdout = subprocess.Popen(
|
|
|
|
f"cd demo/{demo} && git ls-files --error-unmatch {file}",
|
|
|
|
shell=True,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
close_fds=True,
|
|
|
|
)
|
2022-12-28 20:40:03 +08:00
|
|
|
osstdout.wait()
|
|
|
|
return not osstdout.returncode
|
|
|
|
|
2024-02-14 01:05:19 +08:00
|
|
|
|
|
|
|
for demo in demos:
|
2022-11-23 08:56:46 +08:00
|
|
|
nb = nbf.v4.new_notebook()
|
|
|
|
text = f"# Gradio Demo: {demo}"
|
|
|
|
|
|
|
|
if os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "DESCRIPTION.md")):
|
2024-02-14 01:05:19 +08:00
|
|
|
with open(
|
|
|
|
os.path.join(GRADIO_DEMO_DIR, demo, "DESCRIPTION.md"), encoding="utf8"
|
|
|
|
) as f:
|
2022-11-23 08:56:46 +08:00
|
|
|
description = f.read()
|
|
|
|
text += f"""\n### {description}
|
|
|
|
"""
|
|
|
|
|
|
|
|
files = os.listdir(os.path.join(GRADIO_DEMO_DIR, demo))
|
2024-02-14 01:05:19 +08:00
|
|
|
skip = [
|
|
|
|
"run.py",
|
|
|
|
"run.ipynb",
|
|
|
|
"setup.sh",
|
|
|
|
".gitignore",
|
|
|
|
"requirements.txt",
|
|
|
|
"DESCRIPTION.md",
|
|
|
|
"screenshot.png",
|
|
|
|
"screenshot.gif",
|
|
|
|
]
|
2022-12-28 20:40:03 +08:00
|
|
|
files = [file for file in files if file not in skip if git_tracked(demo, file)]
|
2022-11-23 08:56:46 +08:00
|
|
|
files.sort()
|
2024-02-14 01:05:19 +08:00
|
|
|
if files:
|
2022-11-23 08:56:46 +08:00
|
|
|
get_files = "# Downloading files from the demo repo\nimport os"
|
|
|
|
for file in files:
|
|
|
|
if os.path.isdir(os.path.join(GRADIO_DEMO_DIR, demo, file)):
|
|
|
|
sub_files = os.listdir(os.path.join(GRADIO_DEMO_DIR, demo, file))
|
2024-02-14 01:05:19 +08:00
|
|
|
sub_files = [
|
|
|
|
sub
|
|
|
|
for sub in sub_files
|
|
|
|
if sub not in skip
|
|
|
|
if git_tracked(demo, f"{file}/{sub}")
|
|
|
|
]
|
2022-11-23 08:56:46 +08:00
|
|
|
sub_files.sort()
|
2022-12-28 20:40:03 +08:00
|
|
|
if sub_files:
|
|
|
|
get_files += f"\nos.mkdir('{file}')"
|
2022-11-23 08:56:46 +08:00
|
|
|
for sub_file in sub_files:
|
|
|
|
get_files += f"\n!wget -q -O {file}/{sub_file} https://github.com/gradio-app/gradio/raw/main/demo/{demo}/{file}/{sub_file}"
|
|
|
|
else:
|
|
|
|
get_files += f"\n!wget -q https://github.com/gradio-app/gradio/raw/main/demo/{demo}/{file}"
|
|
|
|
|
|
|
|
requirements = ""
|
|
|
|
if os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "requirements.txt")):
|
2024-02-14 01:05:19 +08:00
|
|
|
with open(
|
|
|
|
os.path.join(GRADIO_DEMO_DIR, demo, "requirements.txt"),
|
|
|
|
encoding="utf8",
|
|
|
|
) as f:
|
2022-11-23 08:56:46 +08:00
|
|
|
requirements = f.read().split("\n")
|
|
|
|
requirements = " ".join(requirements)
|
|
|
|
|
|
|
|
installs = f"!pip install -q gradio {requirements}"
|
|
|
|
|
2024-02-14 01:05:19 +08:00
|
|
|
with open(os.path.join(GRADIO_DEMO_DIR, demo, "run.py"), encoding="utf8") as f:
|
2022-11-23 08:56:46 +08:00
|
|
|
code = f.read()
|
|
|
|
code = code.replace("os.path.dirname(__file__)", "os.path.abspath('')")
|
2024-02-14 01:05:19 +08:00
|
|
|
|
|
|
|
if files:
|
|
|
|
nb["cells"] = [
|
|
|
|
nbf.v4.new_markdown_cell(text),
|
|
|
|
nbf.v4.new_code_cell(installs),
|
|
|
|
nbf.v4.new_code_cell(get_files),
|
|
|
|
nbf.v4.new_code_cell(code),
|
|
|
|
]
|
2022-11-23 08:56:46 +08:00
|
|
|
else:
|
2024-02-14 01:05:19 +08:00
|
|
|
nb["cells"] = [
|
|
|
|
nbf.v4.new_markdown_cell(text),
|
|
|
|
nbf.v4.new_code_cell(installs),
|
|
|
|
nbf.v4.new_code_cell(code),
|
|
|
|
]
|
|
|
|
|
2022-11-23 08:56:46 +08:00
|
|
|
output_notebook = os.path.join(GRADIO_DEMO_DIR, demo, "run.ipynb")
|
|
|
|
|
2024-02-14 01:05:19 +08:00
|
|
|
with open(output_notebook, "w", encoding="utf8") as f:
|
2022-11-23 08:56:46 +08:00
|
|
|
nbf.write(nb, f)
|
|
|
|
|
2024-02-14 01:05:19 +08:00
|
|
|
with open(output_notebook, encoding="utf8") as f:
|
2022-11-23 08:56:46 +08:00
|
|
|
content = f.read()
|
2024-02-14 01:05:19 +08:00
|
|
|
|
2022-11-23 08:56:46 +08:00
|
|
|
content = json.loads(content)
|
|
|
|
for i, cell in enumerate(content["cells"]):
|
|
|
|
random.seed(i)
|
2023-10-16 09:16:57 +08:00
|
|
|
cell["id"] = str(random.getrandbits(128))
|
2022-11-23 08:56:46 +08:00
|
|
|
|
2023-03-25 07:28:38 +08:00
|
|
|
with open(output_notebook, "w", encoding="utf8") as f:
|
2023-10-16 09:16:57 +08:00
|
|
|
f.write(json.dumps(content))
|