File size: 2,106 Bytes
16c970a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60191c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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}"