1616 import tomli as tomllib
1717
1818import gradio
19+ import httpx
1920import huggingface_hub
2021from gradio_client import Client , handle_file
2122from httpx import ReadTimeout
3435SPACE_URL = "https://huggingface.co/spaces/{space_id}"
3536
3637
37- def _readme_linked_hub_yaml (dataset_id : str | None , bucket_id : str | None ) -> str :
38- parts = []
38+ def _readme_linked_hub_yaml (dataset_id : str | None ) -> str :
3939 if dataset_id is not None :
40- parts .append (f"datasets:\n - { dataset_id } \n " )
41- if bucket_id is not None :
42- parts .append (f"buckets:\n - { bucket_id } \n " )
43- return "" .join (parts )
40+ return f"datasets:\n - { dataset_id } \n "
41+ return ""
42+
43+
44+ def _space_app_py_content (bucket_id : str | None ) -> str :
45+ if bucket_id is None :
46+ return "import trackio\n trackio.show()\n "
47+ path = Path (files ("trackio" )) / "space_bucket_app.py"
48+ return path .read_text (encoding = "utf-8" )
4449
4550
4651def _retry_hf_write (op_name : str , fn , retries : int = 4 , initial_delay : float = 1.5 ):
@@ -157,15 +162,8 @@ def deploy_as_space(
157162 readme_content = readme_content .replace ("{GRADIO_VERSION}" , gradio .__version__ )
158163 readme_content = readme_content .replace ("{APP_FILE}" , "app.py" )
159164 readme_content = readme_content .replace (
160- "{LINKED_HUB_METADATA}" , _readme_linked_hub_yaml (dataset_id , bucket_id )
165+ "{LINKED_HUB_METADATA}" , _readme_linked_hub_yaml (dataset_id )
161166 )
162- if bucket_id is not None :
163- bucket_mount = (
164- f"hf_mount:\n - src: hf://buckets/{ bucket_id } \n dst: /data/trackio\n "
165- )
166- else :
167- bucket_mount = ""
168- readme_content = readme_content .replace ("{BUCKET_MOUNT}" , bucket_mount )
169167 readme_buffer = io .BytesIO (readme_content .encode ("utf-8" ))
170168 hf_api .upload_file (
171169 path_or_fileobj = readme_buffer ,
@@ -217,8 +215,7 @@ def deploy_as_space(
217215 ],
218216 )
219217
220- app_file_content = """import trackio
221- trackio.show()"""
218+ app_file_content = _space_app_py_content (bucket_id )
222219 app_file_buffer = io .BytesIO (app_file_content .encode ("utf-8" ))
223220 hf_api .upload_file (
224221 path_or_fileobj = app_file_buffer ,
@@ -231,6 +228,7 @@ def deploy_as_space(
231228 huggingface_hub .add_space_secret (space_id , "HF_TOKEN" , hf_token )
232229 if bucket_id is not None :
233230 huggingface_hub .add_space_variable (space_id , "TRACKIO_BUCKET_ID" , bucket_id )
231+ huggingface_hub .add_space_variable (space_id , "TRACKIO_DIR" , "/data/trackio" )
234232 elif dataset_id is not None :
235233 huggingface_hub .add_space_variable (space_id , "TRACKIO_DATASET_ID" , dataset_id )
236234 if logo_light_url := os .environ .get ("TRACKIO_LOGO_LIGHT_URL" ):
@@ -307,15 +305,32 @@ def _wait_until_space_running(space_id: str, timeout: int = 300) -> None:
307305 hf_api = huggingface_hub .HfApi ()
308306 start = time .time ()
309307 delay = 2
308+ request_timeout = 45.0
309+ failure_stages = frozenset (
310+ ("NO_APP_FILE" , "CONFIG_ERROR" , "BUILD_ERROR" , "RUNTIME_ERROR" )
311+ )
310312 while time .time () - start < timeout :
311313 try :
312- info = hf_api .space_info (space_id )
313- if info .runtime and info .runtime .stage == "RUNNING" :
314- return
315- except (huggingface_hub .utils .HfHubHTTPError , ReadTimeout ):
314+ info = hf_api .space_info (space_id , timeout = request_timeout )
315+ if info .runtime :
316+ stage = str (info .runtime .stage )
317+ if stage in failure_stages :
318+ raise RuntimeError (
319+ f"Space { space_id } entered terminal stage { stage } . "
320+ "Fix README.md or app files; see build logs on the Hub."
321+ )
322+ if stage == "RUNNING" :
323+ return
324+ except RuntimeError :
325+ raise
326+ except (huggingface_hub .utils .HfHubHTTPError , httpx .RequestError ):
316327 pass
317328 time .sleep (delay )
318329 delay = min (delay * 1.5 , 15 )
330+ raise TimeoutError (
331+ f"Space { space_id } did not reach RUNNING within { timeout } s. "
332+ "Check status and build logs on the Hub."
333+ )
319334
320335
321336def wait_until_space_exists (
@@ -337,7 +352,7 @@ def wait_until_space_exists(
337352 try :
338353 hf_api .space_info (space_id )
339354 return
340- except (huggingface_hub .utils .HfHubHTTPError , ReadTimeout ):
355+ except (huggingface_hub .utils .HfHubHTTPError , httpx . RequestError ):
341356 time .sleep (delay )
342357 delay = min (delay * 2 , 60 )
343358 raise TimeoutError ("Waiting for space to exist took longer than expected" )
@@ -569,7 +584,7 @@ def deploy_as_static_space(
569584 else :
570585 raise ValueError (f"Failed to create Space: { e } " )
571586
572- linked = _readme_linked_hub_yaml (dataset_id , bucket_id )
587+ linked = _readme_linked_hub_yaml (dataset_id )
573588 readme_content = (
574589 "---\n sdk: static\n pinned: false\n tags:\n - trackio\n "
575590 f"{ linked } ---\n "
0 commit comments