AngeT10 commited on
Commit
156316e
1 Parent(s): fef87f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import zipfile
5
  import requests
6
  from TTS.api import TTS
 
7
 
8
  # Set environment variable
9
  os.environ["COQUI_TOS_AGREED"] = "1"
@@ -11,6 +12,7 @@ os.environ["COQUI_TOS_AGREED"] = "1"
11
  # Define constants
12
  MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
13
  LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
 
14
 
15
  # Automatically detect and use GPU if available
16
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@@ -19,14 +21,35 @@ print(f"Using device: {device}")
19
  # Load TTS model
20
  tts = TTS(MODEL_PATH).to(device)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def clone(text, url, language):
23
- # Download and extract audio file
24
  response = requests.get(url)
25
  with open("temp.zip", "wb") as f:
26
  f.write(response.content)
 
 
27
  with zipfile.ZipFile("temp.zip", "r") as zip_ref:
28
- zip_ref.extractall()
29
- audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]
 
 
 
 
 
 
30
 
31
  # Generate audio using TTS model
32
  tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")
 
4
  import zipfile
5
  import requests
6
  from TTS.api import TTS
7
+ from pydub import AudioSegment
8
 
9
  # Set environment variable
10
  os.environ["COQUI_TOS_AGREED"] = "1"
 
12
  # Define constants
13
  MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
14
  LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
15
+ AUDIO_FORMATS = [".wav", ".mp3", ".flac", ".mp4"]
16
 
17
  # Automatically detect and use GPU if available
18
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
21
  # Load TTS model
22
  tts = TTS(MODEL_PATH).to(device)
23
 
24
+ def convert_to_wav(audio_file):
25
+ # Extract file extension
26
+ file_extension = os.path.splitext(audio_file)[-1].lower()
27
+
28
+ # Convert audio file to .wav format
29
+ if file_extension != ".wav":
30
+ audio = AudioSegment.from_file(audio_file)
31
+ audio.export("temp.wav", format="wav")
32
+ os.remove(audio_file)
33
+ audio_file = "temp.wav"
34
+
35
+ return audio_file
36
+
37
  def clone(text, url, language):
38
+ # Download zip file
39
  response = requests.get(url)
40
  with open("temp.zip", "wb") as f:
41
  f.write(response.content)
42
+
43
+ # Extract audio file from zip archive
44
  with zipfile.ZipFile("temp.zip", "r") as zip_ref:
45
+ for file in zip_ref.namelist():
46
+ if os.path.splitext(file)[-1].lower() in AUDIO_FORMATS:
47
+ zip_ref.extract(file, ".")
48
+ audio_file = file
49
+ break
50
+
51
+ # Convert audio file to .wav format
52
+ audio_file = convert_to_wav(audio_file)
53
 
54
  # Generate audio using TTS model
55
  tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")