NikhilSetiya commited on
Commit
d7d97f6
·
1 Parent(s): 01b94bb

Fix: Update image generation and clean config files

Browse files
Files changed (1) hide show
  1. agent.py +19 -32
agent.py CHANGED
@@ -48,46 +48,33 @@ def call_groq(system_prompt, user_input):
48
 
49
  import time
50
 
51
- def generate_ad_image(prompt, model="gpt-image-1", retries=3, backoff=2):
52
  headers = {
53
  "Authorization": f"Bearer {OPENAI_API_KEY}",
54
  "Content-Type": "application/json"
55
  }
56
  payload = {
57
  "model": model,
58
- "messages": [
59
- {"role": "system", "content": "You are an expert image generator. Provide only a valid image URL, no text."},
60
- {"role": "user", "content": prompt}
61
- ]
62
  }
63
 
64
- for attempt in range(retries):
65
- response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
66
- if response.status_code == 200:
67
- data = response.json()
68
- if 'choices' not in data or not data['choices']:
69
- raise Exception(f"{model} returned no choices.")
70
-
71
- image_urls = []
72
- for choice in data['choices']:
73
- content = choice['message'].get('content', '')
74
- urls = [word for word in content.split() if word.startswith('http')]
75
- image_urls.extend(urls)
76
-
77
- if not image_urls:
78
- raise Exception(f"No valid image URLs found in {model} response.")
79
-
80
- return image_urls
81
-
82
- elif response.status_code >= 500:
83
- # server error → retry after backoff
84
- print(f"Attempt {attempt + 1} failed with 500 error. Retrying in {backoff} seconds...")
85
- time.sleep(backoff)
86
- else:
87
- # client error → don’t retry
88
- raise Exception(f"{model} API error: {response.status_code} - {response.text}")
89
-
90
- raise Exception(f"{model} failed after {retries} retries due to server errors.")
91
 
92
  def ad_copy_agent(product, description, audience, tone):
93
  system_prompt = load_prompt("ad_copy_prompt.txt")
 
48
 
49
  import time
50
 
51
+ def generate_ad_image(prompt, n_images=1, size="1024x1024", model="dall-e-3"):
52
  headers = {
53
  "Authorization": f"Bearer {OPENAI_API_KEY}",
54
  "Content-Type": "application/json"
55
  }
56
  payload = {
57
  "model": model,
58
+ "prompt": prompt,
59
+ "n": n_images,
60
+ "size": size
 
61
  }
62
 
63
+ response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
64
+ if response.status_code != 200:
65
+ raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
66
+
67
+ data = response.json()
68
+ print("OpenAI API raw response:", data) # 👈 ADDED for debugging
69
+
70
+ if 'data' not in data or not data['data']:
71
+ raise Exception("OpenAI API returned no image data. Check prompt, model, or account limits.")
72
+
73
+ image_urls = [item.get('url') for item in data['data'] if 'url' in item]
74
+ if not image_urls:
75
+ raise Exception("No valid image URLs found in OpenAI API response. Possible safety filter block or model issue.")
76
+
77
+ return image_urls
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  def ad_copy_agent(product, description, audience, tone):
80
  system_prompt = load_prompt("ad_copy_prompt.txt")