From e6d2649d5dde5398314e409db1a4d195ab8c03d2 Mon Sep 17 00:00:00 2001 From: JB Date: Sun, 12 Oct 2025 06:16:26 -0700 Subject: [PATCH] Refactor job preparation templates and routes - Removed `prepare_chapters.html` and `prepare_entities.html` templates as they are no longer needed. - Updated `routes.py` to remove references to the removed templates and adjusted job preparation logic. - Simplified response handling in job preparation routes by removing unnecessary parameters. - Consolidated job preparation logic to improve maintainability and clarity. --- abogen/web/routes.py | 91 +--- abogen/web/templates/prepare_chapters.html | 187 ------- abogen/web/templates/prepare_entities.html | 579 --------------------- abogen/web/templates/prepare_job.html | 3 - 4 files changed, 17 insertions(+), 843 deletions(-) delete mode 100644 abogen/web/templates/prepare_chapters.html delete mode 100644 abogen/web/templates/prepare_entities.html delete mode 100644 abogen/web/templates/prepare_job.html diff --git a/abogen/web/routes.py b/abogen/web/routes.py index 73231f3..e1b95a3 100644 --- a/abogen/web/routes.py +++ b/abogen/web/routes.py @@ -18,6 +18,7 @@ from xml.etree import ElementTree as ET from flask import ( Blueprint, + Response, abort, current_app, jsonify, @@ -28,10 +29,13 @@ from flask import ( url_for, ) from flask.typing import ResponseReturnValue -from werkzeug.utils import secure_filename import numpy as np import soundfile as sf + +from werkzeug.utils import secure_filename + +from abogen.chunking import ChunkLevel, build_chunks_for_chapters from abogen.constants import ( LANGUAGE_DESCRIPTIONS, SAMPLE_VOICE_TEXTS, @@ -40,11 +44,9 @@ from abogen.constants import ( SUPPORTED_SOUND_FORMATS, VOICES_INTERNAL, ) -from abogen.chunking import ChunkLevel, build_chunks_for_chapters from abogen.kokoro_text_normalization import normalize_roman_numeral_titles from abogen.utils import ( calculate_text_length, - clean_text, get_user_output_path, load_config, load_numpy_kpipeline, @@ -2825,8 +2827,8 @@ def enqueue_job() -> ResponseReturnValue: if isinstance(languages, list): pending.speaker_voice_languages = [code for code in languages if isinstance(code, str)] if _wants_wizard_json(): - return _wizard_json_response(pending, "chapters", embed_scripts=False) - return redirect(url_for("web.prepare_job", pending_id=pending.id)) + return _wizard_json_response(pending, "chapters") + return redirect(url_for("web.index")) @web_bp.get("/jobs/prepare/") @@ -2835,8 +2837,8 @@ def prepare_job(pending_id: str) -> ResponseReturnValue: requested_step = request.args.get("step") or "chapters" normalized_step = _normalize_wizard_step(requested_step, pending) if _wants_wizard_json(): - return _wizard_json_response(pending, normalized_step, embed_scripts=False) - return _render_prepare_page(pending, active_step=normalized_step) + return _wizard_json_response(pending, normalized_step) + return redirect(url_for("web.index")) @web_bp.post("/jobs/prepare//analyze") @@ -2862,10 +2864,9 @@ def analyze_pending_job(pending_id: str) -> ResponseReturnValue: pending, "chapters", error=message, - embed_scripts=False, status=400, ) - return _render_prepare_page(pending, error=message, active_step="chapters") + abort(400, message) if pending.speaker_mode != "multi": setattr(pending, "analysis_requested", False) @@ -2877,14 +2878,9 @@ def analyze_pending_job(pending_id: str) -> ResponseReturnValue: pending, "chapters", error=error_message, - embed_scripts=False, status=400, ) - return _render_prepare_page( - pending, - error=error_message, - active_step="chapters", - ) + abort(400, error_message) if not enabled_overrides: setattr(pending, "analysis_requested", False) @@ -2896,14 +2892,9 @@ def analyze_pending_job(pending_id: str) -> ResponseReturnValue: pending, "chapters", error=error_message, - embed_scripts=False, status=400, ) - return _render_prepare_page( - pending, - error=error_message, - active_step="chapters", - ) + abort(400, error_message) raw_chunks = build_chunks_for_chapters(enabled_overrides, level=chunk_level_literal) analysis_chunks = build_chunks_for_chapters(enabled_overrides, level="sentence") @@ -2957,9 +2948,8 @@ def analyze_pending_job(pending_id: str) -> ResponseReturnValue: pending, "entities", notice=notice_message, - embed_scripts=False, ) - return _render_prepare_page(pending, notice=notice_message, active_step="entities") + return redirect(url_for("web.index")) @web_bp.post("/jobs/prepare/") @@ -2987,14 +2977,9 @@ def finalize_job(pending_id: str) -> ResponseReturnValue: pending, normalized_step, error=message, - embed_scripts=False, status=400, ) - return _render_prepare_page( - pending, - error=message, - active_step=normalized_step, - ) + abort(400, message) if pending.speaker_mode != "multi": setattr(pending, "analysis_requested", False) @@ -3007,14 +2992,9 @@ def finalize_job(pending_id: str) -> ResponseReturnValue: pending, "chapters", error=error_message, - embed_scripts=False, status=400, ) - return _render_prepare_page( - pending, - error=error_message, - active_step="chapters", - ) + abort(400, error_message) active_step = (request.form.get("active_step") or "chapters").strip().lower() if active_step == "speakers": @@ -3091,13 +3071,8 @@ def finalize_job(pending_id: str) -> ResponseReturnValue: pending, "entities", notice=notice_message or None, - embed_scripts=False, ) - return _render_prepare_page( - pending, - notice=notice_message or None, - active_step="entities", - ) + return redirect(url_for("web.index")) total_characters = selected_total or pending.total_characters service.pop_pending_job(pending_id) @@ -3233,7 +3208,6 @@ def _render_wizard_partial( *, error: Optional[str] = None, notice: Optional[str] = None, - embed_scripts: bool = False, ) -> str: templates = { "book": "partials/new_job_step_book.html", @@ -3248,7 +3222,6 @@ def _render_wizard_partial( "settings": _load_settings(), "error": error, "notice": notice, - "embed_scripts": embed_scripts, } return render_template(template_name, **context) @@ -3288,43 +3261,13 @@ def _wizard_json_response( *, error: Optional[str] = None, notice: Optional[str] = None, - embed_scripts: bool = False, status: int = 200, ) -> ResponseReturnValue: - html = _render_wizard_partial(pending, step, error=error, notice=notice, embed_scripts=embed_scripts) + html = _render_wizard_partial(pending, step, error=error, notice=notice) payload = _wizard_step_payload(pending, step, html, error=error, notice=notice) return jsonify(payload), status -def _render_prepare_page( - pending: PendingJob, - *, - error: Optional[str] = None, - notice: Optional[str] = None, - active_step: Optional[str] = None, -) -> str: - if not active_step: - active_step = ( - request.form.get("active_step") - if request.method == "POST" - else request.args.get("step") - ) or "chapters" - - normalized_step = _normalize_wizard_step(active_step, pending) - - template_name = "prepare_entities.html" if normalized_step == "entities" else "prepare_chapters.html" - - return render_template( - template_name, - pending=pending, - options=_template_options(), - settings=_load_settings(), - error=error, - notice=notice, - active_step=normalized_step, - ) - - @web_bp.get("/jobs/") def job_detail(job_id: str) -> str: job = _service().get_job(job_id) diff --git a/abogen/web/templates/prepare_chapters.html b/abogen/web/templates/prepare_chapters.html deleted file mode 100644 index b545dfa..0000000 --- a/abogen/web/templates/prepare_chapters.html +++ /dev/null @@ -1,187 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Prepare · {{ pending.original_filename }}{% endblock %} - -{% set is_multi_speaker = pending.speaker_mode == 'multi' %} -{% set total_steps = 3 if is_multi_speaker else 2 %} - -{% block content %} -
- -
-{% with pending=pending, readonly=True, active_step='chapters' %} - {% include "partials/upload_modal.html" %} -{% endwith %} -{% endblock %} - -{% block scripts %} - {{ super() }} - - - - - - -{% endblock %} diff --git a/abogen/web/templates/prepare_entities.html b/abogen/web/templates/prepare_entities.html deleted file mode 100644 index d810c5b..0000000 --- a/abogen/web/templates/prepare_entities.html +++ /dev/null @@ -1,579 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Prepare · {{ pending.original_filename }}{% endblock %} - -{% set is_multi_speaker = pending.speaker_mode == 'multi' %} -{% set analysis = pending.speaker_analysis or {} %} -{% set analysis_speakers = analysis.get('speakers', {}) %} -{% set speaker_configs = options.speaker_configs or [] %} -{% set recommended_template = options.speaker_pronunciation_sentence or "This is {{name}} speaking." %} -{% set total_steps = 3 if is_multi_speaker else 2 %} - -{% block content %} -
- -
-{% with pending=pending, readonly=True, active_step='entities' %} - {% include "partials/upload_modal.html" %} -{% endwith %} -{% endblock %} - -{% block scripts %} - {{ super() }} - - - - - - - - - - -{% endblock %} diff --git a/abogen/web/templates/prepare_job.html b/abogen/web/templates/prepare_job.html deleted file mode 100644 index a349ad4..0000000 --- a/abogen/web/templates/prepare_job.html +++ /dev/null @@ -1,3 +0,0 @@ -{# This template is intentionally left empty. - Step-specific templates now live in `prepare_chapters.html` and `prepare_entities.html`. - The file is kept as a placeholder to avoid breaking documentation references. #}