Spaces:
Runtime error
app.py
Browse filesimport gradio as gr
from transformers import pipeline
import soundfile as sf
from moviepy.editor import VideoClip, AudioFileClip
from PIL import Image, ImageDraw, ImageFont
import numpy as np
# ===== Criar pipeline TTS =====
tts_model = pipeline(
"text-to-speech",
model="espnet/kan-bayashi_ljspeech_tts_train_tacotron2"
)
def gerar_video_premium(prompt_texto):
"""
Função:
1. Gera áudio via TTS
2. Cria vídeo animado com cores dinâmicas
3. Adiciona áudio
4. Retorna caminho do vídeo
"""
# ===== Gerar áudio =====
tts_output = tts_model(prompt_texto)
sf.write("audio.wav", tts_output["array"], samplerate=tts_output["sampling_rate"])
audio_clip = AudioFileClip("audio.wav")
duracao = audio_clip.duration
# ===== Função de frame animado =====
largura, altura = 1280, 720
def make_frame(t):
# Fundo animado com cores oscilando
frame = np.zeros((altura, largura, 3), dtype=np.uint8)
r = int((np.sin(t*2*np.pi/5) + 1) * 127)
g = int((np.sin(t*2*np.pi/3 + 1) + 1) * 127)
b = int((np.sin(t*2*np.pi/4 + 2) + 1) * 127)
frame[:, :, 0] = r
frame[:, :, 1] = g
frame[:, :, 2] = b
# Adicionar texto centralizado
pil_img = Image.fromarray(frame)
draw = ImageDraw.Draw(pil_img)
font = ImageFont.load_default()
text_size = draw.textsize(prompt_texto, font=font)
draw.text(
((largura - text_size[0]) / 2, (altura - text_size[1]) / 2),
prompt_texto, fill=(255, 255, 255), font=font
)
return np.array(pil_img)
# ===== Criar vídeo =====
video_clip = VideoClip(make_frame, duration=duracao).set_audio(audio_clip)
# ===== Salvar vídeo =====
video_clip.write_videofile("video_final.mp4", fps=24, codec="libx264", audio_codec="aac")
return "video_final.mp4"
# ===== Interface Gradio =====
iface = gr.Interface(
fn=gerar_video_premium,
inputs=gr.Textbox(label="Digite o prompt para gerar vídeo e áudio"),
outputs=gr.Video(label="Vídeo gerado"),
title="Gerador Premium de Vídeo + Áudio",
description="Digite qualquer prompt. O Space gera um vídeo animado profissional com TTS."
)
iface.launch()

|
@@ -1,35 +1,69 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import soundfile as sf
|
| 3 |
-
from moviepy.editor import
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
-
# =====
|
| 8 |
tts_model = pipeline(
|
| 9 |
"text-to-speech",
|
| 10 |
model="espnet/kan-bayashi_ljspeech_tts_train_tacotron2"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# =====
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
font = ImageFont.load_default()
|
| 24 |
-
draw.text((50, altura//2 - 10), texto, fill=(255, 255, 255), font=font)
|
| 25 |
-
img.save("background.png")
|
| 26 |
-
|
| 27 |
-
# ===== 4️⃣ Criar clipe de vídeo =====
|
| 28 |
-
audio_clip = AudioFileClip("saida.wav")
|
| 29 |
-
image_clip = ImageClip("background.png").set_duration(audio_clip.duration)
|
| 30 |
-
video_clip = image_clip.set_audio(audio_clip)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
video_clip.write_videofile("saida.mp4", fps=24)
|
| 34 |
|
| 35 |
-
print("Vídeo gerado com sucesso! Verifique o arquivo 'saida.mp4'.")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import soundfile as sf
|
| 4 |
+
from moviepy.editor import VideoClip, AudioFileClip
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# ===== Criar pipeline TTS =====
|
| 9 |
tts_model = pipeline(
|
| 10 |
"text-to-speech",
|
| 11 |
model="espnet/kan-bayashi_ljspeech_tts_train_tacotron2"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
def gerar_video_premium(prompt_texto):
|
| 15 |
+
"""
|
| 16 |
+
Função:
|
| 17 |
+
1. Gera áudio via TTS
|
| 18 |
+
2. Cria vídeo animado com cores dinâmicas
|
| 19 |
+
3. Adiciona áudio
|
| 20 |
+
4. Retorna caminho do vídeo
|
| 21 |
+
"""
|
| 22 |
+
# ===== Gerar áudio =====
|
| 23 |
+
tts_output = tts_model(prompt_texto)
|
| 24 |
+
sf.write("audio.wav", tts_output["array"], samplerate=tts_output["sampling_rate"])
|
| 25 |
+
audio_clip = AudioFileClip("audio.wav")
|
| 26 |
+
duracao = audio_clip.duration
|
| 27 |
+
|
| 28 |
+
# ===== Função de frame animado =====
|
| 29 |
+
largura, altura = 1280, 720
|
| 30 |
+
def make_frame(t):
|
| 31 |
+
# Fundo animado com cores oscilando
|
| 32 |
+
frame = np.zeros((altura, largura, 3), dtype=np.uint8)
|
| 33 |
+
r = int((np.sin(t*2*np.pi/5) + 1) * 127)
|
| 34 |
+
g = int((np.sin(t*2*np.pi/3 + 1) + 1) * 127)
|
| 35 |
+
b = int((np.sin(t*2*np.pi/4 + 2) + 1) * 127)
|
| 36 |
+
frame[:, :, 0] = r
|
| 37 |
+
frame[:, :, 1] = g
|
| 38 |
+
frame[:, :, 2] = b
|
| 39 |
+
|
| 40 |
+
# Adicionar texto centralizado
|
| 41 |
+
pil_img = Image.fromarray(frame)
|
| 42 |
+
draw = ImageDraw.Draw(pil_img)
|
| 43 |
+
font = ImageFont.load_default()
|
| 44 |
+
text_size = draw.textsize(prompt_texto, font=font)
|
| 45 |
+
draw.text(
|
| 46 |
+
((largura - text_size[0]) / 2, (altura - text_size[1]) / 2),
|
| 47 |
+
prompt_texto, fill=(255, 255, 255), font=font
|
| 48 |
+
)
|
| 49 |
+
return np.array(pil_img)
|
| 50 |
+
|
| 51 |
+
# ===== Criar vídeo =====
|
| 52 |
+
video_clip = VideoClip(make_frame, duration=duracao).set_audio(audio_clip)
|
| 53 |
+
|
| 54 |
+
# ===== Salvar vídeo =====
|
| 55 |
+
video_clip.write_videofile("video_final.mp4", fps=24, codec="libx264", audio_codec="aac")
|
| 56 |
+
|
| 57 |
+
return "video_final.mp4"
|
| 58 |
|
| 59 |
+
# ===== Interface Gradio =====
|
| 60 |
+
iface = gr.Interface(
|
| 61 |
+
fn=gerar_video_premium,
|
| 62 |
+
inputs=gr.Textbox(label="Digite o prompt para gerar vídeo e áudio"),
|
| 63 |
+
outputs=gr.Video(label="Vídeo gerado"),
|
| 64 |
+
title="Gerador Premium de Vídeo + Áudio",
|
| 65 |
+
description="Digite qualquer prompt. O Space gera um vídeo animado profissional com TTS."
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
iface.launch()
|
|
|
|
| 69 |
|
|
|