gradio/website/demos/run_demos.py

44 lines
1.2 KiB
Python
Raw Normal View History

import time
import os
import requests
import json
import importlib
import sys
import threading
import subprocess
LAUNCH_PERIOD = 60
GRADIO_DEMO_DIR = "../../demo"
sys.path.insert(0, GRADIO_DEMO_DIR)
with open("demos.json") as demos_file:
demo_port_sets = json.load(demos_file)
2021-12-21 09:20:29 +08:00
def launch_demo(demo_folder):
subprocess.call(f"cd {demo_folder} && python run.py", shell=True)
2021-12-17 06:13:59 +08:00
for demo_name, port in demo_port_sets:
2021-12-21 09:20:29 +08:00
demo_folder = os.path.join(GRADIO_DEMO_DIR, demo_name)
demo_file = os.path.join(demo_folder, "run.py")
2021-12-17 06:13:59 +08:00
with open(demo_file, 'r') as file:
filedata = file.read()
filedata = filedata.replace(
2021-12-27 14:30:19 +08:00
f'iface.launch()',
f'iface.launch(server_port={port})')
2021-12-17 06:13:59 +08:00
with open(demo_file, 'w') as file:
file.write(filedata)
2021-12-21 09:20:29 +08:00
demo_thread = threading.Thread(target=launch_demo, args=(demo_folder,))
demo_thread.start()
start_time = time.time()
while True:
for demo_name, _ in demo_port_sets:
r = requests.head(f"http://localhost:80/demo/{demo_name}/")
if r.status_code != 200:
print(demo_name, "down")
if time.time() - start_time > LAUNCH_PERIOD:
raise ValueError(f"{demo_name} is down.")
else:
print(demo_name, "up")
time.sleep(10)