Spaces:
Running on Zero
Running on Zero
Download src/ui/preload_alignment.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/ui/preload_alignment.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/ui/preload_alignment.py
-
curl -L -o preload_alignment.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/ui/preload_alignment.py
3.74 kB
| """Move a selected preload recording into the ordinary upload alignment flow.""" | |
| import re | |
| import shutil | |
| import tempfile | |
| from pathlib import Path | |
| from urllib.parse import urljoin, urlsplit | |
| import gradio as gr | |
| import httpx | |
| def preload_align_visibility(url, content_mode, source, slug): | |
| """Offer "Align recitation" only for an audio-only row we can actually align. | |
| A delivery in one of the sixteen riwayat the aligner has no Quran text for | |
| has no alignable output, so the button stays hidden rather than failing on | |
| click. | |
| """ | |
| from src.preload.riwayah_map import delivery_riwayah | |
| ready = bool(url) and content_mode == "Audio only" and source == "preload" | |
| return gr.update(visible=ready and delivery_riwayah(slug) is not None) | |
| def prepare_preload_alignment(slug, chapter_label, content_mode, request: gr.Request): | |
| from config import PAD_LEFT_DEFAULT, PAD_RIGHT_DEFAULT, VISIBLE_ASR_MODEL_DEFAULT | |
| from src.preload.riwayah_map import delivery_riwayah | |
| from src.ui.preload_audio_mode import _surah_choices, repo_loader | |
| from src.ui.preload_events import switch_from_preload | |
| if content_mode != "Audio only" or not slug or not chapter_label: | |
| raise gr.Error("Select a recitation and chapter in Audio only mode first.") | |
| # The recitation's own riwayah drives the alignment; the button is hidden for | |
| # the rest, so this only fires on a stale click. | |
| riwayah = delivery_riwayah(slug) | |
| if riwayah is None: | |
| raise gr.Error("This recitation's riwayah is not supported for alignment.") | |
| chapter = dict(_surah_choices(slug)).get(chapter_label) | |
| if chapter is None: | |
| raise gr.Error("This chapter is no longer available. Select it again.") | |
| url = repo_loader.resolve_chapter_audio_url(slug, chapter) | |
| if not url: | |
| raise gr.Error("The chapter audio is unavailable.") | |
| # Keep a meaningful filename through Gradio's file cache and audio label. | |
| name = re.sub(r'[<>:"/\\|?*\x00-\x1f]', "-", f"{slug} - {chapter_label}")[:180] | |
| directory = Path(tempfile.mkdtemp(prefix="preload-align-")) | |
| suffix = Path(urlsplit(url).path).suffix.lower() | |
| if suffix not in {".mp3", ".wav", ".flac", ".ogg", ".m4a"}: | |
| suffix = ".mp3" | |
| path = directory / f"{name}{suffix}" | |
| try: | |
| from src.ui.preload_audio_route import _resolve_audio_file | |
| mounted = _resolve_audio_file(slug, str(chapter)) | |
| if mounted: | |
| shutil.copyfile(mounted, path) | |
| else: | |
| # Resolve a local preload route against this server; CDN URLs remain | |
| # absolute. The URL is generated by the catalog, never user input. | |
| absolute = urljoin(str(request.request.base_url), url) | |
| with httpx.stream("GET", absolute, follow_redirects=True, timeout=120) as response: | |
| response.raise_for_status() | |
| with path.open("wb") as output: | |
| for chunk in response.iter_bytes(): | |
| output.write(chunk) | |
| if not path.stat().st_size: | |
| raise ValueError("The audio download was empty.") | |
| from src.core import request_log | |
| request_log.remember_preload_file(str(path), f"{slug}/{chapter}") | |
| outputs = list(switch_from_preload("Upload")) | |
| outputs[14] = gr.update(value=str(path), visible=True, interactive=True) | |
| outputs[16] = str(path) | |
| # Reset the actual visible settings, not just hidden invocation values. | |
| return (*outputs, PAD_LEFT_DEFAULT, PAD_RIGHT_DEFAULT, | |
| VISIBLE_ASR_MODEL_DEFAULT, riwayah) | |
| except Exception: | |
| shutil.rmtree(directory, ignore_errors=True) | |
| raise | |