File size: 13,233 Bytes
8b274aa
15e8c2d
bafc5cd
15e8c2d
b8bd6c3
6bc8144
15e8c2d
 
6bc8144
 
 
 
3a7d955
6bc8144
 
 
2b5730b
b8bd6c3
0b39b27
b8bd6c3
b82e6a6
1829fd6
0b39b27
2b5730b
6bc8144
2b5730b
6bc8144
 
 
 
 
 
0b39b27
6bc8144
2b5730b
0b39b27
 
1829fd6
6bc8144
2b5730b
0b39b27
6bc8144
2b5730b
6bc8144
b8bd6c3
0b39b27
6bc8144
0b39b27
 
2b5730b
0b39b27
6bc8144
 
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc8144
0b39b27
 
6bc8144
0b39b27
 
6bc8144
 
 
0b39b27
2b5730b
6bc8144
0b39b27
2b5730b
6bc8144
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc8144
0b39b27
 
 
 
 
 
2b5730b
 
0b39b27
6bc8144
 
0b39b27
6bc8144
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d525bc
2b5730b
 
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a7d955
 
0b39b27
 
 
 
50a2015
0b39b27
6bc8144
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a2015
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a2015
0b39b27
2b5730b
0b39b27
 
6bc8144
 
0b39b27
 
6bc8144
0b39b27
 
 
163c0da
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b82e6a6
6bc8144
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc8144
 
0b39b27
 
6bc8144
0b39b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163c0da
15e8c2d
2b5730b
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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 subprocess
import re
import math
from pydub import AudioSegment
from pexelsapi.pexels import Pexels

# Configuraci贸n de logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Clave API de Pexels
PEXELS_API_KEY = os.environ.get("PEXELS_API_KEY")

# Inicializaci贸n de modelos
MODEL_NAME = "gpt2" 
try:
    tokenizer = GPT2Tokenizer.from_pretrained(MODEL_NAME)
    model = GPT2LMHeadModel.from_pretrained(MODEL_NAME).eval()
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    logger.info(f"Modelo GPT-2 cargado exitosamente.")
except Exception as e:
    logger.error(f"Error al cargar modelo GPT-2: {e}")
    tokenizer = None
    model = None

try:
    kw_model = KeyBERT('multi-qa-MiniLM-L6-cos-v1')
    logger.info("Modelo KeyBERT cargado exitosamente.")
except Exception as e:
    logger.error(f"Error al cargar KeyBERT: {e}")
    kw_model = None

def generate_script(prompt, max_length=250): 
    if not tokenizer or not model:
        logger.error("Modelo GPT-2 no disponible")
        return "Lo siento, el generador de guiones no est谩 disponible."
    
    logger.info("Generando guion con GPT-2...")
    try:
        inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        inputs = {k: v.to(device) for k, v in inputs.items()}
        model.to(device)

        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_length=max_length,
                do_sample=True,
                top_p=0.95,
                top_k=60,
                temperature=0.9,
                pad_token_id=tokenizer.pad_token_id,
                eos_token_id=tokenizer.eos_token_id
            )
        text = tokenizer.decode(outputs[0], skip_special_tokens=True)
        logger.info(f"Guion generado: {text[:200]}...")
        return text
    except Exception as e:
        logger.error(f"Error generando guion: {e}")
        return "No se pudo generar el guion. Intenta con otro prompt."

async def text_to_speech(text, voice="es-ES-ElviraNeural", output_path="voz.mp3"):
    logger.info(f"Generando audio TTS...")
    try:
        communicate = edge_tts.Communicate(text, voice)
        await communicate.save(output_path)
        logger.info(f"Audio TTS guardado")
        return True
    except Exception as e:
        logger.error(f"Error TTS: {e}")
        return False

def download_video_file(url, temp_dir): 
    if not url:
        return None
    
    file_name = url.split('/')[-1].split('?')[0]
    if not file_name.endswith('.mp4'): 
        file_name = f"video_temp_{os.getpid()}_{datetime.now().strftime('%f')}.mp4"
    
    output_path = os.path.join(temp_dir, file_name)
    logger.info(f"Descargando video: {url}")
    try:
        response = requests.get(url, stream=True, timeout=30) 
        response.raise_for_status()

        with open(output_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)
        logger.info(f"Video descargado")
        return output_path
    except Exception as e:
        logger.error(f"Error descargando video: {e}")
        if os.path.exists(output_path):
            os.remove(output_path)
        return None

