Spaces:
Sleeping
Sleeping
import os | |
import subprocess | |
import requests | |
import gradio as gr | |
from moviepy.editor import * | |
from datetime import datetime | |
import logging | |
import re | |
import torch | |
from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
import warnings | |
# Configuraci贸n inicial | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# Suprimir warnings no deseados | |
warnings.filterwarnings("ignore", category=UserWarning) | |
warnings.filterwarnings("ignore", category=DeprecationWarning) | |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY") | |
# Lista de voces v谩lidas | |
VOICES = [ | |
"es-MX-DaliaNeural", "es-ES-ElviraNeural", "es-AR-ElenaNeural", | |
"es-MX-JorgeNeural", "es-ES-AlvaroNeural", "es-AR-TomasNeural", | |
"en-US-JennyNeural", "fr-FR-DeniseNeural", "de-DE-KatjaNeural" | |
] | |
# Cargar modelo GPT-2 con configuraci贸n optimizada | |
try: | |
tokenizer = GPT2Tokenizer.from_pretrained("datificate/gpt2-small-spanish") | |
model = GPT2LMHeadModel.from_pretrained("datificate/gpt2-small-spanish") | |
logger.info("Modelo GPT-2 cargado correctamente") | |
except Exception as e: | |
logger.error(f"Error cargando modelo: {str(e)}") | |
model = None | |
tokenizer = None | |
def generar_texto(tema): | |
"""Genera texto largo sobre el tema sin estructuras predefinidas""" | |
if model is None or tokenizer is None: | |
return f"Contenido sobre {tema}. " * 50 | |
try: | |
# Prompt directo y simple | |
prompt = f"Describe detalladamente {tema}" | |
# Codificar el texto con truncamiento | |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True) | |
# Generar texto con par谩metros optimizados | |
outputs = model.generate( | |
inputs.input_ids, | |
max_length=800, | |
do_sample=True, | |
temperature=0.7, | |
top_k=40, | |
num_return_sequences=1, | |
pad_token_id=tokenizer.eos_token_id | |
) | |
texto = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return re.sub(r'\s+', ' ', texto).strip() | |
except Exception as e: | |
logger.error(f"Error generando texto: {str(e)}") | |
return f"Texto generado sobre {tema}. " * 50 | |
def obtener_videos(tema): | |
"""Obtiene videos de Pexels con manejo robusto de errores""" | |
try: | |
headers = {"Authorization": PEXELS_API_KEY} | |
response = requests.get( | |
f"https://api.pexels.com/videos/search?query={tema}&per_page=3", | |
headers=headers, | |
timeout=10 | |
) | |
return response.json().get("videos", [])[:3] | |
except Exception as e: | |
logger.error(f"Error obteniendo videos: {str(e)}") | |
return [] | |
def crear_video(prompt, voz_seleccionada): | |
try: | |
# 1. Generar texto | |
texto = generar_texto(prompt) | |
logger.info(f"Texto generado: {len(texto)} caracteres") | |
# 2. Crear narraci贸n de voz | |
voz_file = "narracion.mp3" | |
subprocess.run([ | |
'edge-tts', | |
'--voice', voz_seleccionada, | |
'--text', texto, | |
'--write-media', voz_file | |
], check=True) | |
audio = AudioFileClip(voz_file) | |
duracion = audio.duration | |
# 3. Obtener y procesar videos | |
videos = obtener_videos(prompt) or obtener_videos("nature") | |
clips = [] | |
for i, video in enumerate(videos): | |
try: | |
# Seleccionar video de mayor calidad | |
video_file = max(video['video_files'], key=lambda x: x.get('width', 0)) | |
temp_file = f"temp_{i}.mp4" | |
# Descargar video | |
with requests.get(video_file['link'], stream=True) as r: | |
r.raise_for_status() | |
with open(temp_file, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=8192): | |
f.write(chunk) | |
# Procesar clip | |
clip = VideoFileClip(temp_file) | |
clip_duration = min(duracion/len(videos), clip.duration) | |
clips.append(clip.subclip(0, clip_duration)) | |
except Exception as e: | |
logger.error(f"Error procesando video {i}: {str(e)}") | |
# 4. Crear video final | |
if not clips: | |
final_clip = ColorClip((1280, 720), (0, 0, 0), duration=duracion) | |
else: | |
final_clip = concatenate_videoclips(clips).set_duration(duracion) | |
final_clip = final_clip.set_audio(audio) | |
# 5. Exportar video | |
output_file = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4" | |
final_clip.write_videofile( | |
output_file, | |
fps=24, | |
codec="libx264", | |
audio_codec="aac", | |
threads=2, | |
preset='fast' | |
) | |
return output_file | |
except Exception as e: | |
logger.error(f"Error cr铆tico: {str(e)}") | |
return None | |
finally: | |
# Limpieza de archivos temporales | |
for f in [voz_file, *[f"temp_{i}.mp4" for i in range(3)]]: | |
if os.path.exists(f): | |
try: | |
os.remove(f) | |
except: | |
pass | |
# Interfaz minimalista | |
with gr.Blocks() as app: | |
with gr.Row(): | |
with gr.Column(): | |
tema = gr.Textbox(label="Tema del video") | |
voz = gr.Dropdown(label="Voz", choices=VOICES, value=VOICES[0]) | |
btn = gr.Button("Generar Video") | |
with gr.Column(): | |
video = gr.Video(label="Resultado") | |
btn.click( | |
fn=crear_video, | |
inputs=[tema, voz], | |
outputs=video | |
) | |
if __name__ == "__main__": | |
app.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False | |
) |