Skip to content

Commit

Permalink
#62 Add GitHub folder downloader (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Only-bottle authored Dec 20, 2023
1 parent b06d616 commit 0362ccf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ pip install -e .

## Quick Start

### Download config folder from netspresso-trainer

If you want to train the trainer as a yaml file, download the config folder and use it.

```bash
python tools/github_download.py --repo Nota-NetsPresso/netspresso-trainer --path config
```


### Login

To use the PyNetsPresso, please enter the email and password registered in NetsPresso.
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ requests==2.30.0
email-validator==2.0.0
pytz==2023.3
typing_extensions==4.5.0
torch>=1.13.0
torchvision>=0.14.0
netspresso_trainer==0.0.10
PyGithub>=2.1.1
57 changes: 57 additions & 0 deletions tools/github_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from argparse import ArgumentParser
from pathlib import Path

import requests
from github import ContentFile, Github, Repository
from loguru import logger


class GithubDownloader:
def __init__(self, repo: Repository) -> None:
self.github_client = Github()
self.repo = self.github_client.get_repo(repo)

def download(self, content: ContentFile, out: str):
r = requests.get(content.download_url)
output_path = Path(out) / content.path
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "wb") as f:
logger.info(f"Downloading {content.path} to {output_path}")
f.write(r.content)

def download_folder(self, folder: str, out: str, recursive: bool = True):
contents = self.repo.get_contents(folder)
for content in contents:
if content.download_url is None:
if recursive:
self.download_folder(content.path, out, recursive)
continue
self.download(content, out)


def get_args():
parser = ArgumentParser()
parser.add_argument("--repo", help="The repo where the file or folder is stored")
parser.add_argument("--path", help="The folder or file you want to download")
parser.add_argument(
"-o",
"--out",
default="./",
required=False,
help="Path to folder you want to download "
"to. Default is current folder + "
"'downloads'",
)
parser.add_argument(
"-f",
"--file",
action="store_true",
help="Set flag to download a single file, instead of a " "folder.",
)
return parser.parse_args()


if __name__ == "__main__":
args = get_args()
github_downloader = GithubDownloader(repo=args.repo)
github_downloader.download_folder(args.path, args.out)

0 comments on commit 0362ccf

Please sign in to comment.