AngeT10 commited on
Commit
9776097
1 Parent(s): cd3c050

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -30
app.py CHANGED
@@ -1,51 +1,87 @@
1
- import os
2
  import gradio as gr
 
 
3
  import torch
4
  import zipfile
5
  from TTS.api import TTS
6
  from pydub import AudioSegment
7
 
8
- # Constants
9
- AUDIO_FORMATS = [".wav", ".mp3", ".flac", ".mp4"]
 
10
  LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
 
11
 
12
- # Device setup
13
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
14
  print(f"Using device: {device}")
15
 
16
- # TTS model setup
17
- os.environ["COQUI_TOS_AGREED"] = "1"
18
- MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
19
  tts = TTS(MODEL_PATH).to(device)
20
 
21
- def generate_audio(text, language, speed, pitch, volume):
22
- # Prepare input
23
- input_text = {"text": text, "language": language}
24
- tts.prepare_input(input_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Generate audio
27
- audio = tts.generate_audio(input_text, speed=speed, pitch=pitch, volume=volume)
 
 
 
 
 
 
28
 
29
- # Save audio
30
- audio_path = "output.wav"
31
- tts.save_audio(audio_path, audio)
 
32
 
33
- # Convert to mp3
34
- audio_segment = AudioSegment.from_wav(audio_path)
35
- audio_segment.export(audio_path[:-4] + ".mp3", format="mp3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Return audio path
38
- return audio_path[:-4] + ".mp3"
39
 
40
  iface = gr.Interface(
41
- generate_audio,
42
- inputs=["text", "language", "speed", "pitch", "volume"],
43
- outputs="audio",
44
- audio_output_type="mp3",
45
- title="Text-to-Speech",
46
- description="Convert text to speech in multiple languages.",
47
- allow_flagging=False,
48
- cache_examples=False,
49
  )
50
 
51
- iface.launch()
 
 
1
  import gradio as gr
2
+ import os
3
+ import requests
4
  import torch
5
  import zipfile
6
  from TTS.api import TTS
7
  from pydub import AudioSegment
8
 
9
+ os.environ["COQUI_TOS_AGREED"] = "1"
10
+
11
+ MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
12
  LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
13
+ AUDIO_FORMATS = [".wav", ".mp3", ".flac", ".mp4"]
14
 
 
15
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
16
  print(f"Using device: {device}")
17
 
 
 
 
18
  tts = TTS(MODEL_PATH).to(device)
19
 
20
+ def download_audio_file(url):
21
+ try:
22
+ response = requests.get(url)
23
+ file_extension = os.path.splitext(url)[-1].lower()
24
+ file_name = f"temp{file_extension}"
25
+ with open(file_name, "wb") as f:
26
+ f.write(response.content)
27
+ return file_name
28
+ except requests.exceptions.RequestException as e:
29
+ print(f"Error downloading audio file: {e}")
30
+ return None
31
+
32
+ def extract_zip_file(zip_file):
33
+ try:
34
+ with zipfile.ZipFile(zip_file, 'r') as zip_ref:
35
+ zip_ref.extractall()
36
+ return True
37
+ except zipfile.BadZipfile as e:
38
+ print(f"Error extracting zip file: {e}")
39
+ return False
40
 
41
+ def convert_to_wav(input_audio_file):
42
+ file_extension = os.path.splitext(input_audio_file)[-1].lower()
43
+ if file_extension!= ".wav":
44
+ audio = AudioSegment.from_file(input_audio_file)
45
+ audio.export("temp.wav", format="wav")
46
+ os.remove(input_audio_file)
47
+ return "temp.wav"
48
+ return input_audio_file
49
 
50
+ def synthesize_text(text, input_audio_file, language):
51
+ input_audio_file = convert_to_wav(input_audio_file)
52
+ tts.tts_to_file(text=text, speaker_wav=input_audio_file, language=language, file_path="./output.wav")
53
+ return "./output.wav"
54
 
55
+ def clone(text, input_file, language, url=None, use_url=False):
56
+ if use_url:
57
+ if url is None:
58
+ return None
59
+ input_audio_file = download_audio_file(url)
60
+ if input_audio_file is None:
61
+ return None
62
+ else:
63
+ if input_file is None:
64
+ return None
65
+ if input_file.name.endswith(".zip"):
66
+ if extract_zip_file(input_file):
67
+ input_audio_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(tuple(AUDIO_FORMATS))]
68
+ if len(input_audio_file) == 1:
69
+ input_audio_file = input_audio_file[0]
70
+ else:
71
+ return "Error: Please select a single audio file from the extracted files."
72
+ else:
73
+ input_audio_file = input_file.name
74
 
75
+ output_file_path = synthesize_text(text, input_audio_file, language)
76
+ return output_file_path
77
 
78
  iface = gr.Interface(
79
+ fn=clone,
80
+ inputs=["text", gr.File(label="Input File", file_types=[".zip", *AUDIO_FORMATS]), gr.Dropdown(choices=LANGUAGES, label="Language"), gr.Text(label="URL"), gr.Checkbox(label="Use URL", value=False)],
81
+ outputs=gr.Audio(type='filepath'),
82
+ title='Voice Clone',
83
+ description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
84
+ theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")
 
 
85
  )
86
 
87
+ iface.launch(share=True)