Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,10 @@ import numpy as np
|
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
| 8 |
from transformers import AutoProcessor, BarkModel
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
device = "cpu"
|
| 12 |
processor = AutoProcessor.from_pretrained("suno/bark-small")
|
| 13 |
model = BarkModel.from_pretrained("suno/bark-small").to(device)
|
| 14 |
|
|
@@ -23,7 +24,7 @@ BARK_VOICES = {
|
|
| 23 |
|
| 24 |
def generate_bark_acting_segment(text, voice_preset, output_path):
|
| 25 |
"""
|
| 26 |
-
Gera áudio com Bark
|
| 27 |
"""
|
| 28 |
inputs = processor(text, voice_preset=voice_preset)
|
| 29 |
with torch.no_grad():
|
|
@@ -31,24 +32,62 @@ def generate_bark_acting_segment(text, voice_preset, output_path):
|
|
| 31 |
|
| 32 |
sampling_rate = model.generation_config.sample_rate
|
| 33 |
audio_data = speech_output.cpu().numpy().squeeze()
|
| 34 |
-
|
| 35 |
-
wavfile.write(output_path, rate=sampling_rate, data=(audio_data * 32767).astype(np.int16))
|
| 36 |
|
| 37 |
-
|
| 38 |
-
if
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
work_dir = "/tmp/dubbing_bark_workdir"
|
| 43 |
os.makedirs(work_dir, exist_ok=True)
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
extracted_audio = os.path.join(work_dir, "extracted.mp3")
|
| 46 |
muted_video = os.path.join(work_dir, "muted.mp4")
|
| 47 |
concat_list_path = os.path.join(work_dir, "concat.txt")
|
| 48 |
output_video_path = "video_dublado_bark.mp4"
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
# 1. Isolar Áudio e Vídeo Muto (
|
| 52 |
progress(0.1, desc="Extraindo áudio e processando estrutura do vídeo...")
|
| 53 |
subprocess.run([
|
| 54 |
"ffmpeg", "-y", "-i", video_path,
|
|
@@ -60,7 +99,7 @@ def process_dubbing_bark(video_path, voice_name, progress=gr.Progress()):
|
|
| 60 |
"-an", "-vcodec", "libx264", "-preset", "ultrafast", muted_video
|
| 61 |
], check=True)
|
| 62 |
|
| 63 |
-
# 2. Transcrição com Minutagem via Groq API
|
| 64 |
progress(0.25, desc="Transcrevendo via Groq (Whisper Large V3)...")
|
| 65 |
with open(extracted_audio, "rb") as audio_file:
|
| 66 |
transcript = groq_client.audio.transcriptions.create(
|
|
@@ -145,7 +184,11 @@ with gr.Blocks(title="Dublador Atuado com Bark + Groq") as demo:
|
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
with gr.Column():
|
| 148 |
-
video_input = gr.Video(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
voice_option = gr.Dropdown(
|
| 150 |
choices=list(BARK_VOICES.keys()),
|
| 151 |
value="Português - Voz 1 (Expressiva)",
|
|
@@ -159,7 +202,7 @@ with gr.Blocks(title="Dublador Atuado com Bark + Groq") as demo:
|
|
| 159 |
|
| 160 |
submit_btn.click(
|
| 161 |
fn=process_dubbing_bark,
|
| 162 |
-
inputs=[video_input, voice_option],
|
| 163 |
outputs=[video_output, status_output]
|
| 164 |
)
|
| 165 |
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
| 8 |
from transformers import AutoProcessor, BarkModel
|
| 9 |
+
import yt_dlp
|
| 10 |
|
| 11 |
+
# Configura o modelo Bark via transformers
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
processor = AutoProcessor.from_pretrained("suno/bark-small")
|
| 14 |
model = BarkModel.from_pretrained("suno/bark-small").to(device)
|
| 15 |
|
|
|
|
| 24 |
|
| 25 |
def generate_bark_acting_segment(text, voice_preset, output_path):
|
| 26 |
"""
|
| 27 |
+
Gera áudio com Bark garantindo normalização e salvamento correto em formato WAV.
|
| 28 |
"""
|
| 29 |
inputs = processor(text, voice_preset=voice_preset)
|
| 30 |
with torch.no_grad():
|
|
|
|
| 32 |
|
| 33 |
sampling_rate = model.generation_config.sample_rate
|
| 34 |
audio_data = speech_output.cpu().numpy().squeeze()
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Normalização segura para evitar clippagem e arquivos WAV corrompidos
|
| 37 |
+
if np.max(np.abs(audio_data)) > 0:
|
| 38 |
+
audio_data = audio_data / np.max(np.abs(audio_data))
|
| 39 |
+
|
| 40 |
+
audio_data_int16 = (audio_data * 32767).astype(np.int16)
|
| 41 |
+
wavfile.write(output_path, rate=sampling_rate, data=audio_data_int16)
|
| 42 |
|
| 43 |
+
def download_youtube_video(youtube_url, output_dir):
|
| 44 |
+
"""
|
| 45 |
+
Baixa vídeo do YouTube usando yt-dlp em formato MP4 compatível.
|
| 46 |
+
"""
|
| 47 |
+
out_template = os.path.join(output_dir, "yt_download.%(ext)s")
|
| 48 |
+
ydl_opts = {
|
| 49 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 50 |
+
'outtmpl': out_template,
|
| 51 |
+
'quiet': True,
|
| 52 |
+
'no_warnings': True,
|
| 53 |
+
'overwrites': True
|
| 54 |
+
}
|
| 55 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 56 |
+
info = ydl.extract_info(youtube_url, download=True)
|
| 57 |
+
filename = ydl.prepare_filename(info)
|
| 58 |
+
# Se for baixado com extensão diferente (ex: .mkv ou .webm), convertemos o caminho
|
| 59 |
+
if not filename.endswith('.mp4') and os.path.exists(filename):
|
| 60 |
+
mp4_path = os.path.join(output_dir, "yt_download.mp4")
|
| 61 |
+
subprocess.run([
|
| 62 |
+
"ffmpeg", "-y", "-i", filename, "-c:v", "libx264", "-preset", "ultrafast", mp4_path
|
| 63 |
+
], check=True)
|
| 64 |
+
return mp4_path
|
| 65 |
+
return filename
|
| 66 |
+
|
| 67 |
+
def process_dubbing_bark(video_file, youtube_url, voice_name, progress=gr.Progress()):
|
| 68 |
work_dir = "/tmp/dubbing_bark_workdir"
|
| 69 |
os.makedirs(work_dir, exist_ok=True)
|
| 70 |
+
|
| 71 |
+
# Resolve a fonte do vídeo (Upload ou Link do YouTube)
|
| 72 |
+
if youtube_url and youtube_url.strip():
|
| 73 |
+
progress(0.05, desc="Baixando vídeo do YouTube...")
|
| 74 |
+
try:
|
| 75 |
+
video_path = download_youtube_video(youtube_url.strip(), work_dir)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return None, f"Erro ao baixar do YouTube: {str(e)}"
|
| 78 |
+
elif video_file:
|
| 79 |
+
video_path = video_file
|
| 80 |
+
else:
|
| 81 |
+
return None, "Por favor, envie um arquivo de vídeo ou cole uma URL do YouTube."
|
| 82 |
+
|
| 83 |
+
voice_preset = BARK_VOICES.get(voice_name, "v2/pt_speaker_1")
|
| 84 |
extracted_audio = os.path.join(work_dir, "extracted.mp3")
|
| 85 |
muted_video = os.path.join(work_dir, "muted.mp4")
|
| 86 |
concat_list_path = os.path.join(work_dir, "concat.txt")
|
| 87 |
output_video_path = "video_dublado_bark.mp4"
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
# 1. Isolar Áudio e Vídeo Muto (Garante padrão H.264 / MP4)
|
| 91 |
progress(0.1, desc="Extraindo áudio e processando estrutura do vídeo...")
|
| 92 |
subprocess.run([
|
| 93 |
"ffmpeg", "-y", "-i", video_path,
|
|
|
|
| 99 |
"-an", "-vcodec", "libx264", "-preset", "ultrafast", muted_video
|
| 100 |
], check=True)
|
| 101 |
|
| 102 |
+
# 2. Transcrição com Minutagem via Groq API
|
| 103 |
progress(0.25, desc="Transcrevendo via Groq (Whisper Large V3)...")
|
| 104 |
with open(extracted_audio, "rb") as audio_file:
|
| 105 |
transcript = groq_client.audio.transcriptions.create(
|
|
|
|
| 184 |
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column():
|
| 187 |
+
video_input = gr.Video(label="Envie um Arquivo de Vídeo")
|
| 188 |
+
url_input = gr.Textbox(
|
| 189 |
+
label="Ou cole uma URL do YouTube",
|
| 190 |
+
placeholder="https://www.youtube.com/watch?v=..."
|
| 191 |
+
)
|
| 192 |
voice_option = gr.Dropdown(
|
| 193 |
choices=list(BARK_VOICES.keys()),
|
| 194 |
value="Português - Voz 1 (Expressiva)",
|
|
|
|
| 202 |
|
| 203 |
submit_btn.click(
|
| 204 |
fn=process_dubbing_bark,
|
| 205 |
+
inputs=[video_input, url_input, voice_option],
|
| 206 |
outputs=[video_output, status_output]
|
| 207 |
)
|
| 208 |
|