File size: 19,955 Bytes
82f04d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# # # story_script.py
# # import asyncio
# # import json
# # import logging
# # import random

# # logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# # async def generate_story(script: str) -> dict:
# #     """
# #     Convert the ad script into a structured storyboard JSON format:
# #     - Characters
# #     - Scenes with keyframes, camera instructions, and music
# #     """
# #     # Sample character extraction (for simplicity, can be improved)
# #     characters = [{"name": name, "seed": idx+1} for idx, name in enumerate(["MainGuy", "Friend1", "Friend2"])]

# #     # Split script into lines and create scenes (basic heuristic, can be improved)
# #     lines = [line.strip() for line in script.split("\n") if line.strip()]
# #     scenes = {}
# #     for idx, line in enumerate(lines, start=1):
# #         char = characters[idx % len(characters)]["name"]
# #         seed = characters[idx % len(characters)]["seed"]
# #         scenes[f"scene{idx}"] = {
# #             "character": char,
# #             "scene": line,
# #             "keyframes": [
# #                 {
# #                     "seed": seed,
# #                     "keyframe1": f"{char} in action based on script line: '{line[:40]}...'",
# #                     "keyframe2": f"{char} expressive close-up reacting to: '{line[:40]}...'"
# #                 }
# #             ],
# #             "camera": "Medium shot with dynamic zoom-ins",
# #             "music": "Appropriate upbeat or dramatic tune based on action"
# #         }

# #     storyboard = {
# #         "characters": characters,
# #         **scenes
# #     }

# #     logging.info("Story script generated successfully")
# #     return storyboard


# # # best
# # # story_script.py
# # import os
# # import asyncio
# # import httpx
# # import logging
# # from dotenv import load_dotenv
# # from dotenv import load_dotenv

# # dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
# # load_dotenv(dotenv_path)

# # load_dotenv()
# # logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# # OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
# # MODEL_NAME = "deepseek/deepseek-r1-distill-llama-70b:free"
# # OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"

# # async def generate_story(script: str) -> dict:
# #     """
# #     Convert the ad script into a structured storyboard JSON using AI.
# #     The JSON format includes:
# #     - characters (with seeds)
# #     - scenes (character, scene description, keyframes, camera, music)
# #     """
# #     prompt = f"""
# # You are a professional ad storyboard generator.
# # Take this ad script and convert it into a **storyboard JSON**.
# # Follow this format exactly:

# # ADD {{
# #   "characters": [
# #     {{"name": "MainGuy", "seed": 1}},
# #     {{"name": "Friend1", "seed": 2}},
# #     {{"name": "Friend2", "seed": 3}}
# #   ],
# #   "scene1": {{
# #     "character": "MainGuy",
# #     "scene": "Description of the scene",
# #     "keyframes": [
# #       {{
# #         "seed": 1,
# #         "keyframe1": "First keyframe description",
# #         "keyframe2": "Second keyframe description"
# #       }}
# #     ],
# #     "camera": "Camera instructions",
# #     "music": "Music instructions"
# #   }}
# #   ...
# # }}

# # Ensure:
# # - Use the **script lines as scenes**.
# # - Assign characters logically to actions.
# # - Provide **keyframes, camera, and music**.
# # - Return **valid JSON only**, no extra text.
# # Script: 
# # \"\"\"{script}\"\"\"
# # """

# #     headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
# #     payload = {
# #         "model": MODEL_NAME,
# #         "messages": [{"role": "user", "content": prompt}],
# #         "temperature": 0.8,
# #         "max_tokens": 1200
# #     }

# #     async with httpx.AsyncClient(timeout=120) as client:
# #         response = await client.post(OPENROUTER_URL, json=payload, headers=headers)
# #         response.raise_for_status()
# #         data = response.json()

# #     # AI returns the JSON as a string
# #     story_json_str = data["choices"][0]["message"]["content"]

# #     # Remove possible extra text before/after JSON (some AI outputs might wrap with "ADD {...}")
# #     if story_json_str.startswith("ADD"):
# #         story_json_str = story_json_str[story_json_str.find("{"):]

# #     # Convert string to dict
# #     try:
# #         story_dict = eval(story_json_str)  # safe because AI returns JSON-like dict
# #     except Exception as e:
# #         logging.error(f"Failed to parse story JSON: {e}")
# #         story_dict = {}

# #     logging.info("Story script generated successfully using AI")
# #     return story_dict




# # import os
# # import asyncio
# # import httpx
# # import logging
# # import json
# # from dotenv import load_dotenv

# # dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
# # load_dotenv(dotenv_path)

# # logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# # OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
# # MODEL_NAME = "deepseek/deepseek-r1-distill-llama-70b:free"
# # OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"

# # async def generate_story(script: str) -> dict:
# #     """
# #     Convert the ad script into a structured storyboard JSON using AI.
# #     The JSON format includes:
# #     - characters (with seeds)
# #     - scenes (character, scene description, keyframes, camera, music)
# #     """
# #     prompt = f"""
# # You are a professional ad storyboard generator.
# # Take this ad script and convert it into a **storyboard JSON**.
# # Follow this format exactly:

# # ADD {{
# #   "characters": [
# #     {{"name": "MainGuy", "seed": 1}},
# #     {{"name": "Friend1", "seed": 2}},
# #     {{"name": "Friend2", "seed": 3}}
# #   ],
# #   "scene1": {{
# #     "character": "MainGuy",
# #     "scene": "Description of the scene",
# #     "keyframes": [
# #       {{
# #         "seed": 1,
# #         "keyframe1": "First keyframe description",
# #         "keyframe2": "Second keyframe description"
# #       }}
# #     ],
# #     "camera": "Camera instructions",
# #     "music": "Music instructions"
# #   }}
# #   ...
# # }}

# # Ensure:
# # - Use the **script lines as scenes**.
# # - Assign characters logically to actions.
# # - Provide **keyframes, camera, and music**.
# # - Return **valid JSON only**, no extra text, no markdown, no ``` fences.
# # Script: 
# # \"\"\"{script}\"\"\"
# # """

# #     headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
# #     payload = {
# #         "model": MODEL_NAME,
# #         "messages": [{"role": "user", "content": prompt}],
# #         "temperature": 0.8,
# #         "max_tokens": 1500
# #     }

# #     async with httpx.AsyncClient(timeout=120) as client:
# #         response = await client.post(OPENROUTER_URL, json=payload, headers=headers)
# #         response.raise_for_status()
# #         data = response.json()

# #     story_json_str = data["choices"][0]["message"]["content"]

# #     # Clean unwanted wrappers
# #     story_json_str = story_json_str.strip()
# #     if story_json_str.startswith("ADD"):
# #         story_json_str = story_json_str[story_json_str.find("{"):]

# #     # Remove markdown fences
# #     story_json_str = story_json_str.replace("```json", "").replace("```", "").strip()

# #     # Parse safely
# #     try:
# #         story_dict = json.loads(story_json_str)
# #     except json.JSONDecodeError as e:
# #         logging.error(f"❌ Failed to parse story JSON properly: {e}")
# #         story_dict = {"raw_output": story_json_str}

# #     logging.info("Story script generated successfully using AI")
# #     return story_dict


# import os
# import asyncio
# import httpx
# import logging
# import json
# import re
# from dotenv import load_dotenv

# dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
# load_dotenv(dotenv_path)

# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
# MODEL_NAME = "deepseek/deepseek-r1-distill-llama-70b:free"
# OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"


# async def generate_story(script: str) -> dict:
#     """
#     Convert the ad script into a structured storyboard JSON using AI.
#     Returns:
#     {
#         "raw_output": "<original AI string>",
#         "parsed_output": <dict | None>
#     }
#     """
#     prompt = f"""
# You are a professional ad storyboard generator.
# Take this ad script and convert it into a **storyboard JSON**.
# Follow this format exactly:

# ADD {{
#   "characters": [
#     {{"name": "MainGuy-", "seed": 1}},
#     {{"name": "Friend1", "seed": 2}},
#     {{"name": "Friend2", "seed": 3}}
#   ],
#   "scene1": {{
#     "character": "MainGuy",
#     "scene": "Description of the scene",
#     "keyframes": [
#       {{
#         "seed": 1,
#         "keyframe1": "First keyframe description",
#         "keyframe2": "Second keyframe description"
#       }}
#     ],
#     "camera": "Camera instructions",
#     "music": "Music instructions"
#   }}
# }}

# Ensure:
# - Each scene corresponds to a line in the script.
# - Assign logical characters.
# - Return only valid JSON (no markdown or explanations).
# Script:
# \"\"\"{script}\"\"\"
# """

#     headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
#     payload = {
#         "model": MODEL_NAME,
#         "messages": [{"role": "user", "content": prompt}],
#         "temperature": 0.8,
#         "max_tokens": 1500
#     }

#     async with httpx.AsyncClient(timeout=120) as client:
#         response = await client.post(OPENROUTER_URL, json=payload, headers=headers)
#         response.raise_for_status()
#         data = response.json()