def loop_audio_to_length(audio_clip, target_duration):
    if audio_clip.duration >= target_duration:
        return audio_clip.subclip(0, target_duration)
    
    loops = int(target_duration / audio_clip.duration) + 1
    audios = [audio_clip] * loops
    concatenated = concatenate_videoclips(audios)
    return concatenated.subclip(0, target_duration)

def extract_visual_keywords_from_script(script_text, max_keywords_per_segment=2):
    if not kw_model:
        logger.warning("KeyBERT no disponible. Usando m茅todo simple.")
        return [script_text.split('.')[0].strip().replace(" ", "+")] if script_text.strip() else []
    
    logger.info("Extrayendo palabras clave...")
    segments = [s.strip() for s in script_text.split('\n') if s.strip()]
    if not segments: 
        segments = [script_text]

    all_keywords = set() 
    for segment in segments:
        if not segment: continue
        try:
            keywords_with_scores = kw_model.extract_keywords(
                segment,
                keyphrase_ngram_range=(1, 2), 
                stop_words='spanish', 
                top_n=max_keywords_per_segment,
                use_mmr=True, 
                diversity=0.7 
            )
            for kw, score in keywords_with_scores:
                all_keywords.add(kw.replace(" ", "+"))
        except Exception as e:
            logger.warning(f"Error extrayendo keywords: {e}")
            all_keywords.add(segment.split(' ')[0].strip().replace(" ", "+"))

    return list(all_keywords)

def search_pexels_videos(query_list, num_videos_per_query=5, min_duration_sec=7):
    if not PEXELS_API_KEY:
        logger.error("ERROR: PEXELS_API_KEY no configurada.")
        raise ValueError("Configura PEXELS_API_KEY en los Secrets")
    
    if not query_list:
        logger.warning("No hay queries para buscar.")
        return []
    
    pexel = Pexels(PEXELS_API_KEY)
    all_video_urls = []
    
    for query in query_list:
        logger.info(f"Buscando videos para: '{query}'")
        try:
            results = pexel.search_videos(
                query=query,
                orientation='landscape',
                per_page=num_videos_per_query
            )
            
            videos = results.get('videos', [])
            if not videos:
                logger.info(f"No se encontraron videos para: '{query}'")
                continue

            for video in videos:
                video_files = video.get('video_files', [])
                if video_files:
                    best_quality = max(
                        video_files,
                        key=lambda x: x.get('width', 0) * x.get('height', 0)
                    )
                    all_video_urls.append(best_quality['link'])
        except Exception as e:
            logger.error(f"Error buscando videos: {e}")
    
    return all_video_urls

