File size: 11,179 Bytes
7fa9d90
347b758
7fa9d90
347b758
7fa9d90
 
4d81e3f
347b758
7fa9d90
 
 
 
 
 
 
347b758
7fa9d90
 
 
 
4d81e3f
7fa9d90
 
 
b4761cd
 
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9d90
 
 
 
 
 
 
 
 
347b758
7fa9d90
 
 
 
 
 
 
 
 
 
 
347b758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9d90
 
347b758
7fa9d90
 
347b758
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
902efbe
 
7fa9d90
902efbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
fd65e8f
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd65e8f
 
7fa9d90
 
 
 
 
 
 
 
 
 
347b758
 
 
 
 
 
 
7fa9d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347b758
7fa9d90
 
 
 
 
 
 
 
347b758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Script Generator using Groq and Gemini APIs
Generates story scripts from topics for TTS narration
Tries Groq first (works in all regions), falls back to Gemini
"""
import logging
import json
import os
from typing import Optional

logger = logging.getLogger(__name__)


class ScriptGenerator:
    """
    Generates story scripts using Groq API (primary) or Gemini API (fallback).
    
    Features:
    - Topic → Full narration script (<=1000 chars)
    - Character-aware script generation
    - AI image prompt generation
    - Optimized for TTS output
    """
    
    GROQ_MODEL = "openai/gpt-oss-120b"  # 120B reasoning model
    GEMINI_MODEL = "gemini-2.0-flash"
    
    # System prompt for script generation
    SYSTEM_PROMPT = """You are a professional script writer for short-form video content (TikTok, Reels, Shorts).

RULES:
1. Write a narration script for the given topic
2. Maximum 1000 characters (STRICT LIMIT)
3. Write in a natural, engaging voice
4. Focus on storytelling - beginning, middle, end
5. Use simple, clear sentences for TTS
6. NO emojis, NO hashtags, NO special formatting
7. Output ONLY the script text, nothing else

If a character is provided, write the story from their perspective or about them."""

    def __init__(self, gemini_api_key: str = None, groq_api_key: str = None):
        self.gemini_api_key = gemini_api_key
        self.groq_api_key = groq_api_key
        
        # Initialize clients based on available keys
        self.groq_client = None
        self.gemini_client = None
        
        if groq_api_key:
            try:
                from groq import Groq
                self.groq_client = Groq(api_key=groq_api_key)
                logger.info("Groq client initialized (primary)")
            except ImportError:
                logger.warning("Groq package not installed, using Gemini only")
        
        if gemini_api_key:
            try:
                from google import genai
                self.gemini_client = genai.Client(api_key=gemini_api_key)
                logger.info("Gemini client initialized (fallback)")
            except ImportError:
                logger.warning("google-genai package not installed")
        
        if not self.groq_client and not self.gemini_client:
            raise ValueError("At least one API key (GROQ_API or GEMINI_API_KEY) is required")
        
    def generate_script(
        self,
        topic: str,
        character_name: Optional[str] = None,
        max_chars: int = 1000
    ) -> str:
        """
        Generate a story script from topic.
        Tries Groq first, falls back to Gemini if Groq fails.
        """
        # Build the prompt
        user_prompt = f"Topic: {topic}"
        
        if character_name:
            user_prompt += f"\nMain Character: {character_name}"
            
        user_prompt += f"\n\nWrite a short narration script (max {max_chars} characters)."
        
        logger.info(f"Generating script for topic: {topic[:50]}...")
        
        # Try Groq first (works in all regions)
        if self.groq_client:
            try:
                script = self._generate_with_groq(user_prompt)
                if len(script) > max_chars:
                    script = script[:max_chars].rsplit(' ', 1)[0] + "."
                logger.info(f"Generated script with Groq: {len(script)} chars")
                return script.strip()
            except Exception as e:
                logger.warning(f"Groq failed: {e}, trying Gemini...")
        
        # Fallback to Gemini
        if self.gemini_client:
            try:
                script = self._generate_with_gemini(user_prompt)
                if len(script) > max_chars:
                    script = script[:max_chars].rsplit(' ', 1)[0] + "."
                logger.info(f"Generated script with Gemini: {len(script)} chars")
                return script.strip()
            except Exception as e:
                logger.error(f"Gemini also failed: {e}")
                raise Exception(f"Script generation failed: {e}")
        
        raise Exception("No AI backend available for script generation")
    
    def _generate_with_groq(self, user_prompt: str) -> str:
        """Generate using Groq API"""
        completion = self.groq_client.chat.completions.create(
            model=self.GROQ_MODEL,
            messages=[
                {"role": "system", "content": self.SYSTEM_PROMPT},
                {"role": "user", "content": user_prompt}
            ],
            temperature=0.7,
            max_tokens=500,
            top_p=0.9
        )
        return completion.choices[0].message.content
    
    def _generate_with_gemini(self, user_prompt: str) -> str:
        """Generate using Gemini API"""
        response = self.gemini_client.models.generate_content(
            model=self.GEMINI_MODEL,
            contents=self.SYSTEM_PROMPT + "\n\n" + user_prompt
        )
        return response.text
    
    @staticmethod
    def test_connection(gemini_api_key: str = None, groq_api_key: str = None) -> bool:
        """Test API connection"""
        try:
            gen = ScriptGenerator(gemini_api_key=gemini_api_key, groq_api_key=groq_api_key)
            gen.generate_script("test", max_chars=50)
            return True
        except:
            return False
    
    # System prompt for image prompt generation
    IMAGE_PROMPT_SYSTEM = """You are an expert at creating detailed image prompts for AI image generation.

