juliandreykorn commited on
Commit
d03daf4
·
1 Parent(s): 5d79ae9

Updates handler

Browse files
Files changed (3) hide show
  1. .python-version +1 -0
  2. handler.py +42 -12
  3. requirements.txt +1 -0
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.9
handler.py CHANGED
@@ -1,29 +1,59 @@
1
- from TTS.tts.configs.xtts_config import XttsConfig
2
- from TTS.tts.models.xtts import Xtts
 
 
 
 
3
  from typing import Dict, List, Any
4
 
5
  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']
6
 
7
  class EndpointHandler:
8
  def __init__(self, path=""):
9
- config = XttsConfig()
10
- config.load_json(path + "/config.json")
11
- self.model = Xtts.init_from_config(config)
12
- self.model.load_checkpoint(config, checkpoint_dir=path, eval=True)
13
- self.model.cuda()
14
- self.config = config
15
-
16
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
17
  text = data['inputs']
 
 
 
18
  speaker = data.get('speaker', None)
19
- speaker_wav = data.get('speaker_wav', None)
 
20
  if speaker is not None:
21
  if speaker in available_speakers:
22
  #speaker_wav = f"/path/to/{speaker}.wav"
23
- outputs = self.model.synthesize(text, config, speaker=speaker, gpt_cond_len=3, language="en")
 
 
24
  # Save or process the outputs as needed
25
- return outputs
 
 
26
  else:
27
  return "Invalid speaker specified."
28
 
29
  return "No speaker specified."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import requests
3
+ import base64
4
+ import os
5
+ import datetime
6
+ from TTS.api import TTS
7
  from typing import Dict, List, Any
8
 
9
  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']
10
 
11
  class EndpointHandler:
12
  def __init__(self, path=""):
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
15
+
 
 
 
 
16
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
17
  text = data['inputs']
18
+ if text == 'url':
19
+ response = requests.get(data.get('url'))
20
+ text = response.text
21
  speaker = data.get('speaker', None)
22
+ speaker_wav = data.get('speaker_wav', None)
23
+ language = data.get('language', 'en')
24
  if speaker is not None:
25
  if speaker in available_speakers:
26
  #speaker_wav = f"/path/to/{speaker}.wav"
27
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
28
+ output_file = f"/tmp/TTS_{timestamp}.wav"
29
+ self.tts.tts_to_file(text=text, file_path=output_file, speaker=speaker, language="en")
30
  # Save or process the outputs as needed
31
+ result = self.upload_file_and_get_url(output_file)
32
+ os.remove(output_file)
33
+ return result
34
  else:
35
  return "Invalid speaker specified."
36
 
37
  return "No speaker specified."
38
+
39
+ def upload_file_and_get_url(self, file_path):
40
+ try:
41
+ url = 'https://file.io/'
42
+ files = {'file': open(file_path, 'rb')}
43
+ response = requests.post(url, files=files)
44
+ if response.status_code == 200:
45
+ return response.json()['link'] # Rückgabe der öffentlichen URL
46
+ else:
47
+ return f"Fehler beim Hochladen der Datei: {response.status_code}"
48
+ except Exception as e:
49
+ return f"Fehler: {e}"
50
+
51
+ def file_to_base64(self, file_path):
52
+ try:
53
+ with open(file_path, 'rb') as file:
54
+ encoded_string = base64.b64encode(file.read()).decode('utf-8')
55
+ return encoded_string
56
+ except Exception as e:
57
+ return f"Fehler: {e}"
58
+
59
+
requirements.txt CHANGED
@@ -1 +1,2 @@
1
  TTS
 
 
1
  TTS
2
+ requests