AngeT10 commited on
Commit
af37368
1 Parent(s): 2b377db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -40
app.py CHANGED
@@ -15,7 +15,26 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
15
  print(f"Using device: {device}")
16
 
17
  # TTS model setup
18
- tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def download_audio_file(url: str) -> str:
21
  try:
@@ -40,51 +59,31 @@ def extract_zip_file(zip_file: str) -> bool:
40
  print(f"Error extracting zip file: {e}")
41
  return False
42
 
43
- def convert_to_wav(input_audio_file: str) -> str:
44
- file_extension = os.path.splitext(input_audio_file)[-1].lower()
45
- if file_extension!= ".wav":
46
- audio = AudioSegment.from_file(input_audio_file)
47
- audio.export("temp.wav", format="wav")
48
- os.remove(input_audio_file)
49
- return "temp.wav"
50
- return input_audio_file
51
-
52
- def synthesize_text(text: str, input_audio_file: str, language: str) -> str:
53
- input_audio_file = convert_to_wav(input_audio_file)
54
- tts.tts_to_file(text=text, speaker_wav=input_audio_file, language=language, file_path="./output.wav")
55
- return "./output.wav"
56
-
57
- @audio_processing
58
- def process_audio(input_audio_file: str) -> str:
59
- return input_audio_file
60
-
61
- def voice_clone(text: str, input_file: gr.File, language: str, url: gr.Audio) -> str:
62
- if input_file is not None:
63
- if input_file.name.endswith(".zip"):
64
- if extract_zip_file(input_file):
65
- input_audio_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(tuple(AUDIO_FORMATS))]
66
- if len(input_audio_file) == 1:
67
- input_audio_file = input_audio_file[0]
68
- else:
69
- return "Error: Please select a single audio file from the extracted files."
70
- else:
71
- input_audio_file = input_file.name
72
- elif url:
73
- input_audio_file = url.name
74
  else:
75
- return"Error: Please select a file or enter a URL"
76
- if language not in LANGUAGES:
77
- return "Error: Invalid language selected"
78
- output_file_path = synthesize_text(text, input_audio_file,language)
79
  return output_file_path
80
 
81
  iface = gr.Interface(
82
- fn=voice_clone,
83
- inputs=["text", gr.File(label="Input File", file_types=[".zip", *AUDIO_FORMATS]), gr.Dropdown(choices=LANGUAGES, label="Language"), gr.Audio(label="URL")],
84
  outputs=gr.Audio(type='filepath'),
85
  title='Voice Clone',
86
- description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
87
- theme=gr.themes.Base(primary_hue="green"),
 
 
88
  )
89
 
90
  iface.launch()
 
15
  print(f"Using device: {device}")
16
 
17
  # TTS model setup
18
+ MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
19
+ tts = TTS(MODEL_PATH).to(device)
20
+
21
+ class AudioProcessor:
22
+ def __init__(self):
23
+ pass
24
+
25
+ def convert_to_wav(self, input_audio_file: str) -> str:
26
+ file_extension = os.path.splitext(input_audio_file)[-1].lower()
27
+ if file_extension!= ".wav":
28
+ audio = AudioSegment.from_file(input_audio_file)
29
+ audio.export("temp.wav", format="wav")
30
+ os.remove(input_audio_file)
31
+ return "temp.wav"
32
+ return input_audio_file
33
+
34
+ def synthesize_text(self, text: str, input_audio_file: str, language: str) -> str:
35
+ input_audio_file = self.convert_to_wav(input_audio_file)
36
+ tts.tts_to_file(text=text, speaker_wav=input_audio_file, language=language, file_path="./output.wav")
37
+ return "./output.wav"
38
 
39
  def download_audio_file(url: str) -> str:
40
  try:
 
59
  print(f"Error extracting zip file: {e}")
60
  return False
61
 
62
+ def synthesize_audio(text: str, input_file: gr.File, language: str) -> str:
63
+ audio_processor = AudioProcessor()
64
+ if input_file is None:
65
+ return None
66
+ if input_file.name.endswith(".zip"):
67
+ if extract_zip_file(input_file):
68
+ input_audio_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(tuple(AUDIO_FORMATS))]
69
+ if len(input_audio_file) == 1:
70
+ input_audio_file = input_audio_file[0]
71
+ else:
72
+ return "Error: Please select a single audio file from the extracted files."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  else:
74
+ input_audio_file = input_file.name
75
+ output_file_path = audio_processor.synthesize_text(text, input_audio_file, language)
 
 
76
  return output_file_path
77
 
78
  iface = gr.Interface(
79
+ fn=synthesize_audio,
80
+ inputs=["text", gr.File(label="Input File", file_types=[".zip", *AUDIO_FORMATS]), gr.Dropdown(choices=LANGUAGES, label="Language")],
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. Clone any voice with a model and generate a speech waveform.""",
84
+ examples=[["Hello! My name is Voice Clone. What is your name?", None, "en"]],
85
+ height=600,
86
+ width=1200,
87
  )
88
 
89
  iface.launch()