File size: 4,897 Bytes
35d8fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134a1be
35d8fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc49622
35d8fb6
 
 
 
 
134a1be
 
 
 
 
35d8fb6
 
 
 
 
bc49622
35d8fb6
 
 
 
134a1be
 
 
 
 
35d8fb6
 
 
 
bc49622
35d8fb6
 
 
 
 
 
 
 
 
 
bc49622
35d8fb6
 
 
 
 
 
 
 
 
 
 
 
bc49622
3d6ae90
35d8fb6
 
 
 
bc49622
3d6ae90
35d8fb6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""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.

    """

    @functools.wraps(func)
    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
# ---------------------------------------------------------------------------

@_legacy_errors
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,
        )


@_legacy_errors
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,
        )


@_legacy_errors
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,
    )


@_legacy_errors
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
# ---------------------------------------------------------------------------

@_legacy_errors
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)


@_legacy_errors
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,
    )