|
import torch |
|
import requests |
|
import base64 |
|
import os |
|
import shutil |
|
import datetime |
|
from TTS.api import TTS |
|
from typing import Dict, List, Any |
|
|
|
available_speakers = ['Claribel Dervla', 'Daisy Studious', 'Gracie Wise', 'Tammie Ema', 'Alison Dietlinde', 'Ana Florence', 'Annmarie Nele', 'Asya Anara', 'Brenda Stern', 'Gitta Nikolina', 'Henriette Usha', 'Sofia Hellen', 'Tammy Grit', 'Tanja Adelina', 'Vjollca Johnnie', 'Andrew Chipper', 'Badr Odhiambo', 'Dionisio Schuyler', 'Royston Min', 'Viktor Eka', 'Abrahan Mack', 'Adde Michal', 'Baldur Sanjin', 'Craig Gutsy', 'Damien Black', 'Gilberto Mathias', 'Ilkin Urbano', 'Kazuhiko Atallah', 'Ludvig Milivoj', 'Suad Qasim', 'Torcull Diarmuid', 'Viktor Menelaos', 'Zacharie Aimilios', 'Nova Hogarth', 'Maja Ruoho', 'Uta Obando', 'Lidiya Szekeres', 'Chandra MacFarland', 'Szofi Granger', 'Camilla Holmström', 'Lilya Stainthorpe', 'Zofija Kendrick', 'Narelle Moon', 'Barbora MacLean', 'Alexandra Hisakawa', 'Alma María', 'Rosemary Okafor', 'Ige Behringer', 'Filip Traverse', 'Damjan Chapman', 'Wulf Carlevaro', 'Aaron Dreschner', 'Kumar Dahl', 'Eugenio Mataracı', 'Ferran Simen', 'Xavier Hayasaka', 'Luis Moray', 'Marcos Rudaski'] |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
self.agree_tos() |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
text = data['inputs'] |
|
if text == 'url': |
|
response = requests.get(data.get('url')) |
|
text = response.text |
|
speaker = data.get('speaker', None) |
|
speaker_wav = data.get('speaker_wav', None) |
|
language = data.get('language', 'en') |
|
if speaker is not None: |
|
if speaker in available_speakers: |
|
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
|
output_file = f"/tmp/TTS_{timestamp}.wav" |
|
self.tts.tts_to_file(text=text, file_path=output_file, speaker=speaker, language="en") |
|
|
|
result = self.upload_file_and_get_url(output_file) |
|
os.remove(output_file) |
|
return result |
|
else: |
|
return "Invalid speaker specified." |
|
|
|
return "No speaker specified." |
|
|
|
def upload_file_and_get_url(self, file_path): |
|
try: |
|
url = 'https://file.io/' |
|
files = {'file': open(file_path, 'rb')} |
|
response = requests.post(url, files=files) |
|
if response.status_code == 200: |
|
return response.json()['link'] |
|
else: |
|
return f"Fehler beim Hochladen der Datei: {response.status_code}" |
|
except Exception as e: |
|
return f"Fehler: {e}" |
|
|
|
def file_to_base64(self, file_path): |
|
try: |
|
with open(file_path, 'rb') as file: |
|
encoded_string = base64.b64encode(file.read()).decode('utf-8') |
|
return encoded_string |
|
except Exception as e: |
|
return f"Fehler: {e}" |
|
|
|
|
|
def agree_tos(self): |
|
home_dir = os.path.expanduser('~') |
|
target_folder = os.path.join(home_dir, '.local', 'share', 'tts', 'tts_models--multilingual--multi-dataset--xtts_v2') |
|
if not os.path.exists(target_folder): |
|
os.makedirs(target_folder) |
|
target_file = os.path.join(target_folder, 'tos_agreed.txt') |
|
text_to_write = "I have read, understood and agreed to the Terms and Conditions." |
|
with open(target_file, 'w') as file: |
|
file.write(text_to_write) |
|
|