Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,53 +6,54 @@ import asyncio
|
|
| 6 |
import edge_tts
|
| 7 |
from datetime import datetime
|
| 8 |
from moviepy.editor import (
|
| 9 |
-
VideoFileClip, AudioFileClip,
|
| 10 |
-
|
| 11 |
)
|
| 12 |
from transformers import pipeline
|
|
|
|
| 13 |
import nest_asyncio
|
| 14 |
|
| 15 |
nest_asyncio.apply()
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
generador = pipeline("text-generation", model="gpt2")
|
| 19 |
|
| 20 |
-
# Obtener voces
|
| 21 |
async def get_voices():
|
| 22 |
-
|
| 23 |
-
return voices
|
| 24 |
|
| 25 |
VOICES = asyncio.run(get_voices())
|
| 26 |
-
VOICE_OPTIONS = [
|
| 27 |
-
f"{v['Name']} ({v['Gender']}, {v['Locale']})" for v in VOICES
|
| 28 |
-
]
|
| 29 |
VOICE_MAP = {v['Name']: v['ShortName'] for v in VOICES}
|
| 30 |
|
| 31 |
-
#
|
| 32 |
def buscar_videos_mock():
|
| 33 |
return [
|
| 34 |
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
|
| 35 |
"https://samplelib.com/lib/preview/mp4/sample-10s.mp4"
|
| 36 |
]
|
| 37 |
|
| 38 |
-
#
|
| 39 |
def buscar_musica_mock():
|
| 40 |
return "https://samplelib.com/lib/preview/mp3/sample-3s.mp3"
|
| 41 |
|
| 42 |
# Funci贸n principal
|
| 43 |
async def generar_video(prompt, voz_str):
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
texto = generador(prompt, max_length=
|
| 47 |
|
| 48 |
-
|
| 49 |
voz_id = voz_str.split(" ")[0]
|
| 50 |
short_name = VOICE_MAP.get(voz_id, "es-ES-ElviraNeural")
|
| 51 |
voz_path = "voz.mp3"
|
| 52 |
await edge_tts.Communicate(text=texto, voice=short_name).save(voz_path)
|
| 53 |
voz_clip = AudioFileClip(voz_path)
|
| 54 |
|
| 55 |
-
|
| 56 |
video_urls = buscar_videos_mock()
|
| 57 |
clips = []
|
| 58 |
for url in video_urls:
|
|
@@ -61,11 +62,12 @@ async def generar_video(prompt, voz_str):
|
|
| 61 |
for chunk in r.iter_content(1024 * 1024):
|
| 62 |
f.write(chunk)
|
| 63 |
f.flush()
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
video = concatenate_videoclips(clips).set_audio(voz_clip)
|
| 67 |
|
| 68 |
-
|
| 69 |
music_url = buscar_musica_mock()
|
| 70 |
r = requests.get(music_url, stream=True)
|
| 71 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
|
@@ -75,28 +77,29 @@ async def generar_video(prompt, voz_str):
|
|
| 75 |
music_clip = AudioFileClip(f.name)
|
| 76 |
music_loop = afx.audio_loop(music_clip, duration=video.duration).volumex(0.3)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
video = video.set_audio(
|
| 81 |
|
| 82 |
-
# 6. Guardar
|
| 83 |
output_path = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
return output_path
|
| 87 |
except Exception as e:
|
|
|
|
| 88 |
return f"Error: {e}"
|
| 89 |
|
| 90 |
-
# Interfaz
|
| 91 |
-
with gr.Blocks() as
|
| 92 |
-
prompt = gr.Textbox(label="
|
| 93 |
-
|
| 94 |
-
btn = gr.Button("Generar
|
| 95 |
-
|
| 96 |
|
| 97 |
btn.click(fn=lambda p, v: asyncio.run(generar_video(p, v)),
|
| 98 |
-
inputs=[prompt,
|
| 99 |
-
outputs=
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
|
|
|
|
| 6 |
import edge_tts
|
| 7 |
from datetime import datetime
|
| 8 |
from moviepy.editor import (
|
| 9 |
+
VideoFileClip, AudioFileClip, concatenate_videoclips,
|
| 10 |
+
CompositeAudioClip, afx
|
| 11 |
)
|
| 12 |
from transformers import pipeline
|
| 13 |
+
import logging
|
| 14 |
import nest_asyncio
|
| 15 |
|
| 16 |
nest_asyncio.apply()
|
| 17 |
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# Generador de texto real con GPT-2
|
| 22 |
generador = pipeline("text-generation", model="gpt2")
|
| 23 |
|
| 24 |
+
# Obtener voces
|
| 25 |
async def get_voices():
|
| 26 |
+
return await edge_tts.list_voices()
|
|
|
|
| 27 |
|
| 28 |
VOICES = asyncio.run(get_voices())
|
| 29 |
+
VOICE_OPTIONS = [f"{v['Name']} ({v['Gender']}, {v['Locale']})" for v in VOICES]
|
|
|
|
|
|
|
| 30 |
VOICE_MAP = {v['Name']: v['ShortName'] for v in VOICES}
|
| 31 |
|
| 32 |
+
# Simulaci贸n de b煤squeda de videos
|
| 33 |
def buscar_videos_mock():
|
| 34 |
return [
|
| 35 |
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
|
| 36 |
"https://samplelib.com/lib/preview/mp4/sample-10s.mp4"
|
| 37 |
]
|
| 38 |
|
| 39 |
+
# Simulaci贸n de m煤sica de fondo
|
| 40 |
def buscar_musica_mock():
|
| 41 |
return "https://samplelib.com/lib/preview/mp3/sample-3s.mp3"
|
| 42 |
|
| 43 |
# Funci贸n principal
|
| 44 |
async def generar_video(prompt, voz_str):
|
| 45 |
try:
|
| 46 |
+
logger.info("Generando guion...")
|
| 47 |
+
texto = generador(prompt, max_length=500, do_sample=True, truncation=True)[0]['generated_text']
|
| 48 |
|
| 49 |
+
logger.info("Convirtiendo texto en voz...")
|
| 50 |
voz_id = voz_str.split(" ")[0]
|
| 51 |
short_name = VOICE_MAP.get(voz_id, "es-ES-ElviraNeural")
|
| 52 |
voz_path = "voz.mp3"
|
| 53 |
await edge_tts.Communicate(text=texto, voice=short_name).save(voz_path)
|
| 54 |
voz_clip = AudioFileClip(voz_path)
|
| 55 |
|
| 56 |
+
logger.info("Descargando clips de video...")
|
| 57 |
video_urls = buscar_videos_mock()
|
| 58 |
clips = []
|
| 59 |
for url in video_urls:
|
|
|
|
| 62 |
for chunk in r.iter_content(1024 * 1024):
|
| 63 |
f.write(chunk)
|
| 64 |
f.flush()
|
| 65 |
+
clip = VideoFileClip(f.name).subclip(0, 5)
|
| 66 |
+
clips.append(clip)
|
| 67 |
|
| 68 |
video = concatenate_videoclips(clips).set_audio(voz_clip)
|
| 69 |
|
| 70 |
+
logger.info("Descargando m煤sica de fondo...")
|
| 71 |
music_url = buscar_musica_mock()
|
| 72 |
r = requests.get(music_url, stream=True)
|
| 73 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
|
|
|
| 77 |
music_clip = AudioFileClip(f.name)
|
| 78 |
music_loop = afx.audio_loop(music_clip, duration=video.duration).volumex(0.3)
|
| 79 |
|
| 80 |
+
logger.info("Combinando audio de voz y m煤sica...")
|
| 81 |
+
audio_final = CompositeAudioClip([video.audio, music_loop])
|
| 82 |
+
video = video.set_audio(audio_final)
|
| 83 |
|
|
|
|
| 84 |
output_path = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
| 85 |
+
logger.info("Renderizando video final...")
|
| 86 |
+
video.write_videofile(output_path, fps=24, logger=logger)
|
| 87 |
|
| 88 |
return output_path
|
| 89 |
except Exception as e:
|
| 90 |
+
logger.error(f"Error: {e}")
|
| 91 |
return f"Error: {e}"
|
| 92 |
|
| 93 |
+
# Interfaz
|
| 94 |
+
with gr.Blocks() as app:
|
| 95 |
+
prompt = gr.Textbox(label="Tema del video", placeholder="Ejemplo: Top 5 misterios sin resolver")
|
| 96 |
+
voz = gr.Dropdown(VOICE_OPTIONS, label="Voz", value=VOICE_OPTIONS[0])
|
| 97 |
+
btn = gr.Button("Generar Video")
|
| 98 |
+
output = gr.Video(label="Resultado")
|
| 99 |
|
| 100 |
btn.click(fn=lambda p, v: asyncio.run(generar_video(p, v)),
|
| 101 |
+
inputs=[prompt, voz],
|
| 102 |
+
outputs=output)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|