juliandreykorn
commited on
Commit
·
a9dae97
1
Parent(s):
8b65bcb
Adds speaker_wav capability
Browse files- handler.py +14 -6
handler.py
CHANGED
@@ -16,26 +16,34 @@ class EndpointHandler:
|
|
16 |
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
17 |
|
18 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
|
19 |
text = data['inputs']
|
20 |
if text == 'url':
|
21 |
response = requests.get(data.get('url'))
|
22 |
text = response.text
|
23 |
speaker = data.get('speaker', None)
|
24 |
-
|
25 |
language = data.get('language', 'en')
|
|
|
|
|
26 |
if speaker is not None:
|
27 |
if speaker in available_speakers:
|
28 |
-
|
29 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
30 |
-
output_file = f"/tmp/TTS_{timestamp}.wav"
|
31 |
-
self.tts.tts_to_file(text=text, file_path=output_file, speaker=speaker, language="en")
|
32 |
# Save or process the outputs as needed
|
33 |
result = self.upload_file_and_get_url(output_file)
|
34 |
os.remove(output_file)
|
35 |
return result
|
36 |
else:
|
37 |
return "Invalid speaker specified."
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return "No speaker specified."
|
40 |
|
41 |
def upload_file_and_get_url(self, file_path):
|
|
|
16 |
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
17 |
|
18 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
19 |
+
speaker_path = "/tmp/speaker.wav"
|
20 |
text = data['inputs']
|
21 |
if text == 'url':
|
22 |
response = requests.get(data.get('url'))
|
23 |
text = response.text
|
24 |
speaker = data.get('speaker', None)
|
25 |
+
speaker_wav_url = data.get('speaker_wav_url', None)
|
26 |
language = data.get('language', 'en')
|
27 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
28 |
+
output_file = f"/tmp/TTS_{timestamp}.wav"
|
29 |
if speaker is not None:
|
30 |
if speaker in available_speakers:
|
31 |
+
self.tts.tts_to_file(text=text, file_path=output_file, speaker=speaker, language=language)
|
|
|
|
|
|
|
32 |
# Save or process the outputs as needed
|
33 |
result = self.upload_file_and_get_url(output_file)
|
34 |
os.remove(output_file)
|
35 |
return result
|
36 |
else:
|
37 |
return "Invalid speaker specified."
|
38 |
+
elif speaker_wav_url is not None:
|
39 |
+
with open(speaker_path, 'wb') as file:
|
40 |
+
file.write(requests.get(speaker_wav_url).content)
|
41 |
+
self.tts.tts_to_file(text=text, file_path=output_file, speaker_wav=speaker_path, language=language)
|
42 |
+
# Save or process the outputs as needed
|
43 |
+
result = self.upload_file_and_get_url(output_file)
|
44 |
+
os.remove(output_file)
|
45 |
+
os.remove(speaker_path)
|
46 |
+
return result
|
47 |
return "No speaker specified."
|
48 |
|
49 |
def upload_file_and_get_url(self, file_path):
|