Spaces:
Running
Running
Upload 2 files
Browse files- civitai_to_hf.py +20 -1
civitai_to_hf.py
CHANGED
|
@@ -13,6 +13,7 @@ from PIL import Image
|
|
| 13 |
import json
|
| 14 |
import pandas as pd
|
| 15 |
import tempfile
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
TEMP_DIR = tempfile.mkdtemp()
|
|
@@ -64,6 +65,19 @@ def upload_safetensors_to_repo(filename, repo_id, repo_type, is_private, progres
|
|
| 64 |
return url
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def get_safe_filename(filename, repo_id, repo_type):
|
| 68 |
hf_token = get_token()
|
| 69 |
api = HfApi(token=hf_token)
|
|
@@ -71,10 +85,15 @@ def get_safe_filename(filename, repo_id, repo_type):
|
|
| 71 |
try:
|
| 72 |
i = 1
|
| 73 |
while api.file_exists(repo_id=repo_id, filename=Path(new_filename).name, repo_type=repo_type, token=hf_token):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
new_filename = str(Path(Path(filename).parent, f"{Path(filename).stem}_{i}{Path(filename).suffix}"))
|
| 75 |
i += 1
|
| 76 |
if filename != new_filename:
|
| 77 |
-
print(f"{Path(filename).name} is already exists. renaming to {Path(new_filename).name}.")
|
| 78 |
Path(filename).rename(new_filename)
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error occured when renaming {filename}. {e}")
|
|
|
|
| 13 |
import json
|
| 14 |
import pandas as pd
|
| 15 |
import tempfile
|
| 16 |
+
import hashlib
|
| 17 |
|
| 18 |
|
| 19 |
TEMP_DIR = tempfile.mkdtemp()
|
|
|
|
| 65 |
return url
|
| 66 |
|
| 67 |
|
| 68 |
+
def is_same_file(filename: str, cmp_sha256: str, cmp_size: int):
|
| 69 |
+
if cmp_sha256:
|
| 70 |
+
sha256_hash = hashlib.sha256()
|
| 71 |
+
with open(filename, "rb") as f:
|
| 72 |
+
for byte_block in iter(lambda: f.read(4096), b""):
|
| 73 |
+
sha256_hash.update(byte_block)
|
| 74 |
+
sha256 = sha256_hash.hexdigest()
|
| 75 |
+
else: sha256 = ""
|
| 76 |
+
size = os.path.getsize(filename)
|
| 77 |
+
if size == cmp_size and sha256 == cmp_sha256: return True
|
| 78 |
+
else: return False
|
| 79 |
+
|
| 80 |
+
|
| 81 |
def get_safe_filename(filename, repo_id, repo_type):
|
| 82 |
hf_token = get_token()
|
| 83 |
api = HfApi(token=hf_token)
|
|
|
|
| 85 |
try:
|
| 86 |
i = 1
|
| 87 |
while api.file_exists(repo_id=repo_id, filename=Path(new_filename).name, repo_type=repo_type, token=hf_token):
|
| 88 |
+
infos = api.get_paths_info(repo_id=repo_id, paths=[Path(new_filename).name], repo_type=repo_type, token=hf_token)
|
| 89 |
+
if infos and len(infos) == 1:
|
| 90 |
+
repo_fs = infos[0].size
|
| 91 |
+
repo_sha256 = infos[0].lfs.sha256 if infos[0].lfs is not None else ""
|
| 92 |
+
if is_same_file(filename, repo_sha256, repo_fs): break
|
| 93 |
new_filename = str(Path(Path(filename).parent, f"{Path(filename).stem}_{i}{Path(filename).suffix}"))
|
| 94 |
i += 1
|
| 95 |
if filename != new_filename:
|
| 96 |
+
print(f"{Path(filename).name} is already exists but file content is different. renaming to {Path(new_filename).name}.")
|
| 97 |
Path(filename).rename(new_filename)
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error occured when renaming {filename}. {e}")
|