feat(ebook): migrate to async DB/HTTP and parallelize phrase pipeline
Convert the ebook-search web app to async end to end and add concurrency to the protected-phrase extraction and judging pipeline so large books no longer block the event loop or the UI. ORM / infra: - Add get_async_postgres_engine and factor shared URL/connect_args building into build_postgres_url (reused by the sync and async engine builders) - Add async FastAPI session helpers (get_async_db, AsyncDbSession) with expire_on_commit=False to avoid implicit IO under asyncio App: - Use AsyncEngine/AsyncSession throughout routes, search, ingest, embeddings, answer, rerank and LLM calls; convert handlers to async - Share a single httpx.AsyncClient in app state for LLM requests; size the connection pool for concurrent phrase-judging workers - Add judge_tasks: run per-book judging as tracked background tasks so a book already being judged isn't double-queued Protected phrases: - Add a process pool (pool.py) and worker-count config (extraction/judge book/phrase workers) to parallelize candidate generation and judging - Split admin actions into all/missing variants for generation and judging Config: - Add protected_phrase_extraction_workers, phrase_judge_book_workers, phrase_judge_phrase_workers
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
"""Background BM25 refresh tasks for the web app."""
|
||||
"""Background BM25 refresh tasks for the web app.
|
||||
|
||||
The refresh is scheduled on the event loop instead of a thread because the async psycopg
|
||||
driver only works from the loop; a bare thread cannot open a session on the async engine.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from threading import Timer
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from python.ebook_search.bm25_corpus import load_bm25_corpus, refresh_bm25_corpus
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
|
||||
from python.ebook_search.config import EbookSearchConfig
|
||||
|
||||
@@ -20,15 +24,18 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def schedule_bm25_refresh(app: FastAPI) -> None:
|
||||
"""Schedule a delayed BM25 corpus refresh, replacing any pending refresh."""
|
||||
existing_timer = getattr(app.state, "bm25_refresh_timer", None)
|
||||
if existing_timer is not None:
|
||||
existing_timer.cancel()
|
||||
"""Schedule a delayed BM25 corpus refresh, replacing any pending refresh.
|
||||
|
||||
timer = Timer(app.state.config.bm25_refresh_delay_seconds, refresh_bm25_for_app, args=(app,))
|
||||
timer.daemon = True
|
||||
timer.start()
|
||||
app.state.bm25_refresh_timer = timer
|
||||
Only called from route handlers, so a running event loop is guaranteed.
|
||||
"""
|
||||
cancel_bm25_refresh(app)
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
def start_refresh() -> None:
|
||||
app.state.bm25_refresh_task = loop.create_task(refresh_bm25_for_app(app))
|
||||
|
||||
app.state.bm25_refresh_timer = loop.call_later(app.state.config.bm25_refresh_delay_seconds, start_refresh)
|
||||
logger.info(
|
||||
"ebook_bm25_refresh_scheduled delay_seconds=%s",
|
||||
app.state.config.bm25_refresh_delay_seconds,
|
||||
@@ -36,25 +43,31 @@ def schedule_bm25_refresh(app: FastAPI) -> None:
|
||||
|
||||
|
||||
def cancel_bm25_refresh(app: FastAPI) -> None:
|
||||
"""Cancel any pending BM25 corpus refresh."""
|
||||
"""Cancel any pending BM25 corpus refresh timer and in-flight refresh task."""
|
||||
existing_timer = getattr(app.state, "bm25_refresh_timer", None)
|
||||
if existing_timer is not None:
|
||||
existing_timer.cancel()
|
||||
app.state.bm25_refresh_timer = None
|
||||
logger.info("ebook_bm25_refresh_cancelled")
|
||||
|
||||
existing_task = getattr(app.state, "bm25_refresh_task", None)
|
||||
if existing_task is not None:
|
||||
if not existing_task.done():
|
||||
existing_task.cancel()
|
||||
app.state.bm25_refresh_task = None
|
||||
|
||||
def refresh_bm25_for_app(app: FastAPI) -> None:
|
||||
|
||||
async def refresh_bm25_for_app(app: FastAPI) -> None:
|
||||
"""Refresh the BM25 corpus using the app engine and config."""
|
||||
try:
|
||||
refresh_bm25_for_engine(app.state.engine, app.state.config)
|
||||
await refresh_bm25_for_engine(app.state.engine, app.state.config)
|
||||
except Exception:
|
||||
logger.exception("ebook_bm25_refresh_failed")
|
||||
|
||||
|
||||
def refresh_bm25_for_engine(engine: Engine, config: EbookSearchConfig) -> None:
|
||||
"""Refresh the BM25 corpus using a SQLAlchemy engine."""
|
||||
with Session(engine) as session:
|
||||
refresh_bm25_corpus(session, config)
|
||||
async def refresh_bm25_for_engine(engine: AsyncEngine, config: EbookSearchConfig) -> None:
|
||||
"""Refresh the BM25 corpus using an async SQLAlchemy engine."""
|
||||
async with AsyncSession(engine) as session:
|
||||
await refresh_bm25_corpus(session, config)
|
||||
load_bm25_corpus.cache_clear()
|
||||
logger.info("ebook_bm25_corpus_cache_cleared_after_refresh")
|
||||
|
||||
@@ -4,8 +4,9 @@ from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
import httpx
|
||||
from fastapi import Depends, Request
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
|
||||
from python.ebook_search.config import EbookSearchConfig
|
||||
|
||||
@@ -15,10 +16,16 @@ def get_config(request: Request) -> EbookSearchConfig:
|
||||
return request.app.state.config
|
||||
|
||||
|
||||
def get_engine(request: Request) -> Engine:
|
||||
def get_engine(request: Request) -> AsyncEngine:
|
||||
"""Get the database engine from app state."""
|
||||
return request.app.state.engine
|
||||
|
||||
|
||||
def get_http_client(request: Request) -> httpx.AsyncClient:
|
||||
"""Get the shared LLM HTTP client from app state."""
|
||||
return request.app.state.http_client
|
||||
|
||||
|
||||
AppConfig = Annotated[EbookSearchConfig, Depends(get_config)]
|
||||
AppEngine = Annotated[Engine, Depends(get_engine)]
|
||||
AppEngine = Annotated[AsyncEngine, Depends(get_engine)]
|
||||
AppHttpClient = Annotated[httpx.AsyncClient, Depends(get_http_client)]
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
"""Background phrase-judging tasks for the web app.
|
||||
|
||||
Judging a book sends one LLM request per candidate phrase, which can take minutes, so it must
|
||||
not run inside the request where it would block the UI. Judgments run as async FastAPI
|
||||
background tasks, awaited on the event loop after the response is sent, and are tracked per
|
||||
book in app state so a second judge request for a book that is already being judged is
|
||||
rejected instead of doubling the work.
|
||||
|
||||
State is loop-confined: every read and mutation happens on the event loop (async route
|
||||
handlers and async background tasks) and no critical section contains an ``await``, so each
|
||||
mutation is atomic per loop iteration and no locking is needed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from python.ebook_search.protected_phrases.judge_ngrams import judge_candidate_phrases_for_books
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import BackgroundTasks, FastAPI
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class JudgeTaskState:
|
||||
"""Running book judgments and last outcome messages, keyed by book id."""
|
||||
|
||||
running_book_ids: set[int] = field(default_factory=set)
|
||||
outcome_messages: dict[int, str] = field(default_factory=dict)
|
||||
|
||||
|
||||
def get_judge_task_state(app: FastAPI) -> JudgeTaskState:
|
||||
"""Return the app's judge task state, creating it on first use.
|
||||
|
||||
Args:
|
||||
app (FastAPI): App whose state holds the judge task registry.
|
||||
|
||||
Returns:
|
||||
JudgeTaskState: The shared judge task state for this app.
|
||||
"""
|
||||
state = getattr(app.state, "judge_tasks", None)
|
||||
if state is None:
|
||||
state = JudgeTaskState()
|
||||
app.state.judge_tasks = state
|
||||
return state
|
||||
|
||||
|
||||
def start_book_phrase_judgment(app: FastAPI, background_tasks: BackgroundTasks, source_id: int) -> bool:
|
||||
"""Queue judging of one book's candidate phrases as a FastAPI background task.
|
||||
|
||||
The book is claimed before the response returns, so a repeated judge request cannot queue
|
||||
a second run while one is pending or running.
|
||||
|
||||
Args:
|
||||
app (FastAPI): App supplying the engine, config, and judge task state.
|
||||
background_tasks (BackgroundTasks): Request's background tasks to queue the judgment on.
|
||||
source_id (int): Book to judge candidates for.
|
||||
|
||||
Returns:
|
||||
bool: True when a judgment was queued, False when one is already running for this book.
|
||||
"""
|
||||
state = get_judge_task_state(app)
|
||||
if source_id in state.running_book_ids:
|
||||
logger.info("ebook_book_phrase_judgment_already_running source_id=%s", source_id)
|
||||
return False
|
||||
state.running_book_ids.add(source_id)
|
||||
state.outcome_messages.pop(source_id, None)
|
||||
background_tasks.add_task(judge_book_phrases_for_app, app, source_id)
|
||||
logger.info("ebook_book_phrase_judgment_queued source_id=%s", source_id)
|
||||
return True
|
||||
|
||||
|
||||
async def judge_book_phrases_for_app(app: FastAPI, source_id: int) -> None:
|
||||
"""Judge one book using the app engine and config, recording the outcome message.
|
||||
|
||||
Args:
|
||||
app (FastAPI): App supplying the engine, config, and judge task state.
|
||||
source_id (int): Book to judge candidates for.
|
||||
"""
|
||||
state = get_judge_task_state(app)
|
||||
try:
|
||||
result = await judge_candidate_phrases_for_books(app.state.engine, app.state.config, source_ids=[source_id])
|
||||
logger.info(
|
||||
"ebook_book_phrase_judgment_complete source_id=%s judged=%s protected=%s mentions=%s failed=%s",
|
||||
source_id,
|
||||
result.candidates_judged,
|
||||
result.protected_phrases,
|
||||
result.phrase_mentions,
|
||||
result.books_failed,
|
||||
)
|
||||
if result.books_failed:
|
||||
message = "Judging failed; see server logs for details"
|
||||
else:
|
||||
message = (
|
||||
f"Judged {result.candidates_judged} candidates; {result.protected_phrases} protected phrases promoted"
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("ebook_book_phrase_judgment_task_failed source_id=%s", source_id)
|
||||
message = "Judging failed; see server logs for details"
|
||||
state.running_book_ids.discard(source_id)
|
||||
state.outcome_messages[source_id] = message
|
||||
|
||||
|
||||
def is_judging_book(app: FastAPI, source_id: int) -> bool:
|
||||
"""Report whether a judgment is currently queued or running for one book.
|
||||
|
||||
Args:
|
||||
app (FastAPI): App supplying the judge task state.
|
||||
source_id (int): Book to check.
|
||||
|
||||
Returns:
|
||||
bool: True while the book's judgment is pending or running.
|
||||
"""
|
||||
return source_id in get_judge_task_state(app).running_book_ids
|
||||
|
||||
|
||||
def pop_book_judgment_outcome(app: FastAPI, source_id: int) -> str | None:
|
||||
"""Return and clear the outcome message from one book's last finished judgment.
|
||||
|
||||
Args:
|
||||
app (FastAPI): App supplying the judge task state.
|
||||
source_id (int): Book to fetch the outcome for.
|
||||
|
||||
Returns:
|
||||
str | None: The outcome message, or None when there is nothing new to report.
|
||||
"""
|
||||
return get_judge_task_state(app).outcome_messages.pop(source_id, None)
|
||||
@@ -6,11 +6,12 @@ import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import TYPE_CHECKING, Annotated
|
||||
|
||||
import httpx
|
||||
import typer
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from python.common import configure_logger
|
||||
from python.ebook_search.api.bm25_tasks import cancel_bm25_refresh
|
||||
@@ -18,8 +19,9 @@ from python.ebook_search.api.routes import admin_router, health_router, page_rou
|
||||
from python.ebook_search.api.web import STATIC_DIR
|
||||
from python.ebook_search.bm25_corpus import ensure_bm25_corpus
|
||||
from python.ebook_search.config import load_config
|
||||
from python.ebook_search.protected_phrases.pool import shutdown_extraction_pool
|
||||
from python.fastapi_tools import ZstdMiddleware
|
||||
from python.orm.common import get_postgres_engine
|
||||
from python.orm.common import get_async_postgres_engine
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import AsyncIterator
|
||||
@@ -48,15 +50,24 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
)
|
||||
if not config.library_paths:
|
||||
logger.warning("ebook_search_no_library_paths_configured")
|
||||
app.state.engine = get_postgres_engine(name="RICHIE", vector_engine=True)
|
||||
with Session(app.state.engine) as session:
|
||||
ensure_bm25_corpus(session, config)
|
||||
# Concurrent phrase judging opens one session per book worker on this engine, so size the pool
|
||||
# to cover those plus headroom for ordinary web requests.
|
||||
app.state.engine = get_async_postgres_engine(
|
||||
name="RICHIE",
|
||||
vector_engine=True,
|
||||
pool_size=config.phrase_judge_book_workers + 10,
|
||||
)
|
||||
app.state.http_client = httpx.AsyncClient()
|
||||
async with AsyncSession(app.state.engine, expire_on_commit=False) as session:
|
||||
await ensure_bm25_corpus(session, config)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
logger.info("ebook_search_shutdown")
|
||||
cancel_bm25_refresh(app)
|
||||
app.state.engine.dispose()
|
||||
shutdown_extraction_pool()
|
||||
await app.state.http_client.aclose()
|
||||
await app.state.engine.dispose()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
|
||||
@@ -8,15 +8,18 @@ from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
|
||||
from python.ebook_search.api.bm25_tasks import schedule_bm25_refresh
|
||||
from python.ebook_search.api.dependencies import (
|
||||
AppConfig, # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.ebook_search.api.dependencies import ( # noqa: TC001 FastAPI resolves these annotated dependencies at runtime
|
||||
AppConfig,
|
||||
AppEngine,
|
||||
AppHttpClient,
|
||||
)
|
||||
from python.ebook_search.api.web import templates
|
||||
from python.ebook_search.embeddings import embed_missing_chunks, embedding_model_stats
|
||||
from python.ebook_search.ingest import ingest_configured_paths
|
||||
from python.ebook_search.protected_phrases.generate_ngrams import generate_candidate_phrases_for_books
|
||||
from python.ebook_search.protected_phrases.judge_ngrams import judge_candidate_phrases_for_books
|
||||
from python.fastapi_tools import DbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.ebook_search.protected_phrases.store import book_ids_pending_first_judgment, corpus_phrase_stats
|
||||
from python.fastapi_tools import AsyncDbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -24,19 +27,29 @@ router = APIRouter(prefix="/admin")
|
||||
|
||||
|
||||
@router.get("", response_class=HTMLResponse)
|
||||
def admin(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
async def admin(request: Request, config: AppConfig, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Render the admin page."""
|
||||
stats = embedding_model_stats(session)
|
||||
logger.info("ebook_admin_page_loaded models=%s", len(stats))
|
||||
return templates.TemplateResponse(request, "admin.html", {"config": config, "stats": stats})
|
||||
stats = await embedding_model_stats(session)
|
||||
phrase_stats = await corpus_phrase_stats(session)
|
||||
logger.info(
|
||||
"ebook_admin_page_loaded models=%s candidate_phrases=%s protected_phrases=%s",
|
||||
len(stats),
|
||||
phrase_stats.candidate_phrases,
|
||||
phrase_stats.protected_phrases,
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin.html",
|
||||
{"config": config, "stats": stats, "phrase_stats": phrase_stats},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/scan", response_class=HTMLResponse)
|
||||
def scan_library(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
async def scan_library(request: Request, config: AppConfig, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Scan configured library paths for EPUB changes."""
|
||||
try:
|
||||
count = ingest_configured_paths(session, config)
|
||||
session.commit()
|
||||
count = await ingest_configured_paths(session, config)
|
||||
await session.commit()
|
||||
except Exception as error:
|
||||
logger.exception("ebook_admin_scan_failed")
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
@@ -47,48 +60,119 @@ def scan_library(request: Request, config: AppConfig, session: DbSession) -> HTM
|
||||
return templates.TemplateResponse(request, "partials/admin_status.html", {"message": f"Indexed {count} EPUBs"})
|
||||
|
||||
|
||||
@router.post("/generate-ngrams", response_class=HTMLResponse)
|
||||
def generate_ngrams(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
"""Generate candidate n-grams for indexed books without LLM judging."""
|
||||
@router.post("/phrases/generate-all", response_class=HTMLResponse)
|
||||
async def generate_all_phrases(request: Request, config: AppConfig, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Regenerate candidate phrases for every indexed book without LLM judging."""
|
||||
return await run_phrase_generation(request, config, session, only_missing=False)
|
||||
|
||||
|
||||
@router.post("/phrases/generate-missing", response_class=HTMLResponse)
|
||||
async def generate_missing_phrases(request: Request, config: AppConfig, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Generate candidate phrases only for books that have none yet."""
|
||||
return await run_phrase_generation(request, config, session, only_missing=True)
|
||||
|
||||
|
||||
async def run_phrase_generation(
|
||||
request: Request,
|
||||
config: AppConfig,
|
||||
session: AsyncDbSession,
|
||||
*,
|
||||
only_missing: bool,
|
||||
) -> HTMLResponse:
|
||||
"""Run candidate phrase generation and render the outcome as an admin status partial.
|
||||
|
||||
Args:
|
||||
request (Request): Current request, for template rendering.
|
||||
config (AppConfig): Runtime phrase-tuning settings.
|
||||
session (AsyncDbSession): Active database session.
|
||||
only_missing (bool): Only generate for books without candidates instead of every book.
|
||||
|
||||
Returns:
|
||||
HTMLResponse: Status partial describing the generation outcome.
|
||||
"""
|
||||
try:
|
||||
result = generate_candidate_phrases_for_books(session, config)
|
||||
session.commit()
|
||||
result = await generate_candidate_phrases_for_books(session, config, only_missing=only_missing)
|
||||
await session.commit()
|
||||
except Exception as error:
|
||||
session.rollback()
|
||||
logger.exception("ebook_admin_generate_ngrams_failed")
|
||||
await session.rollback()
|
||||
logger.exception("ebook_admin_generate_phrases_failed only_missing=%s", only_missing)
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
|
||||
logger.info(
|
||||
"ebook_admin_generate_ngrams_complete books_seen=%s books_built=%s candidates=%s",
|
||||
"ebook_admin_generate_phrases_complete only_missing=%s books_seen=%s books_built=%s candidates=%s",
|
||||
only_missing,
|
||||
result.books_seen,
|
||||
result.books_built,
|
||||
result.candidate_phrases,
|
||||
)
|
||||
if only_missing and result.books_seen == 0:
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/admin_status.html",
|
||||
{"message": "All books already have candidate phrases"},
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/admin_status.html",
|
||||
{
|
||||
"message": (
|
||||
f"Generated n-grams for {result.books_built} of {result.books_seen} books; "
|
||||
f"Generated phrases for {result.books_built} of {result.books_seen} books; "
|
||||
f"{result.candidate_phrases} candidates stored"
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/judge-ngrams", response_class=HTMLResponse)
|
||||
def judge_ngrams(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
"""Judge stored candidate n-grams and promote accepted protected phrases."""
|
||||
@router.post("/phrases/judge-all", response_class=HTMLResponse)
|
||||
async def judge_all_phrases(request: Request, engine: AppEngine, config: AppConfig) -> HTMLResponse:
|
||||
"""Judge unjudged candidate phrases across every indexed book."""
|
||||
return await run_phrase_judgment(request, engine, config, source_ids=None)
|
||||
|
||||
|
||||
@router.post("/phrases/judge-missing", response_class=HTMLResponse)
|
||||
async def judge_missing_phrases(
|
||||
request: Request,
|
||||
engine: AppEngine,
|
||||
config: AppConfig,
|
||||
session: AsyncDbSession,
|
||||
) -> HTMLResponse:
|
||||
"""Judge candidate phrases only for books where judging has never run."""
|
||||
source_ids = await book_ids_pending_first_judgment(session)
|
||||
if not source_ids:
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/admin_status.html",
|
||||
{"message": "All books with candidate phrases have been judged"},
|
||||
)
|
||||
return await run_phrase_judgment(request, engine, config, source_ids=source_ids)
|
||||
|
||||
|
||||
async def run_phrase_judgment(
|
||||
request: Request,
|
||||
engine: AppEngine,
|
||||
config: AppConfig,
|
||||
*,
|
||||
source_ids: list[int] | None,
|
||||
) -> HTMLResponse:
|
||||
"""Run LLM judging for candidate phrases and render the outcome as an admin status partial.
|
||||
|
||||
Args:
|
||||
request (Request): Current request, for template rendering.
|
||||
engine (AppEngine): Engine used to open per-book judging sessions.
|
||||
config (AppConfig): Runtime phrase-tuning settings.
|
||||
source_ids (list[int] | None): Books to judge; ``None`` judges every indexed book.
|
||||
|
||||
Returns:
|
||||
HTMLResponse: Status partial describing the judging outcome.
|
||||
"""
|
||||
try:
|
||||
result = judge_candidate_phrases_for_books(session, config)
|
||||
session.commit()
|
||||
result = await judge_candidate_phrases_for_books(engine, config, source_ids=source_ids)
|
||||
except Exception as error:
|
||||
session.rollback()
|
||||
logger.exception("ebook_admin_judge_ngrams_failed")
|
||||
logger.exception("ebook_admin_judge_phrases_failed")
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
|
||||
logger.info(
|
||||
"ebook_admin_judge_ngrams_complete books_seen=%s books_judged=%s books_failed=%s candidates_judged=%s "
|
||||
"ebook_admin_judge_phrases_complete books_seen=%s books_judged=%s books_failed=%s candidates_judged=%s "
|
||||
"protected=%s mentions=%s",
|
||||
result.books_seen,
|
||||
result.books_judged,
|
||||
@@ -112,11 +196,16 @@ def judge_ngrams(request: Request, config: AppConfig, session: DbSession) -> HTM
|
||||
|
||||
|
||||
@router.post("/embed-missing", response_class=HTMLResponse)
|
||||
def embed_missing(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
async def embed_missing(
|
||||
request: Request,
|
||||
config: AppConfig,
|
||||
session: AsyncDbSession,
|
||||
client: AppHttpClient,
|
||||
) -> HTMLResponse:
|
||||
"""Embed chunks missing vectors for the configured model."""
|
||||
try:
|
||||
count = embed_missing_chunks(session, config)
|
||||
session.commit()
|
||||
count = await embed_missing_chunks(session, client, config)
|
||||
await session.commit()
|
||||
except Exception as error:
|
||||
logger.exception("ebook_admin_embed_missing_failed")
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
@@ -130,16 +219,21 @@ def embed_missing(request: Request, config: AppConfig, session: DbSession) -> HT
|
||||
|
||||
|
||||
@router.post("/embed-all", response_class=HTMLResponse)
|
||||
def embed_all(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
async def embed_all(
|
||||
request: Request,
|
||||
config: AppConfig,
|
||||
session: AsyncDbSession,
|
||||
client: AppHttpClient,
|
||||
) -> HTMLResponse:
|
||||
"""Embed all chunks missing vectors in fixed-size batches."""
|
||||
total = 0
|
||||
batches = 0
|
||||
try:
|
||||
while True:
|
||||
count = embed_missing_chunks(session, config)
|
||||
count = await embed_missing_chunks(session, client, config)
|
||||
if count == 0:
|
||||
break
|
||||
session.commit()
|
||||
await session.commit()
|
||||
total += count
|
||||
batches += 1
|
||||
logger.info(
|
||||
|
||||
@@ -11,15 +11,17 @@ from fastapi.responses import JSONResponse
|
||||
from sqlalchemy import literal, select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from python.ebook_search.api.dependencies import (
|
||||
AppConfig, # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.ebook_search.api.dependencies import ( # noqa: TC001 FastAPI resolves these annotated dependencies at runtime
|
||||
AppConfig,
|
||||
AppHttpClient,
|
||||
)
|
||||
from python.ebook_search.bm25_corpus import bm25_index_exists, bm25_index_path, read_bm25_manifest
|
||||
from python.ebook_search.llm_interface import check_chat_endpoint, check_embedding_endpoint
|
||||
from python.fastapi_tools import DbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.fastapi_tools import AsyncDbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sqlalchemy.orm import Session
|
||||
import httpx
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from python.ebook_search.config import EbookSearchConfig
|
||||
|
||||
@@ -29,17 +31,17 @@ router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
async def health() -> dict[str, str]:
|
||||
"""Liveness probe that returns ok without touching dependencies."""
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/ready")
|
||||
def ready(config: AppConfig, session: DbSession) -> JSONResponse:
|
||||
async def ready(config: AppConfig, session: AsyncDbSession, client: AppHttpClient) -> JSONResponse:
|
||||
"""Readiness probe reporting database, embedding endpoint, and BM25 index status."""
|
||||
database_ok = check_database(session)
|
||||
embedding_ok = check_embedding_endpoint(config)
|
||||
chat_status = chat_endpoint_status(config)
|
||||
database_ok = await check_database(session)
|
||||
embedding_ok = await check_embedding_endpoint(client, config)
|
||||
chat_status = await chat_endpoint_status(client, config)
|
||||
bm25_status = check_bm25_status(config)
|
||||
|
||||
checks = {
|
||||
@@ -69,17 +71,17 @@ def ready(config: AppConfig, session: DbSession) -> JSONResponse:
|
||||
return JSONResponse(content={"status": status, "checks": checks}, status_code=status_code)
|
||||
|
||||
|
||||
def chat_endpoint_status(config: EbookSearchConfig) -> str:
|
||||
async def chat_endpoint_status(client: httpx.AsyncClient, config: EbookSearchConfig) -> str:
|
||||
"""Return the answering chat endpoint status, or disabled when answers are off."""
|
||||
if not config.answer_enabled:
|
||||
return "disabled"
|
||||
return "ok" if check_chat_endpoint(config) else "fail"
|
||||
return "ok" if await check_chat_endpoint(client, config) else "fail"
|
||||
|
||||
|
||||
def check_database(session: Session) -> bool:
|
||||
async def check_database(session: AsyncSession) -> bool:
|
||||
"""Return whether the database answers a trivial query."""
|
||||
try:
|
||||
session.execute(select(literal(1)))
|
||||
await session.execute(select(literal(1)))
|
||||
except SQLAlchemyError as error:
|
||||
logger.warning("ebook_ready_database_unavailable error=%s", error)
|
||||
return False
|
||||
|
||||
@@ -3,18 +3,23 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from python.ebook_search.api.dependencies import (
|
||||
AppConfig, # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
)
|
||||
from python.ebook_search.api.judge_tasks import is_judging_book, pop_book_judgment_outcome, start_book_phrase_judgment
|
||||
from python.ebook_search.api.web import templates
|
||||
from python.ebook_search.protected_phrases.generate_ngrams import recalculate_candidate_phrases_for_book
|
||||
from python.fastapi_tools import DbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.orm.richie import EbookCandidatePhrase, EbookProtectedPhrase, EbookSource
|
||||
from python.fastapi_tools import AsyncDbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
from python.orm.richie import EbookCandidatePhrase, EbookChapter, EbookChunk, EbookProtectedPhrase, EbookSource
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,30 +27,41 @@ router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
def index(request: Request, config: AppConfig) -> HTMLResponse:
|
||||
async def index(request: Request, config: AppConfig) -> HTMLResponse:
|
||||
"""Render the search page."""
|
||||
return templates.TemplateResponse(request, "search.html", {"config": config})
|
||||
|
||||
|
||||
@router.get("/books", response_class=HTMLResponse)
|
||||
def books(request: Request, session: DbSession) -> HTMLResponse:
|
||||
async def books(request: Request, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Render the indexed books page."""
|
||||
sources = list(session.scalars(select(EbookSource).order_by(EbookSource.title)).all())
|
||||
sources = list((await session.scalars(select(EbookSource).order_by(EbookSource.title))).all())
|
||||
logger.info("ebook_books_page_loaded count=%s", len(sources))
|
||||
return templates.TemplateResponse(request, "books.html", {"sources": sources})
|
||||
|
||||
|
||||
def get_candidate_count(session: DbSession, book_id: int) -> int:
|
||||
async def get_chapter_count(session: AsyncSession, book_id: int) -> int:
|
||||
"""Return the number of indexed chapters for one book."""
|
||||
return await session.scalar(select(func.count(EbookChapter.id)).where(EbookChapter.source_id == book_id)) or 0
|
||||
|
||||
|
||||
async def get_chunk_count(session: AsyncSession, book_id: int) -> int:
|
||||
"""Return the number of indexed chunks for one book."""
|
||||
return await session.scalar(select(func.count(EbookChunk.id)).where(EbookChunk.source_id == book_id)) or 0
|
||||
|
||||
|
||||
async def get_candidate_count(session: AsyncSession, book_id: int) -> int:
|
||||
"""Return the number of indexed candidates for one book."""
|
||||
return (
|
||||
session.scalar(select(func.count(EbookCandidatePhrase.id)).where(EbookCandidatePhrase.book_id == book_id)) or 0
|
||||
await session.scalar(select(func.count(EbookCandidatePhrase.id)).where(EbookCandidatePhrase.book_id == book_id))
|
||||
or 0
|
||||
)
|
||||
|
||||
|
||||
def get_judged_candidate_count(session: DbSession, book_id: int) -> int:
|
||||
async def get_judged_candidate_count(session: AsyncSession, book_id: int) -> int:
|
||||
"""Return the number of judged candidates for one book."""
|
||||
return (
|
||||
session.scalar(
|
||||
await session.scalar(
|
||||
select(func.count(EbookCandidatePhrase.id)).where(
|
||||
EbookCandidatePhrase.book_id == book_id,
|
||||
EbookCandidatePhrase.llm_judged.is_(True),
|
||||
@@ -55,17 +71,18 @@ def get_judged_candidate_count(session: DbSession, book_id: int) -> int:
|
||||
)
|
||||
|
||||
|
||||
def get_protected_count(session: DbSession, book_id: int) -> int:
|
||||
async def get_protected_count(session: AsyncSession, book_id: int) -> int:
|
||||
"""Return the number of protected phrases for one book."""
|
||||
return (
|
||||
session.scalar(select(func.count(EbookProtectedPhrase.id)).where(EbookProtectedPhrase.book_id == book_id)) or 0
|
||||
await session.scalar(select(func.count(EbookProtectedPhrase.id)).where(EbookProtectedPhrase.book_id == book_id))
|
||||
or 0
|
||||
)
|
||||
|
||||
|
||||
def get_candidates(session: DbSession, book_id: int) -> list[EbookCandidatePhrase]:
|
||||
async def get_candidates(session: AsyncSession, book_id: int) -> list[EbookCandidatePhrase]:
|
||||
"""Return the indexed candidates for one book."""
|
||||
return list(
|
||||
session.scalars(
|
||||
await session.scalars(
|
||||
select(EbookCandidatePhrase)
|
||||
.where(EbookCandidatePhrase.book_id == book_id)
|
||||
.order_by(EbookCandidatePhrase.candidate_score.desc())
|
||||
@@ -74,10 +91,10 @@ def get_candidates(session: DbSession, book_id: int) -> list[EbookCandidatePhras
|
||||
)
|
||||
|
||||
|
||||
def get_protected_phrases(session: DbSession, book_id: int) -> list[EbookProtectedPhrase]:
|
||||
async def get_protected_phrases(session: AsyncSession, book_id: int) -> list[EbookProtectedPhrase]:
|
||||
"""Return the protected phrases for one book."""
|
||||
return list(
|
||||
session.scalars(
|
||||
await session.scalars(
|
||||
select(EbookProtectedPhrase)
|
||||
.where(EbookProtectedPhrase.book_id == book_id)
|
||||
.order_by(EbookProtectedPhrase.importance.desc())
|
||||
@@ -87,21 +104,27 @@ def get_protected_phrases(session: DbSession, book_id: int) -> list[EbookProtect
|
||||
|
||||
|
||||
@router.get("/books/{source_id}", response_class=HTMLResponse)
|
||||
def book_detail(source_id: int, request: Request, session: DbSession) -> HTMLResponse:
|
||||
async def book_detail(source_id: int, request: Request, session: AsyncDbSession) -> HTMLResponse:
|
||||
"""Render details for one indexed book."""
|
||||
source = session.get(EbookSource, source_id)
|
||||
source = await session.get(EbookSource, source_id)
|
||||
phrase_status_message = None
|
||||
recalculated = request.query_params.get("phrases_recalculated")
|
||||
if recalculated is not None:
|
||||
phrase_status_message = f"Recalculated phrases; {recalculated} candidates generated"
|
||||
judgment_outcome = pop_book_judgment_outcome(request.app, source_id)
|
||||
if judgment_outcome is not None:
|
||||
phrase_status_message = judgment_outcome
|
||||
judging_in_progress = is_judging_book(request.app, source_id)
|
||||
if judging_in_progress:
|
||||
phrase_status_message = "Judging candidate phrases in the background; refresh to see progress"
|
||||
if source is not None:
|
||||
chapter_count = len(source.chapters)
|
||||
chunk_count = len(source.chunks)
|
||||
candidate_count = get_candidate_count(session, source.id)
|
||||
judged_candidate_count = get_judged_candidate_count(session, source.id)
|
||||
protected_count = get_protected_count(session, source.id)
|
||||
candidates = get_candidates(session, source.id)
|
||||
protected_phrases = get_protected_phrases(session, source.id)
|
||||
chapter_count = await get_chapter_count(session, source.id)
|
||||
chunk_count = await get_chunk_count(session, source.id)
|
||||
candidate_count = await get_candidate_count(session, source.id)
|
||||
judged_candidate_count = await get_judged_candidate_count(session, source.id)
|
||||
protected_count = await get_protected_count(session, source.id)
|
||||
candidates = await get_candidates(session, source.id)
|
||||
protected_phrases = await get_protected_phrases(session, source.id)
|
||||
else:
|
||||
chapter_count = 0
|
||||
chunk_count = 0
|
||||
@@ -129,6 +152,7 @@ def book_detail(source_id: int, request: Request, session: DbSession) -> HTMLRes
|
||||
"chapter_count": chapter_count,
|
||||
"chunk_count": chunk_count,
|
||||
"judged_candidate_count": judged_candidate_count,
|
||||
"judging_in_progress": judging_in_progress,
|
||||
"protected_count": protected_count,
|
||||
"protected_phrases": protected_phrases,
|
||||
"phrase_status_message": phrase_status_message,
|
||||
@@ -138,13 +162,13 @@ def book_detail(source_id: int, request: Request, session: DbSession) -> HTMLRes
|
||||
|
||||
|
||||
@router.post("/books/{source_id}/recalculate-phrases")
|
||||
def recalculate_book_phrases(source_id: int, config: AppConfig, session: DbSession) -> RedirectResponse:
|
||||
async def recalculate_book_phrases(source_id: int, config: AppConfig, session: AsyncDbSession) -> RedirectResponse:
|
||||
"""Clear and regenerate candidate phrases for one indexed book."""
|
||||
source = session.get(EbookSource, source_id)
|
||||
source = await session.get(EbookSource, source_id)
|
||||
if source is None:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
result = recalculate_candidate_phrases_for_book(session, source, config)
|
||||
result = await recalculate_candidate_phrases_for_book(session, source, config, use_process_pool=True)
|
||||
logger.info(
|
||||
"ebook_book_phrase_recalculation_complete source_id=%s candidates=%s deleted_candidates=%s "
|
||||
"deleted_protected=%s deleted_aliases=%s deleted_mentions=%s",
|
||||
@@ -159,3 +183,20 @@ def recalculate_book_phrases(source_id: int, config: AppConfig, session: DbSessi
|
||||
url=f"/books/{source_id}?phrases_recalculated={result.candidate_phrases}",
|
||||
status_code=303,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/books/{source_id}/judge-phrases")
|
||||
async def judge_book_phrases(
|
||||
source_id: int,
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
session: AsyncDbSession,
|
||||
) -> RedirectResponse:
|
||||
"""Queue background judging of one book's candidate phrases and return immediately."""
|
||||
source = await session.get(EbookSource, source_id)
|
||||
if source is None:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
started = start_book_phrase_judgment(request.app, background_tasks, source.id)
|
||||
logger.info("ebook_book_phrase_judgment_requested source_id=%s started=%s", source_id, started)
|
||||
return RedirectResponse(url=f"/books/{source_id}", status_code=303)
|
||||
|
||||
@@ -14,6 +14,7 @@ from python.ebook_search.answer import answer_query
|
||||
from python.ebook_search.api.dependencies import ( # noqa: TC001 FastAPI resolves these annotated dependencies at runtime
|
||||
AppConfig,
|
||||
AppEngine,
|
||||
AppHttpClient,
|
||||
)
|
||||
from python.ebook_search.api.web import templates
|
||||
from python.ebook_search.guardrails import (
|
||||
@@ -26,6 +27,8 @@ from python.ebook_search.search import SearchResponse, search_ebooks
|
||||
from python.ebook_search.timing import runtime_step_from_start
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import httpx
|
||||
|
||||
from python.ebook_search.config import EbookSearchConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -33,7 +36,8 @@ logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def build_answer(
|
||||
async def build_answer(
|
||||
client: httpx.AsyncClient,
|
||||
query: str,
|
||||
response: SearchResponse,
|
||||
config: EbookSearchConfig,
|
||||
@@ -56,7 +60,7 @@ def build_answer(
|
||||
return answer, True, None
|
||||
|
||||
try:
|
||||
answer = answer_query(query, response.results, config)
|
||||
answer = await answer_query(client, query, response.results, config)
|
||||
except RuntimeError as error:
|
||||
logger.warning("ebook_answer_request_failed_falling_back error=%s", error)
|
||||
return "Answer generation failed. Source chunks are still shown below.", False, None
|
||||
@@ -74,18 +78,20 @@ def build_answer(
|
||||
|
||||
|
||||
@router.post("/search", response_class=HTMLResponse)
|
||||
def search(
|
||||
async def search(
|
||||
request: Request,
|
||||
config: AppConfig,
|
||||
engine: AppEngine,
|
||||
client: AppHttpClient,
|
||||
query: Annotated[str, Form()],
|
||||
rerank: Annotated[str | None, Form()] = None,
|
||||
phrase_matching: Annotated[str | None, Form()] = None,
|
||||
) -> HTMLResponse:
|
||||
"""Run a search and render HTMX results."""
|
||||
try:
|
||||
response = search_ebooks(
|
||||
response = await search_ebooks(
|
||||
engine,
|
||||
client,
|
||||
query,
|
||||
config,
|
||||
rerank=rerank == "true",
|
||||
@@ -96,7 +102,7 @@ def search(
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
|
||||
answer_start = perf_counter()
|
||||
answer, low_confidence, citation_report = build_answer(query, response, config)
|
||||
answer, low_confidence, citation_report = await build_answer(client, query, response, config)
|
||||
answer_step_name = "Answer generation" if config.answer_enabled else "Answer skipped"
|
||||
response = replace(
|
||||
response,
|
||||
|
||||
@@ -8,37 +8,25 @@ head %}
|
||||
<form hx-post="/admin/scan" hx-target="#admin-status" hx-swap="innerHTML">
|
||||
<button type="submit">Scan</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/generate-ngrams"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Generate n-grams</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/judge-ngrams"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Judge n-grams</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/embed-missing"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Embed</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/embed-all"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Embed all</button>
|
||||
</form>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Embeddings</h2>
|
||||
<section class="actions">
|
||||
<form
|
||||
hx-post="/admin/embed-missing"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Embed</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/embed-all"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Embed all</button>
|
||||
</form>
|
||||
</section>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -62,4 +50,61 @@ head %}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Protected phrases</h2>
|
||||
<section class="actions">
|
||||
<form
|
||||
hx-post="/admin/phrases/generate-all"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Regenerate all phrases</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/phrases/generate-missing"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Add missing phrases</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/phrases/judge-all"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Judge all phrases</button>
|
||||
</form>
|
||||
<form
|
||||
hx-post="/admin/phrases/judge-missing"
|
||||
hx-target="#admin-status"
|
||||
hx-swap="innerHTML"
|
||||
>
|
||||
<button type="submit">Judge missing phrases</button>
|
||||
</form>
|
||||
</section>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Candidates</th>
|
||||
<th>Judged</th>
|
||||
<th>Unjudged</th>
|
||||
<th>Protected</th>
|
||||
<th>Books indexed</th>
|
||||
<th>Books generated</th>
|
||||
<th>Books fully judged</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>{{ phrase_stats.candidate_phrases }}</td>
|
||||
<td>{{ phrase_stats.judged_candidates }}</td>
|
||||
<td>{{ phrase_stats.unjudged_candidates }}</td>
|
||||
<td>{{ phrase_stats.protected_phrases }}</td>
|
||||
<td>{{ phrase_stats.total_books }}</td>
|
||||
<td>{{ phrase_stats.books_with_candidates }}</td>
|
||||
<td>{{ phrase_stats.books_fully_judged }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
{% endblock %}
|
||||
|
||||
@@ -30,6 +30,15 @@
|
||||
>
|
||||
<button type="submit">Recalculate phrases</button>
|
||||
</form>
|
||||
<form
|
||||
method="post"
|
||||
action="/books/{{ source.id }}/judge-phrases"
|
||||
onsubmit="return confirm('Judge candidate phrases for this book with the LLM?');"
|
||||
>
|
||||
<button type="submit"{% if judging_in_progress %} disabled{% endif %}>
|
||||
{% if judging_in_progress %}Judging…{% else %}Judge phrases{% endif %}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<section>
|
||||
<h2>Candidate n-grams</h2>
|
||||
|
||||
Reference in New Issue
Block a user