NikhilSetiya
commited on
Commit
·
01b94bb
1
Parent(s):
13ba412
Fix: Update image generation and clean config files
Browse files
agent.py
CHANGED
@@ -46,7 +46,9 @@ def call_groq(system_prompt, user_input):
|
|
46 |
|
47 |
return data['choices'][0]['message']['content']
|
48 |
|
49 |
-
|
|
|
|
|
50 |
headers = {
|
51 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
52 |
"Content-Type": "application/json"
|
@@ -59,26 +61,33 @@ def generate_ad_image(prompt, model="gpt-image-1"):
|
|
59 |
]
|
60 |
}
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
def ad_copy_agent(product, description, audience, tone):
|
84 |
system_prompt = load_prompt("ad_copy_prompt.txt")
|
|
|
46 |
|
47 |
return data['choices'][0]['message']['content']
|
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"
|
|
|
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")
|