Spaces:
Running on Zero
Running on Zero
Download src/api/services/validation.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 2.32 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/validation.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/api/services/validation.py
-
curl -L -o validation.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/validation.py
2.32 kB
| """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 | |