Spaces:
Sleeping
Sleeping
Create weights_tools.py
Browse files- weights_tools.py +21 -0
weights_tools.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, zipfile, shutil
|
| 2 |
+
|
| 3 |
+
BASE = "models"
|
| 4 |
+
|
| 5 |
+
def ensure_dir(path: str):
|
| 6 |
+
os.makedirs(path, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
def save_and_unpack(files, target: str):
|
| 9 |
+
"""files: list of temp file paths from Gradio; target: rtlucassen/medivlad/segmentation"""
|
| 10 |
+
dst = os.path.join(BASE, target)
|
| 11 |
+
ensure_dir(dst)
|
| 12 |
+
saved = []
|
| 13 |
+
for f in files or []:
|
| 14 |
+
name = os.path.basename(f.name if hasattr(f, "name") else str(f))
|
| 15 |
+
dst_path = os.path.join(dst, name)
|
| 16 |
+
shutil.copyfile(f.name if hasattr(f, "name") else f, dst_path)
|
| 17 |
+
saved.append(dst_path)
|
| 18 |
+
if name.lower().endswith(".zip"):
|
| 19 |
+
with zipfile.ZipFile(dst_path, "r") as z:
|
| 20 |
+
z.extractall(dst)
|
| 21 |
+
return {"target": target, "saved": [os.path.basename(s) for s in saved], "dst": dst}
|