Spaces:
Sleeping
Sleeping
from huggingface_hub import HfApi, create_repo | |
import os | |
class HFUploader: | |
def __init__(self): | |
self.hf_api = HfApi() | |
self.repo_id = os.getenv("HUGGINGFACE_REPO_ID") | |
self._ensure_repo_exists() | |
def _ensure_repo_exists(self): | |
try: | |
# Vérifie si le repo existe | |
self.hf_api.repo_info(repo_id=self.repo_id, repo_type="dataset") | |
print(f"Repository {self.repo_id} exists") | |
except Exception as e: | |
if "404" in str(e): # Repo n'existe pas | |
try: | |
create_repo( | |
self.repo_id, | |
private=False, | |
repo_type="dataset", | |
token=os.getenv("HUGGINGFACE_TOKEN") | |
) | |
print(f"Created repository {self.repo_id}") | |
except Exception as create_error: | |
if "You already created this dataset repo" not in str(create_error): | |
raise create_error | |
else: | |
raise e | |
def ensure_folder_structure(self, sport_id: str): | |
paths = [ | |
f"{sport_id}/raw", | |
f"{sport_id}/compressed", | |
f"{sport_id}/clips" | |
] | |
for path in paths: | |
try: | |
self.hf_api.upload_file( | |
path_or_fileobj="", | |
path_in_repo=f"{path}/.gitkeep", | |
repo_id=self.repo_id, | |
repo_type="dataset", | |
token=os.getenv("HUGGINGFACE_TOKEN") | |
) | |
except Exception: | |
pass | |
def upload_video(self, file_path: str, destination_path: str): | |
self.hf_api.upload_file( | |
path_or_fileobj=file_path, | |
path_in_repo=destination_path, | |
repo_id=self.repo_id, | |
repo_type="dataset", | |
token=os.getenv("HUGGINGFACE_TOKEN") | |
) | |
return f"https://huggingface.co/datasets/{self.repo_id}/raw/main/{destination_path}" |