NikhilSetiya
commited on
Commit
·
d7d97f6
1
Parent(s):
01b94bb
Fix: Update image generation and clean config files
Browse files
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,
|
52 |
headers = {
|
53 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
54 |
"Content-Type": "application/json"
|
55 |
}
|
56 |
payload = {
|
57 |
"model": model,
|
58 |
-
"
|
59 |
-
|
60 |
-
|
61 |
-
]
|
62 |
}
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
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")
|