File size: 12,352 Bytes
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a240c6
 
9834af3
6a240c6
9834af3
6a240c6
 
 
 
 
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db23a91
 
 
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
db23a91
 
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fba411c
9834af3
 
fba411c
9834af3
 
fba411c
9834af3
 
 
fba411c
 
9834af3
e20283b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9834af3
e20283b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9834af3
 
 
fba411c
9834af3
fba411c
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db23a91
 
 
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb7b8e3
 
9834af3
bb7b8e3
9834af3
bb7b8e3
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fba411c
9834af3
 
 
 
 
fba411c
 
 
9834af3
 
 
 
 
 
 
 
 
 
 
e93bb43
9834af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Fact Creator Service
Main orchestrator for generating fact-image videos
"""
import asyncio
import logging
import uuid
import time
from pathlib import Path
from typing import Dict, List, Optional
from datetime import datetime

from ..schemas import JobStatus, ImageModel
from .text_overlay import TextOverlay

logger = logging.getLogger(__name__)


# Master prompt for image generation (CLEAN, NO TEXT)
IMAGE_SYSTEM_PROMPT = """Generate a clean, cinematic, vertical image (9:16 aspect ratio) based on this description:

{image_prompt}

IMPORTANT:
- Do NOT add any text, words, letters, numbers, or watermarks in the image
- Leave empty/soft area at bottom 40% for text overlay
- Clean aesthetic, modern TikTok/Instagram style
- High-quality, beautiful lighting"""


class FactCreator:
    """
    Main orchestrator for fact-image video generation.
    
    Pipeline:
    1. Generate clean image (NVIDIA/Cloudflare/Pexels)
    2. Add text overlay (PIL)
    3. Create short video (MoviePy)
    4. Upload to cloud (optional)
    """
    
    # Video settings
    TARGET_WIDTH = 1080
    TARGET_HEIGHT = 1920
    FPS = 24
    FADE_DURATION = 0.3
    
    def __init__(
        self,
        config,
        nvidia_client=None,
        cloudflare_client=None,
        pexels_client=None
    ):
        self.config = config
        self.nvidia = nvidia_client
        self.cloudflare = cloudflare_client
        self.pexels = pexels_client
        self.text_overlay = TextOverlay()
        
        # Job tracking
        self.jobs: Dict[str, Dict] = {}
        self.queue: List[Dict] = []
        self.processing = False
    
    def add_to_queue(
        self,
        model: ImageModel,
        image_prompt: str,
        fact_text: str,
        duration: int = 5,
        fact_heading: str = None,
        heading_background: dict = None
    ) -> str:
        """
        Add fact-image job to queue.
        
        Returns:
            job_id for tracking
        """
        job_id = str(uuid.uuid4()).replace('-', '')[:16]
        
        job = {
            "id": job_id,
            "model": model,
            "image_prompt": image_prompt,
            "fact_heading": fact_heading,
            "heading_background": heading_background,
            "fact_text": fact_text,
            "duration": duration,
            "status": JobStatus.queued,
            "progress": 0,
            "created_at": datetime.now().isoformat(),
            "video_url": None,
            "error": None
        }
        
        self.jobs[job_id] = job
        self.queue.append(job)
        
        logger.info(f"Added job {job_id} to queue. Queue length: {len(self.queue)}")
        
        # Start processing if not already running
        if not self.processing:
            asyncio.create_task(self.process_queue())
        
        return job_id
    
    async def process_queue(self):
        """Process jobs in queue"""
        if self.processing:
            return
        
        self.processing = True
        
        try:
            while self.queue:
                job = self.queue[0]
                job_id = job["id"]
                
                logger.info(f"Processing job {job_id}")
                
                try:
                    await self._process_job(job)
                    job["status"] = JobStatus.ready
                    job["progress"] = 100
                    logger.info(f"Job {job_id} completed successfully")
                except Exception as e:
                    logger.error(f"Job {job_id} failed: {e}", exc_info=True)
                    job["status"] = JobStatus.failed
                    job["error"] = str(e)
                finally:
                    self.queue.pop(0)
        finally:
            self.processing = False
    
    async def _process_job(self, job: Dict):
        """Process a single fact-image job"""
        job_id = job["id"]
        temp_dir = self.config.temp_dir_path / job_id
        temp_dir.mkdir(parents=True, exist_ok=True)
        
        try:
            # ====================
            # Step 1: Generate Image
            # ====================
            job["status"] = JobStatus.generating_image
            job["progress"] = 10
            logger.info(f"[{job_id}] Generating image with {job['model']}...")
            
            image_path = temp_dir / "base_image.png"
            
            # Build full prompt
            full_prompt = IMAGE_SYSTEM_PROMPT.format(image_prompt=job["image_prompt"])
            
            if job["model"] == ImageModel.nvidia and self.nvidia:
                # NVIDIA: uses aspect_ratio "9:16" internally (no width/height params)
                self.nvidia.generate_and_save(
                    prompt=full_prompt,
                    output_path=image_path
                )
            elif job["model"] == ImageModel.cloudflare and self.cloudflare:
                # Cloudflare: supports width/height (1080x1920)
                self.cloudflare.generate_and_save(
                    prompt=full_prompt,
                    output_path=image_path,
                    width=1080,
                    height=1920
                )
            elif job["model"] == ImageModel.pexels:
                # Pexels: Direct API call to get FIRST (most relevant) photo
                import requests as pexels_requests
                import os
                pexels_key = os.getenv("PEXELS_API_KEY")
                
                if not pexels_key:
                    raise Exception("PEXELS_API_KEY not configured")
                
                logger.info(f"[{job_id}] Searching Pexels for: {job['image_prompt']}")
                
                resp = pexels_requests.get(
                    "https://api.pexels.com/v1/search",
                    headers={"Authorization": pexels_key},
                    params={
                        "query": job["image_prompt"],
                        "orientation": "portrait",
                        "per_page": 5,
                        "size": "large"
                    },
                    timeout=15
                )
                
                if resp.status_code != 200:
                    raise Exception(f"Pexels API error: {resp.status_code}")
                
                photos = resp.json().get("photos", [])
                if not photos:
                    raise Exception(f"No Pexels photos found for: {job['image_prompt']}")
                
                # Select FIRST (most relevant) photo, NOT random
                photo = photos[0]
                photo_url = photo.get("src", {}).get("original") or photo.get("src", {}).get("large")
                
                if not photo_url:
                    raise Exception("No valid photo URL from Pexels")
                
                logger.info(f"[{job_id}] Selected Pexels photo ID {photo['id']}")
                
                # Download photo
                img_resp = pexels_requests.get(photo_url, timeout=30)
                img_resp.raise_for_status()
                image_path.parent.mkdir(parents=True, exist_ok=True)
                image_path.write_bytes(img_resp.content)
                logger.info(f"[{job_id}] Downloaded Pexels photo")
            else:
                # Fallback to any available client
                if self.nvidia:
                    self.nvidia.generate_and_save(full_prompt, image_path)
                elif self.cloudflare:
                    self.cloudflare.generate_and_save(full_prompt, image_path, width=1080, height=1920)
                else:
                    raise Exception("No image generation client available!")
            
            job["progress"] = 40
            
            # ====================
            # Step 2: Add Text Overlay
            # ====================
            job["status"] = JobStatus.adding_text
            logger.info(f"[{job_id}] Adding text overlay...")
            
            overlay_path = temp_dir / "overlay_image.png"
            self.text_overlay.add_text(
                image_path=image_path,
                text=job["fact_text"],
                output_path=overlay_path,
                heading=job.get("fact_heading"),
                heading_background=job.get("heading_background")
            )
            
            job["progress"] = 60
            
            # ====================
            # Step 3: Create Video
            # ====================
            job["status"] = JobStatus.creating_video
            logger.info(f"[{job_id}] Creating {job['duration']}s video...")
            
            output_path = self.config.videos_dir_path / f"{job_id}.mp4"
            await self._create_video(overlay_path, output_path, job["duration"])
            
            job["video_url"] = str(output_path)
            job["progress"] = 90
            
            # ====================
            # Step 4: Upload to Cloud (Optional)
            # ====================
            from modules.shared.services.hf_storage import get_hf_storage
            hf_client = get_hf_storage()
            
            if hf_client and hf_client.enabled:
                logger.info(f"[{job_id}] Uploading to HF Hub...")
                cloud_url = hf_client.upload_video(output_path, job_id, "fact_image")
                if cloud_url:
                    job["video_url"] = cloud_url
                    job["storage"] = "cloud"
                    # Save cloud URL to metadata file
                    cloud_file = output_path.with_suffix('.cloud')
                    cloud_file.write_text(cloud_url)
                    # Delete local file
                    output_path.unlink()
                    logger.info(f"[{job_id}] Uploaded to cloud, local file deleted")
            
            logger.info(f"[{job_id}] Video ready: {job['video_url']}")
            
        finally:
            # Cleanup temp files
            import shutil
            if temp_dir.exists():
                shutil.rmtree(temp_dir, ignore_errors=True)
    
    async def _create_video(self, image_path: Path, output_path: Path, duration: int):
        """Create video from single image with fade effects (9:16 = 1080x1920)"""
        from moviepy.editor import ImageClip
        
        # Create image clip
        clip = ImageClip(str(image_path)).set_duration(duration)
        
        # Ensure 9:16 aspect ratio (1080x1920)
        clip = clip.resize((self.TARGET_WIDTH, self.TARGET_HEIGHT))
        
        # Add fade in/out
        clip = clip.fadein(self.FADE_DURATION)
        clip = clip.fadeout(self.FADE_DURATION)
        
        # Write video
        logger.info(f"Writing video: {duration}s, fade in/out {self.FADE_DURATION}s")
        clip.write_videofile(
            str(output_path),
            fps=self.FPS,
            codec='libx264',
            audio=False,  # No audio for fact videos
            preset='ultrafast',  # Fast export (3-5x faster)
            ffmpeg_params=[
                '-pix_fmt', 'yuv420p',
                '-movflags', '+faststart',
                '-profile:v', 'baseline',
                '-level', '3.0'
            ]
        )
        
        clip.close()
    
    def get_status(self, job_id: str) -> Dict:
        """Get job status"""
        job = self.jobs.get(job_id)
        
        if not job:
            # Check for .cloud file (cloud-stored video)
            cloud_file = self.config.videos_dir_path / f"{job_id}.cloud"
            if cloud_file.exists():
                return {
                    "job_id": job_id,
                    "status": JobStatus.ready,
                    "progress": 100,
                    "video_url": cloud_file.read_text().strip()
                }
            
            return {
                "job_id": job_id,
                "status": JobStatus.failed,
                "progress": 0,
                "error": "Job not found"
            }
        
        return {
            "job_id": job["id"],
            "status": job["status"],
            "progress": job.get("progress", 0),
            "video_url": job.get("video_url"),
            "error": job.get("error")
        }
    
    def get_video_path(self, job_id: str) -> Optional[Path]:
        """Get video file path"""
        return self.config.videos_dir_path / f"{job_id}.mp4"