File size: 11,148 Bytes
f1f8e2a
1e90d4c
bf48cd0
 
 
 
1e90d4c
bf48cd0
 
 
 
1e90d4c
87b526f
 
1e90d4c
 
 
 
6692a78
a45d148
1e90d4c
8336be3
f1f8e2a
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf48cd0
f1f8e2a
1e90d4c
 
 
8b182fa
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1f8e2a
1e90d4c
 
c6e67aa
1e90d4c
 
 
a45d148
1e90d4c
 
 
a45d148
1e90d4c
 
c6e67aa
1e90d4c
c6e67aa
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6e67aa
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
bf48cd0
1e90d4c
 
 
 
 
9c2cc80
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaddca7
1e90d4c
eaddca7
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
9c2cc80
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f206a0f
1e90d4c
 
 
 
 
9c2cc80
1e90d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90d3598
1e90d4c
 
 
 
90d3598
1e90d4c
90d3598
1e90d4c
 
 
90d3598
1e90d4c
 
 
 
 
 
 
90d3598
a45d148
1e90d4c
 
 
 
 
 
 
 
 
 
 
96a2f23
 
1e90d4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
import asyncio
import logging
import tempfile
import requests
from datetime import datetime
import edge_tts
import gradio as gr
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from keybert import KeyBERT
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, CompositeAudioClip
import re
import json
import uuid
import threading
from queue import Queue
import time

# Configuración de logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Directorio persistente para archivos
PERSIST_DIR = "persistent_data"
os.makedirs(PERSIST_DIR, exist_ok=True)

# Cola de procesamiento
processing_queue = Queue()
task_status = {}

# Clase para manejar tareas
class VideoTask:
    def __init__(self, task_id, prompt_type, input_text, musica_file=None):
        self.task_id = task_id
        self.prompt_type = prompt_type
        self.input_text = input_text
        self.musica_file = musica_file
        self.status = "pending"
        self.progress = 0
        self.result = None
        self.error = None
        self.steps_completed = []
        
    def to_dict(self):
        return {
            "task_id": self.task_id,
            "status": self.status,
            "progress": self.progress,
            "result": self.result,
            "error": self.error,
            "steps_completed": self.steps_completed
        }

# Worker thread para procesar videos
def video_processor_worker():
    while True:
        try:
            task = processing_queue.get(timeout=1)
            if task is None:
                break
                
            logger.info(f"Procesando tarea: {task.task_id}")
            process_video_task(task)
            
        except:
            time.sleep(0.1)
            continue

# Iniciar worker thread
worker_thread = threading.Thread(target=video_processor_worker, daemon=True)
worker_thread.start()

def save_task_state(task):
    """Guarda el estado de la tarea en un archivo JSON"""
    task_file = os.path.join(PERSIST_DIR, f"{task.task_id}.json")
    with open(task_file, 'w') as f:
        json.dump(task.to_dict(), f)

def load_task_state(task_id):
    """Carga el estado de una tarea desde archivo"""
    task_file = os.path.join(PERSIST_DIR, f"{task_id}.json")
    if os.path.exists(task_file):
        with open(task_file, 'r') as f:
            return json.load(f)
    return None

def process_video_task(task):
    """Procesa una tarea de video paso a paso"""
    try:
        task.status = "processing"
        task.progress = 0
        save_task_state(task)
        
        # Paso 1: Generar guión
        task.progress = 10
        save_task_state(task)
        
        if task.prompt_type == "Generar Guion con IA":
            guion = generate_script_simple(task.input_text)
        else:
            guion = task.input_text.strip()
            
        task.steps_completed.append("guion_generado")
        task.progress = 20
        save_task_state(task)
        
        # Paso 2: Generar audio TTS
        audio_path = os.path.join(PERSIST_DIR, f"{task.task_id}_audio.mp3")
        success = asyncio.run(text_to_speech_simple(guion, audio_path))
        
        if not success:
            raise Exception("Error generando audio")
            
        task.steps_completed.append("audio_generado")
        task.progress = 40
        save_task_state(task)
        
        # Paso 3: Buscar videos (simplificado)
        keywords = extract_keywords_simple(guion)
        video_urls = search_videos_simple(keywords)
        
        task.steps_completed.append("videos_encontrados")
        task.progress = 60
        save_task_state(task)
        
        # Paso 4: Crear video final (simplificado)
        output_path = os.path.join(PERSIST_DIR, f"{task.task_id}_final.mp4")
        
        # Simulación de creación de video
        # En producción, aquí iría tu lógica de moviepy
        create_simple_video(video_urls, audio_path, output_path, task.musica_file)
        
        task.steps_completed.append("video_creado")
        task.progress = 100
        task.status = "completed"
        task.result = output_path
        save_task_state(task)
        
    except Exception as e:
        logger.error(f"Error procesando tarea {task.task_id}: {str(e)}")
        task.status = "error"
        task.error = str(e)
        save_task_state(task)

# Funciones simplificadas
def generate_script_simple(prompt):
    """Versión simplificada de generación de guión"""
    # Aquí puedes usar tu lógica GPT-2 existente
    return f"Este es un video sobre {prompt}. Es fascinante y educativo."

async def text_to_speech_simple(text, output_path):
    """TTS simplificado"""
    try:
        communicate = edge_tts.Communicate(text, "es-ES-JuanNeural")
        await communicate.save(output_path)
        return os.path.exists(output_path)
    except:
        return False

