Spaces:
Running on Zero
Running on Zero
Download src/api/services/splits.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/splits.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/api/services/splits.py
-
curl -L -o splits.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/splits.py
2.12 kB
| """Segment subdivision service: split a session's segments by verse/word/duration.""" | |
| from __future__ import annotations | |
| from src.api.errors import AlignmentFailedError, SessionNotFoundError, ValidationError | |
| from src.api.services import response, sessions | |
| SAMPLE_RATE = sessions.SAMPLE_RATE | |
| def split_segments(audio_id, max_verses=1, max_words=None, max_duration=None, | |
| require_stop_sign=False, request=None) -> dict: | |
| """Subdivide the session's current segments using MFA word timings. | |
| Args: | |
| max_verses: 1..SPLIT_MAX_VERSES_MAX. The max (or None/0) disables the | |
| verse criterion. | |
| max_words: 10..SPLIT_MAX_WORDS_MAX. The max (or None/0) disables it. | |
| max_duration: seconds, 3..30. 30 (or None/0) disables it. | |
| require_stop_sign: skip segments without a waqf mark instead of | |
| equal-word splitting. The verse pass is unaffected. | |
| """ | |
| from src.segments import Segment, segments_to_json | |
| session = sessions.load(audio_id) | |
| if session is None: | |
| raise SessionNotFoundError() | |
| saved = sessions.load_segments(audio_id) | |
| if not saved: | |
| raise ValidationError( | |
| "No segments stored for this session. Run an alignment first." | |
| ) | |
| segments = [Segment.from_json_dict(d, index=i) for i, d in enumerate(saved)] | |
| from src.pipeline import split_segments_audio | |
| result = split_segments_audio( | |
| segments, | |
| session["audio"], SAMPLE_RATE, | |
| session["speech_intervals"], session["is_complete"], | |
| session["intervals"], | |
| max_verses, max_words, max_duration, | |
| require_stop_sign=bool(require_stop_sign), | |
| cached_segment_dir=None, | |
| request=request, endpoint="split_segments", | |
| ) | |
| new_segments = result[1] | |
| if new_segments is None: | |
| raise AlignmentFailedError("Split failed") | |
| json_output = (segments_to_json(new_segments) | |
| if isinstance(new_segments, list) else new_segments) | |
| sessions.update(audio_id, intervals=result[6]) | |
| return response.build(audio_id, json_output) | |