File size: 23,578 Bytes
c490f2c
89d05d0
c490f2c
 
 
 
 
 
 
48a6754
c490f2c
c7ff119
 
c490f2c
 
c7ff119
47a0253
c490f2c
 
 
 
 
774d989
c490f2c
 
 
 
 
 
 
 
 
9577f14
c490f2c
 
 
 
 
 
 
 
89d05d0
c490f2c
89d05d0
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372c71b
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48a1d98
c490f2c
 
 
47a0253
c490f2c
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
47a0253
c490f2c
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
89d05d0
c490f2c
 
 
 
 
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89d05d0
47a0253
c490f2c
 
 
 
 
 
47a0253
c490f2c
 
 
 
 
1125cdd
c490f2c
 
 
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AI Documentary Video Generator
Version: 2.0
Author: AI Assistant
Description: Creates documentary-style videos from text prompts using TTS, media APIs, and video processing
"""

import os
import sys
import re
import time
import random
import tempfile
import shutil
import traceback
import math
from typing import Optional, Tuple, Dict, List, Union
from dataclasses import dataclass
from concurrent.futures import ThreadPoolExecutor
from urllib.parse import quote

# Third-party imports
import numpy as np
import cv2
import requests
from PIL import Image
import soundfile as sf
import torch
from bs4 import BeautifulSoup
import gradio as gr

# MoviePy imports
from moviepy.editor import (
    VideoFileClip, AudioFileClip, ImageClip, 
    concatenate_videoclips, CompositeVideoClip,
    CompositeAudioClip, TextClip
)
import moviepy.video.fx.all as vfx
import moviepy.config as mpy_config

# Custom imports
try:
    from kokoro import KPipeline
except ImportError:
    print("Warning: Kokoro TTS not available. TTS features will be disabled.")

# ======================
# CONSTANTS & CONFIG
# ======================
DEFAULT_CONFIG = {
    'PEXELS_API_KEY': os.getenv('PEXELS_API_KEY', 'your_pexels_key_here'),
    'OPENROUTER_API_KEY': os.getenv('OPENROUTER_API_KEY', 'your_openrouter_key_here'),
    'OUTPUT_VIDEO': "documentary_output.mp4",
    'USER_AGENT': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    'MAX_RETRIES': 3,
    'RETRY_DELAY': 2,
    'MAX_VIDEO_DURATION': 600,  # 10 minutes
    'TTS_SAMPLE_RATE': 24000,
    'DEFAULT_VOICE': 'en',
    'CAPTION_FONT': 'Arial-Bold',
    'CAPTION_FONT_SIZES': {
        '1080p': 40,
        '720p': 32,
        '480p': 24
    },
    'BACKGROUND_MUSIC_VOLUME': 0.08,
    'DEFAULT_FPS': 30,
    'VIDEO_PROBABILITY': 0.45,
    'RESOLUTIONS': {
        '1080p': (1920, 1080),
        '720p': (1280, 720),
        '480p': (854, 480),
        'vertical_1080p': (1080, 1920),
        'vertical_720p': (720, 1280)
    }
}

# ======================
# CORE CLASSES
# ======================

@dataclass
class VideoSegment:
    media_path: str
    tts_path: str
    narration: str
    duration: float
    media_type: str  # 'image' or 'video'
    effects: Dict
    caption_style: Dict

class DocumentaryGenerator:
    def __init__(self, config: Optional[Dict] = None):
        self.config = config or DEFAULT_CONFIG
        self.tts_pipeline = None
        self.temp_dir = None
        self.current_resolution = None
        self.caption_color = None
        
        # Initialize subsystems
        self._initialize_tts()
        self._configure_imagemagick()
        
    def _initialize_tts(self):
        """Initialize the TTS pipeline"""
        try:
            if 'KPipeline' in globals():
                print("Initializing Kokoro TTS pipeline...")
                self.tts_pipeline = KPipeline(lang_code='a')
                print("TTS pipeline ready")
        except Exception as e:
            print(f"Could not initialize TTS: {str(e)}")
            self.tts_pipeline = None
            
    def _configure_imagemagick(self):
        """Configure ImageMagick paths"""
        try:
            common_paths = [
                "/usr/bin/convert", 
                "/usr/local/bin/convert",
                "/opt/homebrew/bin/convert"
            ]
            for path in common_paths:
                if os.path.exists(path):
                    mpy_config.change_settings({"IMAGEMAGICK_BINARY": path})
                    print(f"ImageMagick configured: {path}")
                    break
            else:
                print("ImageMagick not found - text rendering may be limited")
        except Exception as e:
            print(f"ImageMagick config error: {str(e)}")
    
    def _create_temp_dir(self):
        """Create a temporary working directory"""
        if self.temp_dir and os.path.exists(self.temp_dir):
            shutil.rmtree(self.temp_dir)
        self.temp_dir = tempfile.mkdtemp(prefix="docgen_")
        print(f"Created temp directory: {self.temp_dir}")
        return self.temp_dir
    
    def _cleanup(self):
        """Clean up temporary resources"""
        if self.temp_dir and os.path.exists(self.temp_dir):
            try:
                shutil.rmtree(self.temp_dir)
                print("Cleaned up temporary files")
            except Exception as e:
                print(f"Cleanup error: {str(e)}")
    
    def generate_script(self, topic: str) -> str:
        """Generate a documentary script using OpenRouter API"""
        if not self.config['OPENROUTER_API_KEY']:
            return "Error: OpenRouter API key not configured"
            
        prompt = f"""Create a funny, engaging documentary script about {topic}.
        Format each section with [TITLE] followed by narration text.
        Keep narration concise (1-2 sentences per section).
        Include at least 5 sections.
        End with a humorous call-to-action."""
        
        headers = {
            'Authorization': f'Bearer {self.config['OPENROUTER_API_KEY']}',
            'Content-Type': 'application/json'
        }
        
        data = {
            "model": "mistralai/mistral-small-3.1-24b-instruct:free",
            "messages": [{"role": "user", "content": prompt}],
            "temperature": 0.7,
            "max_tokens": 1024
        }
        
        try:
            response = requests.post(
                'https://openrouter.ai/api/v1/chat/completions',
                headers=headers,
                json=data,
                timeout=30
            )
            response.raise_for_status()
            return response.json()['choices'][0]['message']['content']
        except Exception as e:
            return f"Error generating script: {str(e)}"
    
    def _download_media(self, url: str, filename: str) -> Optional[str]:
        """Download media file from URL"""
        local_path = os.path.join(self.temp_dir, filename)
        
        for attempt in range(self.config['MAX_RETRIES']):
            try:
                with requests.get(url, stream=True, timeout=15) as r:
                    r.raise_for_status()
                    with open(local_path, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=8192):
                            f.write(chunk)
                
                # Validate downloaded file
                if os.path.getsize(local_path) > 1024:
                    return local_path
                
            except Exception as e:
                print(f"Download attempt {attempt + 1} failed: {str(e)}")
                time.sleep(self.config['RETRY_DELAY'] * (attempt + 1))
        
        return None
    
    def _search_pexels_video(self, query: str) -> Optional[str]:
        """Search for videos on Pexels"""
        if not self.config['PEXELS_API_KEY']:
            return None
            
        headers = {'Authorization': self.config['PEXELS_API_KEY']}
        params = {
            'query': query,
            'per_page': 15,
            'orientation': 'landscape'
        }
        
        try:
            response = requests.get(
                'https://api.pexels.com/videos/search',
                headers=headers,
                params=params,
                timeout=10
            )
            response.raise_for_status()
            
            videos = response.json().get('videos', [])
            if videos:
                video_files = videos[0].get('video_files', [])
                for file in video_files:
                    if file.get('quality') == 'hd':
                        return file.get('link')
        except Exception as e:
            print(f"Pexels search error: {str(e)}")
        
        return None
    
    def _generate_tts(self, text: str) -> Optional[str]:
        """Generate TTS audio for narration"""
        if not self.tts_pipeline:
            return None
            
        safe_name = re.sub(r'[^\w\-_]', '', text[:20]) + '.wav'
        output_path = os.path.join(self.temp_dir, safe_name)
        
        try:
            audio_segments = []
            for chunk in self.tts_pipeline(text, speed=1.0):
                if isinstance(chunk, tuple):
                    chunk = chunk[-1]  # Get audio data from tuple
                audio_segments.append(chunk)
            
            full_audio = np.concatenate(audio_segments)
            sf.write(output_path, full_audio, self.config['TTS_SAMPLE_RATE'])
            return output_path
        except Exception as e:
            print(f"TTS generation error: {str(e)}")
            return None
    
    def _create_video_segment(self, segment: VideoSegment) -> Optional[VideoClip]:
        """Create a single video segment with media, audio, and effects"""
        try:
            # Load media
            if segment.media_type == 'video':
                media_clip = VideoFileClip(segment.media_path)
            else:
                media_clip = ImageClip(segment.media_path).set_duration(segment.duration)
            
            # Apply effects
            if segment.media_type == 'image':
                media_clip = self._apply_kenburns(media_clip)
            else:
                media_clip = self._resize_clip(media_clip)
            
            # Add audio
            audio_clip = AudioFileClip(segment.tts_path)
            media_clip = media_clip.set_audio(audio_clip)
            
            # Add captions if enabled
            if segment.caption_style.get('enabled', False):
                media_clip = self._add_captions(media_clip, segment.narration, segment.caption_style)
            
            return media_clip
        except Exception as e:
            print(f"Segment creation error: {str(e)}")
            traceback.print_exc()
            return None
    
    def _apply_kenburns(self, clip: ImageClip) -> VideoClip:
        """Apply Ken Burns effect to an image clip"""
        try:
            target_w, target_h = self.current_resolution
            base_scale = 1.2
            
            # Choose random effect
            effects = {
                'zoom_in': {'start_scale': 1.0, 'end_scale': base_scale},
                'zoom_out': {'start_scale': base_scale, 'end_scale': 1.0},
                'pan_left': {'start_pos': (0.7, 0.5), 'end_pos': (0.3, 0.5)},
                'pan_right': {'start_pos': (0.3, 0.5), 'end_pos': (0.7, 0.5)}
            }
            effect = random.choice(list(effects.values()))
            
            def transform(get_frame, t):
                ratio = t / clip.duration
                scale = effect['start_scale'] + (effect['end_scale'] - effect['start_scale']) * ratio
                pos_x = effect['start_pos'][0] + (effect['end_pos'][0] - effect['start_pos'][0]) * ratio
                pos_y = effect['start_pos'][1] + (effect['end_pos'][1] - effect['start_pos'][1]) * ratio
                
                frame = get_frame(t)
                h, w = frame.shape[:2]
                crop_size = (int(w/scale), int(h/scale))
                
                center_x = int(pos_x * w)
                center_y = int(pos_y * h)
                
                x = max(0, min(center_x - crop_size[0]//2, w - crop_size[0]))
                y = max(0, min(center_y - crop_size[1]//2, h - crop_size[1]))
                
                cropped = frame[y:y+crop_size[1], x:x+crop_size[0]]
                return cv2.resize(cropped, (target_w, target_h), interpolation=cv2.INTER_LANCZOS4)
            
            return clip.fl(transform)
        except Exception as e:
            print(f"Ken Burns error: {str(e)}")
            return self._resize_clip(clip)
    
    def _resize_clip(self, clip: Union[VideoClip, ImageClip]) -> VideoClip:
        """Resize clip to target resolution"""
        try:
            target_w, target_h = self.current_resolution
            clip_aspect = clip.w / clip.h
            target_aspect = target_w / target_h
            
            if abs(clip_aspect - target_aspect) < 0.01:
                return clip.resize((target_w, target_h))
            
            if clip_aspect > target_aspect:
                # Wider than target
                new_height = target_h
                new_width = int(clip.w * (new_height / clip.h))
                resized = clip.resize(height=new_height)
                crop = (resized.w - target_w) / 2
                return resized.crop(x1=crop, y1=0, x2=crop+target_w, y2=target_h)
            else:
                # Taller than target
                new_width = target_w
                new_height = int(clip.h * (new_width / clip.w))
                resized = clip.resize(width=new_width)
                crop = (resized.h - target_h) / 2
                return resized.crop(x1=0, y1=crop, x2=target_w, y2=crop+target_h)
        except Exception as e:
            print(f"Resize error: {str(e)}")
            return clip
    
    def _add_captions(self, clip: VideoClip, text: str, style: Dict) -> VideoClip:
        """Add captions to a video clip"""
        try:
            words = text.split()
            chunks = []
            current_chunk = []
            char_count = 0
            
            # Split text into manageable chunks
            for word in words:
                if char_count + len(word) > 30 and current_chunk:
                    chunks.append(' '.join(current_chunk))
                    current_chunk = [word]
                    char_count = len(word)
                else:
                    current_chunk.append(word)
                    char_count += len(word) + 1
            
            if current_chunk:
                chunks.append(' '.join(current_chunk))
            
            # Create text clips
            text_clips = []
            duration_per_chunk = clip.duration / len(chunks)
            
            for i, chunk in enumerate(chunks):
                txt_clip = TextClip(
                    chunk,
                    fontsize=style['font_size'],
                    font=style['font'],
                    color=style['color'],
                    bg_color=style['bg_color'],
                    stroke_color=style['stroke_color'],
                    stroke_width=style['stroke_width'],
                    method='caption',
                    size=(style['max_width'], None)
                ).set_start(i * duration_per_chunk).set_duration(duration_per_chunk)
                
                txt_clip = txt_clip.set_position(('center', style['y_position']))
                text_clips.append(txt_clip)
            
            return CompositeVideoClip([clip] + text_clips)
        except Exception as e:
            print(f"Caption error: {str(e)}")
            return clip
    
    def generate_video(self, script: str, resolution: str, captions: bool, 
                      music_path: Optional[str] = None) -> Optional[str]:
        """Main video generation pipeline"""
        start_time = time.time()
        
        try:
            # Setup
            self._create_temp_dir()
            self.current_resolution = self.config['RESOLUTIONS'].get(resolution, (1920, 1080))
            self.caption_color = 'white' if captions else None
            
            # Parse script into segments
            segments = self._parse_script(script)
            if not segments:
                print("Error: No valid segments found in script")
                return None
            
            # Process segments in parallel
            with ThreadPoolExecutor() as executor:
                video_segments = list(executor.map(self._process_segment, segments))
            
            # Combine segments
            final_clip = concatenate_videoclips(
                [s for s in video_segments if s is not None],
                method="compose"
            )
            
            # Add background music if provided
            if music_path and os.path.exists(music_path):
                music_clip = AudioFileClip(music_path).volumex(self.config['BACKGROUND_MUSIC_VOLUME'])
                if music_clip.duration < final_clip.duration:
                    music_clip = music_clip.loop(duration=final_clip.duration)
                final_clip = final_clip.set_audio(
                    CompositeAudioClip([final_clip.audio, music_clip])
                )
            
            # Export final video
            output_path = self.config['OUTPUT_VIDEO']
            final_clip.write_videofile(
                output_path,
                codec='libx264',
                audio_codec='aac',
                fps=self.config['DEFAULT_FPS'],
                threads=os.cpu_count() or 4
            )
            
            print(f"Video generated in {time.time() - start_time:.2f} seconds")
            return output_path
            
        except Exception as e:
            print(f"Video generation failed: {str(e)}")
            traceback.print_exc()
            return None
        finally:
            self._cleanup()
    
    def _parse_script(self, script: str) -> List[Dict]:
        """Parse script into media and narration segments"""
        segments = []
        current_title = None
        current_text = ""
        
        for line in script.split('\n'):
            line = line.strip()
            if not line:
                continue
                
            # Check for section title
            title_match = re.match(r'^\s*\[([^\]]+)\]\s*(.*)', line)
            if title_match:
                if current_title and current_text:
                    segments.append({
                        'title': current_title,
                        'text': current_text.strip()
                    })
                current_title = title_match.group(1).strip()
                current_text = title_match.group(2).strip() + " "
            elif current_title:
                current_text += line + " "
        
        # Add the last segment
        if current_title and current_text:
            segments.append({
                'title': current_title,
                'text': current_text.strip()
            })
        
        return segments
    
    def _process_segment(self, segment: Dict) -> Optional[VideoSegment]:
        """Process a single script segment into a video segment"""
        try:
            # Get media
            use_video = random.random() < self.config['VIDEO_PROBABILITY']
            if use_video:
                media_url = self._search_pexels_video(segment['title'])
                media_type = 'video'
            else:
                media_url = self._search_pexels_image(segment['title'])
                media_type = 'image'
            
            if not media_url:
                print(f"No media found for: {segment['title']}")
                return None
            
            # Download media
            media_ext = '.mp4' if media_type == 'video' else '.jpg'
            media_filename = f"media_{len(segment['title'])}_media_ext"
            media_path = self._download_media(media_url, media_filename)
            
            if not media_path:
                print(f"Failed to download media for: {segment['title']}")
                return None
            
            # Generate TTS
            tts_path = self._generate_tts(segment['text'])
            if not tts_path:
                print(f"Failed to generate TTS for: {segment['title']}")
                return None
            
            # Calculate duration based on TTS
            tts_duration = AudioFileClip(tts_path).duration
            duration = max(3.0, min(tts_duration * 1.1, 10.0))  # 3-10 seconds
            
            # Prepare caption style
            caption_style = {
                'enabled': self.caption_color is not None,
                'font_size': self._get_font_size(),
                'font': self.config['CAPTION_FONT'],
                'color': self.caption_color or 'white',
                'bg_color': 'rgba(0,0,0,0.5)',
                'stroke_color': 'black',
                'stroke_width': 1.5,
                'max_width': int(self.current_resolution[0] * 0.8),
                'y_position': int(self.current_resolution[1] * 0.8)
            }
            
            return VideoSegment(
                media_path=media_path,
                tts_path=tts_path,
                narration=segment['text'],
                duration=duration,
                media_type=media_type,
                effects={'type': 'random'},
                caption_style=caption_style
            )
            
        except Exception as e:
            print(f"Segment processing error: {str(e)}")
            return None
    
    def _get_font_size(self) -> int:
        """Get appropriate font size for current resolution"""
        if self.current_resolution[1] >= 1080:
            return self.config['CAPTION_FONT_SIZES']['1080p']
        elif self.current_resolution[1] >= 720:
            return self.config['CAPTION_FONT_SIZES']['720p']
        else:
            return self.config['CAPTION_FONT_SIZES']['480p']

# ======================
# GRADIO INTERFACE
# ======================

def create_gradio_interface():
    """Create the Gradio web interface"""
    generator = DocumentaryGenerator()
    
    with gr.Blocks(title="AI Documentary Maker", theme="soft") as app:
        gr.Markdown("# AI Documentary Video Generator")
        
        with gr.Row():
            with gr.Column():
                topic_input = gr.Textbox(label="Documentary Topic", placeholder="Enter your topic...")
                generate_script_btn = gr.Button("Generate Script")
                script_output = gr.Textbox(label="Generated Script", lines=10, interactive=True)
                
                with gr.Accordion("Advanced Options", open=False):
                    resolution = gr.Dropdown(
                        list(generator.config['RESOLUTIONS'].keys()),
                        value="1080p",
                        label="Resolution"
                    )
                    captions = gr.Checkbox(
                        value=True,
                        label="Enable Captions"
                    )
                    music_input = gr.Audio(
                        label="Background Music",
                        type="filepath",
                        optional=True
                    )
                    video_prob = gr.Slider(
                        minimum=0.1,
                        maximum=1.0,
                        value=generator.config['VIDEO_PROBABILITY'],
                        label="Video Clip Probability"
                    )
                
                generate_video_btn = gr.Button("Generate Video", variant="primary")
                
            with gr.Column():
                video_output = gr.Video(label="Generated Documentary")
                status_output = gr.Textbox(label="Status", interactive=False)
        
        # Event handlers
        generate_script_btn.click(
            fn=generator.generate_script,
            inputs=[topic_input],
            outputs=[script_output]
        )
        
        generate_video_btn.click(
            fn=generator.generate_video,
            inputs=[script_output, resolution, captions, music_input],
            outputs=[video_output],
            api_name="generate"
        )
    
    return app

# ======================
# MAIN EXECUTION
# ======================

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