asemxin commited on
Commit
622794b
·
1 Parent(s): 5678c30

Fix: use config file for port setting instead of CLI args

Browse files
Files changed (3) hide show
  1. DEPLOY_GUIDE.md +69 -0
  2. Dockerfile +10 -3
  3. deploy_to_hf.py +58 -0
DEPLOY_GUIDE.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CLIProxyAPI 部署到 Hugging Face Spaces 说明
2
+
3
+ ## 已完成的准备工作
4
+
5
+ 已在 `c:\Users\asemx\.gemini\antigravity\playground\deep-crab` 目录下创建了以下文件:
6
+
7
+ 1. **Dockerfile** - 适配 HF Spaces 的 Docker 配置
8
+ - 使用 7860 端口(HF Spaces 要求)
9
+ - 设置非 root 用户运行
10
+ - 自动从 GitHub 克隆 CLIProxyAPI 源码并编译
11
+
12
+ 2. **README.md** - HF Spaces 元数据配置文件
13
+ - 设置 SDK 为 Docker
14
+ - 包含基本说明
15
+
16
+ ## 手动部署步骤
17
+
18
+ ### 步骤 1:创建 HF Space
19
+
20
+ 1. 访问 https://huggingface.co/new-space
21
+ 2. 登录您的 Hugging Face 账户
22
+ 3. 填写表单:
23
+ - **Space name**: `CLIProxyAPI`(或其他可用名称)
24
+ - **SDK**: 选择 `Docker`
25
+ - **Visibility**: 按需选择
26
+ 4. 点击 **Create Space**
27
+
28
+ ### 步骤 2:推送代码
29
+
30
+ 创建 Space 后,HF 会显示 git 仓库地址,格式类似:
31
+ ```
32
+ https://huggingface.co/spaces/YOUR_USERNAME/CLIProxyAPI
33
+ ```
34
+
35
+ 在 PowerShell 中执行以下命令:
36
+
37
+ ```powershell
38
+ cd c:\Users\asemx\.gemini\antigravity\playground\deep-crab
39
+
40
+ # 添加 HF Spaces 远程仓库(替换 YOUR_USERNAME)
41
+ git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/CLIProxyAPI
42
+
43
+ # 推送代码
44
+ git push -u origin master
45
+ ```
46
+
47
+ 如果提示输入凭据,使用:
48
+ - 用户名:您的 HF 用户名
49
+ - 密码:您的 HF Access Token(在 https://huggingface.co/settings/tokens 生成)
50
+
51
+ ### 步骤 3:等待构建
52
+
53
+ 推送后,HF Spaces 会自动开始构建 Docker 镜像。您可以在 Space 页面查看构建日志。
54
+
55
+ ### 步骤 4:配置环境变量(可选)
56
+
57
+ 在 Space 的 Settings 页面,您可以添加以下环境变量:
58
+ - `API_KEYS`: 逗号分隔的 API 密钥列表
59
+
60
+ ## 验证部署
61
+
62
+ 构建完成后,访问您的 Space URL:
63
+ ```
64
+ https://YOUR_USERNAME-cliproxyapi.hf.space
65
+ ```
66
+
67
+ API 端点示例:
68
+ - Health check: `GET /`
69
+ - OpenAI 兼容: `POST /v1/chat/completions`
Dockerfile CHANGED
@@ -30,16 +30,23 @@ COPY --from=builder /app/config.example.yaml /CLIProxyAPI/config.example.yaml
30
 
31
  WORKDIR /CLIProxyAPI
32
 
 
 
 
 
 
 
 
 
33
  # HF Spaces requires port 7860
34
  EXPOSE 7860
35
 
36
  ENV TZ=Asia/Shanghai
37
- ENV PORT=7860
38
 
39
  RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime && echo "${TZ}" > /etc/timezone
40
 
41
  # Switch to non-root user
42
  USER user
43
 
44
- # Start with port 7860 for HF Spaces
45
- CMD ["./CLIProxyAPI", "--port", "7860", "--host", "0.0.0.0"]
 
30
 
31
  WORKDIR /CLIProxyAPI
32
 
33
+ # Create config file with port 7860 for HF Spaces
34
+ RUN echo 'host: "0.0.0.0"' > /CLIProxyAPI/config.yaml && \
35
+ echo 'port: 7860' >> /CLIProxyAPI/config.yaml && \
36
+ echo 'debug: false' >> /CLIProxyAPI/config.yaml && \
37
+ echo 'api-keys:' >> /CLIProxyAPI/config.yaml && \
38
+ echo ' - "default-key"' >> /CLIProxyAPI/config.yaml && \
39
+ chown user:user /CLIProxyAPI/config.yaml
40
+
41
  # HF Spaces requires port 7860
42
  EXPOSE 7860
43
 
44
  ENV TZ=Asia/Shanghai
 
45
 
46
  RUN cp /usr/share/zoneinfo/${TZ} /etc/localtime && echo "${TZ}" > /etc/timezone
47
 
48
  # Switch to non-root user
49
  USER user
50
 
51
+ # Start with config file
52
+ CMD ["./CLIProxyAPI", "-config", "/CLIProxyAPI/config.yaml"]
deploy_to_hf.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Deploy CLIProxyAPI to Hugging Face Spaces"""
3
+
4
+ from huggingface_hub import HfApi, create_repo, upload_folder, login
5
+ import os
6
+
7
+ # Login with token
8
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
9
+ if HF_TOKEN:
10
+ login(token=HF_TOKEN)
11
+
12
+ # Initialize API
13
+ api = HfApi(token=HF_TOKEN if HF_TOKEN else None)
14
+
15
+ # Get current user info
16
+ try:
17
+ user_info = api.whoami()
18
+ username = user_info["name"]
19
+ print(f"Logged in as: {username}")
20
+ except Exception as e:
21
+ print(f"Error: Not logged in to Hugging Face. Please run: huggingface-cli login")
22
+ print(f"Details: {e}")
23
+ exit(1)
24
+
25
+ # Create the Space
26
+ repo_id = f"{username}/CLIProxyAPI"
27
+ print(f"Creating Space: {repo_id}")
28
+
29
+ try:
30
+ create_repo(
31
+ repo_id=repo_id,
32
+ repo_type="space",
33
+ space_sdk="docker",
34
+ exist_ok=True,
35
+ private=False
36
+ )
37
+ print(f"Space created/exists: https://huggingface.co/spaces/{repo_id}")
38
+ except Exception as e:
39
+ print(f"Error creating Space: {e}")
40
+ exit(1)
41
+
42
+ # Upload files
43
+ print("Uploading files...")
44
+ current_dir = os.path.dirname(os.path.abspath(__file__))
45
+
46
+ try:
47
+ api.upload_folder(
48
+ folder_path=current_dir,
49
+ repo_id=repo_id,
50
+ repo_type="space",
51
+ ignore_patterns=["*.py", ".git/*", "DEPLOY_GUIDE.md"]
52
+ )
53
+ print(f"Files uploaded successfully!")
54
+ print(f"\nYour Space URL: https://huggingface.co/spaces/{repo_id}")
55
+ print(f"Direct access: https://{username}-cliproxyapi.hf.space")
56
+ except Exception as e:
57
+ print(f"Error uploading files: {e}")
58
+ exit(1)