File size: 2,318 Bytes
35d8fb6
 
 
 
 
 
 
 
 
003b05a
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
"""Parameter validation shared by /api/v1 and the legacy Gradio adapters.

Every helper raises ``ValidationError`` rather than returning an error dict, so
each interface renders the failure in its own shape.
"""

from __future__ import annotations

from config import VISIBLE_ASR_MODELS

from src.api.errors import ValidationError

VALID_MODELS = tuple(VISIBLE_ASR_MODELS)
VALID_DEVICES = ("GPU", "CPU")
VALID_GRANULARITIES = ("words",)


def validate_model(model_name: str) -> str:
    """This deployment exposes only the aliases in ``VISIBLE_ASR_MODELS``.

    Other entries exist in the SDK's KNOWN_MODELS but are deliberately not
    served here — rejecting loudly beats silently downgrading to Base, which
    would hand the caller lower-quality results with no signal.
    """
    if model_name not in VALID_MODELS:
        valid = ", ".join(VALID_MODELS)
        raise ValidationError(
            f"Invalid model_name '{model_name}'. Must be one of: {valid}"
        )
    return model_name


def validate_device(device: str) -> str:
    normalized = (device or "GPU").upper()
    if normalized not in VALID_DEVICES:
        valid = ", ".join(VALID_DEVICES)
        raise ValidationError(f"Invalid device '{device}'. Must be one of: {valid}")
    return normalized


def validate_riwayah(value) -> str:
    from qua_domain import normalize_riwayah

    try:
        return normalize_riwayah(value or "hafs")
    except (TypeError, ValueError):
        raise ValidationError(
            f"Invalid riwayah '{value}'. Use one of: hafs, warsh, qalun, shuba"
        ) from None


def validate_granularity(value) -> str:
    """Only word-level timing is served; ``words+chars`` stays disabled."""
    granularity = value or "words"
    if granularity not in VALID_GRANULARITIES:
        raise ValidationError(
            f"Invalid granularity '{value}'. Only 'words' is currently available."
        )
    return granularity


def validate_pad(value, name: str) -> int:
    from config import PAD_MAX, PAD_MIN

    try:
        pad = int(value)
    except (TypeError, ValueError):
        raise ValidationError(f"{name} must be an integer number of milliseconds") from None
    if not PAD_MIN <= pad <= PAD_MAX:
        raise ValidationError(f"{name} must be between {PAD_MIN} and {PAD_MAX} ms")
    return pad