File size: 4,384 Bytes
58b2f84
 
 
 
3251e7e
 
 
58b2f84
 
 
 
 
 
3251e7e
d3ac099
3251e7e
 
 
 
8ac4e72
3251e7e
 
 
 
 
 
 
 
 
 
 
 
58b2f84
3251e7e
8ac4e72
ca8f9fa
3251e7e
ca8f9fa
 
 
 
 
 
 
 
 
 
3251e7e
 
 
 
ca8f9fa
 
 
 
 
 
 
cfb5f6d
58b2f84
cfb5f6d
 
 
 
 
 
 
8ac4e72
3251e7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ac4e72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import subprocess
import os
import torch
import urllib.request
import librosa
from moviepy.editor import VideoFileClip
from TTS.api import TTS

# Set the device to CPU
device = "cpu"

os.environ["COQUI_TOS_AGREED"] = "1"

tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)

def convert_audio_to_wav(file_path):
    """Convert the given audio file to WAV format."""
    file_name = os.path.basename(file_path)
    file_ext = os.path.splitext(file_name)[1].lower()

    if file_ext == ".mp3":
        audio, sr = librosa.load(file_path)
        librosa.output.write_wav(f"temp_{file_name}", audio, sr)
        file_path = f"temp_{file_name}"
    elif file_ext == ".flac":
        os.system(f"ffmpeg -i {file_path} -acodec pcm_s16le -ar 16000 temp_{file_name}")
        file_path = f"temp_{file_name}"
    elif file_ext == ".mp4":
        clip = VideoFileClip(file_path, audio_codec="aac")
        audio = clip.audio
        audio.write_audiofile(f"temp_{file_name}")
        file_path = f"temp_{file_name}"

    return file_path

def clone(text, url, language):
    """Generate a voice clone using the given parameters."""
    response = requests.get(url)

    with open("temp.zip", "wb") as f:
        f.write(response.content)

    with zipfile.ZipFile("temp.zip", "r") as zip_ref:
        zip_ref.extractall()

    audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]

    # Convert the audio file to WAV format
    if os.path.splitext(audio_file)[1].lower() not in [".wav", ".flac"]:
        audio_file = convert_audio_to_wav(audio_file)

    tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")

    os.remove(audio_file)
    os.remove("temp.zip")

    return "./output.wav"

iface = gr.Interface(fn=clone,
                     inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"], label="Language")],
                     outputs=gr.Audio(type='filepath'),
                     title='Voice Clone',
                     description="""
                     by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ )
                    use this colab with caution <3.
                     """,
                     theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"))

iface.launch(share=True)

    with open("temp.zip", "wb") as f:
        f.write(response.content)

    with zipfile.ZipFile("temp.zip", "r") as zip_ref:
        zip_ref.extractall()

    audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]

    # Convert the audio file to the desired format
    if output_format == "mp3":
        audio, sr = librosa.load(audio_file)
        librosa.output.write_wav("temp.wav", audio, sr)
        os.system(f"ffmpeg -i temp.wav -acodec libmp3lame -qscale:a 4 {audio_file[:-4]}.mp3")
        os.remove("temp.wav")
        audio_file = f"{audio_file[:-4]}.mp3"
    elif output_format == "flac":
        os.system(f"ffmpeg -i {audio_file} {audio_file[:-4]}.flac")
        audio_file = f"{audio_file[:-4]}.flac"
    elif output_format == "mp4":
        clip = VideoFileClip(audio_file, audio_codec="aac")
        clip.write_videofile("output.mp4", fps=24, codec="libx264")
        audio_file = "output.mp4"

    tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")

    os.remove(audio_file)
    os.remove("temp.zip")

    return "./output.wav"

iface = gr.Interface(fn=clone,
                     inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"], label="Language"), gr.Dropdown(choices=["wav", "mp3", "flac", "mp4"], label="Output Format")],
                     outputs=gr.Audio(type='filepath'),
                     title='Voice Clone',
                     description="""
                     by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ )
                    use this colab with caution <3.
                     """,
                     theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"))

iface.launch(share=True)