forked from OpenScienceLabs/opensciencelabs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
141 lines (116 loc) · 6.73 KB
/
validators.py
File metadata and controls
141 lines (116 loc) · 6.73 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
from pathlib import Path
from typing import Optional
AUDIO_EXTS = {".mp3", ".wav", ".ogg", ".flac", ".m4a", ".aac"}
TEXT_EXTS = {".txt", ".md", ".srt", ".vtt", ".json"}
def _ext_of(path: Optional[str]) -> str:
return Path(path).suffix.lower() if path else ""
def validate_io_paths(input_path: Optional[str], output_path: Optional[str], operation: Optional[str] = None) -> None:
"""
Validate input/output extensions for common operations.
operation can be:
- 'text-to-speech' / 'text->speech' / 'tts'
- 'speech-to-text' / 'speech->text' / 'stt'
- None (best-effort inference)
Raises ValueError on invalid combinations.
"""
op = (operation or "").lower()
in_ext = _ext_of(input_path)
out_ext = _ext_of(output_path)
tts_ops = {"text-to-speech", "text->speech", "tts"}
stt_ops = {"speech-to-text", "speech->text", "stt"}
if op in tts_ops:
if not output_path or out_ext == "":
raise ValueError("text->speech requires an audio output file (e.g. --output-path out.mp3).")
if out_ext not in AUDIO_EXTS:
raise ValueError(f"Invalid output audio format '{out_ext}'. Supported: {', '.join(sorted(AUDIO_EXTS))}")
elif op in stt_ops:
if not input_path or in_ext == "" or in_ext not in AUDIO_EXTS:
raise ValueError(f"speech->text requires an audio input file (supported: {', '.join(sorted(AUDIO_EXTS))}).")
if not output_path or out_ext == "" or out_ext not in TEXT_EXTS:
raise ValueError(f"speech->text requires a text output file (supported: {', '.join(sorted(TEXT_EXTS))}).")
else:
# Infer operation by extensions and validate.
inferred_op = None
if in_ext:
if in_ext in AUDIO_EXTS:
inferred_op = "speech-to-text"
elif in_ext in TEXT_EXTS:
inferred_op = "text-to-speech"
if not inferred_op and out_ext:
if out_ext in AUDIO_EXTS:
inferred_op = "text-to-speech"
elif out_ext in TEXT_EXTS:
inferred_op = "speech-to-text"
if inferred_op == "text-to-speech":
if not output_path or out_ext == "":
raise ValueError("text->speech requires an audio output file (e.g. --output-path out.mp3).")
if out_ext not in AUDIO_EXTS:
raise ValueError(f"Invalid output audio format '{out_ext}'. Supported: {', '.join(sorted(AUDIO_EXTS))}")
elif inferred_op == "speech-to-text":
if not input_path or in_ext == "" or in_ext not in AUDIO_EXTS:
raise ValueError(f"speech->text requires an audio input file (supported: {', '.join(sorted(AUDIO_EXTS))}).")
if not output_path or out_ext == "" or out_ext not in TEXT_EXTS:
raise ValueError(f"speech->text requires a text output file (supported: {', '.join(sorted(TEXT_EXTS))}).")
else:
if input_path and in_ext and in_ext not in AUDIO_EXTS.union(TEXT_EXTS):
raise ValueError(f"Unsupported input extension '{in_ext}'. Supported: audio {', '.join(sorted(AUDIO_EXTS))} or text {', '.join(sorted(TEXT_EXTS))}.")
if output_path and out_ext and out_ext not in AUDIO_EXTS.union(TEXT_EXTS):
raise ValueError(f"Unsupported output extension '{out_ext}'. Supported: audio {', '.join(sorted(AUDIO_EXTS))} or text {', '.join(sorted(TEXT_EXTS))}.")
from pathlib import Path
from typing import Optional
AUDIO_EXTS = {".mp3", ".wav", ".ogg", ".flac", ".m4a", ".aac"}
TEXT_EXTS = {".txt", ".md", ".srt", ".vtt", ".json"}
def _ext_of(path: Optional[str]) -> str:
return Path(path).suffix.lower() if path else ""
def validate_io_paths(input_path: Optional[str], output_path: Optional[str], operation: Optional[str] = None) -> None:
"""
Validate input/output extensions for common operations.
operation can be:
- 'text-to-speech' / 'text->speech' / 'tts'
- 'speech-to-text' / 'speech->text' / 'stt'
- None (best-effort inference)
Raises ValueError on invalid combinations.
"""
op = (operation or "").lower()
in_ext = _ext_of(input_path)
out_ext = _ext_of(output_path)
tts_ops = {"text-to-speech", "text->speech", "tts"}
stt_ops = {"speech-to-text", "speech->text", "stt"}
if op in tts_ops:
if not output_path or out_ext == "":
raise ValueError("text->speech requires an audio output file (e.g. --output-path out.mp3).")
if out_ext not in AUDIO_EXTS:
raise ValueError(f"Invalid output audio format '{out_ext}'. Supported: {', '.join(sorted(AUDIO_EXTS))}")
elif op in stt_ops:
if not input_path or in_ext == "" or in_ext not in AUDIO_EXTS:
raise ValueError(f"speech->text requires an audio input file (supported: {', '.join(sorted(AUDIO_EXTS))}).")
if not output_path or out_ext == "" or out_ext not in TEXT_EXTS:
raise ValueError(f"speech->text requires a text output file (supported: {', '.join(sorted(TEXT_EXTS))}).")
else:
# Infer operation by extensions and validate.
inferred_op = None
if in_ext:
if in_ext in AUDIO_EXTS:
inferred_op = "speech-to-text"
elif in_ext in TEXT_EXTS:
inferred_op = "text-to-speech"
if not inferred_op and out_ext:
if out_ext in AUDIO_EXTS:
inferred_op = "text-to-speech"
elif out_ext in TEXT_EXTS:
inferred_op = "speech-to-text"
if inferred_op == "text-to-speech":
if not output_path or out_ext == "":
raise ValueError("text->speech requires an audio output file (e.g. --output-path out.mp3).")
if out_ext not in AUDIO_EXTS:
raise ValueError(f"Invalid output audio format '{out_ext}'. Supported: {', '.join(sorted(AUDIO_EXTS))}")
elif inferred_op == "speech-to-text":
if not input_path or in_ext == "" or in_ext not in AUDIO_EXTS:
raise ValueError(f"speech->text requires an audio input file (supported: {', '.join(sorted(AUDIO_EXTS))}).")
if not output_path or out_ext == "" or out_ext not in TEXT_EXTS:
raise ValueError(f"speech->text requires a text output file (supported: {', '.join(sorted(TEXT_EXTS))}).")
else:
if input_path and in_ext and in_ext not in AUDIO_EXTS.union(TEXT_EXTS):
raise ValueError(f"Unsupported input extension '{in_ext}'. Supported: audio {', '.join(sorted(AUDIO_EXTS))} or text {', '.join(sorted(TEXT_EXTS))}.")
if output_path and out_ext and out_ext not in AUDIO_EXTS.union(TEXT_EXTS):
raise ValueError(f"Unsupported output extension '{out_ext}'. Supported: audio {', '.join(sorted(AUDIO_EXTS))} or text {', '.join(sorted(TEXT_EXTS))}.")