Clean readme update script (#2)
Browse files- Clean readme update script (5f3e4e7b665e4ba7d394fb988aa24ea52ae856e3)
Co-authored-by: Lucain Pouget <Wauplin@users.noreply.huggingface.co>
12345678910111213144 unmodified lines19202122232425262728293031323334353637383940414243444546474849import osfrom datetime import datetimefrom pathlib import Pathfrom shutil import rmtreeimport pytzfrom huggingface_hub import HfApi, RepositoryGENERATED_BELOW_MARKER = "--- Generated Part of README Below ---"hf_token = os.environ["HUGGINGFACE_AUTH_TOKEN"]local_repo_path = "./readme_repo"def update_dataset_readme(dataset_name: str, subreddit: str, new_rows: int) -> None:4 unmodified linesdataset_name (str): Name of the dataset repository.subreddit (str): Name of the subreddit being used for dataset creation.new_rows (int): Number of new rows added in the latest update.hf_token (str): Hugging Face authentication token.local_repo_path (str): Local path to clone the repository."""# Initialize HfApiapi = HfApi()if Path(local_repo_path).exists():rmtree(local_repo_path)# Clone the repository locallyrepo = Repository(local_repo_path, clone_from=dataset_name, repo_type='dataset', use_auth_token=hf_token)# Read the README filewith open(f"{local_repo_path}/README.md", "r") as file:old_readme = file.read()# Modify the READMEnew_readme = append_to_readme(subreddit=subreddit, new_rows=new_rows, old_readme=old_readme)# Write the updated README back to the repositorywith open(f"{local_repo_path}/README.md", "w") as file:file.write(new_readme)# Push the changesrepo.push_to_hub(blocking=True, commit_message=f'Pushing {new_rows} new rows')def append_to_readme(subreddit: str, new_rows: int, old_readme: str) -> str:123456789104 unmodified lines1516171819202122232425262728293031323334353637383940414243import osfrom datetime import datetimeimport pytzfrom huggingface_hub import HfApiGENERATED_BELOW_MARKER = "--- Generated Part of README Below ---"def update_dataset_readme(dataset_name: str, subreddit: str, new_rows: int) -> None:4 unmodified linesdataset_name (str): Name of the dataset repository.subreddit (str): Name of the subreddit being used for dataset creation.new_rows (int): Number of new rows added in the latest update."""# Initialize HfApiapi = HfApi()# Download README filereadme_path = api.hf_hub_download(repo_id=dataset_name, repo_type="dataset", filename="README.md", token=hf_token)# Read itwith open(readme_path, "r") as file:old_readme = file.read()# Modify itnew_readme = append_to_readme(subreddit=subreddit, new_rows=new_rows, old_readme=old_readme)# Commit modificationsapi.upload_file(path_or_fileobj=new_readme.encode(),path_in_repo="README.md",repo_id=dataset_name,repo_type="dataset",token=hf_token,commit_message=f'Pushing {new_rows} new rows')def append_to_readme(subreddit: str, new_rows: int, old_readme: str) -> str:
src/readme_update.py+16-22