6
6
from ..utils import commons
7
7
from ..utils .exceptions import BadRequest
8
8
from ..utils .requests import Requests as requests
9
- from ..utils .supportedlangs import tts_lang , TSL_CODES , TSL_NAMES
9
+ from ..utils .supportedlangs import Languages
10
10
11
11
12
12
# --- Front page ---
@@ -148,16 +148,17 @@ def scratch_team_members() -> dict:
148
148
return json .loads (text )
149
149
150
150
151
- def translate (language : str , text : str = "hello" ):
152
- if language not in TSL_CODES :
153
- if language .lower () in TSL_CODES :
154
- language = language .lower ()
155
- elif language .title () in TSL_NAMES :
156
- language = TSL_CODES [TSL_NAMES .index (language .title ())]
157
- else :
158
- warnings .warn (f"'{ language } ' is probably not a supported language" )
151
+ def translate (language : str | Languages , text : str = "hello" ):
152
+ if language .lower () not in Languages .all_of ("code" , str .lower ):
153
+ if language .lower () in Languages .all_of ("name" , str .lower ):
154
+ language = Languages .find (language .lower (), apply_func = str .lower ).code
155
+
156
+ lang = Languages .find (language , "code" , str .lower )
157
+ if lang is None :
158
+ raise ValueError (f"{ language } is not a supported translate language" )
159
+
159
160
response_json = requests .get (
160
- f"https://translate-service.scratch.mit.edu/translate?language={ language } &text={ text } " ).json ()
161
+ f"https://translate-service.scratch.mit.edu/translate?language={ lang . code } &text={ text } " ).json ()
161
162
162
163
if "result" in response_json :
163
164
return response_json ["result" ]
@@ -182,22 +183,29 @@ def text2speech(text: str = "hello", gender: str = "female", language: str = "en
182
183
gender = ("male" , .84 )
183
184
elif gender == "kitten" :
184
185
gender = ("female" , 1.41 )
186
+ split = text .split (' ' )
187
+ text = ''
188
+ for token in split :
189
+ if token .strip () != '' :
190
+ text += "meow "
185
191
else :
186
192
gender = ("female" , 1 )
187
193
188
- if language not in TSL_NAMES :
189
- if language . lower () in TSL_NAMES :
190
- language = language .lower ()
194
+ og_lang = language
195
+ if isinstance ( language , Languages ) :
196
+ language = language .value . tts_locale
191
197
192
- elif language . title () in TSL_CODES :
193
- language = TSL_NAMES [ TSL_CODES . index ( language . title ())]
198
+ if language is None :
199
+ raise ValueError ( f"Language ' { og_lang } ' is not a supported tts language" )
194
200
195
- lang = tts_lang (language .title ())
196
- if lang is None :
197
- warnings .warn (f"Language '{ language } ' is probably not a supported language" )
198
- else :
199
- language = lang ["speechSynthLocale" ]
201
+ if language .lower () not in Languages .all_of ("tts_locale" , str .lower ):
202
+ if language .lower () in Languages .all_of ("name" , str .lower ):
203
+ language = Languages .find (language .lower (), apply_func = str .lower ).tts_locale
204
+
205
+ lang = Languages .find (language , "tts_locale" )
206
+ if lang is None or language is None :
207
+ raise ValueError (f"Language '{ og_lang } ' is not a supported tts language" )
200
208
201
209
response = requests .get (f"https://synthesis-service.scratch.mit.edu/synth"
202
- f"?locale={ language } &gender={ gender [0 ]} &text={ text } " )
210
+ f"?locale={ lang . tts_locale } &gender={ gender [0 ]} &text={ text } " )
203
211
return response .content , gender [1 ]
0 commit comments