use token for discussions, ignore repos with discussions disabled.
Browse files- convert.py +19 -11
convert.py
CHANGED
|
@@ -16,6 +16,10 @@ from transformers.pipelines.base import infer_framework_load_model
|
|
| 16 |
from safetensors.torch import save_file
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def shared_pointers(tensors):
|
| 20 |
ptrs = defaultdict(list)
|
| 21 |
for k, v in tensors.items():
|
|
@@ -142,8 +146,12 @@ def check_final_model(model_id: str, folder: str):
|
|
| 142 |
torch.testing.assert_close(sf_logits, pt_logits)
|
| 143 |
print(f"Model {model_id} is ok !")
|
| 144 |
|
| 145 |
-
def previous_pr(model_id: str, pr_title: str) -> Optional["Discussion"]:
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:
|
| 148 |
return discussion
|
| 149 |
|
|
@@ -159,13 +167,13 @@ def convert(api: "HfApi", model_id: str, force: bool=False) -> Optional["CommitI
|
|
| 159 |
new_pr = None
|
| 160 |
try:
|
| 161 |
operations = None
|
| 162 |
-
pr = previous_pr(model_id, pr_title)
|
| 163 |
if ("model.safetensors" in filenames or "model_index.safetensors.index.json" in filenames) and not force:
|
| 164 |
-
raise
|
| 165 |
elif pr is not None and not force:
|
| 166 |
url = f"https://huggingface.co/{model_id}/discussions/{pr.num}"
|
| 167 |
new_pr = pr
|
| 168 |
-
raise
|
| 169 |
elif "pytorch_model.bin" in filenames:
|
| 170 |
operations = convert_single(model_id, folder)
|
| 171 |
elif "pytorch_model.bin.index.json" in filenames:
|
|
@@ -175,12 +183,12 @@ def convert(api: "HfApi", model_id: str, force: bool=False) -> Optional["CommitI
|
|
| 175 |
|
| 176 |
if operations:
|
| 177 |
check_final_model(model_id, folder)
|
| 178 |
-
new_pr = api.create_commit(
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
)
|
| 184 |
finally:
|
| 185 |
shutil.rmtree(folder)
|
| 186 |
return new_pr
|
|
|
|
| 16 |
from safetensors.torch import save_file
|
| 17 |
|
| 18 |
|
| 19 |
+
class AlreadyExists(Exception):
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def shared_pointers(tensors):
|
| 24 |
ptrs = defaultdict(list)
|
| 25 |
for k, v in tensors.items():
|
|
|
|
| 146 |
torch.testing.assert_close(sf_logits, pt_logits)
|
| 147 |
print(f"Model {model_id} is ok !")
|
| 148 |
|
| 149 |
+
def previous_pr(api: "HfApi", model_id: str, pr_title: str) -> Optional["Discussion"]:
|
| 150 |
+
try:
|
| 151 |
+
discussions = api.get_repo_discussions(repo_id=model_id)
|
| 152 |
+
except Exception:
|
| 153 |
+
return None
|
| 154 |
+
for discussion in discussions:
|
| 155 |
if discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:
|
| 156 |
return discussion
|
| 157 |
|
|
|
|
| 167 |
new_pr = None
|
| 168 |
try:
|
| 169 |
operations = None
|
| 170 |
+
pr = previous_pr(api, model_id, pr_title)
|
| 171 |
if ("model.safetensors" in filenames or "model_index.safetensors.index.json" in filenames) and not force:
|
| 172 |
+
raise AlreadyExists(f"Model {model_id} is already converted, skipping..")
|
| 173 |
elif pr is not None and not force:
|
| 174 |
url = f"https://huggingface.co/{model_id}/discussions/{pr.num}"
|
| 175 |
new_pr = pr
|
| 176 |
+
raise AlreadyExists(f"Model {model_id} already has an open PR check out {url}")
|
| 177 |
elif "pytorch_model.bin" in filenames:
|
| 178 |
operations = convert_single(model_id, folder)
|
| 179 |
elif "pytorch_model.bin.index.json" in filenames:
|
|
|
|
| 183 |
|
| 184 |
if operations:
|
| 185 |
check_final_model(model_id, folder)
|
| 186 |
+
# new_pr = api.create_commit(
|
| 187 |
+
# repo_id=model_id,
|
| 188 |
+
# operations=operations,
|
| 189 |
+
# commit_message=pr_title,
|
| 190 |
+
# create_pr=True,
|
| 191 |
+
# )
|
| 192 |
finally:
|
| 193 |
shutil.rmtree(folder)
|
| 194 |
return new_pr
|