Spaces:
Runtime error
Runtime error
Commit
•
c9feef4
1
Parent(s):
ef553b2
Update uploader.py
Browse files- uploader.py +15 -11
uploader.py
CHANGED
@@ -5,10 +5,7 @@ from huggingface_hub import HfApi
|
|
5 |
|
6 |
class Uploader:
|
7 |
def __init__(self, hf_token: str | None):
|
8 |
-
self.
|
9 |
-
|
10 |
-
def get_username(self) -> str:
|
11 |
-
return self.api.whoami()['name']
|
12 |
|
13 |
def upload(self,
|
14 |
folder_path: str,
|
@@ -16,13 +13,18 @@ class Uploader:
|
|
16 |
organization: str = '',
|
17 |
repo_type: str = 'model',
|
18 |
private: bool = True,
|
19 |
-
delete_existing_repo: bool = False
|
|
|
|
|
|
|
|
|
20 |
if not folder_path:
|
21 |
raise ValueError
|
22 |
if not repo_name:
|
23 |
raise ValueError
|
24 |
if not organization:
|
25 |
-
organization =
|
|
|
26 |
repo_id = f'{organization}/{repo_name}'
|
27 |
if delete_existing_repo:
|
28 |
try:
|
@@ -30,11 +32,13 @@ class Uploader:
|
|
30 |
except Exception:
|
31 |
pass
|
32 |
try:
|
33 |
-
self.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
url = f'https://huggingface.co/{repo_id}'
|
39 |
message = f'Your model was successfully uploaded to <a href="{url}" target="_blank">{url}</a>.'
|
40 |
except Exception as e:
|
|
|
5 |
|
6 |
class Uploader:
|
7 |
def __init__(self, hf_token: str | None):
|
8 |
+
self.hf_token = hf_token
|
|
|
|
|
|
|
9 |
|
10 |
def upload(self,
|
11 |
folder_path: str,
|
|
|
13 |
organization: str = '',
|
14 |
repo_type: str = 'model',
|
15 |
private: bool = True,
|
16 |
+
delete_existing_repo: bool = False,
|
17 |
+
input_token: str | None = False) -> str:
|
18 |
+
|
19 |
+
api = HfAPi(token=self.hf_token if self.hf_token else input_token)
|
20 |
+
|
21 |
if not folder_path:
|
22 |
raise ValueError
|
23 |
if not repo_name:
|
24 |
raise ValueError
|
25 |
if not organization:
|
26 |
+
organization = api.whoami()['name']
|
27 |
+
|
28 |
repo_id = f'{organization}/{repo_name}'
|
29 |
if delete_existing_repo:
|
30 |
try:
|
|
|
32 |
except Exception:
|
33 |
pass
|
34 |
try:
|
35 |
+
api = HfAPi(token=self.hf_token if self.hf_token else input_token)
|
36 |
+
|
37 |
+
api.create_repo(repo_id, repo_type=repo_type, private=private)
|
38 |
+
api.upload_folder(repo_id=repo_id,
|
39 |
+
folder_path=folder_path,
|
40 |
+
path_in_repo='.',
|
41 |
+
repo_type=repo_type)
|
42 |
url = f'https://huggingface.co/{repo_id}'
|
43 |
message = f'Your model was successfully uploaded to <a href="{url}" target="_blank">{url}</a>.'
|
44 |
except Exception as e:
|