File size: 3,063 Bytes
fc8bf44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import requests
import logging
from PIL import Image
import io
from pathlib import Path
class AIImageGenerator:
def __init__(self, openai_key=None, anthropic_key=None):
self.openai_key = openai_key
self.anthropic_key = anthropic_key
def generate_image_openai(self, prompt, model="dall-e-3", size="1024x1024"):
if not self.openai_key:
return None, "β OpenAI API key not provided"
try:
headers = {
"Authorization": f"Bearer {self.openai_key}",
"Content-Type": "application/json"
}
data = {
"model": model,
"prompt": prompt,
"size": size,
"n": 1
}
response = requests.post(
"https://api.openai.com/v1/images/generations",
headers=headers,
json=data,
timeout=60
)
if response.status_code == 200:
result = response.json()
image_url = result["data"][0]["url"]
img_response = requests.get(image_url)
img = Image.open(io.BytesIO(img_response.content))
output_path = f"generated_image_{hash(prompt) % 10000}.png"
img.save(output_path)
return output_path, f"β
Image generated successfully with {model}"
else:
return None, f"β OpenAI API Error: {response.text}"
except Exception as e:
logging.exception("OpenAI image generation failed")
return None, f"β Error generating image: {str(e)}"
def generate_image_anthropic(self, prompt):
if not self.anthropic_key:
return None, "β Anthropic API key not provided"
try:
headers = {
"x-api-key": self.anthropic_key,
"Content-Type": "application/json",
"anthropic-version": "2023-06-01"
}
data = {
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": f"Create a detailed visual description for an AI image generator based on this prompt: {prompt}. Make it artistic and detailed."
}]
}
response = requests.post(
"https://api.anthropic.com/v1/messages",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
result = response.json()
enhanced_prompt = result["content"][0]["text"]
return None, f"β
Enhanced prompt created: {enhanced_prompt[:200]}..."
else:
return None, f"β Anthropic API Error: {response.text}"
except Exception as e:
logging.exception("Anthropic image generation failed")
return None, f"β Error with Anthropic API: {str(e)}"
|