NikhilSetiya commited on
Commit
13ba412
·
1 Parent(s): cf14191

Fix: Update image generation and clean config files

Browse files
Files changed (1) hide show
  1. agent.py +17 -10
agent.py CHANGED
@@ -9,7 +9,7 @@ if not GROQ_API_KEY:
9
  raise ValueError("GROQ_API_KEY is missing! Set it in Hugging Face Secrets.")
10
 
11
 
12
- OPENAI_API_URL = "https://api.openai.com/v1/images/generations"
13
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Set this in Hugging Face Secrets
14
 
15
  if not OPENAI_API_KEY:
@@ -46,16 +46,17 @@ def call_groq(system_prompt, user_input):
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
  }
60
 
61
  response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
@@ -63,11 +64,17 @@ def generate_ad_image(prompt, n_images=1, size="1024x1024", model="gpt-image-1")
63
  raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
64
 
65
  data = response.json()
66
- if 'data' not in data or not data['data']:
67
- raise Exception("OpenAI API returned no image data.")
 
 
 
 
 
 
 
 
68
 
69
- # Extract the URLs cleanly
70
- image_urls = [item.get('url') for item in data['data'] if 'url' in item]
71
  if not image_urls:
72
  raise Exception("No valid image URLs found in OpenAI API response.")
73
 
 
9
  raise ValueError("GROQ_API_KEY is missing! Set it in Hugging Face Secrets.")
10
 
11
 
12
+ OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"
13
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Set this in Hugging Face Secrets
14
 
15
  if not OPENAI_API_KEY:
 
46
 
47
  return data['choices'][0]['message']['content']
48
 
49
+ def generate_ad_image(prompt, model="gpt-image-1"):
50
  headers = {
51
  "Authorization": f"Bearer {OPENAI_API_KEY}",
52
  "Content-Type": "application/json"
53
  }
54
  payload = {
55
+ "model": model,
56
+ "messages": [
57
+ {"role": "system", "content": "You are an expert image generator. Provide only a valid image URL, no text."},
58
+ {"role": "user", "content": prompt}
59
+ ]
60
  }
61
 
62
  response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
 
64
  raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
65
 
66
  data = response.json()
67
+ if 'choices' not in data or not data['choices']:
68
+ raise Exception("OpenAI API returned no choices.")
69
+
70
+ # Extract image URL(s) from message content
71
+ image_urls = []
72
+ for choice in data['choices']:
73
+ content = choice['message'].get('content', '')
74
+ # Simple heuristic to extract URLs
75
+ urls = [word for word in content.split() if word.startswith('http')]
76
+ image_urls.extend(urls)
77
 
 
 
78
  if not image_urls:
79
  raise Exception("No valid image URLs found in OpenAI API response.")
80