def extract_keywords_simple(text):
    """Extracción simple de keywords"""
    words = text.lower().split()
    # Filtrar palabras comunes
    keywords = [w for w in words if len(w) > 4][:3]
    return keywords if keywords else ["nature", "video", "background"]

def search_videos_simple(keywords):
    """Búsqueda simplificada de videos"""
    # Aquí iría tu lógica de Pexels
    # Por ahora retornamos URLs de ejemplo
    return ["video1.mp4", "video2.mp4"]

def create_simple_video(video_urls, audio_path, output_path, music_path=None):
    """Creación simplificada de video"""
    # Aquí iría tu lógica de MoviePy
    # Por ahora creamos un archivo dummy
    with open(output_path, 'w') as f:
        f.write("dummy video content")
    time.sleep(2)  # Simular procesamiento

# Interfaz Gradio mejorada
def submit_video_request(prompt_type, prompt_ia, prompt_manual, musica_file):
    """Envía una solicitud de video a la cola"""
    input_text = prompt_ia if prompt_type == "Generar Guion con IA" else prompt_manual
    
    if not input_text or not input_text.strip():
        return None, "Por favor ingresa un texto"
    
    task_id = str(uuid.uuid4())
    task = VideoTask(task_id, prompt_type, input_text, musica_file)
    
    # Guardar estado inicial
    task_status[task_id] = task
    save_task_state(task)
    
    # Añadir a la cola
    processing_queue.put(task)
    
    return task_id, f"Tarea creada: {task_id}"

def check_video_status(task_id):
    """Verifica el estado de una tarea"""
    if not task_id:
        return "No hay ID de tarea", None, None
    
    # Intentar cargar desde archivo
    task_data = load_task_state(task_id)
    
    if not task_data:
        return "Tarea no encontrada", None, None
    
    status = task_data['status']
    progress = task_data['progress']
    
    if status == "pending":
        return f"⏳ En cola... ({progress}%)", None, None
    elif status == "processing":
        steps = ", ".join(task_data['steps_completed'])
        return f"🔄 Procesando... ({progress}%) - Completado: {steps}", None, None
    elif status == "completed":
        video_path = task_data['result']
        if os.path.exists(video_path):
            return "✅ Video completado!", video_path, video_path
        else:
            return "❌ Video completado pero archivo no encontrado", None, None
    elif status == "error":
        return f"❌ Error: {task_data['error']}", None, None
    
    return "Estado desconocido", None, None

# Interfaz Gradio
with gr.Blocks(title="Generador de Videos con IA") as app:
    gr.Markdown("# 🎬 Generador de Videos con IA (Sistema de Cola)")
    
    with gr.Tabs():
        with gr.TabItem("Crear Video"):
            with gr.Row():
                with gr.Column():
                    prompt_type = gr.Radio(
                        ["Generar Guion con IA", "Usar Mi Guion"],
                        label="Método de Entrada",
                        value="Generar Guion con IA"
                    )
                    
                    prompt_ia = gr.Textbox(
                        label="Tema para IA",
                        placeholder="Describe el tema del video..."
                    )
                    
                    prompt_manual = gr.Textbox(
                        label="Tu Guion Completo",
                        placeholder="Escribe tu guion aquí...",
                        visible=False
                    )
                    
                    musica_input = gr.Audio(
                        label="Música de fondo (opcional)",
                        type="filepath"
                    )
                    
                    submit_btn = gr.Button("📤 Enviar a Cola", variant="primary")
                
                with gr.Column():
                    task_id_output = gr.Textbox(
                        label="ID de Tarea",
                        interactive=False
                    )
                    submit_status = gr.Textbox(
                        label="Estado de Envío",
                        interactive=False
                    )
        
        with gr.TabItem("Verificar Estado"):
            with gr.Row():
                with gr.Column():
                    task_id_input = gr.Textbox(
                        label="ID de Tarea",
                        placeholder="Pega aquí el ID de tu tarea..."
                    )
                    check_btn = gr.Button("🔍 Verificar Estado")
                    auto_check = gr.Checkbox(
                        label="Verificar automáticamente cada 5 segundos"
                    )
                
                with gr.Column():
                    status_output = gr.Textbox(
                        label="Estado Actual",
                        interactive=False
                    )
                    video_output = gr.Video(
                        label="Video Generado",
                        interactive=False
                    )
                    download_output = gr.File(
                        label="Descargar Video",
                        interactive=False
                    )
    
    # Eventos
    prompt_type.change(
        lambda x: (
            gr.update(visible=x == "Generar Guion con IA"),
            gr.update(visible=x == "Usar Mi Guion")
        ),
        inputs=prompt_type,
        outputs=[prompt_ia, prompt_manual]
    )
    
    submit_btn.click(
        submit_video_request,
        inputs=[prompt_type, prompt_ia, prompt_manual, musica_input],
        outputs=[task_id_output, submit_status]
    )
    
    check_btn.click(
        check_video_status,
        inputs=[task_id_input],
        outputs=[status_output, video_output, download_output]
    )
    
    # Auto-check cada 5 segundos si está activado
    def auto_check_status(task_id, should_check):
        if should_check and task_id:
            return check_video_status(task_id)
        return gr.update(), gr.update(), gr.update()
    
    auto_check.change(
        lambda x: gr.update(visible=x),
        inputs=[auto_check],
        outputs=[check_btn]
    )

if __name__ == "__main__":
    app.launch(server_name="0.0.0.0", server_port=7860)