def crear_video(prompt_type, input_text, musica_url=None):
    logger.info(f"Iniciando creaci贸n de video: {prompt_type}")
    guion = ""
    if prompt_type == "Generar Guion con IA":
        guion = generate_script(input_text)
        if not guion or "No se pudo" in guion:
            raise ValueError(guion) 
    else: 
        guion = input_text
        if not guion.strip():
            raise ValueError("Introduce tu guion.")

    temp_files = []
    downloaded_clip_paths = []
    final_clips = []

    temp_video_dir = tempfile.mkdtemp()
    temp_files.append(temp_video_dir)

    try:
        voz_archivo = os.path.join(tempfile.gettempdir(), f"voz_temp_{os.getpid()}.mp3")
        temp_files.append(voz_archivo)
        if not asyncio.run(text_to_speech(guion, output_path=voz_archivo)):
            raise ValueError("Error generando voz")
        
        audio_tts = AudioFileClip(voz_archivo)

        search_queries = extract_visual_keywords_from_script(guion)
        if not search_queries:
            raise ValueError("No se pudieron extraer palabras clave")

        video_urls = search_pexels_videos(search_queries)
        if not video_urls:
            raise ValueError(f"Pexels no encontr贸 videos para: {search_queries}")

        for url in video_urls:
            path = download_video_file(url, temp_video_dir)
            if path:
                downloaded_clip_paths.append(path)

        if not downloaded_clip_paths:
            raise ValueError("No se pudo descargar videos")

        total_desired_duration = audio_tts.duration * 1.2
        current_duration = 0
        
        for path in downloaded_clip_paths:
            try:
                clip = VideoFileClip(path)
                clip_duration = min(clip.duration, 10)
                if clip_duration > 1:
                    final_clips.append(clip.subclip(0, clip_duration))
                    current_duration += clip_duration
                    if current_duration >= total_desired_duration:
                        break
            except Exception as e:
                logger.warning(f"Error procesando clip: {e}")

        if not final_clips:
            raise ValueError("No hay clips v谩lidos")

        video_base = concatenate_videoclips(final_clips, method="compose")
        if video_base.duration < audio_tts.duration:
            num_repeats = int(audio_tts.duration / video_base.duration) + 1
            video_base = concatenate_videoclips([video_base] * num_repeats)

        final_video_duration = audio_tts.duration
        mezcla_audio = audio_tts

        if musica_url and musica_url.strip():
            musica_path = download_video_file(musica_url, temp_video_dir)
            if musica_path:
                temp_files.append(musica_path)
                try:
                    musica_audio = AudioFileClip(musica_path)
                    musica_loop = loop_audio_to_length(musica_audio, final_video_duration)
                    mezcla_audio = CompositeAudioClip([
                        musica_loop.volumex(0.3),
                        audio_tts.set_duration(final_video_duration).volumex(1.0)
                    ])
                except Exception as e:
                    logger.warning(f"Error m煤sica: {e}")

        video_final = video_base.set_audio(mezcla_audio).subclip(0, final_video_duration)
        output_dir = "output_videos"
        os.makedirs(output_dir, exist_ok=True)
        output_path = os.path.join(output_dir, f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4")
        
        video_final.write_videofile(
            output_path,
            fps=24,
            threads=4,
            codec="libx264",
            audio_codec="aac",
            preset="medium",
            ffmpeg_params=["-movflags", "+faststart"]
        )
        
        return output_path

    except Exception as e:
        logger.error(f"Error general: {e}")
        raise e
    finally:
        for f in temp_files:
            if os.path.exists(f):
                if os.path.isdir(f):
                    import shutil
                    shutil.rmtree(f)
                else:
                    os.remove(f)

def run_app(prompt_type, prompt_ia, prompt_manual, musica_url):
    input_text = ""
    if prompt_type == "Generar Guion con IA":
        input_text = prompt_ia
        if not input_text.strip():
            raise gr.Error("Introduce un tema para el guion.")
    else:
        input_text = prompt_manual
        if not input_text.strip():
            raise gr.Error("Introduce tu guion.")

    try:
        video_path = crear_video(prompt_type, input_text, musica_url if musica_url.strip() else None)
        if video_path:
            return video_path, "隆Video generado exitosamente!"
        else:
            raise gr.Error("Error desconocido")
    except ValueError as ve:
        return None, f"Error: {ve}"
    except Exception as e:
        return None, f"Error grave: {e}"

with gr.Blocks() as app:
    gr.Markdown("### 馃幀 Generador de Video con Pexels")
    
    with gr.Tab("Generar Video"):
        with gr.Row():
            prompt_type = gr.Radio(
                ["Generar Guion con IA", "Usar Mi Guion"], 
                label="M茅todo",
                value="Generar Guion con IA"
            )
        
        with gr.Column(visible=True) as ia_guion_column:
            prompt_ia = gr.Textbox(
                label="Tema para IA", 
                lines=2
            )
        
        with gr.Column(visible=False) as manual_guion_column:
            prompt_manual = gr.Textbox(
                label="Tu Guion", 
                lines=5
            )
        
        musica_input = gr.Textbox(
            label="URL M煤sica (opcional)", 
        )
        
        boton = gr.Button("Generar Video")
        
        with gr.Column():
            salida_video = gr.Video(label="Video Resultado", interactive=False)
            estado_mensaje = gr.Textbox(label="Estado", interactive=False)

    prompt_type.change(
        fn=lambda value: (gr.update(visible=value == "Generar Guion con IA"), 
        gr.update(visible=value == "Usar Mi Guion")),
        inputs=prompt_type,
        outputs=[ia_guion_column, manual_guion_column]
    )

    boton.click(
        fn=lambda: (None, "Procesando..."),
        outputs=[salida_video, estado_mensaje],
        queue=False
    ).then(
        fn=run_app,
        inputs=[prompt_type, prompt_ia, prompt_manual, musica_input],
        outputs=[salida_video, estado_mensaje]
    )

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