Manireddy1508 commited on
Commit
3ba2121
·
1 Parent(s): fdd3761

Update utils/planner.py

Browse files
Files changed (1) hide show
  1. utils/planner.py +13 -8
utils/planner.py CHANGED
@@ -1,26 +1,26 @@
1
  # utils/planner.py
2
 
3
- import openai
4
  import os
 
 
 
5
 
6
- openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  SYSTEM_INSTRUCTIONS = """
9
  You are a scene planning assistant for an AI image generation system.
10
-
11
  Your job is to take the user's prompt and return a structured JSON with:
12
  - scene (environment, setting)
13
  - subject (main actor)
14
  - objects (main product or items)
15
  - layout (foreground/background elements and their placement)
16
  - rules (validation rules to ensure visual correctness)
17
-
18
  Respond ONLY in raw JSON format. Do NOT include explanations.
19
  """
20
 
21
  def extract_scene_plan(prompt: str) -> dict:
22
  try:
23
- response = openai.ChatCompletion.create(
24
  model="gpt-4-0125-preview",
25
  messages=[
26
  {"role": "system", "content": SYSTEM_INSTRUCTIONS},
@@ -29,11 +29,16 @@ def extract_scene_plan(prompt: str) -> dict:
29
  temperature=0.3,
30
  max_tokens=500
31
  )
32
- json_output = response["choices"][0]["message"]["content"]
33
- return eval(json_output) # Converts JSON string to Python dict
 
 
 
 
 
34
 
35
  except Exception as e:
36
- print("LLM Error:", e)
37
  return {
38
  "scene": None,
39
  "subject": None,
 
1
  # utils/planner.py
2
 
 
3
  import os
4
+ import json
5
+ import logging
6
+ from openai import OpenAI
7
 
8
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
 
10
  SYSTEM_INSTRUCTIONS = """
11
  You are a scene planning assistant for an AI image generation system.
 
12
  Your job is to take the user's prompt and return a structured JSON with:
13
  - scene (environment, setting)
14
  - subject (main actor)
15
  - objects (main product or items)
16
  - layout (foreground/background elements and their placement)
17
  - rules (validation rules to ensure visual correctness)
 
18
  Respond ONLY in raw JSON format. Do NOT include explanations.
19
  """
20
 
21
  def extract_scene_plan(prompt: str) -> dict:
22
  try:
23
+ response = client.chat.completions.create(
24
  model="gpt-4-0125-preview",
25
  messages=[
26
  {"role": "system", "content": SYSTEM_INSTRUCTIONS},
 
29
  temperature=0.3,
30
  max_tokens=500
31
  )
32
+
33
+ content = response.choices[0].message.content.strip()
34
+
35
+ # Remove common formatting issues
36
+ content = content.strip("`").replace("json", "")
37
+
38
+ return json.loads(content)
39
 
40
  except Exception as e:
41
+ logging.exception("LLM Error:")
42
  return {
43
  "scene": None,
44
  "subject": None,