Spaces:
Running on Zero
Running on Zero
Download src/api/session_api.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 4.9 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/session_api.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/api/session_api.py
-
curl -L -o session_api.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/session_api.py
4.9 kB
| """Legacy Gradio endpoints — frozen adapters over the shared services. | |
| These signatures are **production's contract** and must not change. Existing | |
| clients call them positionally, so argument order, defaults and response shapes | |
| are all load-bearing: | |
| * ``min_silence_ms`` / ``min_speech_ms`` are accepted and ignored. The old VAD | |
| segmenter they configured is gone; boundary-head segmentation replaced it | |
| and takes no equivalent knobs. | |
| * ``pad_ms`` was a single symmetric pad. It is applied to both sides. The | |
| per-side pads are available on ``/api/v1`` only. | |
| * Riwayah is always Hafs here. Other riwayat are ``/api/v1`` only. | |
| * The matcher is fixed by the ``MATCHER`` Space variable, not by the caller. | |
| Errors keep the legacy convention: HTTP 200 with ``{"error", "error_code", | |
| "segments": []}``. ``/api/v1`` returns real status codes instead. | |
| New integrations should use ``/api/v1`` — see ``/api/v1/docs``. | |
| """ | |
| from __future__ import annotations | |
| import functools | |
| import gradio as gr | |
| from src.api.errors import ApiError | |
| from src.api.services import align as align_service | |
| from src.api.services import sessions, splits, timing | |
| from src.core import request_log | |
| # Riwayah is not selectable on the legacy surface — production never had it. | |
| LEGACY_RIWAYAH = "hafs" | |
| def _legacy_errors(func): | |
| """Render typed service errors in the legacy HTTP-200 shape. | |
| ``audio_id`` is echoed back when the caller supplied one, because | |
| production clients correlate responses by it. | |
| """ | |
| def wrapper(*args, **kwargs): | |
| try: | |
| return func(*args, **kwargs) | |
| except ApiError as e: | |
| audio_id = kwargs.get("audio_id") | |
| if audio_id is None and args and isinstance(args[0], str): | |
| audio_id = args[0] if sessions.is_valid_id(args[0]) else None | |
| return e.to_legacy_dict(audio_id) | |
| return wrapper | |
| # --------------------------------------------------------------------------- | |
| # Alignment | |
| # --------------------------------------------------------------------------- | |
| def process_audio_session(audio_data, min_silence_ms, min_speech_ms, pad_ms, | |
| model_name="Base", device="GPU", | |
| request: gr.Request = None): | |
| """Full pipeline on uploaded audio. Creates a processing session. | |
| ``min_silence_ms`` and ``min_speech_ms`` are ignored (see module docstring). | |
| ``pad_ms`` is applied symmetrically to both segment edges. | |
| """ | |
| with request_log.tagged(endpoint="process_audio_session"): | |
| return align_service.align_audio( | |
| audio_data, pad_ms, pad_ms, model_name, device, | |
| riwayah=LEGACY_RIWAYAH, request=request, | |
| ) | |
| def process_url_session(url, min_silence_ms, min_speech_ms, pad_ms, | |
| model_name="Base", device="GPU", | |
| request: gr.Request = None): | |
| """Full pipeline on audio downloaded from ``url``. | |
| Same response as ``process_audio_session`` plus a ``url_metadata`` block. | |
| """ | |
| with request_log.tagged(endpoint="process_url_session"): | |
| return align_service.align_url( | |
| url, pad_ms, pad_ms, model_name, device, | |
| riwayah=LEGACY_RIWAYAH, request=request, | |
| ) | |
| def realign_from_timestamps(audio_id, timestamps, model_name="Base", device="GPU", | |
| request: gr.Request = None): | |
| """Run ASR + matching on caller-provided segment boundaries.""" | |
| return align_service.realign_from_timestamps( | |
| audio_id, timestamps, model_name, device, | |
| riwayah=LEGACY_RIWAYAH, request=request, | |
| ) | |
| def split_segments(audio_id, max_verses=1, max_words=None, max_duration=None, | |
| require_stop_sign=False, | |
| request: gr.Request = None): | |
| """Subdivide the session's segments by verse / word / duration limits.""" | |
| return splits.split_segments( | |
| audio_id, max_verses, max_words, max_duration, | |
| require_stop_sign=require_stop_sign, request=request, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Word timing | |
| # --------------------------------------------------------------------------- | |
| def timestamps(audio_id, segments_json=None, granularity="words"): | |
| """Compute word timestamps using the session's audio.""" | |
| return timing.timestamps_for_session(audio_id, segments_json, granularity) | |
| def timestamps_direct(audio_data, segments_json, granularity="words"): | |
| """Compute word timestamps from supplied audio and segments.""" | |
| return timing.timestamps_direct( | |
| audio_data, segments_json, granularity, LEGACY_RIWAYAH, | |
| ) | |