hern0425 commited on
Commit
8666ff1
·
verified ·
1 Parent(s): d5d77b6

Upload 13 files

Browse files
.dockerignore ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HuggingFace Spaces .dockerignore
2
+ # Exclude files not needed in the final container image
3
+
4
+ # Git
5
+ .git
6
+ .gitignore
7
+ .gitattributes
8
+
9
+ # Documentation (keep README.md)
10
+ *.md
11
+ !README.md
12
+
13
+ # Environment files (keep .env)
14
+ .env.*
15
+ !.env
16
+
17
+ # Python cache and build files
18
+ __pycache__/
19
+ *.py[cod]
20
+ *$py.class
21
+ *.so
22
+ .Python
23
+ build/
24
+ develop-eggs/
25
+ dist/
26
+ downloads/
27
+ eggs/
28
+ .eggs/
29
+ lib/
30
+ lib64/
31
+ parts/
32
+ sdist/
33
+ var/
34
+ wheels/
35
+ *.egg-info/
36
+ .installed.cfg
37
+ *.egg
38
+
39
+ # Virtual environments
40
+ venv/
41
+ env/
42
+ ENV/
43
+ .venv/
44
+
45
+ # IDE and editor files
46
+ .vscode/
47
+ .idea/
48
+ *.swp
49
+ *.swo
50
+ *~
51
+
52
+ # OS generated files
53
+ .DS_Store
54
+ .DS_Store?
55
+ ._*
56
+ .Spotlight-V100
57
+ .Trashes
58
+ ehthumbs.db
59
+ Thumbs.db
60
+
61
+ # Logs and temporary files
62
+ *.log
63
+ logs/
64
+ tmp/
65
+ temp/
66
+ *.tmp
67
+
68
+ # Runtime data
69
+ pids
70
+ *.pid
71
+ *.seed
72
+ *.pid.lock
73
+
74
+ # Test files and coverage
75
+ tests/
76
+ test_*.py
77
+ *_test.py
78
+ .coverage
79
+ .pytest_cache/
80
+ coverage/
81
+
82
+ # Development tools
83
+ pytest.ini
84
+ tox.ini
85
+ .mypy_cache/
86
+
87
+ # CI/CD files
88
+ .github/
89
+ .gitlab-ci.yml
90
+ .travis.yml
91
+
92
+ # Local configuration overrides
93
+ .env.local
94
+ .env.development
95
+ .env.test
96
+
97
+ # Backup files
98
+ *.bak
99
+ *.backup
100
+
101
+ # Docker specific (Dockerfile will be included)
102
+ docker-compose*.yml
103
+ docker-compose.override.yml
.env ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HuggingFace Spaces Environment Configuration
2
+ # These variables will be available in the running container
3
+
4
+ # Core API Configuration
5
+ FLOW2API_API_KEY=your-api-key-here
6
+ FLOW2API_ADMIN_USERNAME=admin
7
+ FLOW2API_ADMIN_PASSWORD=change-this-password
8
+
9
+ # Server Configuration (for HuggingFace Spaces)
10
+ FLOW2API_HOST=0.0.0.0
11
+ FLOW2API_PORT=7860
12
+
13
+ # Debug Configuration (Important for HuggingFace Spaces)
14
+ FLOW2API_DEBUG_ENABLED=false
15
+ FLOW2API_DEBUG_LOG_REQUESTS=false
16
+ FLOW2API_DEBUG_LOG_RESPONSES=false
17
+ FLOW2API_DEBUG_MASK_TOKEN=true
18
+
19
+ # Note: Set debug logging to false to avoid log file permission issues
20
+ # in HuggingFace Spaces environment
21
+
22
+ # Flow API Configuration
23
+ FLOW2API_LABS_BASE_URL=https://labs.google/fx/api
24
+ FLOW2API_API_BASE_URL=https://aisandbox-pa.googleapis.com/v1
25
+ FLOW2API_TIMEOUT=120
26
+ FLOW2API_MAX_RETRIES=3
27
+ FLOW2API_POLL_INTERVAL=3.0
28
+ FLOW2API_MAX_POLL_ATTEMPTS=200
29
+
30
+ # Generation Configuration
31
+ FLOW2API_IMAGE_TIMEOUT=300
32
+ FLOW2API_VIDEO_TIMEOUT=1500
33
+
34
+ # Cache Configuration
35
+ FLOW2API_CACHE_ENABLED=false
36
+ FLOW2API_CACHE_TIMEOUT=7200
37
+ FLOW2API_CACHE_BASE_URL=
38
+
39
+ # Proxy Configuration (if needed)
40
+ FLOW2API_PROXY_ENABLED=false
41
+ FLOW2API_PROXY_URL=
42
+
43
+ # HuggingFace Spaces Specific
44
+ GRADIO_SERVER_NAME=0.0.0.0
45
+ GRADIO_SERVER_PORT=7860
.gitattributes CHANGED
@@ -1,35 +1,35 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
DEPLOYMENT_GUIDE.md ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Flow2API HuggingFace Spaces 部署指南
2
+
3
+ ## 📋 部署清单
4
+
5
+ 在部署到 HuggingFace Spaces 之前,请确认以下项目:
6
+
7
+ ### ✅ 必需配置
8
+ - [ ] Google VideoFX API 密钥
9
+ - [ ] 自定义管理员用户名和密码
10
+ - [ ] 代理配置(如需要)
11
+ - [ ] 超时和重试设置
12
+
13
+ ### ✅ 文件结构
14
+ 确认以下文件已正确配置:
15
+ ```
16
+ zzz/
17
+ ├── Dockerfile # ✅ 已创建
18
+ ├── requirements.txt # ✅ 已创建
19
+ ├── .env # ✅ 已创建
20
+ ├── README.md # ✅ 已优化
21
+ ├── config/
22
+ │ └── setting.toml # ✅ 已创建
23
+ └── DEPLOYMENT_GUIDE.md # ✅ 本文件
24
+ ```
25
+
26
+ ## 🚀 部署步骤
27
+
28
+ ### 1. 准备 HuggingFace Space
29
+
30
+ 1. **创建新 Space**:
31
+ - 访问 [huggingface.co/spaces](https://huggingface.co/spaces)
32
+ - 点击 "Create new Space"
33
+ - 选择 **Docker** 作为 SDK
34
+ - 设置 Space 名称(例如:flow2api-video)
35
+ - 选择 **Public** 或 **Private**(Private 需要订阅)
36
+
37
+ 2. **上传代码**:
38
+ ```bash
39
+ # 方法1:使用 Git 克隆和推送
40
+ git clone https://huggingface.co/spaces/your-username/flow2api-video
41
+ cd flow2api-video
42
+ cp -r /path/to/zzz/* .
43
+ git add .
44
+ git commit -m "Initial Flow2API deployment"
45
+ git push
46
+
47
+ # 方法2:直接上传文件(通过网页界面)
48
+ # 上传所有文件到 Space
49
+ ```
50
+
51
+ ### 2. 配置环境变量
52
+
53
+ 在 HuggingFace Space 设置中配置以下环境变量:
54
+
55
+ #### 🔑 核心配置(必须设置)
56
+ ```bash
57
+ FLOW2API_API_KEY=your-google-videofx-api-key
58
+ FLOW2API_ADMIN_USERNAME=your-admin-username
59
+ FLOW2API_ADMIN_PASSWORD=your-secure-password-change-this
60
+ ```
61
+
62
+ #### ⚙️ 服务器配置
63
+ ```bash
64
+ FLOW2API_HOST=0.0.0.0
65
+ FLOW2API_PORT=7860
66
+ ```
67
+
68
+ #### 🐛 调试配置(可选)
69
+ ```bash
70
+ FLOW2API_DEBUG_ENABLED=false
71
+ FLOW2API_DEBUG_LOG_REQUESTS=true
72
+ FLOW2API_DEBUG_LOG_RESPONSES=true
73
+ FLOW2API_DEBUG_MASK_TOKEN=true
74
+ ```
75
+
76
+ #### ⏱️ 性能配置(可选)
77
+ ```bash
78
+ FLOW2API_IMAGE_TIMEOUT=600
79
+ FLOW2API_VIDEO_TIMEOUT=1800
80
+ FLOW2API_TIMEOUT=180
81
+ FLOW2API_MAX_RETRIES=5
82
+ ```
83
+
84
+ #### 🌐 代理配置(如果需要)
85
+ ```bash
86
+ FLOW2API_PROXY_ENABLED=true
87
+ FLOW2API_PROXY_URL=http://your-proxy.com:8080
88
+ ```
89
+
90
+ #### 💾 缓存配置(可选)
91
+ ```bash
92
+ FLOW2API_CACHE_ENABLED=true
93
+ FLOW2API_CACHE_TIMEOUT=14400
94
+ FLOW2API_CACHE_BASE_URL=https://your-cdn.com
95
+ ```
96
+
97
+ ### 3. 启动和测试
98
+
99
+ 1. **启动 Space**:
100
+ - 代码上传后会自动开始构建
101
+ - 等待构建完成(通常需要 2-5 分钟)
102
+ - 构建成功后会自动启动
103
+
104
+ 2. **测试访问**:
105
+ ```bash
106
+ # 测试基础连接
107
+ curl https://your-space.hf.space/
108
+
109
+ # 测试健康检查
110
+ curl https://your-space.hf.space/health
111
+
112
+ # 测试 API 端点
113
+ curl -X POST "https://your-space.hf.space/v1/chat/completions" \
114
+ -H "Authorization: Bearer your-api-key" \
115
+ -H "Content-Type: application/json" \
116
+ -d '{"model": "gemini-2.5-flash-image-landscape", "messages": [{"role": "user", "content": "test"}], "stream": false}'
117
+ ```
118
+
119
+ 3. **访问管理界面**:
120
+ - 打开:`https://your-space.hf.space`
121
+ - 使用配置的管理员凭据登录
122
+ - 添加你的 Google VideoFX API 密钥
123
+
124
+ ## 🔧 配置管理
125
+
126
+ ### 通过环境变量修改配置
127
+
128
+ ```bash
129
+ # 示例:启用缓存
130
+ FLOW2API_CACHE_ENABLED=true
131
+ FLOW2API_CACHE_TIMEOUT=7200
132
+
133
+ # 示例:调整超时设置
134
+ FLOW2API_IMAGE_TIMEOUT=450
135
+ FLOW2API_VIDEO_TIMEOUT=1200
136
+
137
+ # 示例:启用调试模式
138
+ FLOW2API_DEBUG_ENABLED=true
139
+ ```
140
+
141
+ ### 通过管理界面修改配置
142
+
143
+ 1. 登录管理界面
144
+ 2. 进入 **Configuration** 页面
145
+ 3. 修改设置
146
+ 4. 点击 **Save Changes**
147
+
148
+ ## 📊 监控和日志
149
+
150
+ ### 查看日志
151
+
152
+ 在 HuggingFace Spaces 界面:
153
+ 1. 进入你的 Space
154
+ 2. 点击 **Files and versions**
155
+ 3. 点击 **Logs** 标签
156
+ 4. 查看实时日志
157
+
158
+ ### 健康检查
159
+
160
+ ```bash
161
+ # 定期检查服务状态
162
+ curl -f https://your-space.hf.space/ || echo "Service down"
163
+
164
+ # 检查响应时间
165
+ time curl https://your-space.hf.space/
166
+ ```
167
+
168
+ ## 🛠️ 故障排除
169
+
170
+ ### 常见问题
171
+
172
+ 1. **构建失败**
173
+ ```bash
174
+ # 检查 Dockerfile 语法
175
+ docker build -t test .
176
+
177
+ # 查看 HuggingFace Spaces 构建日志
178
+ ```
179
+
180
+ 2. **服务无法启动**
181
+ ```bash
182
+ # 检查环境变量配置
183
+ echo $FLOW2API_HOST
184
+ echo $FLOW2API_PORT
185
+
186
+ # 查看容器启动日志
187
+ docker logs container-id
188
+ ```
189
+
190
+ 3. **API 调用失败**
191
+ ```bash
192
+ # 检查 API 密钥配置
193
+ curl -H "Authorization: Bearer your-key" https://your-space.hf.space/v1/models
194
+
195
+ # 检查管理界面中的 API 密钥设置
196
+ ```
197
+
198
+ 4. **权限错误**
199
+ ```bash
200
+ # 确认文件权限设置
201
+ ls -la /app/data
202
+ ls -la /app/config
203
+ ```
204
+
205
+ ### 性能优化
206
+
207
+ 1. **调整超时设置**:
208
+ ```bash
209
+ FLOW2API_POLL_INTERVAL=5.0
210
+ FLOW2API_MAX_POLL_ATTEMPTS=300
211
+ ```
212
+
213
+ 2. **启用缓存**:
214
+ ```bash
215
+ FLOW2API_CACHE_ENABLED=true
216
+ FLOW2API_CACHE_TIMEOUT=3600
217
+ ```
218
+
219
+ 3. **调整并发设置**:
220
+ 在管理界面的 **Performance** 部分调整并发限制
221
+
222
+ ## 🔒 安全最佳实践
223
+
224
+ 1. **立即更改默认密码**:
225
+ - 管理员用户名和密码
226
+ - API 密钥
227
+
228
+ 2. **使用 Private Space**(如果包含敏感信息)
229
+
230
+ 3. **定期更新**:
231
+ - 监控基础镜像更新
232
+ - 定期检查安全漏洞
233
+
234
+ 4. **监控使用情况**:
235
+ - 检查 API 调用日志
236
+ - 监控资源使用情况
237
+
238
+ ## 📈 扩展部署
239
+
240
+ ### 多区域部署
241
+
242
+ ```bash
243
+ # 为不同地区创建多个 Space
244
+ # 美国:https://us-flow2api.hf.space
245
+ # 欧洲:https://eu-flow2api.hf.space
246
+ # 亚洲:https://asia-flow2api.hf.space
247
+ ```
248
+
249
+ ### 负载均衡
250
+
251
+ 使用 HuggingFace Spaces 的内置负载均衡:
252
+ 1. 创建多个相同的 Space
253
+ 2. 使用 CDN 或负载均衡器分发请求
254
+ 3. 监控各个 Space 的健康状态
255
+
256
+ ### 自定义域名
257
+
258
+ ```bash
259
+ # 在 Space 设置中添加自定义域名
260
+ FLOW2API_HOST=your-custom-domain.com
261
+ ```
262
+
263
+ ## 📞 支持
264
+
265
+ 如果遇到问题:
266
+
267
+ 1. **查看官方文档**:[HuggingFace Spaces Docs](https://huggingface.co/docs/hub/spaces)
268
+ 2. **检查项目 Issues**:[GitHub Issues](https://github.com/thesmallhancat/gdtiti_flow2api/issues)
269
+ 3. **社区支持**:[GitHub Discussions](https://github.com/thesmallhancat/gdtiti_flow2api/discussions)
270
+
271
+ ## 🔄 更新和维护
272
+
273
+ ### 更新到最新版本
274
+
275
+ ```bash
276
+ # 更新基础镜像引用(如果有新版本)
277
+ FROM ghcr.io/gdtiti/flow2api:latest
278
+
279
+ # 重新部署
280
+ git add Dockerfile
281
+ git commit -m "Update to latest Flow2API version"
282
+ git push
283
+ ```
284
+
285
+ ### 备份配置
286
+
287
+ ```bash
288
+ # 导出当前配置
289
+ curl https://your-space.hf.space/api/config > config-backup.json
290
+
291
+ # 定期备份重要的配置文件
292
+ ```
293
+
294
+ ---
295
+
296
+ **部署成功后,你的 Flow2API 服务将在 HuggingFace Spaces 上运行,提供强大的图像和视频生成能力!** 🎉
DOCKERFILE_FIX.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile 修复说明
2
+
3
+ ## 🐛 问题描述
4
+
5
+ 在 HuggingFace Spaces 构建过程中遇到错误:
6
+
7
+ ```
8
+ ERROR: failed to calculate checksum of ref y3ta9lvs57rkhtkwy85l33nw8::p4k2tqiytyjk27fh7j54w4smm: "/||": not found
9
+ ```
10
+
11
+ 这个错误是由于在 `COPY` 指令中使用了不支持的语法造成的。
12
+
13
+ ## 🔧 修复过程
14
+
15
+ ### 第一次尝试的问题
16
+ ```dockerfile
17
+ COPY --chown=app:app config/setting.toml /app/config/setting.toml || true # ❌
18
+ ```
19
+
20
+ 问题:`COPY` 指令不支持 `|| true` 语法。
21
+
22
+ ### 第二次尝试的问题
23
+ ```dockerfile
24
+ RUN ls config/setting.toml 2>/dev/null && cp config/setting.toml /app/config/setting.toml || echo "No local config file" # ❌
25
+ ```
26
+
27
+ 问题:基础镜像已经包含了完整的目录结构和权限设置,不需要重复创建。
28
+
29
+ ### 最终解决方案
30
+
31
+ **简化 Dockerfile**,只包含 HuggingFace Spaces 特定的配置:
32
+
33
+ ```dockerfile
34
+ # Use the pre-built Flow2API image from GitHub Container Registry
35
+ FROM ghcr.io/gdtiti/flow2api:latest
36
+
37
+ # Set HuggingFace Spaces specific environment variables
38
+ ENV PYTHONUNBUFFERED=1
39
+ ENV PYTHONDONTWRITEBYTECODE=1
40
+ ENV FLOW2API_HOST=0.0.0.0
41
+ ENV FLOW2API_PORT=7860
42
+
43
+ # Expose the HuggingFace Spaces default port
44
+ EXPOSE 7860
45
+
46
+ # Health check for HuggingFace Spaces
47
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
48
+ CMD python -c "import socket; s=socket.socket(); s.connect(('localhost', 7860)); s.close()" || exit 1
49
+ ```
50
+
51
+ ## ✅ 修复原因
52
+
53
+ ### 1. **基础镜像完整性**
54
+ `ghcr.io/gdtiti/flow2api:latest` 镜像已经包含:
55
+ - ✅ 完整的目录结构 (`/app/data`, `/app/config` 等)
56
+ - ✅ 正确的文件权限
57
+ - ✅ 默认配置文件
58
+ - ✅ 应用程序代码
59
+ - ✅ 依赖包
60
+
61
+ ### 2. **HuggingFace Spaces 适配需求**
62
+ 只需要配置 HuggingFace Spaces 特定的设置:
63
+ - 🌐 端口从 8000 改为 7860
64
+ - 🏥 健康检查端口更新
65
+ - 📦 环境变量设置
66
+
67
+ ### 3. **减少构建复杂性**
68
+ - 🔧 移除不必要的文件操作
69
+ - ⚡ 加快构建速度
70
+ - 🛡️ 降低出错风险
71
+
72
+ ## 📋 当前 Dockerfile 说明
73
+
74
+ ```dockerfile
75
+ FROM ghcr.io/gdtiti/flow2api:latest
76
+ ```
77
+ 使用预构建镜像,包含完整的 Flow2API 应用。
78
+
79
+ ```dockerfile
80
+ ENV FLOW2API_HOST=0.0.0.0
81
+ ENV FLOW2API_PORT=7860
82
+ ```
83
+ 设置 HuggingFace Spaces 所需的环境变量。
84
+
85
+ ```dockerfile
86
+ EXPOSE 7860
87
+ ```
88
+ 暴露 HuggingFace Spaces 的默认端口。
89
+
90
+ ```dockerfile
91
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
92
+ CMD python -c "import socket; s=socket.socket(); s.connect(('localhost', 7860)); s.close()" || exit 1
93
+ ```
94
+ 配置健康检查,使用正确的端口号 7860。
95
+
96
+ ## 🚀 部署验证
97
+
98
+ 修复后的 Dockerfile 应该能够:
99
+ 1. ✅ 成功构建
100
+ 2. ✅ 正确启动服务
101
+ 3. ✅ 响应健康检查
102
+ 4. ✅ 提供完整的 API 功能
103
+
104
+ ## 📝 重要说明
105
+
106
+ ### 配置管理
107
+ - 应用配置通过环境变量设置
108
+ - 默认配置文件在基础镜像中已包含
109
+ - 可以通过 HuggingFace Spaces 的环境变量覆盖默认设置
110
+
111
+ ### 端口配置
112
+ - 原始端口:8000
113
+ - HuggingFace Spaces 端口:7860
114
+ - 环境变量 `FLOW2API_PORT=7860` 自动处理端口变更
115
+
116
+ ### 文件权限
117
+ - 基础镜像已正确设置所有文件权限
118
+ - 不需要额外处理权限问题
119
+
120
+ ## 🔍 故障排除
121
+
122
+ 如果仍然遇到问题,请检查:
123
+
124
+ 1. **基础镜像可用性**
125
+ ```bash
126
+ docker pull ghcr.io/gdtiti/flow2api:latest
127
+ ```
128
+
129
+ 2. **环境变量配置**
130
+ - 确保在 HuggingFace Spaces 设置中正确配置环境变量
131
+ - 特别是 `FLOW2API_API_KEY` 等关键配置
132
+
133
+ 3. **日志查看**
134
+ - 查看 HuggingFace Spaces 的构建日志
135
+ - 检查应用启动日志
136
+
137
+ 这个修复确保了 Docker 构建的成功和服务的正常运行。
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Simplified Dockerfile for troubleshooting
2
+ FROM ghcr.io/gdtiti/flow2api:latest
3
+
4
+ # Fix all permissions first
5
+ USER root
6
+ RUN cd /app && \
7
+ mkdir -p data logs config && \
8
+ touch logs.txt && \
9
+ chmod -R 777 /app && \
10
+ echo "All permissions set to 777 for debugging"
11
+
12
+ # Set environment variables
13
+ ENV PYTHONUNBUFFERED=1
14
+ ENV PYTHONDONTWRITEBYTECODE=1
15
+ ENV FLOW2API_HOST=0.0.0.0
16
+ ENV FLOW2API_PORT=7860
17
+ ENV FLOW2API_DEBUG_ENABLED=false
18
+
19
+ # Switch to root user for maximum compatibility
20
+ USER root
21
+
22
+ # Expose port
23
+ EXPOSE 7860
24
+
25
+ # Health check with long startup time
26
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=180s --retries=3 \
27
+ CMD python -c "import socket; s=socket.socket(); s.connect(('localhost', 7860)); s.close()" || exit 1
28
+
29
+ # Start with debugging script if main app fails
30
+ COPY start_simple.py /app/start_simple.py
31
+ RUN chmod +x /app/start_simple.py
32
+
33
+ # Try main app first, fallback to simple version
34
+ CMD ["/bin/bash", "-c", "python main.py || python start_simple.py"]
Dockerfile.old ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the pre-built Flow2API image from GitHub Container Registry
2
+ FROM ghcr.io/gdtiti/flow2api:latest
3
+
4
+ # Switch to root to fix permissions and setup
5
+ USER root
6
+
7
+ # Fix file permissions for HuggingFace Spaces
8
+ RUN cd /app && \
9
+ # Create and fix log file permissions
10
+ touch logs.txt && \
11
+ chmod 666 logs.txt && \
12
+ # Ensure data directory exists with proper permissions
13
+ mkdir -p data && \
14
+ chmod 755 data && \
15
+ # Try to set proper ownership
16
+ chown -R app:app /app 2>/dev/null || \
17
+ # If app user doesn't exist, make critical files world writable
18
+ (chmod 777 logs.txt data && echo "Made files world writable as fallback") && \
19
+ echo "File permissions fixed"
20
+
21
+ # Add startup script for debugging
22
+ RUN echo '#!/bin/bash\n\
23
+ echo "=== Startup Debug Info ==="\n\
24
+ echo "User: $(whoami)"\n\
25
+ echo "Current dir: $(pwd)"\n\
26
+ echo "Listing /app:"\n\
27
+ ls -la /app/\n\
28
+ echo "Checking logs.txt permissions:"\n\
29
+ ls -la /app/logs.txt\n\
30
+ echo "=== End Debug Info ==="\n\
31
+ exec python main.py' > /app/start_debug.sh && \
32
+ chmod +x /app/start_debug.sh
33
+
34
+ # Set HuggingFace Spaces specific environment variables
35
+ ENV PYTHONUNBUFFERED=1
36
+ ENV PYTHONDONTWRITEBYTECODE=1
37
+ ENV FLOW2API_HOST=0.0.0.0
38
+ ENV FLOW2API_PORT=7860
39
+
40
+ # Add debug environment variables
41
+ ENV FLOW2API_DEBUG_ENABLED=true
42
+ ENV FLOW2API_DEBUG_LOG_REQUESTS=false
43
+ ENV FLOW2API_DEBUG_LOG_RESPONSES=false
44
+
45
+ # Switch back to app user if it exists
46
+ USER app 2>/dev/null || USER root
47
+
48
+ # Expose the HuggingFace Spaces default port
49
+ EXPOSE 7860
50
+
51
+ # Health check for HuggingFace Spaces with longer startup time
52
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
53
+ CMD python -c "import socket; s=socket.socket(); s.connect(('localhost', 7860)); s.close()" || exit 1
54
+
55
+ # Use debug startup script
56
+ CMD ["/app/start_debug.sh"]
PERMISSION_FIX.md ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 日志权限问题修复说明
2
+
3
+ ## 🐛 问题描述
4
+
5
+ 在 HuggingFace Spaces 部署时遇到权限错误:
6
+
7
+ ```
8
+ PermissionError: [Errno 13] Permission denied: '/app/logs.txt'
9
+ ```
10
+
11
+ **错误原因**:
12
+ - Flow2API 应用尝试创建 `/app/logs.txt` 文件
13
+ - 容器中的 `app` 用户没有 `/app` 目录的写权限
14
+ - HuggingFace Spaces 的环境与标准 Docker 环境在用户权限方面有所不同
15
+
16
+ ## 🔧 修复方案
17
+
18
+ ### 方案 1:修复文件权限(已实施)
19
+
20
+ **修改后的 Dockerfile**:
21
+ ```dockerfile
22
+ # Switch to root to create log file with proper permissions
23
+ USER root
24
+
25
+ # Create logs.txt file with proper permissions for HuggingFace Spaces
26
+ RUN cd /app && \
27
+ touch logs.txt && \
28
+ chmod 666 logs.txt && \
29
+ # Try to set proper ownership
30
+ chown app:app logs.txt 2>/dev/null || \
31
+ # If app user doesn't exist, make it world writable
32
+ chmod 777 logs.txt
33
+
34
+ # Switch back to app user if it exists
35
+ USER app 2>/dev/null || USER root
36
+ ```
37
+
38
+ ### 方案 2:通过环境变量禁用日志文件
39
+
40
+ **在 HuggingFace Spaces 设置中配置**:
41
+ ```bash
42
+ FLOW2API_DEBUG_ENABLED=false
43
+ FLOW2API_DEBUG_LOG_REQUESTS=false
44
+ FLOW2API_DEBUG_LOG_RESPONSES=false
45
+ ```
46
+
47
+ 这样可以避免创建日志文件,所有日志输出到 stdout。
48
+
49
+ ## 📋 修复详解
50
+
51
+ ### 权限修复逻辑
52
+
53
+ 1. **切换到 root 用户**:获取足够的权限来创建和修改文件
54
+ 2. **创建日志文件**:`touch logs.txt` 创建空的日志文件
55
+ 3. **设置权限**:
56
+ - `chmod 666 logs.txt` - 设置文件为读写权限
57
+ - 尝试 `chown app:app logs.txt` 设置所有��
58
+ - 如果失败,使用 `chmod 777 logs.txt` 使文件全局可写
59
+ 4. **切换回应用用户**:恢复到安全的用户身份运行应用
60
+
61
+ ### 环境变量配置
62
+
63
+ ```bash
64
+ # 禁用调试日志(推荐用于生产环境)
65
+ FLOW2API_DEBUG_ENABLED=false
66
+ FLOW2API_DEBUG_LOG_REQUESTS=false
67
+ FLOW2API_DEBUG_LOG_RESPONSES=false
68
+
69
+ # 如果需要调试,可以临时启用
70
+ FLOW2API_DEBUG_ENABLED=true
71
+ ```
72
+
73
+ ## 🚀 部署步骤
74
+
75
+ ### 1. 更新 Dockerfile
76
+ 修复后的 Dockerfile 已包含权限修复逻辑。
77
+
78
+ ### 2. 配置环境变量
79
+ 在 HuggingFace Space 设置中添加:
80
+ ```bash
81
+ FLOW2API_DEBUG_ENABLED=false
82
+ FLOW2API_DEBUG_LOG_REQUESTS=false
83
+ FLOW2API_DEBUG_LOG_RESPONSES=false
84
+ FLOW2API_ADMIN_PASSWORD=your-secure-password
85
+ ```
86
+
87
+ ### 3. 重新部署
88
+ 推送修复后的 Dockerfile 并等待构建完成。
89
+
90
+ ## ✅ 验证修复
91
+
92
+ ### 检查应用启动状态
93
+ ```bash
94
+ # 检查应用是否正常启动
95
+ curl -f https://your-space.hf.space/ || echo "Service not responding"
96
+
97
+ # 检查健康状态
98
+ curl -f https://your-space.hf.space/health || echo "Health check failed"
99
+ ```
100
+
101
+ ### 查看容器日志
102
+ 在 HuggingFace Spaces 控制台查看应用启动日志,应该看到:
103
+ ```
104
+ ===== Application Startup at YYYY-MM-DD HH:MM:SS =====
105
+ Uvicorn running on http://0.0.0.0:7860
106
+ ```
107
+
108
+ ### 测试 API 功能
109
+ ```bash
110
+ # 测试基础连接
111
+ curl https://your-space.hf.space/v1/models
112
+
113
+ # 测试 API 调用(需要有效 API 密钥)
114
+ curl -X POST "https://your-space.hf.space/v1/chat/completions" \
115
+ -H "Authorization: Bearer your-api-key" \
116
+ -H "Content-Type: application/json" \
117
+ -d '{"model": "gemini-2.5-flash-image-landscape", "messages": [{"role": "user", "content": "test"}], "stream": false}'
118
+ ```
119
+
120
+ ## 🔍 故障排除
121
+
122
+ ### 如果仍然遇到权限错误
123
+
124
+ 1. **检查日志文件**:
125
+ ```bash
126
+ # 在容器中检查文件权限
127
+ ls -la /app/logs.txt
128
+ ```
129
+
130
+ 2. **手动创建日志文件**(如果需要):
131
+ ```bash
132
+ # 通过 HuggingFace Spaces 终端执行(如果可用)
133
+ touch /app/logs.txt
134
+ chmod 777 /app/logs.txt
135
+ ```
136
+
137
+ 3. **完全禁用日志文件**:
138
+ 设置环境变量:
139
+ ```bash
140
+ FLOW2API_DEBUG_ENABLED=false
141
+ FLOW2API_DEBUG_LOG_REQUESTS=false
142
+ FLOW2API_DEBUG_LOG_RESPONSES=false
143
+ ```
144
+
145
+ ### HuggingFace Spaces 特殊性
146
+
147
+ HuggingFace Spaces 有以下特殊考虑:
148
+
149
+ 1. **用户权限**:可能没有预定义的 `app` 用户
150
+ 2. **文件系统**:可能有特殊的挂载和权限限制
151
+ 3. **日志输出**:推荐将日志输出到 stdout 而不是文件
152
+ 4. **持久化**:文件可能不会持久化存储
153
+
154
+ ## 📝 最佳实践建议
155
+
156
+ 1. **生产环境**:
157
+ - 禁用调试日志:`FLOW2API_DEBUG_ENABLED=false`
158
+ - 监控 HuggingFace Spaces 的标准日志输出
159
+
160
+ 2. **开发/调试**:
161
+ - 临时启用调试:`FLOW2API_DEBUG_ENABLED=true`
162
+ - 使用 HuggingFace Spaces 的实时日志查看功能
163
+
164
+ 3. **性能优化**:
165
+ - 禁用不必要的日志记录
166
+ - 调整 API 调用频率和并发限制
167
+
168
+ ## 🔄 替代方案
169
+
170
+ 如果权限问题持续存在,可以考虑:
171
+
172
+ ### 方案 A:完全基于 stdout 的日志
173
+ 修改 logger.py 将所有日志输出到 stdout 而不是文件。
174
+
175
+ ### 方案 B:使用 /tmp 目录
176
+ ��日志文件写入 `/tmp` 目录,该目录通常有更宽松的权限。
177
+
178
+ ### 方案 C:使用环境变量控制
179
+ 通过环境变量控制日志行为,避免文件系统依赖。
180
+
181
+ ## 📚 相关资源
182
+
183
+ - [HuggingFace Spaces 文档](https://huggingface.co/docs/hub/spaces)
184
+ - [Docker 权限最佳实践](https://docs.docker.com/engine/security/userns-remap/)
185
+ - [Python logging 模块](https://docs.python.org/3/library/logging.html)
186
+
187
+ ---
188
+
189
+ 这个修复确保了 Flow2API 在 HuggingFace Spaces 环境中的正常运行,同时保持了应用的完整功能。
README.md CHANGED
@@ -1,10 +1,9 @@
1
- ---
2
- title: Zzz
3
- emoji: 📈
4
- colorFrom: indigo
5
- colorTo: pink
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: zzz - Flow2
3
+ emoji: 🎨
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ ---
 
STARTUP_TROUBLESHOOTING.md ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Flow2API HuggingFace Spaces 启动问题诊断和解决方案
2
+
3
+ ## 🐛 问题描述
4
+
5
+ 应用在 HuggingFace Spaces 上启动后卡在:
6
+ ```
7
+ ===== Application Startup at 2025-11-24 17:30:58 =====
8
+ ```
9
+
10
+ ## 🔍 可能的原因分析
11
+
12
+ ### 1. **数据库初始化阻塞**
13
+ - SQLite 数据库创建时的权限问题
14
+ - 异步数据库操作在容器环境中被阻塞
15
+ - 数据库迁移过程中的死锁
16
+
17
+ ### 2. **异步操作超时**
18
+ - 应用启动时的并发初始化操作
19
+ - 资源竞争导致的阻塞
20
+ - HuggingFace Spaces 的资源限制
21
+
22
+ ### 3. **文件系统权限问题**
23
+ - 日志文件写入权限
24
+ - 数据目录创建权限
25
+ - 配置文件读取权限
26
+
27
+ ### 4. **网络��接问题**
28
+ - Google API 连接检查
29
+ - 代理设置配置
30
+ - 网络超时设置
31
+
32
+ ## 🔧 提供的解决方案
33
+
34
+ ### 方案 1:增强调试版 Dockerfile
35
+ ```dockerfile
36
+ # 已创建 Dockerfile,包含:
37
+ - 详细的启动调试信息
38
+ - 文件权限修复
39
+ - 更长的健康检查启动时间(120s)
40
+ - 调试启动脚本
41
+ ```
42
+
43
+ ### 方案 2:简化版 Dockerfile
44
+ ```dockerfile
45
+ # 已创建 Dockerfile.simple:
46
+ - 最大权限设置(777)
47
+ - 使用 root 用户运行
48
+ - 180秒启动时间
49
+ - 备用启动脚本
50
+ ```
51
+
52
+ ### 方案 3:诊断脚本
53
+ ```python
54
+ # 已创建 start_simple.py:
55
+ - 环境变量检查
56
+ - 文件权限验证
57
+ - 数据库连接测试
58
+ - 简单的备用服务器
59
+ ```
60
+
61
+ ## 🚀 部署步骤
62
+
63
+ ### 第一步:尝试增强版
64
+ 1. **推送当前的 Dockerfile**
65
+ 2. **配置环境变量**:
66
+ ```bash
67
+ FLOW2API_DEBUG_ENABLED=true
68
+ FLOW2API_DEBUG_LOG_REQUESTS=false
69
+ FLOW2API_DEBUG_LOG_RESPONSES=false
70
+ ```
71
+ 3. **查看启动日志**,应该看到详细的调试信息
72
+
73
+ ### 第二步:如果仍然卡住,使用简化版
74
+ 1. **重命名文件**:
75
+ ```bash
76
+ mv Dockerfile Dockerfile.backup
77
+ mv Dockerfile.simple Dockerfile
78
+ ```
79
+ 2. **重新部署**
80
+ 3. **监控启动过程**
81
+
82
+ ### 第三步:手动调试
83
+ 如果自动启动仍然失败,可以:
84
+ 1. **使用 start_simple.py** 作为主启动脚本
85
+ 2. **通过 HuggingFace Spaces 终端**(如果可用)手动执行:
86
+ ```bash
87
+ cd /app
88
+ python start_simple.py
89
+ ```
90
+
91
+ ## 📋 诊断检查清单
92
+
93
+ ### 环境变量确认
94
+ 在 HuggingFace Spaces 设置中确认:
95
+ - [ ] `FLOW2API_HOST=0.0.0.0`
96
+ - [ ] `FLOW2API_PORT=7860`
97
+ - [ ] `PYTHONUNBUFFERED=1`
98
+ - [ ] `FLOW2API_DEBUG_ENABLED=false`(生产环境)或 `true`(调试)
99
+
100
+ ### 文件权限检查
101
+ 查看启动日志中是否显示:
102
+ - [ ] `File permissions fixed`
103
+ - [ ] 用户信息正确
104
+ - [ ] 日志文件权限 `666` 或 `777`
105
+
106
+ ### 数据库初始化检查
107
+ 查看日志中的数据库相关消息:
108
+ - [ ] `Database initialized`
109
+ - [ ] `Total tokens: 0`
110
+ - [ ] 没有 SQLite 错误
111
+
112
+ ### 服务状态检查
113
+ 部署后检查:
114
+ - [ ] 健康检查通过
115
+ - [ ] 根路由响应:`curl https://your-space.hf.space/`
116
+ - [ ] API 路由响应:`curl https://your-space.hf.space/v1/models`
117
+
118
+ ## 🛠️ 高级调试技巧
119
+
120
+ ### 启用详细日志
121
+ ```bash
122
+ # 在环境变量中设置
123
+ FLOW2API_DEBUG_ENABLED=true
124
+ PYTHONUNBUFFERED=1
125
+ ```
126
+
127
+ ### 检查容器内部
128
+ 如果可以访问容器终端:
129
+ ```bash
130
+ # 检查进程
131
+ ps aux
132
+
133
+ # 检查端口
134
+ netstat -tlnp
135
+
136
+ # 检查文件
137
+ ls -la /app/
138
+
139
+ # 手动测试数据库
140
+ python -c "import aiosqlite; print('OK')"
141
+ ```
142
+
143
+ ### 替代启动方法
144
+ 如果主启动脚本失败,可以:
145
+ 1. **直接使用 Uvicorn**:
146
+ ```bash
147
+ uvicorn src.main:app --host 0.0.0.0 --port 7860
148
+ ```
149
+
150
+ 2. **禁用某些功能**:
151
+ ```bash
152
+ # 通过环境变量禁用缓存和调试
153
+ FLOW2API_CACHE_ENABLED=false
154
+ FLOW2API_DEBUG_ENABLED=false
155
+ ```
156
+
157
+ ## 🔄 迁移到生产配置
158
+
159
+ 一旦应用成功启动,建议:
160
+
161
+ 1. **禁用调试**:
162
+ ```bash
163
+ FLOW2API_DEBUG_ENABLED=false
164
+ FLOW2API_DEBUG_LOG_REQUESTS=false
165
+ FLOW2API_DEBUG_LOG_RESPONSES=false
166
+ ```
167
+
168
+ 2. **优化性能**:
169
+ ```bash
170
+ FLOW2API_IMAGE_TIMEOUT=600
171
+ FLOW2API_VIDEO_TIMEOUT=1800
172
+ FLOW2API_CACHE_ENABLED=true
173
+ ```
174
+
175
+ 3. **安全配置**:
176
+ ```bash
177
+ # 更改默认密码
178
+ FLOW2API_ADMIN_PASSWORD=your-secure-password
179
+ # 配置真实的 API 密钥
180
+ FLOW2API_API_KEY=your-google-api-key
181
+ ```
182
+
183
+ ## 📞 故障排除联系
184
+
185
+ 如果问题仍然存在:
186
+
187
+ 1. **查看详细日志**:HuggingFace Spaces 构建日志
188
+ 2. **尝试本地测试**:使用相同配置在本地运行
189
+ 3. **报告问题**:在 GitHub 上创建 issue 并提供:
190
+ - 完整的启动日志
191
+ - 环境变量配置
192
+ - 使用的是哪个 Dockerfile 版本
193
+
194
+ ## 📚 相关资源
195
+
196
+ - [HuggingFace Spaces 文档](https://huggingface.co/docs/hub/spaces)
197
+ - [FastAPI 调试指南](https://fastapi.tiangolo.com/tutorial/debugging/)
198
+ - [SQLite 权限问题](https://www.sqlite.org/permissionless.html)
199
+
200
+ ---
201
+
202
+ **目标**���让 Flow2API 在 HuggingFace Spaces 上稳定运行,提供完整的图像和视频生成 API 服务。
config/setting.toml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Flow2API Configuration for HuggingFace Spaces
2
+ # This is the default configuration, can be overridden by environment variables
3
+
4
+ [global]
5
+ api_key = "your-api-key-here"
6
+ admin_username = "admin"
7
+ admin_password = "change-this-password"
8
+
9
+ [flow]
10
+ labs_base_url = "https://labs.google/fx/api"
11
+ api_base_url = "https://aisandbox-pa.googleapis.com/v1"
12
+ timeout = 120
13
+ max_retries = 3
14
+ poll_interval = 3.0
15
+ max_poll_attempts = 200
16
+
17
+ [server]
18
+ host = "0.0.0.0"
19
+ port = 7860
20
+
21
+ [debug]
22
+ enabled = false
23
+ log_requests = true
24
+ log_responses = true
25
+ mask_token = true
26
+
27
+ [proxy]
28
+ proxy_enabled = false
29
+ proxy_url = ""
30
+
31
+ [generation]
32
+ image_timeout = 300
33
+ video_timeout = 1500
34
+
35
+ [cache]
36
+ enabled = false
37
+ timeout = 7200
38
+ base_url = ""
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HuggingFace Spaces requirements
2
+ # This file is primarily for documentation since we're using a pre-built Docker image
3
+ # but it helps with local development and potential customizations
4
+
5
+ fastapi==0.119.0
6
+ uvicorn[standard]==0.32.1
7
+ aiosqlite==0.20.0
8
+ pydantic==2.10.4
9
+ curl-cffi==0.7.3
10
+ tomli==2.2.1
11
+ bcrypt==4.2.1
12
+ python-multipart==0.0.20
13
+ python-dateutil==2.8.2
14
+
15
+ # HuggingFace Spaces specific
16
+ gradio==4.44.0
start_simple.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Simple startup script for debugging Flow2API on HuggingFace Spaces"""
3
+
4
+ import os
5
+ import sys
6
+ import time
7
+ import asyncio
8
+ from pathlib import Path
9
+
10
+ def debug_environment():
11
+ """Print environment debug information"""
12
+ print("=" * 60)
13
+ print("🔍 FLOW2API DEBUG STARTUP")
14
+ print("=" * 60)
15
+ print(f"Python version: {sys.version}")
16
+ print(f"Current working directory: {os.getcwd()}")
17
+ print(f"User ID: {os.getuid()}")
18
+ print(f"Group ID: {os.getgid()}")
19
+ print(f"Environment variables:")
20
+ for key, value in sorted(os.environ.items()):
21
+ if key.startswith('FLOW2API') or key in ['HOST', 'PORT', 'HOME', 'PATH']:
22
+ print(f" {key}: {value}")
23
+ print("=" * 60)
24
+
25
+ def check_file_permissions():
26
+ """Check critical file permissions"""
27
+ files_to_check = [
28
+ '/app',
29
+ '/app/logs.txt',
30
+ '/app/data',
31
+ '/app/config'
32
+ ]
33
+
34
+ print("📁 FILE PERMISSIONS CHECK:")
35
+ for file_path in files_to_check:
36
+ path = Path(file_path)
37
+ if path.exists():
38
+ stat_info = path.stat()
39
+ print(f" ✅ {file_path}: exists, mode={oct(stat_info.st_mode)}")
40
+ if path.is_file():
41
+ try:
42
+ # Test write permission
43
+ with open(path, 'a') as f:
44
+ f.write("")
45
+ print(f" ✅ {file_path}: writable")
46
+ except Exception as e:
47
+ print(f" ❌ {file_path}: NOT writable - {e}")
48
+ else:
49
+ print(f" ❌ {file_path}: does not exist")
50
+ print("=" * 60)
51
+
52
+ async def test_basic_import():
53
+ """Test if basic modules can be imported"""
54
+ print("📦 IMPORT TEST:")
55
+ try:
56
+ import fastapi
57
+ print(f" ✅ FastAPI: {fastapi.__version__}")
58
+ except Exception as e:
59
+ print(f" ❌ FastAPI import failed: {e}")
60
+
61
+ try:
62
+ import uvicorn
63
+ print(f" ✅ Uvicorn: available")
64
+ except Exception as e:
65
+ print(f" ❌ Uvicorn import failed: {e}")
66
+
67
+ try:
68
+ import aiosqlite
69
+ print(f" ✅ aiosqlite: available")
70
+ except Exception as e:
71
+ print(f" ❌ aiosqlite import failed: {e}")
72
+
73
+ print("=" * 60)
74
+
75
+ async def test_database_connection():
76
+ """Test database creation and connection"""
77
+ print("💾 DATABASE TEST:")
78
+ try:
79
+ import aiosqlite
80
+
81
+ # Test database directory creation
82
+ data_dir = Path("/app/data")
83
+ data_dir.mkdir(exist_ok=True)
84
+ print(f" ✅ Data directory: {data_dir}")
85
+
86
+ # Test database connection
87
+ db_path = "/app/data/test.db"
88
+ async with aiosqlite.connect(db_path) as db:
89
+ await db.execute("SELECT 1")
90
+ print(f" ✅ Database connection: successful")
91
+
92
+ # Clean up test file
93
+ os.remove(db_path)
94
+ print(f" ✅ Test database: cleaned up")
95
+
96
+ except Exception as e:
97
+ print(f" ❌ Database test failed: {e}")
98
+ import traceback
99
+ traceback.print_exc()
100
+
101
+ print("=" * 60)
102
+
103
+ def start_simple_server():
104
+ """Start a simple test server"""
105
+ print("🌐 SIMPLE SERVER TEST:")
106
+ try:
107
+ from fastapi import FastAPI
108
+ import uvicorn
109
+
110
+ app = FastAPI()
111
+
112
+ @app.get("/")
113
+ async def root():
114
+ return {"message": "Flow2API is running", "status": "ok"}
115
+
116
+ @app.get("/health")
117
+ async def health():
118
+ return {"status": "healthy"}
119
+
120
+ print(" ✅ Simple FastAPI app created")
121
+ print(" 🚀 Starting simple server on port 7860...")
122
+
123
+ # Start server
124
+ uvicorn.run(
125
+ app,
126
+ host="0.0.0.0",
127
+ port=7860,
128
+ log_level="info"
129
+ )
130
+
131
+ except Exception as e:
132
+ print(f" ❌ Simple server failed: {e}")
133
+ import traceback
134
+ traceback.print_exc()
135
+
136
+ async def main():
137
+ """Main debugging function"""
138
+ debug_environment()
139
+ check_file_permissions()
140
+ await test_basic_import()
141
+ await test_database_connection()
142
+
143
+ print("🎯 STARTING FLOW2API:")
144
+ try:
145
+ # Import and start the main application
146
+ from src.main import app
147
+ print(" ✅ Main app imported successfully")
148
+
149
+ # Start with uvicorn directly
150
+ import uvicorn
151
+ print(" 🚀 Starting main application...")
152
+
153
+ uvicorn.run(
154
+ app,
155
+ host="0.0.0.0",
156
+ port=7860,
157
+ log_level="info",
158
+ access_log=True
159
+ )
160
+
161
+ except Exception as e:
162
+ print(f" ❌ Main app startup failed: {e}")
163
+ import traceback
164
+ traceback.print_exc()
165
+ print("🔄 FALLING BACK TO SIMPLE SERVER...")
166
+ start_simple_server()
167
+
168
+ if __name__ == "__main__":
169
+ print("Starting Flow2API with debug mode...")
170
+ asyncio.run(main())