hoansu
commited on
Commit
·
98aa923
1
Parent(s):
270b26a
Add bark and vc code
Browse files- app.py +129 -4
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,7 +1,132 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
|
| 4 |
+
command = "git clone https://github.com/OlaWod/FreeVC.git"
|
| 5 |
+
subprocess.run(command, shell=True)
|
| 6 |
|
| 7 |
+
command = "git clone https://github.com/OlaWod/FreeVC.git"
|
| 8 |
+
subprocess.run(command, shell=True)
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import numpy as np
|
| 12 |
+
import os
|
| 13 |
+
from scipy.io.wavfile import write
|
| 14 |
+
import tempfile
|
| 15 |
+
import zipfile
|
| 16 |
+
import shutil
|
| 17 |
+
from pydub import AudioSegment
|
| 18 |
+
from pydub.silence import split_on_silence
|
| 19 |
+
from IPython.display import Audio
|
| 20 |
+
import nltk # we'll use this to split into sentences
|
| 21 |
+
import subprocess
|
| 22 |
+
|
| 23 |
+
from bark import SAMPLE_RATE, generate_audio, preload_models
|
| 24 |
+
from IPython.display import Audio, display
|
| 25 |
+
import numpy as np
|
| 26 |
+
|
| 27 |
+
from bark.generation import (
|
| 28 |
+
generate_text_semantic,
|
| 29 |
+
preload_models,
|
| 30 |
+
)
|
| 31 |
+
from bark.api import semantic_to_waveform
|
| 32 |
+
from bark import generate_audio, SAMPLE_RATE
|
| 33 |
+
|
| 34 |
+
# Preload models if necessary
|
| 35 |
+
preload_models()
|
| 36 |
+
|
| 37 |
+
def process_audio_files_with_logging(script, speaker, cloneFile):
|
| 38 |
+
log_messages = "Starting audio processing...\n"
|
| 39 |
+
sentences = script.split('\n')
|
| 40 |
+
sentences = [item.strip() for item in sentences if item.strip()]
|
| 41 |
+
GEN_TEMP = 0.4 # Example temperature, adjust as necessary
|
| 42 |
+
|
| 43 |
+
temp_dir = tempfile.mkdtemp()
|
| 44 |
+
|
| 45 |
+
for idx, sentence in enumerate(sentences):
|
| 46 |
+
log_messages += f"Processing sentence {idx + 1}: {sentence}\n"
|
| 47 |
+
semantic_tokens = generate_text_semantic(
|
| 48 |
+
sentence,
|
| 49 |
+
history_prompt=speaker,
|
| 50 |
+
temp=GEN_TEMP,
|
| 51 |
+
min_eos_p=0.05,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
audio_array = semantic_to_waveform(semantic_tokens, history_prompt=speaker)
|
| 55 |
+
filename = os.path.join(temp_dir, f"audio_{idx:02d}.wav")
|
| 56 |
+
write(filename, SAMPLE_RATE, audio_array)
|
| 57 |
+
log_messages += f"Generated audio for sentence {idx + 1}.\n"
|
| 58 |
+
|
| 59 |
+
log_messages += "All sentences processed. Starting silence reduction...\n"
|
| 60 |
+
|
| 61 |
+
# Process each file to remove or reduce silence
|
| 62 |
+
for root, _, files in os.walk(temp_dir):
|
| 63 |
+
with open("FreeVC/convert.txt", "w") as f:
|
| 64 |
+
for file in files:
|
| 65 |
+
file_path = os.path.join(root, file)
|
| 66 |
+
audio = AudioSegment.from_file(file_path, format="wav")
|
| 67 |
+
|
| 68 |
+
# Detect non-silent chunks and process
|
| 69 |
+
processed_audio = process_audio_for_silence(audio, log_messages)
|
| 70 |
+
|
| 71 |
+
# Overwrite the original file with processed audio
|
| 72 |
+
processed_audio.export(file_path, format="wav")
|
| 73 |
+
|
| 74 |
+
file_name_without_extension, file_extension = os.path.splitext(file)
|
| 75 |
+
line = f"{file_name_without_extension}|{file_path}|{cloneFile[0]}\n"
|
| 76 |
+
f.write(line)
|
| 77 |
+
log_messages += line + "\n"
|
| 78 |
+
#command = "python FreeVC/convert.py --hpfile FreeVC/configs/freevc.json --ptfile FreeVC/checkpoints/freevc.pth --txtpath FreeVC/convert.txt --outdir FreeVC/outputs/freevc"
|
| 79 |
+
#subprocess.run(command, shell=True)
|
| 80 |
+
log_messages += "Silence reduction complete. Zipping files...\n"
|
| 81 |
+
|
| 82 |
+
# Zip the processed files
|
| 83 |
+
zip_filename = zip_processed_files(temp_dir, log_messages)
|
| 84 |
+
|
| 85 |
+
# Clean up the temporary directory
|
| 86 |
+
shutil.rmtree(temp_dir)
|
| 87 |
+
|
| 88 |
+
log_messages += "Processing complete. Files ready for download.\n"
|
| 89 |
+
return zip_filename, log_messages
|
| 90 |
+
|
| 91 |
+
def process_audio_for_silence(audio, log_messages):
|
| 92 |
+
# Parameters for silence detection
|
| 93 |
+
silence_thresh = -32 # Silence threshold in dB
|
| 94 |
+
min_silence_len = 1000 # Minimum length of silence to consider in ms
|
| 95 |
+
keep_silence = 300 # Amount of silence to keep after the silence in ms
|
| 96 |
+
|
| 97 |
+
# Detect non-silent chunks
|
| 98 |
+
non_silent_chunks = split_on_silence(
|
| 99 |
+
audio,
|
| 100 |
+
min_silence_len=min_silence_len,
|
| 101 |
+
silence_thresh=silence_thresh,
|
| 102 |
+
keep_silence=keep_silence
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Combine the non-silent chunks back into a single audio segment
|
| 106 |
+
processed_audio = AudioSegment.empty()
|
| 107 |
+
for chunk in non_silent_chunks:
|
| 108 |
+
processed_audio += chunk
|
| 109 |
+
|
| 110 |
+
log_messages += "Audio processed for silence.\n"
|
| 111 |
+
return processed_audio
|
| 112 |
+
|
| 113 |
+
def zip_processed_files(temp_dir, log_messages):
|
| 114 |
+
zip_filename = os.path.join(tempfile.gettempdir(), "processed_audio_files.zip")
|
| 115 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
| 116 |
+
for root, _, files in os.walk(temp_dir):
|
| 117 |
+
for file in files:
|
| 118 |
+
zipf.write(os.path.join(root, file), file)
|
| 119 |
+
|
| 120 |
+
log_messages += "Files zipped successfully.\n"
|
| 121 |
+
return zip_filename
|
| 122 |
+
|
| 123 |
+
# Define the Gradio interface
|
| 124 |
+
interface = gr.Interface(
|
| 125 |
+
fn=process_audio_files_with_logging,
|
| 126 |
+
inputs=[gr.Textbox(label="Script", lines=10), gr.Dropdown(label="Speaker", choices=[("French","v2/fr_speaker_7"), ("English","v2/en_speaker_7"), ("Japanese","v2/ja_speaker_2"), ("German","v2/de_speaker_6"), ("Hindi","v2/hi_speaker_2"), ("Italian","v2/it_speaker_6"), ("Korean","v2/ko_speaker_0"), ("Polish","v2/pl_speaker_2"), ("Portuguese","v2/pt_speaker_5"), ("Russian","v2/ru_speaker_4"), ("Spanish","v2/es_speaker_0"), ("Turkish","v2/tr_speaker_1")]), gr.Files(label="clone voice")],
|
| 127 |
+
outputs=[gr.File(label="Download Processed Files"), gr.Textbox(label="Log Messages", lines=20)],
|
| 128 |
+
title="Audio Processing and Generation",
|
| 129 |
+
description="Enter a script and select a speaker to generate and process audio files. Process logs will be displayed below."
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
interface.launch()
|
requirements.txt
CHANGED
|
@@ -7,4 +7,6 @@ tensorboard
|
|
| 7 |
torch
|
| 8 |
torchvision
|
| 9 |
webrtcvad
|
|
|
|
|
|
|
| 10 |
git+https://github.com/suno-ai/bark.git
|
|
|
|
| 7 |
torch
|
| 8 |
torchvision
|
| 9 |
webrtcvad
|
| 10 |
+
pydub
|
| 11 |
+
nltk
|
| 12 |
git+https://github.com/suno-ai/bark.git
|