import os import uuid from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi) def hf_file(data,fpath,repo_name,token): tmp = uuid.uuid4() api = HfApi(token=token) if "/" in fpath: fdr = fpath.split("/",1)[0] if "/" in fpath.split("/",1)[1]: fdr2 = fpath.split("/",2)[1] if not os.path.exists(fdr2): os.makedirs(fdr2) if not os.path.exists(fdr): os.makedirs(fdr) with open(f"{tmp}", "w") as outfile: outfile.write(data) try: api.upload_file( path_or_fileobj=f"{tmp}", path_in_repo=fpath, repo_id=repo_name, token=token, repo_type="space", ) os.remove(f"{tmp}") mes = f"Data written to: {repo_name}/{fpath}" except Exception as e: mes = f"Error: {e}" pass return mes