#     story_json_str = data["choices"][0]["message"]["content"].strip()
#     raw_output = story_json_str

#     # --- Cleaning Stage ---
#     story_json_str = story_json_str.strip()
#     if story_json_str.startswith("ADD"):
#         story_json_str = story_json_str[story_json_str.find("{"):]

#     # Remove markdown code fences and artifacts
#     story_json_str = story_json_str.replace("```json", "").replace("```", "").strip()

#     # Remove unwanted triple quotes, trailing commas, and unescaped slashes
#     story_json_str = re.sub(r',\s*}', '}', story_json_str)
#     story_json_str = re.sub(r',\s*\]', ']', story_json_str)
#     story_json_str = story_json_str.replace('\\"', '"').replace("\\'", "'")

#     parsed_story = None

#     # --- Parsing Stage ---
#     try:
#         parsed_story = json.loads(story_json_str)
#     except json.JSONDecodeError:
#         try:
#             # Handle double-encoded or escaped JSON
#             cleaned_str = bytes(story_json_str, "utf-8").decode("unicode_escape")
#             parsed_story = json.loads(cleaned_str)
#         except Exception as e:
#             logging.error(f"❌ JSON parse failed after cleaning: {e}")
#             parsed_story = None

#     logging.info("βœ… Storyboard generation completed")
#     return parsed_story


##best best
# import os
# import asyncio
# import httpx
# import logging
# import json
# import re
# from dotenv import load_dotenv

# dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
# load_dotenv(dotenv_path)

# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
# MODEL_NAME = "deepseek/deepseek-r1-distill-llama-70b:free"
# OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"


# async def generate_story(script: str) -> dict:
#     """
#     Convert the ad script into a structured storyboard JSON using AI.
#     Always returns:
#     {
#         "raw_output": "<original text from AI>",
#         "parsed_output": <dict or None>
#     }
#     """
#     prompt = f"""
# You are a professional ad storyboard generator.
# Convert this ad script into a **storyboard JSON** only β€” no extra text.

# Format example:
# {{
#   "characters": [
#     {{"name": "MainGuy","Description":"complete decsripiton of the character", "seed": 1}},
#     {{"name": "Dog","Description":"complete decsripiton of the character", "seed": 2}}
#   ],
#   "scene1": {{
#     "character": "MainGuy",
#     "scene": "Man wakes up late and rushes outside",
#     "keyframes": [
#       {{"seed": 1, "keyframe1": "Man stepping in puddle", "keyframe2": "Reaction close-up"}}
#     ],
#     "camera": "Medium shot with soft lighting",
#     "music": "Playful upbeat tune"
#   }}
# }}
# Script:
# \"\"\"{script}\"\"\"
# Return **only valid JSON**, no markdown or commentary.
# """

#     headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
#     payload = {
#         "model": MODEL_NAME,
#         "messages": [{"role": "user", "content": prompt}],
#         "temperature": 0.7,
#         "max_tokens": 1500
#     }

#     async with httpx.AsyncClient(timeout=120) as client:
#         response = await client.post(OPENROUTER_URL, json=payload, headers=headers)
#         response.raise_for_status()
#         data = response.json()

#     story_json_str = data["choices"][0]["message"]["content"].strip()
#     raw_output = story_json_str

#     # --- Clean the output ---
#     story_json_str = re.sub(r"^ADD\s*", "", story_json_str)
#     story_json_str = story_json_str.replace("```json", "").replace("```", "")
#     story_json_str = story_json_str.replace("β€œ", "\"").replace("”", "\"").replace("’", "'")
#     story_json_str = re.sub(r",\s*([}\]])", r"\1", story_json_str)  # remove trailing commas
#     story_json_str = story_json_str.strip()

#     # --- Parse the JSON safely ---
#     parsed_story = None
#     try:
#         parsed_story = json.loads(story_json_str)
#     except json.JSONDecodeError as e:
#         logging.warning(f"JSON parse failed: {e}")
#         try:
#             # try to find JSON substring in case AI wrapped it with text
#             match = re.search(r"\{.*\}", story_json_str, re.DOTALL)
#             if match:
#                 parsed_story = json.loads(match.group(0))
#         except Exception as e2:
#             logging.error(f"Final parsing failed: {e2}")
#             parsed_story = None

#     logging.info("βœ… Storyboard generation completed")
#     return parsed_story





import os
import asyncio
import httpx
import logging
import json
import re
from dotenv import load_dotenv

# --- Load environment variables ---
dotenv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')
load_dotenv(dotenv_path)

