Spaces:
Running
Running
Upload 9 files
Browse files- .env.example +62 -0
- .gitattributes +2 -35
- .gitignore +47 -0
- Dockerfile +13 -0
- LICENSE +21 -0
- README.md +342 -11
- accounts_config.example.json +26 -0
- main.py +0 -0
- requirements.txt +4 -0
.env.example
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================
|
| 2 |
+
# Gemini Business2API 配置示例
|
| 3 |
+
# ============================================
|
| 4 |
+
|
| 5 |
+
# 代理设置(可选)
|
| 6 |
+
# PROXY=http://127.0.0.1:7890
|
| 7 |
+
|
| 8 |
+
# API 访问密钥(可选,用于保护 API 端点)
|
| 9 |
+
# API_KEY=your-secret-api-key-here
|
| 10 |
+
|
| 11 |
+
# 路径前缀(必需,用于隐藏端点路径)
|
| 12 |
+
PATH_PREFIX=your-random-path-prefix
|
| 13 |
+
|
| 14 |
+
# 管理员密钥(必需,用于访问管理端点)
|
| 15 |
+
ADMIN_KEY=your-admin-secret-key
|
| 16 |
+
|
| 17 |
+
# 服务器完整 URL(可选,用于反代公开日志和图片 URL)
|
| 18 |
+
# BASE_URL=https://your-domain.com
|
| 19 |
+
|
| 20 |
+
# ============================================
|
| 21 |
+
# 高级配置(可选,使用默认值)
|
| 22 |
+
# ============================================
|
| 23 |
+
|
| 24 |
+
# 新会话创建最多尝试账户数(默认:5)
|
| 25 |
+
# MAX_NEW_SESSION_TRIES=5
|
| 26 |
+
|
| 27 |
+
# 请求失败最多重试次数(默认:3)
|
| 28 |
+
# MAX_REQUEST_RETRIES=3
|
| 29 |
+
|
| 30 |
+
# 每次重试找账户的最大尝试次数(默认:5)
|
| 31 |
+
# MAX_ACCOUNT_SWITCH_TRIES=5
|
| 32 |
+
|
| 33 |
+
# 账户连续失败阈值(默认:3次)
|
| 34 |
+
# ACCOUNT_FAILURE_THRESHOLD=3
|
| 35 |
+
|
| 36 |
+
# 账户冷却时间,单位秒(默认:300秒=5分钟)
|
| 37 |
+
# ACCOUNT_COOLDOWN_SECONDS=300
|
| 38 |
+
|
| 39 |
+
# 会话缓存过期时间,单位秒(默认:3600秒=1小时)
|
| 40 |
+
# SESSION_CACHE_TTL_SECONDS=3600
|
| 41 |
+
|
| 42 |
+
# ============================================
|
| 43 |
+
# 公开展示配置(可选)
|
| 44 |
+
# ============================================
|
| 45 |
+
# Logo URL(公开,为空则不显示)
|
| 46 |
+
# LOGO_URL=https://your-domain.com/logo.png
|
| 47 |
+
|
| 48 |
+
# 开始对话链接(公开,为空则不显示)
|
| 49 |
+
# CHAT_URL=https://your-chat-app.com
|
| 50 |
+
|
| 51 |
+
# 模型名称(公开,默认:gemini-business)
|
| 52 |
+
# MODEL_NAME=gemini-business
|
| 53 |
+
|
| 54 |
+
# ============================================
|
| 55 |
+
# 多账户配置(必需)
|
| 56 |
+
# ============================================
|
| 57 |
+
# 使用 JSON 数组格式配置多个 Gemini 账户
|
| 58 |
+
# 必需字段:secure_c_ses, csesidx, config_id
|
| 59 |
+
# 可选字段:id, host_c_oses, proxy, expires_at
|
| 60 |
+
# 详细配置请参考 accounts_config.example.json
|
| 61 |
+
|
| 62 |
+
ACCOUNTS_CONFIG=[{"secure_c_ses":"your-cookie-here","csesidx":"your-idx","config_id":"your-config","proxy":""}]
|
.gitattributes
CHANGED
|
@@ -1,35 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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 |
+
# Auto detect text files and perform LF normalization
|
| 2 |
+
* text=auto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
wheels/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
|
| 23 |
+
# Virtual Environment
|
| 24 |
+
venv/
|
| 25 |
+
env/
|
| 26 |
+
ENV/
|
| 27 |
+
|
| 28 |
+
# IDE
|
| 29 |
+
.vscode/
|
| 30 |
+
.idea/
|
| 31 |
+
*.swp
|
| 32 |
+
*.swo
|
| 33 |
+
*~
|
| 34 |
+
|
| 35 |
+
# Project specific
|
| 36 |
+
.env
|
| 37 |
+
*.log
|
| 38 |
+
stats.json
|
| 39 |
+
|
| 40 |
+
# Generated files
|
| 41 |
+
images/
|
| 42 |
+
static/
|
| 43 |
+
logs/
|
| 44 |
+
|
| 45 |
+
# OS
|
| 46 |
+
.DS_Store
|
| 47 |
+
Thumbs.db
|
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY requirements.txt .
|
| 4 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 5 |
+
gcc \
|
| 6 |
+
&& pip install --no-cache-dir -r requirements.txt \
|
| 7 |
+
&& apt-get purge -y gcc \
|
| 8 |
+
&& apt-get autoremove -y \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
COPY main.py .
|
| 11 |
+
# 复制 util 目录
|
| 12 |
+
COPY util ./util
|
| 13 |
+
CMD ["python", "-u", "main.py"]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 yu
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
CHANGED
|
@@ -1,11 +1,342 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gemini Business2API
|
| 2 |
+
|
| 3 |
+
将 Google Gemini Business API 转换为 OpenAI 兼容接口,支持多账户负载均衡。
|
| 4 |
+
|
| 5 |
+
[](https://opensource.org/licenses/MIT)
|
| 6 |
+
[](https://www.python.org/downloads/)
|
| 7 |
+
|
| 8 |
+
**快速部署到 HuggingFace Spaces:**
|
| 9 |
+
|
| 10 |
+
[](https://huggingface.co/spaces/YOUR_USERNAME/gemini-business2api?duplicate=true)
|
| 11 |
+
|
| 12 |
+
## ✨ 功能特性
|
| 13 |
+
|
| 14 |
+
- ✅ **OpenAI API 完全兼容** - 无缝对接现有工具
|
| 15 |
+
- ✅ **流式响应支持** - 实时输出
|
| 16 |
+
- ✅ **多模态支持** - 文本 + 图片输入
|
| 17 |
+
- ✅ **图片生成 & 图生图** - 支持 `gemini-3-pro-preview` 模型
|
| 18 |
+
- ✅ **多账户负载均衡** - 支持多账户轮询,故障自动转移
|
| 19 |
+
- ✅ **智能会话复用** - 自动管理对话历史
|
| 20 |
+
- ✅ **JWT自动管理** - 无需手动刷新令牌
|
| 21 |
+
- 📊 **可视化管理面板** - 实时监控账户状态
|
| 22 |
+
- 📝 **公开日志系统** - 实时查看服务运行状态
|
| 23 |
+
|
| 24 |
+
## 📸 功能展示
|
| 25 |
+
|
| 26 |
+
### 管理面板
|
| 27 |
+
<!-- 在这里添加管理面板截图 -->
|
| 28 |
+
|
| 29 |
+
### 图片生成效果
|
| 30 |
+
<!-- 在这里添加图片生成示例截图 -->
|
| 31 |
+
|
| 32 |
+
### 公开日志系统
|
| 33 |
+
<!-- 在这里添加日志系统截图 -->
|
| 34 |
+
|
| 35 |
+
## 🚀 快速开始
|
| 36 |
+
|
| 37 |
+
### 方法一: HuggingFace Spaces 部署(推荐)
|
| 38 |
+
|
| 39 |
+
1. Fork 本项目到你的 GitHub 账户
|
| 40 |
+
2. 在 [HuggingFace Spaces](https://huggingface.co/spaces) 创建新 Space
|
| 41 |
+
3. 选择 Docker SDK,关联你的 GitHub 仓库
|
| 42 |
+
4. 配置环境变量(Settings → Variables and secrets):
|
| 43 |
+
```bash
|
| 44 |
+
ACCOUNTS_CONFIG='[{"secure_c_ses":"your_cookie","csesidx":"your_idx","config_id":"your_config"}]'
|
| 45 |
+
PATH_PREFIX=path_prefix
|
| 46 |
+
ADMIN_KEY=your_admin_key
|
| 47 |
+
API_KEY=your_api_key
|
| 48 |
+
LOGO_URL=https://your-domain.com/logo.png
|
| 49 |
+
CHAT_URL=https://your-chat-app.com
|
| 50 |
+
```
|
| 51 |
+
5. 等待构建完成(约 2-3 分钟)
|
| 52 |
+
6. 访问你的 Space URL 开始使用
|
| 53 |
+
|
| 54 |
+
### 方法二: Docker 部署
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
# 1. 克隆项目
|
| 58 |
+
git clone https://github.com/YOUR_USERNAME/gemini-business2api.git
|
| 59 |
+
cd gemini-business2api
|
| 60 |
+
|
| 61 |
+
# 2. 构建并运行
|
| 62 |
+
docker build -t gemini-business2api .
|
| 63 |
+
docker run -d \
|
| 64 |
+
-p 7860:7860 \
|
| 65 |
+
-e ACCOUNTS_CONFIG='[{"secure_c_ses":"your_cookie","csesidx":"your_idx","config_id":"your_config"}]' \
|
| 66 |
+
-e PATH_PREFIX=path_prefix \
|
| 67 |
+
-e ADMIN_KEY=your_admin_key \
|
| 68 |
+
-e API_KEY=your_api_key \
|
| 69 |
+
-e LOGO_URL=https://your-domain.com/logo.png \
|
| 70 |
+
-e CHAT_URL=https://your-chat-app.com \
|
| 71 |
+
gemini-business2api
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### 方法三: 本地运行
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
# 1. 安装依赖
|
| 78 |
+
pip install -r requirements.txt
|
| 79 |
+
|
| 80 |
+
# 2. 配置环境变量
|
| 81 |
+
cp .env.example .env
|
| 82 |
+
# 编辑 .env 文件,填入实际配置
|
| 83 |
+
|
| 84 |
+
# 3. 启动服务
|
| 85 |
+
python main.py
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
服务将在 `http://localhost:7860` 启动
|
| 89 |
+
|
| 90 |
+
## ⚙️ 配置说明
|
| 91 |
+
|
| 92 |
+
### 必需的环境变量
|
| 93 |
+
|
| 94 |
+
```bash
|
| 95 |
+
# 账户配置(必需)
|
| 96 |
+
ACCOUNTS_CONFIG='[{"secure_c_ses":"your_cookie","csesidx":"your_idx","config_id":"your_config"}]'
|
| 97 |
+
|
| 98 |
+
# 路径前缀(必需)
|
| 99 |
+
PATH_PREFIX=path_prefix
|
| 100 |
+
|
| 101 |
+
# 管理员密钥(必需)
|
| 102 |
+
ADMIN_KEY=your_admin_key
|
| 103 |
+
|
| 104 |
+
# API访问密钥(可选,推荐设置)
|
| 105 |
+
API_KEY=your_api_key
|
| 106 |
+
|
| 107 |
+
# 图片URL生成(可选,推荐设置)
|
| 108 |
+
BASE_URL=https://your-domain.com
|
| 109 |
+
|
| 110 |
+
# 全局代理(可选)
|
| 111 |
+
PROXY=http://127.0.0.1:7890
|
| 112 |
+
|
| 113 |
+
# 公开展示配置(可选)
|
| 114 |
+
LOGO_URL=https://your-domain.com/logo.png
|
| 115 |
+
CHAT_URL=https://your-chat-app.com
|
| 116 |
+
MODEL_NAME=gemini-business
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
### 多账户配置示例
|
| 120 |
+
|
| 121 |
+
```bash
|
| 122 |
+
ACCOUNTS_CONFIG='[
|
| 123 |
+
{
|
| 124 |
+
"id": "account_1",
|
| 125 |
+
"secure_c_ses": "CSE.Ad...",
|
| 126 |
+
"csesidx": "498...",
|
| 127 |
+
"config_id": "0cd...",
|
| 128 |
+
"host_c_oses": "COS.Af...",
|
| 129 |
+
"expires_at": "2025-12-23 23:03:20"
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"id": "account_2",
|
| 133 |
+
"secure_c_ses": "CSE.Ad...",
|
| 134 |
+
"csesidx": "208...",
|
| 135 |
+
"config_id": "782..."
|
| 136 |
+
}
|
| 137 |
+
]'
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
**配置字段说明**:
|
| 141 |
+
- `secure_c_ses` (必需): `__Secure-C_SES` Cookie 值
|
| 142 |
+
- `csesidx` (必需): 会话索引
|
| 143 |
+
- `config_id` (必需): 配置 ID
|
| 144 |
+
- `id` (可选): 账户标识
|
| 145 |
+
- `host_c_oses` (可选): `__Host-C_OSES` Cookie 值
|
| 146 |
+
- `expires_at` (可选): 过期时间,格式 `YYYY-MM-DD HH:MM:SS`
|
| 147 |
+
|
| 148 |
+
**提示**: 参考项目根目录的 `.env.example` 和 `accounts_config.example.json` 文件
|
| 149 |
+
|
| 150 |
+
## 🔧 获取配置参数
|
| 151 |
+
|
| 152 |
+
1. 访问 [Google Gemini Business](https://business.gemini.google)
|
| 153 |
+
2. 打开浏览器开发者工具 (F12)
|
| 154 |
+
3. 切换到 **Application** → **Cookies**,找到:
|
| 155 |
+
- `__Secure-C_SES` → `secure_c_ses`
|
| 156 |
+
- `__Host-C_OSES` → `host_c_oses` (可选)
|
| 157 |
+
4. 切换到 **Network** 标签,刷新页面
|
| 158 |
+
5. ��到 `streamGenerate` 请求,查看 Payload:
|
| 159 |
+
- `csesidx` → `csesidx`
|
| 160 |
+
- `configId` → `config_id`
|
| 161 |
+
|
| 162 |
+
## 📖 API 使用
|
| 163 |
+
|
| 164 |
+
### 支持的模型
|
| 165 |
+
|
| 166 |
+
| 模型名称 | 说明 | 图片生成 |
|
| 167 |
+
| ------------------------ | ---------------------- | -------- |
|
| 168 |
+
| `gemini-auto` | 自动选择最佳模型(默认) | ❌ |
|
| 169 |
+
| `gemini-2.5-flash` | Flash 2.5 - 快速响应 | ❌ |
|
| 170 |
+
| `gemini-2.5-pro` | Pro 2.5 - 高质量输出 | ❌ |
|
| 171 |
+
| `gemini-3-flash-preview` | Flash 3 预览版 | ❌ |
|
| 172 |
+
| `gemini-3-pro-preview` | Pro 3 预览版 | ✅ |
|
| 173 |
+
|
| 174 |
+
### 基本对话
|
| 175 |
+
|
| 176 |
+
```bash
|
| 177 |
+
curl -X POST http://localhost:7860/v1/v1/chat/completions \
|
| 178 |
+
-H "Content-Type: application/json" \
|
| 179 |
+
-H "Authorization: Bearer your_api_key" \
|
| 180 |
+
-d '{
|
| 181 |
+
"model": "gemini-2.5-flash",
|
| 182 |
+
"messages": [
|
| 183 |
+
{"role": "user", "content": "Hello!"}
|
| 184 |
+
],
|
| 185 |
+
"stream": true
|
| 186 |
+
}'
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
### 图片输入(多模态)
|
| 190 |
+
|
| 191 |
+
```bash
|
| 192 |
+
curl -X POST http://localhost:7860/v1/v1/chat/completions \
|
| 193 |
+
-H "Content-Type: application/json" \
|
| 194 |
+
-H "Authorization: Bearer your_api_key" \
|
| 195 |
+
-d '{
|
| 196 |
+
"model": "gemini-2.5-pro",
|
| 197 |
+
"messages": [
|
| 198 |
+
{
|
| 199 |
+
"role": "user",
|
| 200 |
+
"content": [
|
| 201 |
+
{"type": "text", "text": "这张图片里有什么?"},
|
| 202 |
+
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,<base64_encoded_image>"}}
|
| 203 |
+
]
|
| 204 |
+
}
|
| 205 |
+
]
|
| 206 |
+
}'
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
### 图片生成
|
| 210 |
+
|
| 211 |
+
```bash
|
| 212 |
+
curl -X POST http://localhost:7860/v1/v1/chat/completions \
|
| 213 |
+
-H "Content-Type: application/json" \
|
| 214 |
+
-H "Authorization: Bearer your_api_key" \
|
| 215 |
+
-d '{
|
| 216 |
+
"model": "gemini-3-pro-preview",
|
| 217 |
+
"messages": [
|
| 218 |
+
{"role": "user", "content": "画一只可爱的猫咪"}
|
| 219 |
+
]
|
| 220 |
+
}'
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
### 图生图(Image-to-Image)
|
| 224 |
+
|
| 225 |
+
```bash
|
| 226 |
+
curl -X POST http://localhost:7860/v1/v1/chat/completions \
|
| 227 |
+
-H "Content-Type: application/json" \
|
| 228 |
+
-H "Authorization: Bearer your_api_key" \
|
| 229 |
+
-d '{
|
| 230 |
+
"model": "gemini-3-pro-preview",
|
| 231 |
+
"messages": [
|
| 232 |
+
{
|
| 233 |
+
"role": "user",
|
| 234 |
+
"content": [
|
| 235 |
+
{"type": "text", "text": "将这张图片改成水彩画风格"},
|
| 236 |
+
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,<base64_encoded_image>"}}
|
| 237 |
+
]
|
| 238 |
+
}
|
| 239 |
+
]
|
| 240 |
+
}'
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
### 其他端点
|
| 244 |
+
|
| 245 |
+
| 端点 | 方法 | 说明 |
|
| 246 |
+
| -------------------------- | ---- | --------------------- |
|
| 247 |
+
| `/{PATH_PREFIX}/v1/models` | GET | 获取模型列表 |
|
| 248 |
+
| `/{PATH_PREFIX}/admin` | GET | 管理面板(需ADMIN_KEY) |
|
| 249 |
+
| `/public/log/html` | GET | 公开日志页面 |
|
| 250 |
+
| `/health` | GET | 健康检查 |
|
| 251 |
+
|
| 252 |
+
## ❓ 常见问题
|
| 253 |
+
|
| 254 |
+
### 1. 图片生成后在哪里找到文件?
|
| 255 |
+
|
| 256 |
+
- **临时存储**: 图片保存在 `./images/`,可通过 URL 访问
|
| 257 |
+
- **重启后会丢失**,建议使用持久化存储
|
| 258 |
+
|
| 259 |
+
### 2. 如何设置 BASE_URL?
|
| 260 |
+
|
| 261 |
+
**自动检测**(推荐):
|
| 262 |
+
- 不设置 `BASE_URL` 环境变量
|
| 263 |
+
- 系统自动从请求头检测域名
|
| 264 |
+
|
| 265 |
+
**手动设置**:
|
| 266 |
+
```bash
|
| 267 |
+
BASE_URL=https://your-domain.com
|
| 268 |
+
```
|
| 269 |
+
|
| 270 |
+
**使用反向代理**:
|
| 271 |
+
|
| 272 |
+
如果你使用自己的域名反向代理到 HuggingFace Space,可以通过以下方式配置:
|
| 273 |
+
|
| 274 |
+
**Nginx 配置示例**:
|
| 275 |
+
```nginx
|
| 276 |
+
location / {
|
| 277 |
+
proxy_pass https://your-username-space-name.hf.space;
|
| 278 |
+
proxy_set_header Host your-username-space-name.hf.space;
|
| 279 |
+
proxy_ssl_server_name on;
|
| 280 |
+
}
|
| 281 |
+
```
|
| 282 |
+
|
| 283 |
+
**Deno Deploy 配置示例**:
|
| 284 |
+
```typescript
|
| 285 |
+
async function handler(request: Request): Promise<Response> {
|
| 286 |
+
const url = new URL(request.url);
|
| 287 |
+
url.host = 'your-username-space-name.hf.space';
|
| 288 |
+
return fetch(new Request(url, request));
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
Deno.serve(handler);
|
| 292 |
+
```
|
| 293 |
+
|
| 294 |
+
配置反向代理后,将 `BASE_URL` 设置为你的自定义域名即可。
|
| 295 |
+
|
| 296 |
+
### 3. API_KEY 和 ADMIN_KEY 的区别?
|
| 297 |
+
|
| 298 |
+
- **API_KEY**: 保护聊天接口 (`/v1/chat/completions`)
|
| 299 |
+
- **ADMIN_KEY**: 保护管理面板 (`/admin`)
|
| 300 |
+
|
| 301 |
+
可以设置相同的值,也可以分开
|
| 302 |
+
|
| 303 |
+
### 4. 如何查看日志?
|
| 304 |
+
|
| 305 |
+
- **公开日志**: 访问 `/public/log/html` (无需密钥)
|
| 306 |
+
- **管理面板**: 访问 `/admin?key=YOUR_ADMIN_KEY`
|
| 307 |
+
|
| 308 |
+
## 📁 项目结构
|
| 309 |
+
|
| 310 |
+
```
|
| 311 |
+
gemini-business2api/
|
| 312 |
+
├── main.py # 主程序
|
| 313 |
+
├── util/ # 工具模块
|
| 314 |
+
│ └── streaming_parser.py
|
| 315 |
+
├── requirements.txt # Python依赖
|
| 316 |
+
├── Dockerfile # Docker构建
|
| 317 |
+
├── README.md # 项目文档
|
| 318 |
+
├── .env.example # 环境变量配置示例
|
| 319 |
+
├── accounts_config.example.json # 多账户配置示例
|
| 320 |
+
└── .gitignore # Git忽略文件
|
| 321 |
+
```
|
| 322 |
+
|
| 323 |
+
**运行时生成的目录**:
|
| 324 |
+
- `images/` - 生成的图片存储
|
| 325 |
+
- `static/` - 静态文件缓存
|
| 326 |
+
- `logs/` - 日志文件
|
| 327 |
+
|
| 328 |
+
## 🛠️ 技术栈
|
| 329 |
+
|
| 330 |
+
- **Python 3.11+**
|
| 331 |
+
- **FastAPI** - 现代Web框架
|
| 332 |
+
- **Uvicorn** - ASGI服务器
|
| 333 |
+
- **httpx** - HTTP客户端
|
| 334 |
+
- **Docker** - 容器化部署
|
| 335 |
+
|
| 336 |
+
## 📝 License
|
| 337 |
+
|
| 338 |
+
MIT License - 查看 [LICENSE](LICENSE) 文件了解详情
|
| 339 |
+
|
| 340 |
+
---
|
| 341 |
+
|
| 342 |
+
**如果这个项目对你有帮助,请给个 ⭐ Star!**
|
accounts_config.example.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"id": "account_1",
|
| 4 |
+
"csesidx": "498...",
|
| 5 |
+
"config_id": "0cd...",
|
| 6 |
+
"secure_c_ses": "CSE.Ad...",
|
| 7 |
+
"host_c_oses": "COS.Af...",
|
| 8 |
+
"expires_at": "2025-12-23 23:03:20"
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"id": "account_2",
|
| 12 |
+
"csesidx": "208...",
|
| 13 |
+
"config_id": "782...",
|
| 14 |
+
"secure_c_ses": "CSE.Ad...",
|
| 15 |
+
"host_c_oses": "COS.Af...",
|
| 16 |
+
"expires_at": "2025-12-23 23:04:02"
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"id": "account_3",
|
| 20 |
+
"csesidx": "789...",
|
| 21 |
+
"config_id": "92e...",
|
| 22 |
+
"secure_c_ses": "CSE.Ad...",
|
| 23 |
+
"host_c_oses": "COS.Af...",
|
| 24 |
+
"expires_at": "2025-12-23 23:04:36"
|
| 25 |
+
}
|
| 26 |
+
]
|
main.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.110.0
|
| 2 |
+
uvicorn[standard]==0.29.0
|
| 3 |
+
httpx==0.27.0
|
| 4 |
+
pydantic==2.7.0
|