jackkuo Cursor commited on
Commit
b79645f
·
1 Parent(s): 8b94566

Add demo seed data for Hugging Face Space.

Browse files

Store sample data as seed/demo.sql (text) and import on first startup, avoiding HF binary file restrictions while keeping the public demo populated.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (5) hide show
  1. .dockerignore +1 -0
  2. .gitignore +2 -1
  3. Dockerfile +1 -0
  4. scripts/space_init.py +36 -1
  5. seed/demo.sql +0 -0
.dockerignore CHANGED
@@ -7,4 +7,5 @@ __pycache__
7
  .mypy_cache
8
  data/
9
  *.db
 
10
  .pre-commit-config.yaml
 
7
  .mypy_cache
8
  data/
9
  *.db
10
+ seed/*.db
11
  .pre-commit-config.yaml
.gitignore CHANGED
@@ -18,8 +18,9 @@ wheels/
18
  # 本地数据库目录
19
  data/
20
 
21
- # 数据库文件
22
  *.db
 
23
 
24
  # 环境变量
25
  .env
 
18
  # 本地数据库目录
19
  data/
20
 
21
+ # 数据库文件(Demo 使用 seed/demo.sql 文本导入)
22
  *.db
23
+ seed/*.db
24
 
25
  # 环境变量
26
  .env
Dockerfile CHANGED
@@ -7,6 +7,7 @@ RUN mkdir -p /data
7
  COPY pyproject.toml README.md ./
8
  COPY qa_annotate/ qa_annotate/
9
  COPY scripts/ scripts/
 
10
 
11
  RUN pip install --no-cache-dir .
12
 
 
7
  COPY pyproject.toml README.md ./
8
  COPY qa_annotate/ qa_annotate/
9
  COPY scripts/ scripts/
10
+ COPY seed/demo.sql seed/demo.sql
11
 
12
  RUN pip install --no-cache-dir .
13
 
scripts/space_init.py CHANGED
@@ -1,19 +1,53 @@
1
  #!/usr/bin/env python
2
- """Hugging Face Space 启动初始化:建表并可选创建管理员账号。"""
3
 
4
  import os
 
5
  import sys
6
  from pathlib import Path
7
 
8
  project_root = Path(__file__).parent.parent
9
  sys.path.insert(0, str(project_root))
10
 
 
11
  from qa_annotate.database.base import SessionLocal, init_db # noqa: E402
12
  from qa_annotate.database.crud import UserCRUD # noqa: E402
13
  from qa_annotate.schema.user import UserCreate, UserUpdate # noqa: E402
14
  from qa_annotate.utils.password import hash_password # noqa: E402
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def ensure_superuser() -> None:
18
  username = os.environ.get("ADMIN_USERNAME", "admin").strip()
19
  password = os.environ.get("ADMIN_PASSWORD", "123456")
@@ -49,6 +83,7 @@ def ensure_superuser() -> None:
49
 
50
 
51
  def main() -> None:
 
52
  init_db()
53
  ensure_superuser()
54
 
 
1
  #!/usr/bin/env python
2
+ """Hugging Face Space 启动初始化:导入 Demo 数据、建表并创建管理员账号。"""
3
 
4
  import os
5
+ import sqlite3
6
  import sys
7
  from pathlib import Path
8
 
9
  project_root = Path(__file__).parent.parent
10
  sys.path.insert(0, str(project_root))
11
 
12
+ from qa_annotate.config import settings # noqa: E402
13
  from qa_annotate.database.base import SessionLocal, init_db # noqa: E402
14
  from qa_annotate.database.crud import UserCRUD # noqa: E402
15
  from qa_annotate.schema.user import UserCreate, UserUpdate # noqa: E402
16
  from qa_annotate.utils.password import hash_password # noqa: E402
17
 
18
 
19
+ def _project_count(db_path: Path) -> int:
20
+ if not db_path.exists():
21
+ return 0
22
+ with sqlite3.connect(db_path) as conn:
23
+ try:
24
+ return conn.execute("SELECT COUNT(*) FROM projects").fetchone()[0]
25
+ except sqlite3.OperationalError:
26
+ return 0
27
+
28
+
29
+ def seed_demo_database() -> None:
30
+ if os.environ.get("SEED_DEMO_DATA", "true").lower() not in ("1", "true", "yes"):
31
+ print("SEED_DEMO_DATA 已关闭,跳过示例数据导入")
32
+ return
33
+
34
+ seed_path = project_root / "seed" / "demo.sql"
35
+ if not seed_path.exists():
36
+ print("未找到 seed/demo.sql,跳过示例数据导入")
37
+ return
38
+
39
+ db_path = settings.db_path
40
+ db_path.parent.mkdir(parents=True, exist_ok=True)
41
+ if _project_count(db_path) > 0:
42
+ print("数据库已有项目数据,跳过示例数据导入")
43
+ return
44
+
45
+ sql = seed_path.read_text(encoding="utf-8")
46
+ with sqlite3.connect(db_path) as conn:
47
+ conn.executescript(sql)
48
+ print(f"已从 seed/demo.sql 导入 Demo 示例数据: {db_path}")
49
+
50
+
51
  def ensure_superuser() -> None:
52
  username = os.environ.get("ADMIN_USERNAME", "admin").strip()
53
  password = os.environ.get("ADMIN_PASSWORD", "123456")
 
83
 
84
 
85
  def main() -> None:
86
+ seed_demo_database()
87
  init_db()
88
  ensure_superuser()
89
 
seed/demo.sql ADDED
The diff for this file is too large to render. See raw diff