2020PATH_REQ_DEFAULT = os .path .join (_PATH_ROOT , "requirements" , "default.txt" )
2121PATH_SCRIPT_RENDER = os .path .join (_PATH_HERE , "_ipynb-render.sh" )
2222PATH_SCRIPT_TEST = os .path .join (_PATH_HERE , "_ipynb-test.sh" )
23+ # https://askubuntu.com/questions/909918/how-to-show-unzip-progress
24+ UNZIP_PROGRESS_BAR = ' | awk \' BEGIN {ORS=" "} {if(NR%10==0)print "."}\' '
2325REPO_NAME = "lightning-tutorials"
2426COLAB_REPO_LINK = "https://colab.research.google.com/github/PytorchLightning"
2527BRANCH_DEFAULT = "main"
@@ -136,8 +138,9 @@ def get_running_torch_version():
136138class AssistantCLI :
137139 """Collection of handy CLI commands."""
138140
139- DEVICE_ACCELERATOR = os .environ .get ("ACCELERATOR" , "cpu" ).lower ()
140- DATASET_FOLDER = os .environ .get ("PATH_DATASETS" , "_datasets" ).lower ()
141+ _LOCAL_ACCELERATOR = "cpu,gpu" if get_running_cuda_version () else "cpu"
142+ DEVICE_ACCELERATOR = os .environ .get ("ACCELERATOR" , _LOCAL_ACCELERATOR ).lower ()
143+ DATASETS_FOLDER = os .environ .get ("PATH_DATASETS" , "_datasets" )
141144 DRY_RUN = bool (int (os .environ .get ("DRY_RUN" , 0 )))
142145 _META_REQUIRED_FIELDS = ("title" , "author" , "license" , "description" )
143146 _SKIP_DIRS = (
@@ -152,7 +155,7 @@ class AssistantCLI:
152155 )
153156 _META_FILE_REGEX = ".meta.{yaml,yml}"
154157 _META_PIP_KEY = "pip__"
155- _META_ACCEL_DEFAULT = ( "CPU" , )
158+ _META_ACCEL_DEFAULT = _LOCAL_ACCELERATOR . split ( "," )
156159
157160 # Map directory names to tag names. Note that dashes will be replaced with spaces in rendered tags in the docs.
158161 _DIR_TO_TAG = {
@@ -268,16 +271,15 @@ def _parse_requirements(folder: str) -> Tuple[str, str]:
268271
269272 @staticmethod
270273 def _bash_download_data (folder : str ) -> List [str ]:
271- """Generate sequence of commands fro optional downloading dataset specified in the meta file.
274+ """Generate sequence of commands for optional downloading dataset specified in the meta file.
272275
273276 Args:
274277 folder: path to the folder with python script, meta and artefacts
275278 """
276- cmd = ["HERE=$PWD" , f"cd { AssistantCLI .DATASET_FOLDER } " ]
277279 meta = AssistantCLI ._load_meta (folder )
278280 datasets = meta .get ("datasets" , {})
279281 data_kaggle = datasets .get ("kaggle" , [])
280- cmd + = [f"python -m kaggle competitions download -c { name } " for name in data_kaggle ]
282+ cmd = [f"python -m kaggle competitions download -c { name } " for name in data_kaggle ]
281283 files = [f"{ name } .zip" for name in data_kaggle ]
282284 data_web = datasets .get ("web" , [])
283285 cmd += [f"wget { web } --progress=bar:force:noscroll --tries=3" for web in data_web ]
@@ -287,11 +289,11 @@ def _bash_download_data(folder: str) -> List[str]:
287289 if ext not in AssistantCLI ._EXT_ARCHIVE :
288290 continue
289291 if ext in AssistantCLI ._EXT_ARCHIVE_ZIP :
290- cmd += [f"mkdir -p { name } " , f" unzip -o { fn } -d { name } " ]
292+ cmd += [f"unzip -o { fn } -d { AssistantCLI . DATASETS_FOLDER } / { name } { UNZIP_PROGRESS_BAR } " ]
291293 else :
292294 cmd += [f"tar -zxvf { fn } --overwrite" ]
293295 cmd += [f"rm { fn } " ]
294- cmd += ["ls -l" , "cd $HERE " ]
296+ cmd += [f"tree -L 2 { AssistantCLI . DATASETS_FOLDER } " ]
295297 return cmd
296298
297299 @staticmethod
@@ -310,26 +312,29 @@ def bash_render(folder: str, output_file: str = PATH_SCRIPT_RENDER) -> Optional[
310312 cmd += AssistantCLI ._bash_download_data (folder )
311313 ipynb_file , meta_file , thumb_file = AssistantCLI ._valid_folder (folder , ext = ".ipynb" )
312314 pub_ipynb = os .path .join (DIR_NOTEBOOKS , f"{ folder } .ipynb" )
315+ pub_meta = pub_ipynb .replace (".ipynb" , ".yaml" )
313316 pub_dir = os .path .dirname (pub_ipynb )
314317 thumb_ext = os .path .splitext (thumb_file )[- 1 ] if thumb_file else "."
315318 pub_thumb = os .path .join (DIR_NOTEBOOKS , f"{ folder } { thumb_ext } " ) if thumb_file else ""
316319 cmd .append (f"mkdir -p { pub_dir } " )
317- pip_req , pip_args = AssistantCLI ._parse_requirements (folder )
318- cmd += [f"pip install { pip_req } { pip_args } " , "pip list" ]
319320 if AssistantCLI .DRY_RUN :
320321 # dry run does not execute the notebooks just takes them as they are
321322 cmd .append (f"cp { ipynb_file } { pub_ipynb } " )
323+ # copy and add meta config
324+ cmd += [f"cp { meta_file } { pub_meta } " , f"cat { pub_meta } " , f"git add { pub_meta } " ]
322325 else :
326+ pip_req , pip_args = AssistantCLI ._parse_requirements (folder )
327+ cmd += [f"pip install { pip_req } --quiet { pip_args } " , "pip list" ]
323328 cmd .append (f"# available: { AssistantCLI .DEVICE_ACCELERATOR } \n " )
324329 if AssistantCLI ._valid_accelerator (folder ):
325330 cmd .append (f"python -m papermill { ipynb_file } { pub_ipynb } --kernel python" )
326331 else :
327332 warn ("Invalid notebook's accelerator for this device. So no outputs will be generated." , RuntimeWarning )
328333 cmd .append (f"cp { ipynb_file } { pub_ipynb } " )
329- # Export the actual packages used in runtime
330- cmd .append (f"meta_file=$(python .actions/assistant.py update-env-details { folder } )" )
331- # copy and add to version the enriched meta config
332- cmd += ["echo $meta_file" , "cat $meta_file" , "git add $meta_file" ]
334+ # Export the actual packages used in runtime
335+ cmd .append (f"meta_file=$(python .actions/assistant.py update-env-details { folder } )" )
336+ # copy and add to version the enriched meta config
337+ cmd += ["echo $meta_file" , "cat $meta_file" , "git add $meta_file" ]
333338 # if thumb image is linked to the notebook, copy and version it too
334339 if thumb_file :
335340 cmd += [f"cp { thumb_file } { pub_thumb } " , f"git add { pub_thumb } " ]
@@ -353,26 +358,36 @@ def bash_test(folder: str, output_file: str = PATH_SCRIPT_TEST) -> Optional[str]
353358 """
354359 cmd = list (AssistantCLI ._BASH_SCRIPT_BASE ) + [f"# Testing: { folder } " ]
355360 cmd += AssistantCLI ._bash_download_data (folder )
356- ipynb_file , _ , _ = AssistantCLI ._valid_folder (folder , ext = ".ipynb" )
361+ ipynb_file , meta_file , _ = AssistantCLI ._valid_folder (folder , ext = ".ipynb" )
357362
358363 # prepare isolated environment with inheriting the global packages
364+ path_venv = os .path .join (folder , "venv" )
359365 cmd += [
360- f"python -m virtualenv --system-site-packages { os . path . join ( folder , 'venv' ) } " ,
361- f"source { os .path .join (folder , 'venv' , 'bin' , 'activate' )} " ,
366+ f"python -m virtualenv --system-site-packages { path_venv } " ,
367+ f"source { os .path .join (path_venv , 'bin' , 'activate' )} " ,
362368 "pip --version" ,
363369 ]
364- # and install specific packages
365- pip_req , pip_args = AssistantCLI ._parse_requirements (folder )
366- cmd += [f"pip install { pip_req } { pip_args } " , "pip list" ]
367- # Export the actual packages used in runtime
368- cmd .append (f"meta_file=$(python .actions/assistant.py update-env-details { folder } --base_path .)" )
369- # show created meta config
370- cmd += ["echo $meta_file" , "cat $meta_file" ]
371370
372371 cmd .append (f"# available: { AssistantCLI .DEVICE_ACCELERATOR } " )
373372 if AssistantCLI ._valid_accelerator (folder ):
373+ # and install specific packages
374+ pip_req , pip_args = AssistantCLI ._parse_requirements (folder )
375+ cmd += [f"pip install { pip_req } --quiet { pip_args } " , "pip list" ]
376+ # Export the actual packages used in runtime
377+ cmd .append (f"meta_file=$(python .actions/assistant.py update-env-details { folder } --base_path .)" )
378+ # show created meta config
379+ cmd += ["echo $meta_file" , "cat $meta_file" ]
374380 cmd .append (f"python -m pytest { ipynb_file } -v --nbval --nbval-cell-timeout=300" )
375381 else :
382+ pub_ipynb = os .path .join (DIR_NOTEBOOKS , f"{ folder } .ipynb" )
383+ pub_meta = pub_ipynb .replace (".ipynb" , ".yaml" )
384+ # copy and add meta config
385+ cmd += [
386+ f"mkdir -p { os .path .dirname (pub_meta )} " ,
387+ f"cp { meta_file } { pub_meta } " ,
388+ f"cat { pub_meta } " ,
389+ f"git add { pub_meta } " ,
390+ ]
376391 warn ("Invalid notebook's accelerator for this device. So no tests will be run!!!" , RuntimeWarning )
377392 # deactivate and clean local environment
378393 cmd += ["deactivate" , f"rm -rf { os .path .join (folder , 'venv' )} " ]
0 commit comments