NikhilSetiya commited on
Commit
ae8b149
·
1 Parent(s): 501992b

Fix: Update image generation and clean config files

Browse files
Files changed (1) hide show
  1. agent.py +14 -3
agent.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import requests
3
 
 
4
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
5
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Secure key from Hugging Face Secrets
6
 
@@ -45,23 +46,33 @@ def call_groq(system_prompt, user_input):
45
 
46
  return data['choices'][0]['message']['content']
47
 
48
- def generate_ad_image(prompt, n_images=1, size="1024x1024"):
49
  headers = {
50
  "Authorization": f"Bearer {OPENAI_API_KEY}",
51
  "Content-Type": "application/json"
52
  }
53
  payload = {
 
54
  "prompt": prompt,
55
  "n": n_images,
56
- "size": size
 
57
  }
 
58
  response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
59
  if response.status_code != 200:
60
  raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
 
61
  data = response.json()
62
  if 'data' not in data or not data['data']:
63
  raise Exception("OpenAI API returned no image data.")
64
- return [item['url'] for item in data['data']]
 
 
 
 
 
 
65
 
66
  def ad_copy_agent(product, description, audience, tone):
67
  system_prompt = load_prompt("ad_copy_prompt.txt")
 
1
  import os
2
  import requests
3
 
4
+
5
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
6
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Secure key from Hugging Face Secrets
7
 
 
46
 
47
  return data['choices'][0]['message']['content']
48
 
49
+ def generate_ad_image(prompt, n_images=1, size="1024x1024", model="gpt-image-1"):
50
  headers = {
51
  "Authorization": f"Bearer {OPENAI_API_KEY}",
52
  "Content-Type": "application/json"
53
  }
54
  payload = {
55
+ "model": model, # Add model explicitly (dall-e-3 recommended)
56
  "prompt": prompt,
57
  "n": n_images,
58
+ "size": size,
59
+ "response_format": "url" # Ensures we get back URLs
60
  }
61
+
62
  response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
63
  if response.status_code != 200:
64
  raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
65
+
66
  data = response.json()
67
  if 'data' not in data or not data['data']:
68
  raise Exception("OpenAI API returned no image data.")
69
+
70
+ # Extract the URLs cleanly
71
+ image_urls = [item.get('url') for item in data['data'] if 'url' in item]
72
+ if not image_urls:
73
+ raise Exception("No valid image URLs found in OpenAI API response.")
74
+
75
+ return image_urls
76
 
77
  def ad_copy_agent(product, description, audience, tone):
78
  system_prompt = load_prompt("ad_copy_prompt.txt")