File size: 2,860 Bytes
a5fc5ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import re
from typing import List, Dict

from dotenv import load_dotenv
from typing import Dict
from openai import OpenAI


# Env variable
load_dotenv()

# Initialize OpenAI client
client = OpenAI()


def narrative_to_storyboard(narrative: str) -> Dict[str, str]:
    """
    Converts a narrative prompt into a structured storyboard dict
    with scene, shot type, and emotion using an LLM.
    """
    system_prompt = (
        "You are a professional storyboard artist and cinematographer. "
        "Given a narrative, extract and describe:\n"
        "- scene: The environment and visual setting\n"
        "- shot_type: The camera angle or framing (e.g., wide shot, close-up)\n"
        "- emotion: The overall mood or emotional tone\n\n"
        "Return the result as a JSON dictionary with keys: scene, shot_type, emotion."
    )

    user_prompt = f"Narrative: {narrative}"

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}
        ],
        temperature=0.3
    )

    content = response.choices[0].message.content.strip()

    # Use regex to extract JSON block
    json_match = re.search(r"\{[\s\S]*\}", content)
    if json_match:
        json_str = json_match.group(0)
        try:
            parsed_json = json.loads(json_str)
            return parsed_json
        except json.JSONDecodeError as e:
            print(f"JSON decode error: {e}")
            return {"scene": "", "shot_type": "", "emotion": ""}
    else:
        print("No JSON block found in response.")
        return {"scene": "", "shot_type": "", "emotion": ""}


def generate_multiple_storyboards(narrative: str, num_versions: int = 5) -> List[Dict[str, str]]:
    """
    Generate multiple storyboards for the same narrative by calling the LLM-based
    narrative_to_storyboard() function multiple times with slight variations.
    """
    storyboards = []

    for i in range(num_versions):
        # Add variation to the narrative to encourage different outputs
        variant_narrative = f"{narrative}\nPlease provide a different creative version #{i+1}."
        storyboard = narrative_to_storyboard(variant_narrative)
        storyboard['version'] = i + 1  # Track version number
        storyboards.append(storyboard)

    return storyboards


if __name__ == "__main__":
    print("Testing Narrative to Storyboard...")
    narrative_text = "A girl walks into a dark forest on a misty night."
    # storyboard_output = narrative_to_storyboard(narrative_text)
    # print(storyboard_output)

    print("Generate 5 Storyboard based on the narrative...")
    storyboard_list = generate_multiple_storyboards(narrative_text)
    for i, sb in enumerate(storyboard_list):
        print(f"Version {i + 1}: {sb}")