feat(ebook): add admin and book-detail UI for protected phrase pipeline
Expose the protected phrase extraction pipeline through the web UI:
- Admin routes: POST /admin/build-phrases, /admin/generate-ngrams, and
/admin/judge-ngrams, each wrapping the protected_phrases.lib backfill
helpers, committing on success, rolling back and rendering an error
partial on failure, and reporting per-book/candidate/mention counts.
- Book detail page: show candidate, judged, and protected phrase counts,
list top candidate n-grams (with kept/rejected status) and protected
phrases, and add a POST /books/{id}/recalculate-phrases action that
clears and regenerates candidates, then redirects back with a status
message.
- Admin template: add Generate/Judge n-gram buttons.
Also reflows admin.html to 2-space HTML formatting.
This commit is contained in:
@@ -14,6 +14,11 @@ from python.ebook_search.api.dependencies import (
|
||||
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.lib import (
|
||||
build_missing_protected_phrases,
|
||||
generate_candidate_phrases_for_books,
|
||||
judge_candidate_phrases_for_books,
|
||||
)
|
||||
from python.fastapi_tools import DbSession # noqa: TC001 FastAPI resolves this annotated dependency at runtime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -45,6 +50,97 @@ 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("/build-phrases", response_class=HTMLResponse)
|
||||
def build_phrases(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
"""Build protected phrases for indexed books that are missing them."""
|
||||
try:
|
||||
result = build_missing_protected_phrases(session, config)
|
||||
session.commit()
|
||||
except Exception as error:
|
||||
session.rollback()
|
||||
logger.exception("ebook_admin_build_phrases_failed")
|
||||
return templates.TemplateResponse(request, "partials/error.html", {"message": str(error)}, status_code=500)
|
||||
|
||||
logger.info(
|
||||
"ebook_admin_build_phrases_complete books_seen=%s books_built=%s protected=%s mentions=%s",
|
||||
result.books_seen,
|
||||
result.books_built,
|
||||
result.protected_phrases,
|
||||
result.phrase_mentions,
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/admin_status.html",
|
||||
{
|
||||
"message": (
|
||||
f"Built phrases for {result.books_built} of {result.books_seen} books; "
|
||||
f"{result.protected_phrases} protected phrases, {result.phrase_mentions} mentions"
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@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."""
|
||||
try:
|
||||
result = generate_candidate_phrases_for_books(session, config)
|
||||
session.commit()
|
||||
except Exception as error:
|
||||
session.rollback()
|
||||
logger.exception("ebook_admin_generate_ngrams_failed")
|
||||
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",
|
||||
result.books_seen,
|
||||
result.books_built,
|
||||
result.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"{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."""
|
||||
try:
|
||||
result = judge_candidate_phrases_for_books(session, config)
|
||||
session.commit()
|
||||
except Exception as error:
|
||||
session.rollback()
|
||||
logger.exception("ebook_admin_judge_ngrams_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 candidates_judged=%s protected=%s mentions=%s",
|
||||
result.books_seen,
|
||||
result.books_judged,
|
||||
result.candidates_judged,
|
||||
result.protected_phrases,
|
||||
result.phrase_mentions,
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/admin_status.html",
|
||||
{
|
||||
"message": (
|
||||
f"Judged {result.candidates_judged} candidates across {result.books_judged} of "
|
||||
f"{result.books_seen} books; {result.protected_phrases} protected phrases, "
|
||||
f"{result.phrase_mentions} mentions"
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/embed-missing", response_class=HTMLResponse)
|
||||
def embed_missing(request: Request, config: AppConfig, session: DbSession) -> HTMLResponse:
|
||||
"""Embed chunks missing vectors for the configured model."""
|
||||
|
||||
Reference in New Issue
Block a user