Manireddy1508 commited on
Commit
69a2277
·
verified ·
1 Parent(s): 4f9120d

Update utils/planner.py

Browse files

variations to a single prompt

Files changed (1) hide show
  1. utils/planner.py +84 -0
utils/planner.py CHANGED
@@ -46,3 +46,87 @@ def extract_scene_plan(prompt: str) -> dict:
46
  }
47
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
 
49
+ # utils/planner.py
50
+
51
+ import openai
52
+ import os
53
+ from openai import OpenAI
54
+ from dotenv import load_dotenv
55
+ import json
56
+ load_dotenv()
57
+
58
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
59
+
60
+ # 🧠 Scene Plan Extractor
61
+ SYSTEM_INSTRUCTIONS = """
62
+ You are a scene planning assistant for an AI image generation system.
63
+ Your job is to take the user's prompt and return a structured JSON with:
64
+ - scene (environment, setting)
65
+ - subject (main actor)
66
+ - objects (main product or items)
67
+ - layout (foreground/background elements and their placement)
68
+ - rules (validation rules to ensure visual correctness)
69
+ Respond ONLY in raw JSON format. Do NOT include explanations.
70
+ """
71
+
72
+ def extract_scene_plan(prompt: str) -> dict:
73
+ try:
74
+ response = client.chat.completions.create(
75
+ model="gpt-4o-mini-2024-07-18",
76
+ messages=[
77
+ {"role": "system", "content": SYSTEM_INSTRUCTIONS},
78
+ {"role": "user", "content": prompt}
79
+ ],
80
+ temperature=0.3,
81
+ max_tokens=500
82
+ )
83
+ json_output = response.choices[0].message.content
84
+ print("🧠 Scene Plan (Raw):", json_output)
85
+ return json.loads(json_output)
86
+
87
+ except Exception as e:
88
+ print("❌ extract_scene_plan() Error:", e)
89
+ return {
90
+ "scene": None,
91
+ "subject": None,
92
+ "objects": [],
93
+ "layout": {},
94
+ "rules": {}
95
+ }
96
+
97
+ # 🧠 GPT-Based Prompt Variation Generator
98
+ def generate_prompt_variations_from_scene(scene_plan: dict, base_prompt: str, n: int = 3) -> list:
99
+ try:
100
+ system_msg = f"""
101
+ You are a creative prompt variation generator for an AI image generation system.
102
+
103
+ Given a base user prompt and its structured scene plan, generate {n} diverse image generation prompts.
104
+ Each prompt should:
105
+ - Be visually rich and descriptive
106
+ - Include stylistic or contextual variation
107
+ - Reference the same product and environment
108
+ - Stay faithful to the base prompt and extracted plan
109
+
110
+ Respond ONLY with a JSON array of strings. No explanations.
111
+ """
112
+
113
+ response = client.chat.completions.create(
114
+ model="gpt-4o-mini-2024-07-18",
115
+ messages=[
116
+ {"role": "system", "content": system_msg},
117
+ {"role": "user", "content": json.dumps({
118
+ "base_prompt": base_prompt,
119
+ "scene_plan": scene_plan
120
+ })}
121
+ ],
122
+ temperature=0.7,
123
+ max_tokens=600
124
+ )
125
+
126
+ content = response.choices[0].message.content
127
+ print("🧠 Prompt Variations (Raw):", content)
128
+ return json.loads(content)
129
+
130
+ except Exception as e:
131
+ print("❌ generate_prompt_variations_from_scene() Error:", e)
132
+ return [base_prompt] # fallback to original if anything fails