Your task: Generate detailed image prompts for each 2-second scene of a story video.

CONTEXT:
- Full story script is provided so you understand the narrative
- Each 2-second chunk needs a visual prompt
- Images will play in SEQUENCE to tell a story
- All images MUST look like they belong to the SAME VIDEO

CRITICAL RULES FOR CONSISTENCY:
1. SAME STYLE: Every prompt MUST start with the exact style name (e.g., "semi-realistic style", "anime style", "sticky animation style")
2. SAME CHARACTER: If a character is described, use IDENTICAL description in EVERY prompt (same clothes, hair, face features)
3. SCENE CONTINUITY: Each scene should logically follow the previous one
   - Example: Scene 1 "boy picking up bag" → Scene 2 "boy walking with bag on shoulder" → Scene 3 "boy approaching school gate"
4. CONSISTENT LIGHTING: Use same lighting style across all scenes
5. CONSISTENT COLOR PALETTE: Maintain similar color tones

PROMPT STRUCTURE:
1. [STYLE] - Always start with style (e.g., "semi-realistic style artwork")
2. [CHARACTER] - Describe the character with exact same details every time
3. [ACTION] - What's happening in THIS specific 2-second moment
4. [ENVIRONMENT] - Where is this taking place
5. [CAMERA] - Camera angle (close-up, medium shot, wide shot)
6. [LIGHTING & MOOD] - Lighting and emotional atmosphere
7. [QUALITY TAGS] - high quality, detailed, cinematic, 8k

CONTINUITY TIPS:
- If character was sitting, show transition to standing (not jumping to running)
- Keep background elements consistent (same room, same street)
- Props should persist (if bag appeared, keep showing it)
- Time progression should be logical

OUTPUT FORMAT:
Return ONLY valid JSON array, no markdown, no explanation:
[
  {"chunk_id": 1, "prompt": "detailed prompt here..."},
  {"chunk_id": 2, "prompt": "detailed prompt here..."}
]"""

    def generate_image_prompts(
        self,
        full_script: str,
        chunks: list,
        image_style: str = "semi-realistic",
        max_batch: int = 30
    ) -> list:
        """
        Generate detailed image prompts for all 2-second chunks.
        """
        all_prompts = []
        total_chunks = len(chunks)
        
        for batch_start in range(0, total_chunks, max_batch):
            batch_end = min(batch_start + max_batch, total_chunks)
            batch_chunks = chunks[batch_start:batch_end]
            
            logger.info(f"Generating prompts for chunks {batch_start+1}-{batch_end} of {total_chunks}")
            
            # Build user prompt
            user_prompt = f"""FULL STORY SCRIPT:
{full_script}

IMAGE STYLE: {image_style}
(Apply this style consistently to ALL images: {image_style}, high quality, detailed, cinematic lighting)

"""
            
            user_prompt += "2-SECOND CHUNKS TO GENERATE PROMPTS FOR:\n"
            for chunk in batch_chunks:
                user_prompt += f"- Chunk {chunk['chunk_id']}: \"{chunk['text']}\"\n"
            
            user_prompt += "\nGenerate detailed image prompts for each chunk. Return ONLY JSON array."
            
            try:
                # Try Groq first
                if self.groq_client:
                    text = self._generate_image_prompts_groq(user_prompt)
                elif self.gemini_client:
                    text = self._generate_image_prompts_gemini(user_prompt)
                else:
                    raise Exception("No AI backend available")
                
                # Clean response - remove markdown if present
                text = text.strip()
                if text.startswith("```"):
                    text = text.split("```")[1]
                    if text.startswith("json"):
                        text = text[4:]
                text = text.strip()
                
                # Parse JSON
                batch_prompts = json.loads(text)
                all_prompts.extend(batch_prompts)
                
                logger.info(f"Generated {len(batch_prompts)} prompts in batch")
                
            except json.JSONDecodeError as e:
                logger.error(f"Failed to parse JSON response: {e}")
                for chunk in batch_chunks:
                    all_prompts.append({
                        "chunk_id": chunk["chunk_id"],
                        "prompt": f"{chunk['text']}, semi-realistic style, high quality, detailed"
                    })
            except Exception as e:
                logger.error(f"AI API error: {e}")
                for chunk in batch_chunks:
                    all_prompts.append({
                        "chunk_id": chunk["chunk_id"],
                        "prompt": f"{chunk['text']}, semi-realistic style, high quality"
                    })
        
        logger.info(f"Generated {len(all_prompts)} total image prompts")
        return all_prompts
    
    def _generate_image_prompts_groq(self, user_prompt: str) -> str:
        """Generate image prompts using Groq"""
        completion = self.groq_client.chat.completions.create(
            model=self.GROQ_MODEL,
            messages=[
                {"role": "system", "content": self.IMAGE_PROMPT_SYSTEM},
                {"role": "user", "content": user_prompt}
            ],
            temperature=0.7,
            max_tokens=4000,
            top_p=0.9
        )
        return completion.choices[0].message.content
    
    def _generate_image_prompts_gemini(self, user_prompt: str) -> str:
        """Generate image prompts using Gemini"""
        response = self.gemini_client.models.generate_content(
            model=self.GEMINI_MODEL,
            contents=self.IMAGE_PROMPT_SYSTEM + "\n\n" + user_prompt
        )
        return response.text