run-script-in-background / status_checker.py
nateraw's picture
Update status_checker.py
c212dfb
raw
history blame
No virus
1.44 kB
import os
from pathlib import Path
from threading import Thread
import gradio as gr
from huggingface_hub import upload_folder, HfFolder, delete_repo
import sys
import time
from huggingface_hub.utils import logging
logger = logging.get_logger(__name__)
HfFolder().save_token(os.getenv("HF_TOKEN"))
output_dataset_id = "nateraw/asdf123"
# Where user will write outputs from their script
outputs_dir = Path('outputs')
# A file that marks that the script has finished running
# TODO - maybe check process like this instead of with done.txt: https://stackoverflow.com/a/2944076
output_file = Path('done.txt')
def status_checker():
logger.info("Waiting to find output_file to check if script is done running")
while True:
if output_file.exists():
logger.info("Found the output file - Uploading the outputs from the script")
upload_folder(repo_id=output_dataset_id, folder_path=str(outputs_dir), path_in_repo='.', repo_type='dataset')
logger.info("Finished uploading outputs from script. Done now!")
return
logger.info("Didn't find it...sleeping for 5 seconds.")
time.sleep(5)
with gr.Blocks() as demo:
gr.Markdown(Path('about.md').read_text())
if not output_file.exists():
logger.info("Didnt find output file on init, so starting thread to watch for it!")
thread = Thread(target=status_checker, daemon=True)
thread.start()
demo.launch()