File size: 1,585 Bytes
8b7a3d1
 
 
 
 
 
 
a3f0757
8b7a3d1
 
 
 
 
 
 
a3f0757
dd59441
 
07a02e5
5eb79d4
07a02e5
8b7a3d1
 
 
 
 
a3f0757
07a02e5
8b7a3d1
 
 
07a02e5
8b7a3d1
 
07a02e5
a3f0757
 
 
 
 
8b7a3d1
dd59441
 
 
8b7a3d1
 
 
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
from __future__ import annotations

from huggingface_hub import HfApi


class Uploader:
    def __init__(self, hf_token: str | None):
        self.hf_token = hf_token

    def upload(self,
               folder_path: str,
               repo_name: str,
               organization: str = '',
               repo_type: str = 'model',
               private: bool = True,
               delete_existing_repo: bool = False,
               input_hf_token: str | None = None,
               return_html_link: bool = True) -> str:

        api = HfApi(token=self.hf_token or input_hf_token)

        if not folder_path:
            raise ValueError
        if not repo_name:
            raise ValueError
        if not organization:
            organization = api.whoami()['name']

        repo_id = f'{organization}/{repo_name}'
        if delete_existing_repo:
            try:
                api.delete_repo(repo_id, repo_type=repo_type)
            except Exception:
                pass
        try:
            api.create_repo(repo_id, repo_type=repo_type, private=private)
            api.upload_folder(repo_id=repo_id,
                              folder_path=folder_path,
                              path_in_repo='.',
                              repo_type=repo_type)
            url = f'https://huggingface.co/{repo_id}'
            if return_html_link:
                url = f'<a href="{url}" target="_blank">{url}</a>'
            message = f'Your model was successfully uploaded to {url}.'
        except Exception as e:
            message = str(e)
        return message