Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# EZ-Voice-Clone-From-Long-Text
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from pathlib import Path
|
6 |
+
from pytube import YouTube
|
7 |
+
from pydub import AudioSegment
|
8 |
+
from TTS.api import TTS
|
9 |
+
import uuid
|
10 |
+
import os
|
11 |
+
|
12 |
+
test_audio="./shufflin.wav"
|
13 |
+
|
14 |
+
uid = uuid.uuid4()
|
15 |
+
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
def custom_bark(inp, in_aud=None, trim_aud=None, in_aud_mic=None):
|
19 |
+
if in_aud_mic != None:
|
20 |
+
speaker_wav=in_aud_mic
|
21 |
+
if in_aud !=None and trim_aud==None:
|
22 |
+
speaker_wav=in_aud
|
23 |
+
#speaker_wav=Path(f"{uid}-tmp_aud.mp4")
|
24 |
+
if trim_aud != None:
|
25 |
+
speaker_wav=Path(f"{uid}-trim.wav")
|
26 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False).to(device)
|
27 |
+
tts.tts_to_file(inp, speaker_wav=speaker_wav, language="en", file_path=f"{uid}-output.wav")
|
28 |
+
return (f"{uid}-output.wav")
|
29 |
+
|
30 |
+
def load_video_yt(vid):
|
31 |
+
yt = YouTube(vid)
|
32 |
+
vid = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename=f"{uid}-tmp.mp4")
|
33 |
+
vid_aud = yt.streams.filter(only_audio=True)[0].download(filename=f"{uid}-tmp_aud.mp4")
|
34 |
+
print (f'Video Length: {yt.length}')
|
35 |
+
return vid, vid_aud, f"{uid}-tmp_aud.mp4"
|
36 |
+
|
37 |
+
def trim_clip(clip, start_t, end_t):
|
38 |
+
clip = Path(f"{clip}")
|
39 |
+
song = AudioSegment.from_file(f"{clip}", format="mp4")
|
40 |
+
#song = AudioSegment.from_file(Path(f"{clip}"), format="mp4")
|
41 |
+
start_min = int(start_t.split(":",1)[0])
|
42 |
+
start_sec = int(start_t.split(":",1)[1])
|
43 |
+
end_min = int(end_t.split(":",1)[0])
|
44 |
+
end_sec = int(end_t.split(":",1)[1])
|
45 |
+
start = ((start_min*60)+start_sec)*1000
|
46 |
+
end = ((end_min*60)+end_sec)*1000
|
47 |
+
song_clip = song[start: end]
|
48 |
+
song_clip.export(f"{uid}-trim.wav", format="wav")
|
49 |
+
print("New Audio file is created and saved")
|
50 |
+
return f"{uid}-trim.wav"
|
51 |
+
|
52 |
+
def pre_aud(inp):
|
53 |
+
print(inp)
|
54 |
+
song = AudioSegment.from_file(Path(f'{inp}'), format="mp4")
|
55 |
+
song.export(f"{uid}-tmp_aud.mp4", format="mp4")
|
56 |
+
print(f'pre_aud:: {f"{uid}-tmp_aud.mp4"}')
|
57 |
+
return inp
|
58 |
+
|
59 |
+
with gr.Blocks() as app:
|
60 |
+
with gr.Group():
|
61 |
+
with gr.Row():
|
62 |
+
in_text = gr.Textbox(lines = 6, max_lines = 20)
|
63 |
+
with gr.Column():
|
64 |
+
alt_go_btn = gr.Button()
|
65 |
+
out_audio = gr.Audio(interactive=False)
|
66 |
+
|
67 |
+
with gr.Group():
|
68 |
+
with gr.Row():
|
69 |
+
gr.Markdown('''<H1> Audio Source:''')
|
70 |
+
with gr.Row():
|
71 |
+
|
72 |
+
with gr.Column():
|
73 |
+
#in_aud_mic = gr.Audio(source='microphone')
|
74 |
+
in_aud_file = gr.Audio(label = 'Audio Source', sources=['microphone','upload'], interactive = True,type='filepath', value=test_audio)
|
75 |
+
aud_file = gr.File(interactive=False,visible=True)
|
76 |
+
with gr.Row():
|
77 |
+
start_time = gr.Textbox(label = "Start", value = "0:00", placeholder = "0:23")
|
78 |
+
end_time = gr.Textbox(label = "End", value = "0:01", placeholder = "1:12")
|
79 |
+
trim_clip_btn = gr.Button("Trim Clip")
|
80 |
+
trim_aud = gr.Audio(label = 'Trimmed Audio Source', sources=['upload'], interactive = False)
|
81 |
+
with gr.Column():
|
82 |
+
in_aud_yt = gr.Textbox(label="YouTube URL")
|
83 |
+
load_yt_btn = gr.Button("Load URL")
|
84 |
+
yt_vid = gr.Video(interactive=False)
|
85 |
+
|
86 |
+
#in_aud_file.change(pre_aud,in_aud_file,aud_file)
|
87 |
+
load_yt_btn.click(load_video_yt, in_aud_yt, [yt_vid,in_aud_file,aud_file])
|
88 |
+
trim_clip_btn.click(trim_clip,[in_aud_file, start_time, end_time],trim_aud)
|
89 |
+
alt_go_btn.click(custom_bark, [in_text,in_aud_file,trim_aud], out_audio)
|
90 |
+
|
91 |
+
app.launch()
|