-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcommon_whisper.js
More file actions
156 lines (149 loc) · 4.06 KB
/
Copy pathcommon_whisper.js
File metadata and controls
156 lines (149 loc) · 4.06 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const WHISPER_LANGUAGES = [
['en', 'english'],
['zh', 'chinese'],
['de', 'german'],
['es', 'spanish'],
['ru', 'russian'],
['ko', 'korean'],
['fr', 'french'],
['ja', 'japanese'],
['pt', 'portuguese'],
['tr', 'turkish'],
['pl', 'polish'],
['ca', 'catalan'],
['nl', 'dutch'],
['ar', 'arabic'],
['sv', 'swedish'],
['it', 'italian'],
['id', 'indonesian'],
['hi', 'hindi'],
['fi', 'finnish'],
['vi', 'vietnamese'],
['he', 'hebrew'],
['uk', 'ukrainian'],
['el', 'greek'],
['ms', 'malay'],
['cs', 'czech'],
['ro', 'romanian'],
['da', 'danish'],
['hu', 'hungarian'],
['ta', 'tamil'],
['no', 'norwegian'],
['th', 'thai'],
['ur', 'urdu'],
['hr', 'croatian'],
['bg', 'bulgarian'],
['lt', 'lithuanian'],
['la', 'latin'],
['mi', 'maori'],
['ml', 'malayalam'],
['cy', 'welsh'],
['sk', 'slovak'],
['te', 'telugu'],
['fa', 'persian'],
['lv', 'latvian'],
['bn', 'bengali'],
['sr', 'serbian'],
['az', 'azerbaijani'],
['sl', 'slovenian'],
['kn', 'kannada'],
['et', 'estonian'],
['mk', 'macedonian'],
['br', 'breton'],
['eu', 'basque'],
['is', 'icelandic'],
['hy', 'armenian'],
['ne', 'nepali'],
['mn', 'mongolian'],
['bs', 'bosnian'],
['kk', 'kazakh'],
['sq', 'albanian'],
['sw', 'swahili'],
['gl', 'galician'],
['mr', 'marathi'],
['pa', 'punjabi'],
['si', 'sinhala'],
['km', 'khmer'],
['sn', 'shona'],
['yo', 'yoruba'],
['so', 'somali'],
['af', 'afrikaans'],
['oc', 'occitan'],
['ka', 'georgian'],
['be', 'belarusian'],
['tg', 'tajik'],
['sd', 'sindhi'],
['gu', 'gujarati'],
['am', 'amharic'],
['yi', 'yiddish'],
['lo', 'lao'],
['uz', 'uzbek'],
['fo', 'faroese'],
['ht', 'haitian creole'],
['ps', 'pashto'],
['tk', 'turkmen'],
['nn', 'nynorsk'],
['mt', 'maltese'],
['sa', 'sanskrit'],
['lb', 'luxembourgish'],
['my', 'myanmar'],
['bo', 'tibetan'],
['tl', 'tagalog'],
['mg', 'malagasy'],
['as', 'assamese'],
['tt', 'tatar'],
['haw', 'hawaiian'],
['ln', 'lingala'],
['ha', 'hausa'],
['ba', 'bashkir'],
['jw', 'javanese'],
['su', 'sundanese'],
];
// @ts-ignore
export const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES);
// @ts-ignore
const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([
...WHISPER_LANGUAGES.map(([k, v]) => [v, k]),
...[
['burmese', 'my'],
['valencian', 'ca'],
['flemish', 'nl'],
['haitian', 'ht'],
['letzeburgesch', 'lb'],
['pushto', 'ps'],
['panjabi', 'pa'],
['moldavian', 'ro'],
['moldovan', 'ro'],
['sinhalese', 'si'],
['castilian', 'es'],
],
]);
/**
* @param {string} language The language name or code
* @returns {string} The language code
*/
export function whisper_language_to_code(language) {
language = language.toLowerCase();
// Map to code from user-friendly name (e.g., "english" -> "en")
let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language);
if (language_code === undefined) {
// User provided something that is not a language name
// Perhaps the user passed the special token itself
const language_special_token = language.match(/^<\|([a-z]{2})\|>$/);
if (language_special_token) {
language = language_special_token[1];
}
if (WHISPER_LANGUAGE_MAPPING.has(language)) {
// User provided the language code directly (e.g., "en")
language_code = language;
} else {
// User provided something that is not a language code or name
const is_language_code = language.length === 2;
const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values();
throw new Error(
`Language "${language}" is not supported. Must be one of: ${JSON.stringify(Array.from(langs))}`,
);
}
}
return language_code;
}