Spaces:
Runtime error
Runtime error
Commit
•
29317f3
1
Parent(s):
6e678fc
Upload status_checker.py with huggingface_hub
Browse files- status_checker.py +117 -0
status_checker.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import time
|
4 |
+
from pathlib import Path
|
5 |
+
from threading import Thread
|
6 |
+
from typing import List, Union
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from huggingface_hub import HfFolder, delete_repo, upload_folder, get_space_runtime, request_space_hardware, DatasetCard
|
10 |
+
|
11 |
+
|
12 |
+
def process_is_complete(process_pid):
|
13 |
+
'''Checks if the process with the given PID is still running'''
|
14 |
+
p = subprocess.Popen(["ps", "-p", process_pid], stdout=subprocess.PIPE)
|
15 |
+
out = p.communicate()[0].decode("utf-8").strip().split("\n")
|
16 |
+
return len(out) == 1
|
17 |
+
|
18 |
+
def get_task_status(output_dataset_id):
|
19 |
+
'''Gets the task status from the output dataset repo'''
|
20 |
+
card = DatasetCard.load(output_dataset_id)
|
21 |
+
return card.data.fuego['status']
|
22 |
+
|
23 |
+
def set_task_status(output_dataset_id, status="done"):
|
24 |
+
'''Sets the task status in the output dataset repo'''
|
25 |
+
card = DatasetCard.load(output_dataset_id)
|
26 |
+
card.data.fuego['status'] = status
|
27 |
+
card.push_to_hub(output_dataset_id)
|
28 |
+
|
29 |
+
def check_for_status(
|
30 |
+
process_pid, this_space_id, output_dataset_id, output_dirs, delete_on_completion, downgrade_hardware_on_completion
|
31 |
+
):
|
32 |
+
task_status = get_task_status(output_dataset_id)
|
33 |
+
print("Task status (found in dataset repo)", task_status)
|
34 |
+
if task_status == "done":
|
35 |
+
print("Task was already done, exiting...")
|
36 |
+
return
|
37 |
+
elif task_status == "preparing":
|
38 |
+
print("Setting task status to running...")
|
39 |
+
set_task_status(output_dataset_id, "running")
|
40 |
+
|
41 |
+
print("Watching PID of script to see if it is done running")
|
42 |
+
while True:
|
43 |
+
if process_is_complete(process_pid):
|
44 |
+
print("Process is complete! Uploading assets to output dataset repo")
|
45 |
+
for output_dir in output_dirs:
|
46 |
+
if Path(output_dir).exists():
|
47 |
+
print("Uploading folder", output_dir)
|
48 |
+
upload_folder(
|
49 |
+
repo_id=output_dataset_id,
|
50 |
+
folder_path=str(output_dir),
|
51 |
+
path_in_repo=str(Path('.outputs') / output_dir),
|
52 |
+
repo_type="dataset",
|
53 |
+
)
|
54 |
+
else:
|
55 |
+
print("Folder", output_dir, "does not exist, skipping")
|
56 |
+
|
57 |
+
print("Finished uploading outputs to dataset repo...Finishing up...")
|
58 |
+
if delete_on_completion:
|
59 |
+
print("Deleting space...")
|
60 |
+
delete_repo(repo_id=this_space_id, repo_type="space")
|
61 |
+
elif downgrade_hardware_on_completion:
|
62 |
+
runtime = get_space_runtime(this_space_id)
|
63 |
+
if runtime.hardware not in [None, "cpu-basic"]:
|
64 |
+
print("Requesting downgrade to CPU Basic...")
|
65 |
+
request_space_hardware(repo_id=this_space_id, hardware="cpu-basic")
|
66 |
+
else:
|
67 |
+
print("Space is already on cpu-basic, not downgrading.")
|
68 |
+
print("Done! Setting task status to done in dataset repo")
|
69 |
+
set_task_status(output_dataset_id, "done")
|
70 |
+
return
|
71 |
+
time.sleep(5)
|
72 |
+
|
73 |
+
|
74 |
+
def main(
|
75 |
+
this_space_repo_id: str,
|
76 |
+
output_dataset_id: str,
|
77 |
+
output_dirs: Union[str, List[str]] = "./outputs",
|
78 |
+
delete_on_completion: bool = True,
|
79 |
+
downgrade_hardware_on_completion: bool = True,
|
80 |
+
):
|
81 |
+
token_env_var = os.getenv("HF_TOKEN")
|
82 |
+
if token_env_var is None:
|
83 |
+
raise ValueError(
|
84 |
+
"Please set HF_TOKEN environment variable to your Hugging Face token. You can do this in the settings tab of your space."
|
85 |
+
)
|
86 |
+
|
87 |
+
if isinstance(output_dirs, str):
|
88 |
+
output_dirs = [output_dirs]
|
89 |
+
|
90 |
+
HfFolder().save_token(token_env_var)
|
91 |
+
|
92 |
+
# Watch python script's process to see when it's done running
|
93 |
+
process_pid = os.getenv("USER_SCRIPT_PID", None)
|
94 |
+
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown(Path("about.md").read_text())
|
97 |
+
|
98 |
+
thread = Thread(
|
99 |
+
target=check_for_status,
|
100 |
+
daemon=True,
|
101 |
+
args=(
|
102 |
+
process_pid,
|
103 |
+
this_space_repo_id,
|
104 |
+
output_dataset_id,
|
105 |
+
output_dirs,
|
106 |
+
delete_on_completion,
|
107 |
+
downgrade_hardware_on_completion,
|
108 |
+
),
|
109 |
+
)
|
110 |
+
thread.start()
|
111 |
+
demo.launch()
|
112 |
+
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
import fire
|
116 |
+
|
117 |
+
fire.Fire(main)
|