Update Dockerfile
Browse files- Dockerfile +60 -6
Dockerfile
CHANGED
@@ -10,8 +10,13 @@ RUN apt-get update && \
|
|
10 |
RUN git clone https://github.com/ErlichLiu/DeepClaude.git /app
|
11 |
WORKDIR /app
|
12 |
|
13 |
-
#
|
14 |
-
RUN pip install --no-cache-dir fastapi uvicorn python-dotenv huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# 如果仓库中有requirements.txt,也安装其中的依赖
|
17 |
RUN if [ -f requirements.txt ]; then \
|
@@ -24,11 +29,60 @@ RUN mkdir -p /app/data && chmod -R 777 /app/data
|
|
24 |
# 创建.env文件
|
25 |
RUN touch .env
|
26 |
|
27 |
-
#
|
28 |
-
COPY sync_data.sh /app/sync_data.sh
|
29 |
-
RUN chmod +x /app/sync_data.sh
|
30 |
-
|
31 |
EXPOSE 7860
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# 使用备份脚本启动服务
|
34 |
CMD ["/app/sync_data.sh"]
|
|
|
10 |
RUN git clone https://github.com/ErlichLiu/DeepClaude.git /app
|
11 |
WORKDIR /app
|
12 |
|
13 |
+
# 安装必要的依赖
|
14 |
+
RUN pip install --no-cache-dir fastapi uvicorn python-dotenv huggingface_hub
|
15 |
+
|
16 |
+
# 自动检测可能需要的依赖(包括colorlog、tiktoken等)
|
17 |
+
RUN grep -r "import" --include="*.py" /app | grep -v "from app" | sort | uniq | \
|
18 |
+
grep -oP "(?<=import )[a-zA-Z0-9_]+|(?<=from )[a-zA-Z0-9_]+" | sort | uniq | \
|
19 |
+
xargs -I{} pip install --no-cache-dir {} || true
|
20 |
|
21 |
# 如果仓库中有requirements.txt,也安装其中的依赖
|
22 |
RUN if [ -f requirements.txt ]; then \
|
|
|
29 |
# 创建.env文件
|
30 |
RUN touch .env
|
31 |
|
32 |
+
# 暴露Hugging Face默认端口
|
|
|
|
|
|
|
33 |
EXPOSE 7860
|
34 |
|
35 |
+
# 创建备份脚本 sync_data.sh
|
36 |
+
RUN echo '#!/bin/bash\n\
|
37 |
+
if [ -z "$HF_TOKEN" ] || [ -z "$DATASET_ID" ]; then\n\
|
38 |
+
echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"\n\
|
39 |
+
exec uvicorn app.main:app --host 0.0.0.0 --port 7860\n\
|
40 |
+
exit 0\n\
|
41 |
+
fi\n\
|
42 |
+
export HUGGING_FACE_HUB_TOKEN=$HF_TOKEN\n\
|
43 |
+
restore_latest() {\n\
|
44 |
+
python3 -c "\n\
|
45 |
+
from huggingface_hub import HfApi\n\
|
46 |
+
import os\n\
|
47 |
+
api = HfApi()\n\
|
48 |
+
files = api.list_repo_files(repo_id='\''$DATASET_ID'\'', repo_type='\''dataset'\'')\n\
|
49 |
+
backup_files = sorted([f for f in files if f.startswith('\''backup_'\'') and f.endswith('\''.tar.gz'\'')])\n\
|
50 |
+
if backup_files:\n\
|
51 |
+
latest = backup_files[-1]\n\
|
52 |
+
api.hf_hub_download(repo_id='\''$DATASET_ID'\'', filename=latest, repo_type='\''dataset'\'', local_dir='\''/tmp'\'')\n\
|
53 |
+
os.system(f'\''tar -xzf /tmp/{latest} -C /app'\'')\n\
|
54 |
+
os.remove(f'\''/tmp/{latest}'\'')\n\
|
55 |
+
print(f'\''Restored from {latest}'\'')\n\
|
56 |
+
else:\n\
|
57 |
+
print('\''No backup found'\'')\n\
|
58 |
+
"\n\
|
59 |
+
}\n\
|
60 |
+
sync_data() {\n\
|
61 |
+
while true; do\n\
|
62 |
+
cd /app\n\
|
63 |
+
timestamp=$(date +%Y%m%d_%H%M%S)\n\
|
64 |
+
backup_file="backup_${timestamp}.tar.gz"\n\
|
65 |
+
if [ -d "data" ] && [ "$(ls -A data 2>/dev/null)" ]; then\n\
|
66 |
+
tar -czf "/tmp/${backup_file}" data/\n\
|
67 |
+
python3 -c "\n\
|
68 |
+
from huggingface_hub import HfApi\n\
|
69 |
+
api = HfApi()\n\
|
70 |
+
api.upload_file(path_or_fileobj='\''/tmp/${backup_file}'\'', path_in_repo='\''${backup_file}'\'', repo_id='\''${DATASET_ID}'\'', repo_type='\''dataset'\'')\n\
|
71 |
+
print('\''Backup uploaded successfully'\'')\n\
|
72 |
+
backup_files = sorted([f for f in api.list_repo_files('\''${DATASET_ID}'\'', repo_type='\''dataset'\'') if f.startswith('\''backup_'\'')])\n\
|
73 |
+
for old_backup in backup_files[:-50]:\n\
|
74 |
+
api.delete_file(path_in_repo=old_backup, repo_id='\''${DATASET_ID}'\'', repo_type='\''dataset'\'')\n\
|
75 |
+
print(f'\''Deleted old backup: {old_backup}'\'')\n\
|
76 |
+
"\n\
|
77 |
+
rm -f "/tmp/${backup_file}"\n\
|
78 |
+
else\n\
|
79 |
+
echo "No data to backup"\n\
|
80 |
+
fi\n\
|
81 |
+
sleep ${SYNC_INTERVAL:-7200}\n\
|
82 |
+
done\n\
|
83 |
+
}\n\
|
84 |
+
(restore_latest; sync_data &) \n\
|
85 |
+
exec uvicorn app.main:app --host 0.0.0.0 --port 7860\n' > /app/sync_data.sh && chmod +x /app/sync_data.sh
|
86 |
+
|
87 |
# 使用备份脚本启动服务
|
88 |
CMD ["/app/sync_data.sh"]
|