Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
6bf8b51
0
Parent(s):
Deploy from GitHub Actions 863023faed1504d258dc4bbe3153d7092146e495
Browse files- .env.example +49 -0
- Dockerfile +44 -0
- README.md +36 -0
- start.sh +34 -0
.env.example
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Grok2API environment example
|
| 2 |
+
#
|
| 3 |
+
# 用法:
|
| 4 |
+
# 1. 复制为 .env 后按需修改
|
| 5 |
+
# 2. 启动期 / 部署期变量放这里
|
| 6 |
+
# 3. 常规运行时配置优先改 ${DATA_DIR}/config.toml
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# ==================== 基础运行 ====================
|
| 10 |
+
TZ=Asia/Shanghai
|
| 11 |
+
LOG_LEVEL=INFO
|
| 12 |
+
# 写入本地文件日志
|
| 13 |
+
LOG_FILE_ENABLED=true
|
| 14 |
+
# 账号目录增量同步间隔(秒)
|
| 15 |
+
ACCOUNT_SYNC_INTERVAL=30
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# ==================== Web 服务 / Docker Compose ====================
|
| 19 |
+
SERVER_HOST=0.0.0.0
|
| 20 |
+
SERVER_PORT=8000
|
| 21 |
+
SERVER_WORKERS=1
|
| 22 |
+
|
| 23 |
+
# Docker Compose 宿主机映射端口
|
| 24 |
+
HOST_PORT=8000
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# ==================== 账号存储(启动期) ====================
|
| 28 |
+
# 可选:local | redis | mysql | postgresql
|
| 29 |
+
ACCOUNT_STORAGE=local
|
| 30 |
+
|
| 31 |
+
# redis: Redis DSN(ACCOUNT_STORAGE=redis 时必填)
|
| 32 |
+
# ACCOUNT_REDIS_URL=redis://:password@host:6379/0
|
| 33 |
+
|
| 34 |
+
# mysql: SQLAlchemy DSN
|
| 35 |
+
# ACCOUNT_MYSQL_URL=mysql+aiomysql://user:password@127.0.0.1:3306/grok2api
|
| 36 |
+
|
| 37 |
+
# postgresql: SQLAlchemy DSN
|
| 38 |
+
# ACCOUNT_POSTGRESQL_URL=postgresql+asyncpg://user:password@127.0.0.1:5432/grok2api
|
| 39 |
+
|
| 40 |
+
# SQL 连接池(mysql / postgresql 模式,以下均为默认值,无需改动即可生效)
|
| 41 |
+
# ACCOUNT_SQL_POOL_SIZE=5
|
| 42 |
+
# ACCOUNT_SQL_MAX_OVERFLOW=10
|
| 43 |
+
# ACCOUNT_SQL_POOL_TIMEOUT=30
|
| 44 |
+
# ACCOUNT_SQL_POOL_RECYCLE=1800
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ==================== 可选:本地数据 / 日志目录 ====================
|
| 48 |
+
DATA_DIR=./data
|
| 49 |
+
LOG_DIR=./logs
|
Dockerfile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.13-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
+
PYTHONUNBUFFERED=1 \
|
| 5 |
+
TZ=Asia/Shanghai \
|
| 6 |
+
VIRTUAL_ENV=/app/.venv \
|
| 7 |
+
PATH=/app/.venv/bin:/root/.local/bin:$PATH \
|
| 8 |
+
SERVER_HOST=0.0.0.0 \
|
| 9 |
+
SERVER_PORT=7860 \
|
| 10 |
+
SERVER_WORKERS=1 \
|
| 11 |
+
DATA_DIR=/data \
|
| 12 |
+
LOG_DIR=/data/logs
|
| 13 |
+
|
| 14 |
+
WORKDIR /app
|
| 15 |
+
|
| 16 |
+
EXPOSE 7860
|
| 17 |
+
|
| 18 |
+
RUN apt-get update \
|
| 19 |
+
&& apt-get install -y --no-install-recommends \
|
| 20 |
+
build-essential \
|
| 21 |
+
curl \
|
| 22 |
+
ca-certificates \
|
| 23 |
+
git \
|
| 24 |
+
libffi-dev \
|
| 25 |
+
libssl-dev \
|
| 26 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 27 |
+
|
| 28 |
+
COPY --from=ghcr.io/astral-sh/uv:0.6.17 /uv /uvx /bin/
|
| 29 |
+
|
| 30 |
+
RUN git clone --depth 1 https://github.com/chenyme/grok2api.git /tmp/src \
|
| 31 |
+
&& cp /tmp/src/pyproject.toml /app/pyproject.toml \
|
| 32 |
+
&& cp /tmp/src/uv.lock /app/uv.lock \
|
| 33 |
+
&& uv sync --frozen --no-dev --no-install-project \
|
| 34 |
+
&& cp -r /tmp/src/app /app/app \
|
| 35 |
+
&& cp /tmp/src/config.defaults.toml /app/config.defaults.toml \
|
| 36 |
+
&& rm -rf /tmp/src
|
| 37 |
+
|
| 38 |
+
COPY start.sh /app/start.sh
|
| 39 |
+
|
| 40 |
+
RUN chmod +x /app/start.sh \
|
| 41 |
+
&& mkdir -p /data /data/logs
|
| 42 |
+
|
| 43 |
+
ENTRYPOINT ["/app/start.sh"]
|
| 44 |
+
CMD ["granian", "--interface", "asgi", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "app.main:app"]
|
README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Grok2Api
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Grok2Api on Hugging Face Spaces
|
| 12 |
+
|
| 13 |
+
This Space runs the Grok2Api FastAPI service in Docker mode.
|
| 14 |
+
|
| 15 |
+
## Runtime Notes
|
| 16 |
+
|
| 17 |
+
- Persistent data is stored in the mounted bucket at `/data`
|
| 18 |
+
- Runtime environment file: `/data/.env`
|
| 19 |
+
- Runtime config file: `/data/config.toml`
|
| 20 |
+
- Logs directory: `/data/logs`
|
| 21 |
+
- Service port: `7860`
|
| 22 |
+
|
| 23 |
+
## Runtime Variables
|
| 24 |
+
|
| 25 |
+
Runtime variables are expected to come from `/data/.env` in the mounted bucket.
|
| 26 |
+
|
| 27 |
+
Optional HF Space variables only used as fallback:
|
| 28 |
+
|
| 29 |
+
- `SPACE_HOST`
|
| 30 |
+
- `SPACE_ID`
|
| 31 |
+
|
| 32 |
+
## Build Mode
|
| 33 |
+
|
| 34 |
+
This deployment does not pull a prebuilt image.
|
| 35 |
+
|
| 36 |
+
The GitHub workflow only syncs the local `huggingface` directory to the Hugging Face Space repository root. During Docker build, the Space clones the latest upstream source from `https://github.com/chenyme/grok2api` and builds from that source.
|
start.sh
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env sh
|
| 2 |
+
set -eu
|
| 3 |
+
|
| 4 |
+
DATA_DIR="${DATA_DIR:-/data}"
|
| 5 |
+
LOG_DIR="${LOG_DIR:-/data/logs}"
|
| 6 |
+
ENV_FILE="$DATA_DIR/.env"
|
| 7 |
+
CONFIG_FILE="$DATA_DIR/config.toml"
|
| 8 |
+
DEFAULT_CONFIG="/app/config.defaults.toml"
|
| 9 |
+
|
| 10 |
+
mkdir -p "$DATA_DIR" "$LOG_DIR"
|
| 11 |
+
|
| 12 |
+
if [ -f "$ENV_FILE" ]; then
|
| 13 |
+
set -a
|
| 14 |
+
. "$ENV_FILE"
|
| 15 |
+
set +a
|
| 16 |
+
fi
|
| 17 |
+
|
| 18 |
+
if [ ! -f "$CONFIG_FILE" ]; then
|
| 19 |
+
cp "$DEFAULT_CONFIG" "$CONFIG_FILE"
|
| 20 |
+
echo "Initialized runtime config at $CONFIG_FILE"
|
| 21 |
+
fi
|
| 22 |
+
|
| 23 |
+
SPACE_HOST="${SPACE_HOST:-}"
|
| 24 |
+
SPACE_ID="${SPACE_ID:-}"
|
| 25 |
+
|
| 26 |
+
if [ -n "$SPACE_HOST" ]; then
|
| 27 |
+
sed -i "s|^app_url = .*|app_url = \"https://$SPACE_HOST\"|" "$CONFIG_FILE"
|
| 28 |
+
elif [ -n "$SPACE_ID" ]; then
|
| 29 |
+
sed -i "s|^app_url = .*|app_url = \"https://${SPACE_ID}.hf.space\"|" "$CONFIG_FILE"
|
| 30 |
+
fi
|
| 31 |
+
|
| 32 |
+
chmod 600 "$CONFIG_FILE" || true
|
| 33 |
+
|
| 34 |
+
exec "$@"
|