Upload 2 files
Browse files- prepare_fulltext_index.py +1 -13
- start.sh +5 -3
prepare_fulltext_index.py
CHANGED
|
@@ -3,7 +3,6 @@
|
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|
| 6 |
-
import hashlib
|
| 7 |
import json
|
| 8 |
import os
|
| 9 |
import sqlite3
|
|
@@ -17,14 +16,6 @@ TOKENIZER_VERSION = "cjk-bigram-boundary-fts5-v5"
|
|
| 17 |
SOURCES = ("CCRD", "CW")
|
| 18 |
|
| 19 |
|
| 20 |
-
def sha256(path: Path) -> str:
|
| 21 |
-
digest = hashlib.sha256()
|
| 22 |
-
with path.open("rb") as handle:
|
| 23 |
-
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
|
| 24 |
-
digest.update(chunk)
|
| 25 |
-
return digest.hexdigest()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
def validated_database(bucket: Path, source: str, details: object) -> Path:
|
| 29 |
if not isinstance(details, dict):
|
| 30 |
raise ValueError(f"{source}: missing manifest details")
|
|
@@ -42,16 +33,13 @@ def validated_database(bucket: Path, source: str, details: object) -> Path:
|
|
| 42 |
path = (bucket / relative_path).resolve()
|
| 43 |
if bucket not in path.parents or not path.is_file():
|
| 44 |
raise ValueError(f"{source}: database is outside the bucket or missing")
|
| 45 |
-
if sha256(path) != expected_hash:
|
| 46 |
-
raise ValueError(f"{source}: database hash mismatch")
|
| 47 |
with sqlite3.connect(f"file:{path}?mode=ro&immutable=1", uri=True) as connection:
|
| 48 |
tokenizer = connection.execute("SELECT value FROM metadata WHERE key = 'tokenizer_version'").fetchone()
|
| 49 |
documents = int(connection.execute("SELECT COUNT(*) FROM documents").fetchone()[0])
|
| 50 |
fts_rows = int(connection.execute("SELECT COUNT(*) FROM content_fts").fetchone()[0])
|
| 51 |
-
integrity = connection.execute("PRAGMA integrity_check").fetchone()[0]
|
| 52 |
if not tokenizer or tokenizer[0] != TOKENIZER_VERSION:
|
| 53 |
raise ValueError(f"{source}: tokenizer version mismatch")
|
| 54 |
-
if documents != expected_documents or fts_rows != expected_fts_rows
|
| 55 |
raise ValueError(f"{source}: database structure mismatch")
|
| 56 |
return path
|
| 57 |
|
|
|
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|
|
|
|
| 6 |
import json
|
| 7 |
import os
|
| 8 |
import sqlite3
|
|
|
|
| 16 |
SOURCES = ("CCRD", "CW")
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def validated_database(bucket: Path, source: str, details: object) -> Path:
|
| 20 |
if not isinstance(details, dict):
|
| 21 |
raise ValueError(f"{source}: missing manifest details")
|
|
|
|
| 33 |
path = (bucket / relative_path).resolve()
|
| 34 |
if bucket not in path.parents or not path.is_file():
|
| 35 |
raise ValueError(f"{source}: database is outside the bucket or missing")
|
|
|
|
|
|
|
| 36 |
with sqlite3.connect(f"file:{path}?mode=ro&immutable=1", uri=True) as connection:
|
| 37 |
tokenizer = connection.execute("SELECT value FROM metadata WHERE key = 'tokenizer_version'").fetchone()
|
| 38 |
documents = int(connection.execute("SELECT COUNT(*) FROM documents").fetchone()[0])
|
| 39 |
fts_rows = int(connection.execute("SELECT COUNT(*) FROM content_fts").fetchone()[0])
|
|
|
|
| 40 |
if not tokenizer or tokenizer[0] != TOKENIZER_VERSION:
|
| 41 |
raise ValueError(f"{source}: tokenizer version mismatch")
|
| 42 |
+
if documents != expected_documents or fts_rows != expected_fts_rows:
|
| 43 |
raise ValueError(f"{source}: database structure mismatch")
|
| 44 |
return path
|
| 45 |
|
start.sh
CHANGED
|
@@ -2,8 +2,10 @@
|
|
| 2 |
set -eu
|
| 3 |
|
| 4 |
mkdir -p /app/data/fulltext
|
| 5 |
-
|
| 6 |
-
python /app/
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
exec uvicorn app:app --host 0.0.0.0 --port 7860 --log-level info
|
|
|
|
| 2 |
set -eu
|
| 3 |
|
| 4 |
mkdir -p /app/data/fulltext
|
| 5 |
+
(
|
| 6 |
+
if ! python /app/prepare_fulltext_index.py; then
|
| 7 |
+
python /app/build_fulltext_db.py
|
| 8 |
+
fi
|
| 9 |
+
) &
|
| 10 |
|
| 11 |
exec uvicorn app:app --host 0.0.0.0 --port 7860 --log-level info
|