File size: 1,433 Bytes
9e3bd6c
 
d3a3bf1
9e3bd6c
3ba2121
d3a3bf1
 
 
9e3bd6c
3ba2121
9e3bd6c
 
 
 
 
 
 
 
 
 
 
 
 
 
3ba2121
d3a3bf1
9e3bd6c
 
 
 
 
 
 
d3a3bf1
 
 
9e3bd6c
 
d3a3bf1
9e3bd6c
 
 
 
 
 
 
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
# utils/planner.py

import openai
import os
from openai import OpenAI
from dotenv import load_dotenv
import json
load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

SYSTEM_INSTRUCTIONS = """
You are a scene planning assistant for an AI image generation system.
Your job is to take the user's prompt and return a structured JSON with:
- scene (environment, setting)
- subject (main actor)
- objects (main product or items)
- layout (foreground/background elements and their placement)
- rules (validation rules to ensure visual correctness)
Respond ONLY in raw JSON format. Do NOT include explanations.
"""

def extract_scene_plan(prompt: str) -> dict:
    try:
        response = client.chat.completions.create(
            model="gpt-4o-mini-2024-07-18",
            messages=[
                {"role": "system", "content": SYSTEM_INSTRUCTIONS},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3,
            max_tokens=500
        )
        json_output = response.choices[0].message.content
        print("🧠 Scene Plan (Raw):", json_output)
        return json.loads(json_output) # Be cautious: Use `json.loads()` if possible

    except Exception as e:
        print("❌ extract_scene_plan() Error:", e)
        return {
            "scene": None,
            "subject": None,
            "objects": [],
            "layout": {},
            "rules": {}
        }