ismdrobiul489 commited on
Commit
4d81e3f
·
1 Parent(s): 11ac04d

Update to google-genai SDK for Gemini API

Browse files
modules/story_reels/services/script_generator.py CHANGED
@@ -1,25 +1,28 @@
1
  """
2
- Script Generator using Gemini API
3
  Generates story scripts from topics for TTS narration
4
  """
5
  import logging
6
- import requests
7
  from typing import Optional
8
 
 
 
9
  logger = logging.getLogger(__name__)
10
 
11
 
12
  class ScriptGenerator:
13
  """
14
- Generates story scripts using Google Gemini API.
15
 
16
  Features:
17
  - Topic → Full narration script (<=1000 chars)
18
  - Character-aware script generation
 
19
  - Optimized for TTS output
20
  """
21
 
22
- GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
23
 
24
  # System prompt for script generation
25
  SYSTEM_PROMPT = """You are a professional script writer for short-form video content (TikTok, Reels, Shorts).
@@ -37,6 +40,7 @@ If a character is provided, write the story from their perspective or about them
37
 
38
  def __init__(self, api_key: str):
39
  self.api_key = api_key
 
40
 
41
  def generate_script(
42
  self,
@@ -66,30 +70,12 @@ If a character is provided, write the story from their perspective or about them
66
  logger.info(f"Generating script for topic: {topic[:50]}...")
67
 
68
  try:
69
- response = requests.post(
70
- f"{self.GEMINI_API_URL}?key={self.api_key}",
71
- headers={"Content-Type": "application/json"},
72
- json={
73
- "contents": [
74
- {
75
- "role": "user",
76
- "parts": [{"text": self.SYSTEM_PROMPT + "\n\n" + user_prompt}]
77
- }
78
- ],
79
- "generationConfig": {
80
- "temperature": 0.7,
81
- "maxOutputTokens": 500,
82
- "topP": 0.9
83
- }
84
- },
85
- timeout=30
86
  )
87
- response.raise_for_status()
88
-
89
- data = response.json()
90
 
91
- # Extract text from response
92
- script = data["candidates"][0]["content"]["parts"][0]["text"]
93
 
94
  # Enforce character limit
95
  if len(script) > max_chars:
@@ -98,12 +84,9 @@ If a character is provided, write the story from their perspective or about them
98
  logger.info(f"Generated script: {len(script)} chars")
99
  return script.strip()
100
 
101
- except requests.exceptions.RequestException as e:
102
  logger.error(f"Gemini API error: {e}")
103
  raise Exception(f"Script generation failed: {e}")
104
- except (KeyError, IndexError) as e:
105
- logger.error(f"Failed to parse Gemini response: {e}")
106
- raise Exception("Invalid response from Gemini API")
107
 
108
  @staticmethod
109
  def test_connection(api_key: str) -> bool:
@@ -160,8 +143,6 @@ Return ONLY valid JSON array, no markdown, no explanation:
160
  Returns:
161
  List of {chunk_id, prompt} dicts
162
  """
163
- import json
164
-
165
  all_prompts = []
166
  total_chunks = len(chunks)
167
 
@@ -198,28 +179,12 @@ IMPORTANT: Include this character description in EVERY prompt!
198
  user_prompt += "\nGenerate detailed image prompts for each chunk. Return ONLY JSON array."
199
 
200
  try:
201
- response = requests.post(
202
- f"{self.GEMINI_API_URL}?key={self.api_key}",
203
- headers={"Content-Type": "application/json"},
204
- json={
205
- "contents": [
206
- {
207
- "role": "user",
208
- "parts": [{"text": self.IMAGE_PROMPT_SYSTEM + "\n\n" + user_prompt}]
209
- }
210
- ],
211
- "generationConfig": {
212
- "temperature": 0.7,
213
- "maxOutputTokens": 4000,
214
- "topP": 0.9
215
- }
216
- },
217
- timeout=60
218
  )
219
- response.raise_for_status()
220
 
221
- data = response.json()
222
- text = data["candidates"][0]["content"]["parts"][0]["text"]
223
 
224
  # Clean response - remove markdown if present
225
  text = text.strip()
 
1
  """
2
+ Script Generator using Google GenAI SDK
3
  Generates story scripts from topics for TTS narration
4
  """
5
  import logging
6
+ import json
7
  from typing import Optional
8
 
9
+ from google import genai
10
+
11
  logger = logging.getLogger(__name__)
12
 
13
 
14
  class ScriptGenerator:
15
  """
16
+ Generates story scripts using Google Gemini API via google-genai SDK.
17
 
18
  Features:
19
  - Topic → Full narration script (<=1000 chars)
20
  - Character-aware script generation
21
+ - AI image prompt generation
22
  - Optimized for TTS output
23
  """
24
 
25
+ MODEL = "gemini-2.0-flash"
26
 
27
  # System prompt for script generation
28
  SYSTEM_PROMPT = """You are a professional script writer for short-form video content (TikTok, Reels, Shorts).
 
40
 
41
  def __init__(self, api_key: str):
42
  self.api_key = api_key
43
+ self.client = genai.Client(api_key=api_key)
44
 
45
  def generate_script(
46
  self,
 
70
  logger.info(f"Generating script for topic: {topic[:50]}...")
71
 
72
  try:
73
+ response = self.client.models.generate_content(
74
+ model=self.MODEL,
75
+ contents=self.SYSTEM_PROMPT + "\n\n" + user_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  )
 
 
 
77
 
78
+ script = response.text
 
79
 
80
  # Enforce character limit
81
  if len(script) > max_chars:
 
84
  logger.info(f"Generated script: {len(script)} chars")
85
  return script.strip()
86
 
87
+ except Exception as e:
88
  logger.error(f"Gemini API error: {e}")
89
  raise Exception(f"Script generation failed: {e}")
 
 
 
90
 
91
  @staticmethod
92
  def test_connection(api_key: str) -> bool:
 
143
  Returns:
144
  List of {chunk_id, prompt} dicts
145
  """
 
 
146
  all_prompts = []
147
  total_chunks = len(chunks)
148
 
 
179
  user_prompt += "\nGenerate detailed image prompts for each chunk. Return ONLY JSON array."
180
 
181
  try:
182
+ response = self.client.models.generate_content(
183
+ model=self.MODEL,
184
+ contents=self.IMAGE_PROMPT_SYSTEM + "\n\n" + user_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  )
 
186
 
187
+ text = response.text
 
188
 
189
  # Clean response - remove markdown if present
190
  text = text.strip()
requirements.txt CHANGED
@@ -18,6 +18,7 @@ numpy<2.0.0
18
 
19
  # AI/ML
20
  faster-whisper
 
21
 
22
  # Utilities
23
  python-multipart
 
18
 
19
  # AI/ML
20
  faster-whisper
21
+ google-genai
22
 
23
  # Utilities
24
  python-multipart