import sys import logging import subprocess import gradio as gr from apscheduler.schedulers.background import BackgroundScheduler logging.basicConfig(level=logging.ERROR) from src.logging import LOGGER, read_logs from src.envs import API, REPO_ID sys.stdout = LOGGER sys.stderr = LOGGER COUNTER = 0 def launch_backend(): global COUNTER _ = subprocess.run(["python", "main_backend_lighteval.py"]) COUNTER += 1 # every 720 seconds = 12 hours, restart the space if COUNTER == 720: COUNTER = 0 API.restart_space(repo_id=REPO_ID) demo = gr.Blocks() with demo: logs = gr.Code(interactive=False) demo.load(read_logs, None, logs, every=1) scheduler = BackgroundScheduler() scheduler.add_job(launch_backend, "interval", seconds=60) # will only allow one job to be run at the same time scheduler.start() demo.queue(default_concurrency_limit=40).launch()