Spaces:
Sleeping
Sleeping
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 | |
import subprocess | |
HfFolder().save_token(os.getenv("HF_TOKEN")) | |
output_dataset_id = "nateraw/asdf123" | |
# Where user will write outputs from their script | |
outputs_dir = Path('outputs') | |
process_pid = os.getenv('USER_SCRIPT_PID', None) | |
print(f"USER SCRIPT PID? - {process_pid} - type {type(process_pid)}") | |
def process_is_complete(): | |
# Attempt at solving the todo shown below | |
p = subprocess.Popen(['ps', '-p', process_pid], stdout=subprocess.PIPE) | |
out = p.communicate()[0].decode('utf-8').strip().split('\n') | |
return len(out) == 1 | |
# 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(): | |
print("Waiting to find output_file to check if script is done running") | |
while True: | |
print(f"Process is complete? {process_is_complete()}") | |
if output_file.exists(): | |
print("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') | |
print("Finished uploading outputs from script. Done now!") | |
return | |
print("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(): | |
print("Didnt find output file on init, so starting thread to watch for it!") | |
thread = Thread(target=status_checker, daemon=True) | |
thread.start() | |
demo.launch() |