Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from pydantic import BaseModel
|
|
|
3 |
|
4 |
import torch
|
5 |
|
@@ -63,72 +64,36 @@ def ping():
|
|
63 |
|
64 |
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
def extract_zip_if_needed(zip_path, dest_dir):
|
69 |
-
if os.path.exists(dest_dir):
|
70 |
-
print(f"β
Already exists: {dest_dir}")
|
71 |
-
return
|
72 |
-
print(f"π Extracting {zip_path} β {dest_dir}")
|
73 |
-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
74 |
-
zip_ref.extractall(dest_dir)
|
75 |
-
print(f"β
Extracted to: {dest_dir}")
|
76 |
-
|
77 |
-
def print_directory_tree(path):
|
78 |
-
printstr = ''
|
79 |
-
for root, dirs, files in os.walk(path):
|
80 |
-
indent = ' ' * (root.count(os.sep) - path.count(os.sep))
|
81 |
-
printstr += indent
|
82 |
-
printstr += os.path.basename(root)
|
83 |
-
printstr += '\n'
|
84 |
-
for f in files:
|
85 |
-
printstr += indent
|
86 |
-
printstr += f
|
87 |
-
return printstr
|
88 |
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
for category in ["models", "tokenizers"]:
|
95 |
-
src_dir = os.path.join(src_base, category)
|
96 |
-
dst_dir = os.path.join(dst_base, category)
|
97 |
-
|
98 |
-
if not os.path.exists(src_dir):
|
99 |
-
continue
|
100 |
|
101 |
-
|
|
|
102 |
|
103 |
-
|
104 |
-
if not name.endswith(".zip"):
|
105 |
-
continue
|
106 |
|
107 |
-
|
108 |
-
|
|
|
|
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
shutil.copy(src_zip, dst_zip)
|
113 |
-
print(f"π€ Copied zip to: {dst_zip}")
|
114 |
|
115 |
-
#
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
print(f"π¦ Extracted zip to: {extract_path}")
|
125 |
-
|
126 |
-
print("\nπ¦ Local hf_cache structure:")
|
127 |
-
printstr1 = print_directory_tree("./hf_cache")
|
128 |
|
129 |
-
print("\nπΎ Persistent /data/hf_cache structure:")
|
130 |
-
printstr2 = print_directory_tree("/data/hf_cache")
|
131 |
-
return printstr1 + '\n\n' + printstr2
|
132 |
|
133 |
|
134 |
|
@@ -174,6 +139,66 @@ def list_data():
|
|
174 |
|
175 |
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
|
179 |
@app.post("/load_model")
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from pydantic import BaseModel
|
3 |
+
from pathlib import Path
|
4 |
|
5 |
import torch
|
6 |
|
|
|
64 |
|
65 |
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
+
@app.post("/migrate_cache")
|
70 |
+
def migrate_hf_cache():
|
71 |
+
src_root = Path("/hf_cache")
|
72 |
+
dst_root = Path("/data/hf_cache")
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
if not src_root.exists():
|
75 |
+
return {"status": "error", "message": "Source directory does not exist"}
|
76 |
|
77 |
+
migrated_files = []
|
|
|
|
|
78 |
|
79 |
+
for src_path in src_root.rglob("*"):
|
80 |
+
if src_path.is_file():
|
81 |
+
relative_path = src_path.relative_to(src_root)
|
82 |
+
dst_path = dst_root / relative_path
|
83 |
|
84 |
+
# Create destination directory if needed
|
85 |
+
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
86 |
|
87 |
+
# Copy file
|
88 |
+
shutil.copy2(src_path, dst_path)
|
89 |
+
migrated_files.append(str(relative_path))
|
90 |
|
91 |
+
return {
|
92 |
+
"status": "done",
|
93 |
+
"files_migrated": migrated_files,
|
94 |
+
"total": len(migrated_files)
|
95 |
+
}
|
|
|
|
|
|
|
|
|
96 |
|
|
|
|
|
|
|
97 |
|
98 |
|
99 |
|
|
|
139 |
|
140 |
|
141 |
|
142 |
+
|
143 |
+
"""
|
144 |
+
|
145 |
+
@app.post("/purge_data")
|
146 |
+
def purge_data():
|
147 |
+
base_path = Path("/data")
|
148 |
+
if not base_path.exists():
|
149 |
+
return {"status": "error", "message": "/data does not exist"}
|
150 |
+
|
151 |
+
deleted = []
|
152 |
+
|
153 |
+
for child in base_path.iterdir():
|
154 |
+
try:
|
155 |
+
if child.is_file() or child.is_symlink():
|
156 |
+
child.unlink()
|
157 |
+
elif child.is_dir():
|
158 |
+
shutil.rmtree(child)
|
159 |
+
deleted.append(str(child.name))
|
160 |
+
except Exception as e:
|
161 |
+
deleted.append(f"FAILED: {child.name} ({e})")
|
162 |
+
|
163 |
+
return {
|
164 |
+
"status": "done",
|
165 |
+
"deleted": deleted,
|
166 |
+
"total": len(deleted)
|
167 |
+
}
|
168 |
+
"""
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
##############################################################
|
202 |
|
203 |
|
204 |
@app.post("/load_model")
|