AngeT10 commited on
Commit
4374369
·
verified ·
1 Parent(s): af37368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -62
app.py CHANGED
@@ -1,6 +1,5 @@
1
- import gradio as gr
2
  import os
3
- import requests
4
  import torch
5
  import zipfile
6
  from TTS.api import TTS
@@ -15,75 +14,39 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
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:
41
- response = requests.get(url)
42
- file_extension = os.path.splitext(url)[-1].lower()
43
- if file_extension not in AUDIO_FORMATS:
44
- raise ValueError(f"Unsupported file extension: {file_extension}")
45
- file_name = f"temp{file_extension}"
46
- with open(file_name, "wb") as f:
47
- f.write(response.content)
48
- return file_name
49
- except requests.exceptions.RequestException as e:
50
- print(f"Error downloading audio file: {e}")
51
- return None
52
 
53
- def extract_zip_file(zip_file: str) -> bool:
54
- try:
55
- with zipfile.ZipFile(zip_file, 'r') as zip_ref:
56
- zip_ref.extractall()
57
- return True
58
- except zipfile.BadZipfile as e:
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()
 
 
1
  import os
2
+ import gradio as gr
3
  import torch
4
  import zipfile
5
  from TTS.api import TTS
 
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=True,
45
+ audio_format="mp3",
46
+ title="Text-to-Speech",
47
+ description="Convert text to speech in multiple languages.",
48
+ allow_flagging=False,
49
+ cache_examples=False,
50
  )
51
 
52
  iface.launch()