# --- Configure logging ---
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

# --- API Constants ---
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
MODEL_NAME = "deepseek/deepseek-r1-distill-llama-70b:free"
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"


async def generate_story(script: str) -> dict:
    """
    Converts ad script into a structured storyboard JSON.
    Each scene = only one action.
    Reuses character seeds for consistency.
    Always returns a non-null dictionary.
    """

    prompt = f"""
You are a professional storyboard generator for AI video production.

Convert the given ad script into a JSON storyboard format. 
Each scene must represent only ONE clear action or emotional beat. 
Do not include any explanations, markdown, or text outside JSON.

### STRICT JSON FORMAT:
{{
  "characters": [
    {{"name": "Man","description":"average build, brown hair, casual outfit","seed":1}},
    {{"name": "Librarian","description":"stern woman, cat-eye glasses, neat bun","seed":2}}
  ],
  "scene1": {{
    "character": "Man",
    "scene": "Man enters the library holding a bag of potato chips",
    "keyframes": [
      {{
        "seed": 1,
        "keyframe1": "Man walking into a quiet library, holding a bag of potato chips, warm sunlight from windows, calm mood",
        "keyframe2": "Side shot of man sitting down at a wooden table, casual expression, sunlight glows behind him"
      }}
    ],
    "camera": "Medium wide shot with soft natural lighting",
    "music": "Gentle ambient tune"
  }}
}}

Rules:
- Each scene = ONE clear action only.
- Use each character’s 'seed' consistently across scenes.
- Each keyframe describes two cinematic angles of that action.
- Keep descriptions detailed but realistic.
- Return valid JSON only β€” no extra text, comments, or markdown.

Script:
\"\"\"{script}\"\"\"
"""

    headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
    payload = {
        "model": MODEL_NAME,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": 1800
    }

    async with httpx.AsyncClient(timeout=180) as client:
        try:
            response = await client.post(OPENROUTER_URL, json=payload, headers=headers)
            response.raise_for_status()
            data = response.json()
        except Exception as e:
            logging.error(f"API request failed: {e}")
            return {"error": "API request failed", "details": str(e)}

    story_json_str = data["choices"][0]["message"]["content"].strip()

    # --- Clean the model output ---
    story_json_str = re.sub(r"```(?:json)?", "", story_json_str)
    story_json_str = story_json_str.replace("β€œ", "\"").replace("”", "\"").replace("’", "'")
    story_json_str = re.sub(r",\s*([}\]])", r"\1", story_json_str).strip()

    parsed_story = None
    try:
        parsed_story = json.loads(story_json_str)
    except json.JSONDecodeError as e:
        logging.warning(f"Initial JSON parse failed: {e}")
        match = re.search(r"\{.*\}", story_json_str, re.DOTALL)
        if match:
            try:
                parsed_story = json.loads(match.group(0))
            except Exception as e2:
                logging.error(f"Fallback parse failed: {e2}")

    # --- Final fallback: minimal structure ---
    if not parsed_story:
        logging.warning("Model output invalid, generating fallback JSON.")
        parsed_story = {
            "characters": [],
            "scene1": {
                "character": "Unknown",
                "scene": "Failed to parse script properly",
                "keyframes": [
                    {
                        "seed": 0,
                        "keyframe1": "Generic placeholder image",
                        "keyframe2": "Generic placeholder image"
                    }
                ],
                "camera": "Static fallback frame",
                "music": "None"
            }
        }

    # --- Ensure consistent seeds & single-action scenes ---
    characters = {c.get("name"): c for c in parsed_story.get("characters", [])}
    for key, scene in parsed_story.items():
        if not key.startswith("scene"):
            continue

        char_name = scene.get("character")
        seed = characters.get(char_name, {}).get("seed", 0)
        desc = characters.get(char_name, {}).get("description", "")

        # Limit to one clear action
        scene_text = scene.get("scene", "")
        scene["scene"] = re.split(r"[,.] and |, then |;| but | while ", scene_text)[0].strip()

        # Keyframe corrections
        for kf in scene.get("keyframes", []):
            kf["seed"] = seed
            for i, k in enumerate(["keyframe1", "keyframe2"]):
                if not kf.get(k):
                    angle = "wide shot" if i == 0 else "close-up"
                    kf[k] = (
                        f"{char_name} ({desc}) performing '{scene['scene']}', "
                        f"{angle}, cinematic tone, photorealistic lighting"
                    )

    logging.info("βœ… Storyboard generated successfully with consistent single-action scenes.")
    print(parsed_story)
    return parsed_story