yifan0sun commited on
Commit
11aee83
Β·
1 Parent(s): a2120da

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +82 -57
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
- def copy_extract_and_report():
91
- src_base = "./hf_cache"
92
- dst_base = "/data/hf_cache"
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
- 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")
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")