AngeT10 commited on
Commit
81e5784
1 Parent(s): 275c9a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -5,7 +5,6 @@ import zipfile
5
  import requests
6
  from TTS.api import TTS
7
  from pydub import AudioSegment
8
- import tempfile
9
 
10
  # Set environment variable
11
  os.environ["COQUI_TOS_AGREED"] = "1"
@@ -22,51 +21,51 @@ print(f"Using device: {device}")
22
  # Load TTS model
23
  tts = TTS(MODEL_PATH).to(device)
24
 
25
- def download_audio_from_url(url: str) -> str:
26
- try:
27
- response = requests.get(url)
28
- response.raise_for_status()
29
- with tempfile.NamedTemporaryFile(suffix=".wav") as tmp:
30
- tmp.write(response.content)
31
- return tmp.name
32
- except requests.RequestException as e:
33
- print(f"Error downloading audio: {e}")
34
- return None
35
-
36
- def convert_audio_to_wav(audio_file: str) -> str:
37
  file_extension = os.path.splitext(audio_file)[-1].lower()
38
- if file_extension!= ".wav":
 
 
39
  audio = AudioSegment.from_file(audio_file)
40
- with tempfile.NamedTemporaryFile(suffix=".wav") as tmp:
41
- audio.export(tmp.name, format="wav")
42
- return tmp.name
 
43
  return audio_file
44
 
45
- def generate_audio(text: str, audio_file: str, language: str) -> str:
46
- try:
47
- tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")
48
- return "./output.wav"
49
- except Exception as e:
50
- print(f"Error generating audio: {e}")
51
- return None
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- def clone(text: str, audio_or_url: str, language: str) -> str:
54
- if isinstance(audio_or_url, str):
55
- audio_file = download_audio_from_url(audio_or_url)
56
- else:
57
- with tempfile.NamedTemporaryFile(suffix=".wav") as tmp:
58
- tmp.write(audio_or_url.read())
59
- audio_file = tmp.name
60
 
61
- audio_file = convert_audio_to_wav(audio_file)
62
- return generate_audio(text, audio_file, language)
63
 
64
  # Create Gradio interface
65
  iface = gr.Interface(
66
  fn=clone,
67
- inputs=["text", gr.Audio(label="Upload audio file or enter URL"), gr.Dropdown(choices=LANGUAGES, label="Language")],
68
- output_type="filepath",
69
- title="Voice Clone",
70
  description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
71
  theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")
72
  )
 
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"
 
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")
56
 
57
+ # Clean up
58
+ os.remove(audio_file)
59
+ os.remove("temp.zip")
 
 
 
 
60
 
61
+ return "./output.wav"
 
62
 
63
  # Create Gradio interface
64
  iface = gr.Interface(
65
  fn=clone,
66
+ inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=LANGUAGES, label="Language")],
67
+ outputs=gr.Audio(type='filepath'),
68
+ title='Voice Clone',
69
  description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
70
  theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")
71
  )