Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
@@ -62,17 +62,8 @@ def ping():
|
|
62 |
return {"message": "pong"}
|
63 |
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
return # already zipped
|
68 |
-
print(f"π¦ Zipping {src_dir} β {zip_path}")
|
69 |
-
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
|
70 |
-
for root, _, files in os.walk(src_dir):
|
71 |
-
for file in files:
|
72 |
-
full_path = os.path.join(root, file)
|
73 |
-
rel_path = os.path.relpath(full_path, src_dir)
|
74 |
-
zf.write(full_path, arcname=rel_path)
|
75 |
-
print(f"β
Created zip: {zip_path}")
|
76 |
|
77 |
def extract_zip_if_needed(zip_path, dest_dir):
|
78 |
if os.path.exists(dest_dir):
|
@@ -94,9 +85,9 @@ def print_directory_tree(path):
|
|
94 |
printstr += indent
|
95 |
printstr += f
|
96 |
return printstr
|
|
|
97 |
|
98 |
-
|
99 |
-
def copy_zip_extract_and_report():
|
100 |
src_base = "./hf_cache"
|
101 |
dst_base = "/data/hf_cache"
|
102 |
|
@@ -110,23 +101,27 @@ def copy_zip_extract_and_report():
|
|
110 |
os.makedirs(dst_dir, exist_ok=True)
|
111 |
|
112 |
for name in os.listdir(src_dir):
|
113 |
-
|
114 |
-
if not os.path.isdir(full_path):
|
115 |
continue
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
dst_zip = os.path.join(dst_dir, zip_name)
|
120 |
-
extract_path = os.path.join(dst_dir, name)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
# Copy zip to /data
|
125 |
if not os.path.exists(dst_zip):
|
126 |
-
shutil.copy(
|
127 |
print(f"π€ Copied zip to: {dst_zip}")
|
128 |
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
print("\nπ¦ Local hf_cache structure:")
|
132 |
printstr1 = print_directory_tree("./hf_cache")
|
@@ -139,10 +134,8 @@ def copy_zip_extract_and_report():
|
|
139 |
|
140 |
@app.get("/copy_and_extract")
|
141 |
def copy_and_extract():
|
142 |
-
import io
|
143 |
-
from contextlib import redirect_stdout
|
144 |
|
145 |
-
printstr =
|
146 |
|
147 |
return {"message": "done", "log": printstr}
|
148 |
|
|
|
62 |
return {"message": "pong"}
|
63 |
|
64 |
|
65 |
+
|
66 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
def extract_zip_if_needed(zip_path, dest_dir):
|
69 |
if os.path.exists(dest_dir):
|
|
|
85 |
printstr += indent
|
86 |
printstr += f
|
87 |
return printstr
|
88 |
+
|
89 |
|
90 |
+
def copy_extract_and_report():
|
|
|
91 |
src_base = "./hf_cache"
|
92 |
dst_base = "/data/hf_cache"
|
93 |
|
|
|
101 |
os.makedirs(dst_dir, exist_ok=True)
|
102 |
|
103 |
for name in os.listdir(src_dir):
|
104 |
+
if not name.endswith(".zip"):
|
|
|
105 |
continue
|
106 |
|
107 |
+
src_zip = os.path.join(src_dir, name)
|
108 |
+
dst_zip = os.path.join(dst_dir, name)
|
|
|
|
|
109 |
|
110 |
+
# Copy zip to /data if not already present
|
|
|
|
|
111 |
if not os.path.exists(dst_zip):
|
112 |
+
shutil.copy(src_zip, dst_zip)
|
113 |
print(f"π€ Copied zip to: {dst_zip}")
|
114 |
|
115 |
+
# Determine the extract folder name (strip ".zip" from the filename)
|
116 |
+
extract_folder = os.path.splitext(name)[0]
|
117 |
+
extract_path = os.path.join(dst_dir, extract_folder)
|
118 |
+
|
119 |
+
# Extract if not already extracted
|
120 |
+
if not os.path.exists(extract_path):
|
121 |
+
os.makedirs(extract_path, exist_ok=True)
|
122 |
+
with zipfile.ZipFile(dst_zip, 'r') as zip_ref:
|
123 |
+
zip_ref.extractall(extract_path)
|
124 |
+
print(f"π¦ Extracted zip to: {extract_path}")
|
125 |
|
126 |
print("\nπ¦ Local hf_cache structure:")
|
127 |
printstr1 = print_directory_tree("./hf_cache")
|
|
|
134 |
|
135 |
@app.get("/copy_and_extract")
|
136 |
def copy_and_extract():
|
|
|
|
|
137 |
|
138 |
+
printstr = copy_extract_and_report()
|
139 |
|
140 |
return {"message": "done", "log": printstr}
|
141 |
|