Commit
·
5702264
1
Parent(s):
13b1979
fix: big space
Browse files- competitions/runner.py +3 -2
- competitions/utils.py +17 -1
competitions/runner.py
CHANGED
|
@@ -6,6 +6,7 @@ import time
|
|
| 6 |
import uuid
|
| 7 |
import shutil
|
| 8 |
import os
|
|
|
|
| 9 |
from dataclasses import dataclass
|
| 10 |
from typing import List, Dict, Any
|
| 11 |
from collections import defaultdict
|
|
@@ -201,7 +202,7 @@ class JobRunner:
|
|
| 201 |
revision=client_commits[0].commit_id,
|
| 202 |
token=user_token,
|
| 203 |
local_dir=client_code_local_dir,
|
| 204 |
-
allow_patterns=["
|
| 205 |
)
|
| 206 |
with open(f"{client_code_local_dir}/README.md", "w", encoding="utf-8") as f:
|
| 207 |
f.write(self._create_readme(space_id))
|
|
@@ -212,7 +213,7 @@ class JobRunner:
|
|
| 212 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 213 |
dockerfile_content = f.read()
|
| 214 |
with open(filepath, "w", encoding="utf-8") as f:
|
| 215 |
-
f.write(dockerfile_modifier.modify_dockerfile_content(dockerfile_content)[0])
|
| 216 |
try:
|
| 217 |
api.upload_folder(
|
| 218 |
repo_id=space_id,
|
|
|
|
| 6 |
import uuid
|
| 7 |
import shutil
|
| 8 |
import os
|
| 9 |
+
import base64
|
| 10 |
from dataclasses import dataclass
|
| 11 |
from typing import List, Dict, Any
|
| 12 |
from collections import defaultdict
|
|
|
|
| 202 |
revision=client_commits[0].commit_id,
|
| 203 |
token=user_token,
|
| 204 |
local_dir=client_code_local_dir,
|
| 205 |
+
allow_patterns=["Dockerfile"],
|
| 206 |
)
|
| 207 |
with open(f"{client_code_local_dir}/README.md", "w", encoding="utf-8") as f:
|
| 208 |
f.write(self._create_readme(space_id))
|
|
|
|
| 213 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 214 |
dockerfile_content = f.read()
|
| 215 |
with open(filepath, "w", encoding="utf-8") as f:
|
| 216 |
+
f.write(dockerfile_modifier.modify_dockerfile_content(dockerfile_content, submission_repo, base64.b64encode(user_token.encode()).decode())[0])
|
| 217 |
try:
|
| 218 |
api.upload_folder(
|
| 219 |
repo_id=space_id,
|
competitions/utils.py
CHANGED
|
@@ -952,7 +952,7 @@ class DockerfileModifier:
|
|
| 952 |
# 如果解析失败,转换为 shell 格式
|
| 953 |
return f'{self.preload_prefix} {command}'
|
| 954 |
|
| 955 |
-
def modify_dockerfile_content(self, content: str) -> Tuple[str, List[str]]:
|
| 956 |
"""
|
| 957 |
修改 Dockerfile 内容
|
| 958 |
返回: (修改后的内容, 修改日志)
|
|
@@ -978,6 +978,8 @@ class DockerfileModifier:
|
|
| 978 |
changes.append(f" 原始: {line}")
|
| 979 |
changes.append(f" 修改: {new_line}")
|
| 980 |
modified_lines.append(new_line)
|
|
|
|
|
|
|
| 981 |
else:
|
| 982 |
modified_lines.append(line)
|
| 983 |
|
|
@@ -986,12 +988,26 @@ class DockerfileModifier:
|
|
| 986 |
if line.startswith("USER"):
|
| 987 |
last_user = line.split()[1].strip()
|
| 988 |
|
|
|
|
| 989 |
if last_user is None:
|
| 990 |
modified_lines.insert(-1, f"COPY {self.source_so_path}" + f" {self.tatget_so_path}")
|
| 991 |
else:
|
|
|
|
| 992 |
modified_lines.insert(-1, f"COPY --chown={last_user}:{last_user} {self.source_so_path} {self.tatget_so_path}")
|
| 993 |
modified_lines.insert(-1, f"RUN chown -R {last_user}:{last_user} {self.tatget_so_dir}")
|
| 994 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
return '\n'.join(modified_lines), changes
|
| 996 |
|
| 997 |
|
|
|
|
| 952 |
# 如果解析失败,转换为 shell 格式
|
| 953 |
return f'{self.preload_prefix} {command}'
|
| 954 |
|
| 955 |
+
def modify_dockerfile_content(self, content: str, user_repo: str, user_token: str) -> Tuple[str, List[str]]:
|
| 956 |
"""
|
| 957 |
修改 Dockerfile 内容
|
| 958 |
返回: (修改后的内容, 修改日志)
|
|
|
|
| 978 |
changes.append(f" 原始: {line}")
|
| 979 |
changes.append(f" 修改: {new_line}")
|
| 980 |
modified_lines.append(new_line)
|
| 981 |
+
elif instruction in ["COPY", "ADD"]:
|
| 982 |
+
pass
|
| 983 |
else:
|
| 984 |
modified_lines.append(line)
|
| 985 |
|
|
|
|
| 988 |
if line.startswith("USER"):
|
| 989 |
last_user = line.split()[1].strip()
|
| 990 |
|
| 991 |
+
modified_lines.insert(2, "COPY --from=builder /app /app")
|
| 992 |
if last_user is None:
|
| 993 |
modified_lines.insert(-1, f"COPY {self.source_so_path}" + f" {self.tatget_so_path}")
|
| 994 |
else:
|
| 995 |
+
modified_lines.insert(3, f"RUN chown -R {last_user}:{last_user} /app")
|
| 996 |
modified_lines.insert(-1, f"COPY --chown={last_user}:{last_user} {self.source_so_path} {self.tatget_so_path}")
|
| 997 |
modified_lines.insert(-1, f"RUN chown -R {last_user}:{last_user} {self.tatget_so_dir}")
|
| 998 |
|
| 999 |
+
|
| 1000 |
+
first_build_stage = [
|
| 1001 |
+
"FROM python:3.10-slim AS builder",
|
| 1002 |
+
"WORKDIR /app",
|
| 1003 |
+
f"ENV USER_HF_TOKEN='{user_token}'",
|
| 1004 |
+
"RUN pip install huggingface_hub",
|
| 1005 |
+
f"RUN python -c \"from huggingface_hub import snapshot_download; import os; import base64; snapshot_download('{user_repo}', repo_type='model', local_dir='.', token=base64.b64decode(os.getenv('USER_HF_TOKEN')).decode())\""
|
| 1006 |
+
]
|
| 1007 |
+
|
| 1008 |
+
first_build_stage.extend(modified_lines)
|
| 1009 |
+
modified_lines = first_build_stage
|
| 1010 |
+
|
| 1011 |
return '\n'.join(modified_lines), changes
|
| 1012 |
|
| 1013 |
|