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")) | |
| # Where outputs from user script will be pushed to for safe keeping | |
| output_dataset_id = "nateraw/asdf123" | |
| # Where user will write outputs from their script | |
| outputs_dir = Path('outputs') | |
| # Watch python script's process to see when it's done running | |
| process_pid = os.getenv('USER_SCRIPT_PID', None) | |
| def process_is_complete(): | |
| p = subprocess.Popen(['ps', '-p', process_pid], stdout=subprocess.PIPE) | |
| out = p.communicate()[0].decode('utf-8').strip().split('\n') | |
| return len(out) == 1 | |
| def status_checker(): | |
| print("Watching PID of script to see if it is done running") | |
| while True: | |
| if process_is_complete(): | |
| print("Process is complete! Uploading assets to output dataset repo") | |
| upload_folder(repo_id=output_dataset_id, folder_path=str(outputs_dir), path_in_repo='.', repo_type='dataset') | |
| print("Finished uploading outputs to dataset repo. Done!") | |
| 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()) | |
| def main(): | |
| thread = Thread(target=status_checker, daemon=True) | |
| thread.start() | |
| demo.launch() | |
| if __name__ == '__main__': | |
| main() |