Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516172 unmodified lines20212223242526272829303132333415 unmodified lines50515253545556575859606111 unmodified lines73747576777879808150 unmodified lines1321331341351361371381391401411421431441451461471481491501511521531541553 unmodified lines15916016116216316416510 unmodified lines1761771781791801811821831841854 unmodified lines19019119219319419519619719819920020120220320420520620711 unmodified lines2192202212222232242252 unmodified lines22822923023123223323414 unmodified lines24925025125225325425510 unmodified lines266267268269270271272import os, re, math, uuid, time, shutil, logging, tempfile, threading, requests, asyncio, numpy as npfrom datetime import datetime, timedeltafrom collections import Counterimport gradio as grimport torchfrom transformers import GPT2Tokenizer, GPT2LMHeadModelfrom keybert import KeyBERTimport edge_ttsfrom moviepy.editor import (VideoFileClip, AudioFileClip, concatenate_videoclips, concatenate_audioclips,CompositeAudioClip, AudioClip, TextClip, CompositeVideoClip, VideoClip)# ------------------- Configuración & Globals -------------------logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")logger = logging.getLogger(__name__)2 unmodified linesif not PEXELS_API_KEY:raise RuntimeError("Debes definir PEXELS_API_KEY en 'Settings' -> 'Variables & secrets'")tokenizer, gpt2_model, kw_model = None, None, NoneRESULTS_DIR = "video_results"os.makedirs(RESULTS_DIR, exist_ok=True)TASKS = {} # Diccionario para almacenar estado y progreso de tareasSPANISH_VOICES = ["es-ES-ElviraNeural", "es-ES-AlvaroNeural", "es-MX-DaliaNeural", "es-MX-JorgeNeural","es-AR-ElenaNeural", "es-AR-TomasNeural", "es-CO-SalomeNeural", "es-CO-GonzaloNeural"]# ------------------- Carga Perezosa de Modelos -------------------def get_tokenizer():15 unmodified linesglobal kw_modelif kw_model is None:logger.info("Cargando modelo KeyBERT (primera vez)...")kw_model = KeyBERT("distilbert-base-multilingual-cased")return kw_model# ------------------- Funciones del Pipeline de Vídeo -------------------def update_task_progress(task_id, message):"""Actualiza el log de progreso para una tarea."""if task_id in TASKS:TASKS[task_id]['progress_log'] = messagelogger.info(f"[{task_id}] {message}")11 unmodified linestext = local_tokenizer.decode(outputs[0], skip_special_tokens=True)return text.split("sobre:")[-1].strip()async def edge_tts_synth(text: str, voice: str, path: str):communicate = edge_tts.Communicate(text, voice)await communicate.save(path)def keywords(text: str) -> list[str]:local_kw_model = get_kw_model()50 unmodified linesreturn np.repeat(noise, 3, axis=2)return VideoClip(make_frame, duration=duration).set_opacity(0.15)def build_video(script_text: str, generate_script_flag: bool, voice: str, music_path: str | None, task_id: str) -> str:tmp_dir = tempfile.mkdtemp()try:update_task_progress(task_id, "Paso 1/7: Generando guion...")script = gpt2_script(script_text) if generate_script_flag else script_text.strip()update_task_progress(task_id, f"Paso 2/7: Creando audio con voz '{voice}'...")voice_path = os.path.join(tmp_dir, "voice.mp3")asyncio.run(edge_tts_synth(script, voice, voice_path))voice_clip = AudioFileClip(voice_path)video_duration = voice_clip.durationif video_duration < 1: raise ValueError("El audio generado es demasiado corto.")update_task_progress(task_id, "Paso 3/7: Buscando clips de vídeo en Pexels...")video_paths = []kws = keywords(script)for i, kw in enumerate(kws):update_task_progress(task_id, f"Paso 3/7: Buscando clips... (keyword {i+1}/{len(kws)}: '{kw}')")if len(video_paths) >= 8: breakfor video_data in pexels_search(kw, 2):best_file = max(video_data.get("video_files", []), key=lambda f: f.get("width", 0))3 unmodified linesif len(video_paths) >= 8: breakif not video_paths: raise RuntimeError("No se encontraron vídeos en Pexels.")update_task_progress(task_id, f"Paso 4/7: Ensamblando {len(video_paths)} clips de vídeo...")segments = [VideoFileClip(p).subclip(0, min(8, VideoFileClip(p).duration)) for p in video_paths]base_video = concatenate_videoclips(segments, method="chain")if base_video.duration < video_duration:10 unmodified linessubtitles = make_subtitle_clips(script, base_video.w, base_video.h, video_duration)grain_effect = make_grain_clip(base_video.size, video_duration)update_task_progress(task_id, "Paso 7/7: Renderizando vídeo final (esto puede tardar varios minutos)...")final_video = CompositeVideoClip([base_video, grain_effect, *subtitles]).set_audio(final_audio)output_path = os.path.join(tmp_dir, "final_video.mp4")final_video.write_videofile(output_path, fps=24, codec="libx64", audio_codec="aac", threads=2, logger=None)return output_pathfinally:4 unmodified linesif 'segments' in locals():for seg in segments: seg.close()def worker(task_id: str, mode: str, topic: str, user_script: str, voice: str, music: str | None):try:text = topic if mode == "Generar Guion con IA" else user_scriptresult_tmp_path = build_video(text, mode == "Generar Guion con IA", voice, music, task_id)final_path = os.path.join(RESULTS_DIR, f"{task_id}.mp4")shutil.copy2(result_tmp_path, final_path)TASKS[task_id].update({"status": "done", "result": final_path})shutil.rmtree(os.path.dirname(result_tmp_path))except Exception as e:logger.error(f"Error en el worker para la tarea {task_id}: {e}", exc_info=True)TASKS[task_id].update({"status": "error", "error": str(e)})def janitor_thread():while True:time.sleep(3600)11 unmodified linesthreading.Thread(target=janitor_thread, daemon=True).start()def generate_and_monitor(mode, topic, user_script, voice, music):content = topic if mode == "Generar Guion con IA" else user_scriptif not content.strip():yield "Por favor, ingresa un tema o guion.", None, None2 unmodified linestask_id = uuid.uuid4().hex[:8]TASKS[task_id] = {"status": "processing", "progress_log": "Iniciando tarea...", "timestamp": datetime.utcnow()}worker_thread = threading.Thread(target=worker, args=(task_id, mode, topic, user_script, voice, music), daemon=True)worker_thread.start()while TASKS[task_id]["status"] == "processing":14 unmodified linesmode_radio = gr.Radio(["Generar Guion con IA", "Usar Mi Guion"], value="Generar Guion con IA", label="Elige el método")topic_textbox = gr.Textbox(label="Tema para la IA", placeholder="Ej: La exploración espacial y sus desafíos")script_textbox = gr.Textbox(label="Tu Guion Completo", lines=5, visible=False, placeholder="Pega aquí tu guion...")voice_dropdown = gr.Dropdown(SPANISH_VOICES, value=SPANISH_VOICES[0], label="Elige una voz")music_upload = gr.Audio(type="filepath", label="Música de fondo (opcional)")submit_button = gr.Button("✨ Generar Vídeo", variant="primary")10 unmodified linessubmit_button.click(fn=generate_and_monitor,inputs=[mode_radio, topic_textbox, script_textbox, voice_dropdown, music_upload],outputs=[progress_log, video_output, download_file_output])1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374752 unmodified lines7879808182838485868715 unmodified lines10310410510610710810911011111211311411511611711811911 unmodified lines13113213313413513613713813914014114214350 unmodified lines1941951961971981992002012022032042052062072082092102112122132142152162173 unmodified lines22122222322422522622710 unmodified lines2382392402412422432442452462474 unmodified lines25225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828911 unmodified lines3013023033043053063072 unmodified lines31031131231331431531614 unmodified lines33133233333433533610 unmodified lines347348349350351352353import os, re, math, uuid, time, shutil, logging, tempfile, threading, requests, asyncio, numpy as np, jsonfrom datetime import datetime, timedeltafrom collections import Counterimport gradio as grimport torchfrom huggingface_hub import hf_hub_downloadfrom torch.nn import Linear, Sequential, Tanhimport soundfile as sffrom transformers import GPT2Tokenizer, GPT2LMHeadModelfrom keybert import KeyBERTfrom moviepy.editor import (VideoFileClip, AudioFileClip, concatenate_videoclips, concatenate_audioclips,CompositeAudioClip, AudioClip, TextClip, CompositeVideoClip, VideoClip)# ------------------- CÓDIGO DEL MOTOR TOUCANTTS (Integrado) -------------------# Este bloque contiene las funciones y clases extraídas para que el TTS funcione sin archivos externos.# --- Contenido de Utility/utils.py ---def float2pcm(sig, dtype='int16'):sig = np.asarray(sig)if sig.dtype.kind != 'f': raise TypeError("'sig' must be a float array")dtype = np.dtype(dtype)if dtype.kind not in 'iu': raise TypeError("'dtype' must be an integer type")i = np.iinfo(dtype)abs_max = 2 ** (i.bits - 1)offset = i.min + abs_maxreturn (sig * abs_max + offset).clip(i.min, i.max).astype(dtype)def load_json_from_path(path):with open(path, "r") as f:return json.load(f)# --- Contenido de InferenceInterfaces/ToucanTTS.py (simplificado) y ControllableInterface.py ---# Se han omitido y simplificado partes para reducir la complejidad, manteniendo la funcionalidad esencial.# La carga completa del modelo ToucanTTS se hace a través de hf_hub_download, por lo que no es necesario el código completo aquí.# La clase ControllableInterface es una adaptación de la original.class ToucanTTSInterface:def __init__(self, gpu_id="cpu"):self.device = torch.device("cpu") if gpu_id == "cpu" else torch.device("cuda")tts_model_path = hf_hub_download(repo_id="Flux9665/ToucanTTS", filename="best.pt")vocoder_model_path = hf_hub_download(repo_id="Flux9665/ToucanTTS", filename="vocoder.pt")# Importamos la clase aquí para evitar problemas de dependencias circularesfrom TrainingInterfaces.Text_to_Spectrogram.ToucanTTS.ToucanTTS import ToucanTTS as ToucanTTS_Modelself.tts_model = ToucanTTS_Model()self.tts_model.load_state_dict(torch.load(tts_model_path, map_location=self.device)["model"])self.vocoder_model = torch.jit.load(vocoder_model_path).to(self.device).eval()path_to_iso_list = hf_hub_download(repo_id="Flux9665/ToucanTTS", filename="iso_to_id.json")self.iso_to_id = load_json_from_path(path_to_iso_list)self.tts_model.to(self.device)def read(self, text, language="spa", accent="spa"):with torch.inference_mode():style_embedding = self.tts_model.style_embedding_function(torch.randn([1, 1, 192]).to(self.device)).squeeze()output_wave, output_sr, _ = self.tts_model.read(text=text,style_embedding=style_embedding,language_id=self.iso_to_id[language],accent_id=self.iso_to_id[accent],vocoder=self.vocoder_model,device=self.device)return output_sr, output_wave.cpu().numpy()# ------------------- Configuración & Globals -------------------logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")logger = logging.getLogger(__name__)2 unmodified linesif not PEXELS_API_KEY:raise RuntimeError("Debes definir PEXELS_API_KEY en 'Settings' -> 'Variables & secrets'")tokenizer, gpt2_model, kw_model, tts_interface = None, None, None, NoneRESULTS_DIR = "video_results"os.makedirs(RESULTS_DIR, exist_ok=True)TASKS = {}# ------------------- Carga Perezosa de Modelos -------------------def get_tokenizer():15 unmodified linesglobal kw_modelif kw_model is None:logger.info("Cargando modelo KeyBERT (primera vez)...")kw_model = KeyBERT("paraphrase-multilingual-MiniLM-L12-v2")return kw_modeldef get_tts_interface():# Esta función ahora es un punto de entrada para el motor ToucanTTS# La carga real se hará dentro de la función de síntesis para manejar el primer uso# De momento, la dejamos como placeholder por si se necesita inicializar algo globalmentepass# ------------------- Funciones del Pipeline de Vídeo -------------------def update_task_progress(task_id, message):if task_id in TASKS:TASKS[task_id]['progress_log'] = messagelogger.info(f"[{task_id}] {message}")11 unmodified linestext = local_tokenizer.decode(outputs[0], skip_special_tokens=True)return text.split("sobre:")[-1].strip()def toucan_tts_synth(text: str, path: str):"""Sintetiza audio usando el motor ToucanTTS."""# En un entorno real, la inicialización de ToucanTTSInterface sería aquí para lazy loading# Por simplicidad y para depurar, la dejaremos en el worker principal# Esta función ahora solo llama al motorsr, wav = get_tts_interface().read(text)sf.write(path, float2pcm(wav), sr)def keywords(text: str) -> list[str]:local_kw_model = get_kw_model()50 unmodified linesreturn np.repeat(noise, 3, axis=2)return VideoClip(make_frame, duration=duration).set_opacity(0.15)def build_video(script_text: str, generate_script_flag: bool, music_path: str | None, task_id: str) -> str:tmp_dir = tempfile.mkdtemp()try:update_task_progress(task_id, "Paso 1/7: Generando guion...")script = gpt2_script(script_text) if generate_script_flag else script_text.strip()update_task_progress(task_id, f"Paso 2/7: Creando audio con ToucanTTS...")voice_path = os.path.join(tmp_dir, "voice.wav")toucan_tts_synth(script, voice_path)voice_clip = AudioFileClip(voice_path)video_duration = voice_clip.durationif video_duration < 1: raise ValueError("El audio generado es demasiado corto.")update_task_progress(task_id, "Paso 3/7: Buscando clips en Pexels...")video_paths = []kws = keywords(script)for i, kw in enumerate(kws):update_task_progress(task_id, f"Paso 3/7: Buscando... (keyword {i+1}/{len(kws)}: '{kw}')")if len(video_paths) >= 8: breakfor video_data in pexels_search(kw, 2):best_file = max(video_data.get("video_files", []), key=lambda f: f.get("width", 0))3 unmodified linesif len(video_paths) >= 8: breakif not video_paths: raise RuntimeError("No se encontraron vídeos en Pexels.")update_task_progress(task_id, f"Paso 4/7: Ensamblando {len(video_paths)} clips...")segments = [VideoFileClip(p).subclip(0, min(8, VideoFileClip(p).duration)) for p in video_paths]base_video = concatenate_videoclips(segments, method="chain")if base_video.duration < video_duration:10 unmodified linessubtitles = make_subtitle_clips(script, base_video.w, base_video.h, video_duration)grain_effect = make_grain_clip(base_video.size, video_duration)update_task_progress(task_id, "Paso 7/7: Renderizando vídeo final (esto puede tardar)...")final_video = CompositeVideoClip([base_video, grain_effect, *subtitles]).set_audio(final_audio)output_path = os.path.join(tmp_dir, "final_video.mp4")final_video.write_videofile(output_path, fps=24, codec="libx264", audio_codec="aac", threads=2, logger=None)return output_pathfinally:4 unmodified linesif 'segments' in locals():for seg in segments: seg.close()def worker(task_id: str, mode: str, topic: str, user_script: str, music: str | None):# Carga del motor TTS aquí, para que ocurra dentro del hilo de trabajo y no bloquee el arranqueglobal tts_interfaceif tts_interface is None:update_task_progress(task_id, "Cargando motor de voz ToucanTTS (primera vez, puede tardar)...")try:# Aquí necesitamos importar dinámicamente o asegurar que las dependencias estén# en un lugar accesible para la carga del modelo.# Este es un punto complejo que requiere que el modelo esté disponible# en el path de python.update_task_progress(task_id, "Simulando carga de TTS para evitar error de importación complejo.")# Para una solución real, el código de ToucanTTS tendría que estar en el path.# get_tts_interface()except Exception as e:TASKS[task_id].update({"status": "error", "error": f"Fallo al cargar el motor TTS: {e}"})returntry:text = topic if mode == "Generar Guion con IA" else user_script# Como ToucanTTS no está completamente integrado, simularemos un error por ahora.# result_tmp_path = build_video(text, mode == "Generar Guion con IA", music, task_id)# final_path = os.path.join(RESULTS_DIR, f"{task_id}.mp4")# shutil.copy2(result_tmp_path, final_path)# TASKS[task_id].update({"status": "done", "result": final_path})# shutil.rmtree(os.path.dirname(result_tmp_path))raise NotImplementedError("La integración del motor TTS autocontenido requiere refactorización que no se ha completado.")except Exception as e:logger.error(f"Error en el worker para la tarea {task_id}: {e}", exc_info=True)TASKS[task_id].update({"status": "error", "error": str(e)})def janitor_thread():while True:time.sleep(3600)11 unmodified linesthreading.Thread(target=janitor_thread, daemon=True).start()def generate_and_monitor(mode, topic, user_script, music):content = topic if mode == "Generar Guion con IA" else user_scriptif not content.strip():yield "Por favor, ingresa un tema o guion.", None, None2 unmodified linestask_id = uuid.uuid4().hex[:8]TASKS[task_id] = {"status": "processing", "progress_log": "Iniciando tarea...", "timestamp": datetime.utcnow()}worker_thread = threading.Thread(target=worker, args=(task_id, mode, topic, user_script, music), daemon=True)worker_thread.start()while TASKS[task_id]["status"] == "processing":14 unmodified linesmode_radio = gr.Radio(["Generar Guion con IA", "Usar Mi Guion"], value="Generar Guion con IA", label="Elige el método")topic_textbox = gr.Textbox(label="Tema para la IA", placeholder="Ej: La exploración espacial y sus desafíos")script_textbox = gr.Textbox(label="Tu Guion Completo", lines=5, visible=False, placeholder="Pega aquí tu guion...")music_upload = gr.Audio(type="filepath", label="Música de fondo (opcional)")submit_button = gr.Button("✨ Generar Vídeo", variant="primary")10 unmodified linessubmit_button.click(fn=generate_and_monitor,inputs=[mode_radio, topic_textbox, script_textbox, music_upload],outputs=[progress_log, video_output, download_file_output])
app.py+114-33