Spaces:
Build error
Build error
Create workflows/image_generator.py
Browse files- workflows/image_generator.py +97 -0
workflows/image_generator.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from prefect import task
|
| 2 |
+
import requests
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from config.settings import settings
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import base64
|
| 8 |
+
|
| 9 |
+
@task(retries=2)
|
| 10 |
+
def generate_image_with_dalle(prompt: str) -> Optional[str]:
|
| 11 |
+
"""Generate image using DALL-E via OpenRouter (if available)"""
|
| 12 |
+
# Note: OpenRouter may not support image generation
|
| 13 |
+
# This is a placeholder - you might need alternative services
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
@task(retries=2)
|
| 17 |
+
def generate_image_with_canva(prompt: str, template_id: str = None) -> Optional[str]:
|
| 18 |
+
"""Generate image using Canva API"""
|
| 19 |
+
if not settings.canva_api_key:
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
# Canva API integration
|
| 23 |
+
# This is a simplified example - actual Canva API may differ
|
| 24 |
+
headers = {
|
| 25 |
+
"Authorization": f"Bearer {settings.canva_api_key}",
|
| 26 |
+
"Content-Type": "application/json"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
payload = {
|
| 30 |
+
"design": {
|
| 31 |
+
"template_id": template_id,
|
| 32 |
+
"elements": [
|
| 33 |
+
{
|
| 34 |
+
"type": "text",
|
| 35 |
+
"content": prompt[:500] # Truncate if needed
|
| 36 |
+
}
|
| 37 |
+
]
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
response = requests.post(
|
| 43 |
+
"https://api.canva.com/v1/designs",
|
| 44 |
+
headers=headers,
|
| 45 |
+
json=payload
|
| 46 |
+
)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
|
| 49 |
+
design_data = response.json()
|
| 50 |
+
return design_data.get("url")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Canva API error: {e}")
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
@task(retries=2)
|
| 56 |
+
def generate_placeholder_image(text: str, size: tuple = (1080, 1080)) -> str:
|
| 57 |
+
"""Generate a simple placeholder image"""
|
| 58 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 59 |
+
|
| 60 |
+
# Create image
|
| 61 |
+
img = Image.new('RGB', size, color='#4A90E2')
|
| 62 |
+
draw = ImageDraw.Draw(img)
|
| 63 |
+
|
| 64 |
+
# Add text
|
| 65 |
+
text_wrapped = text[:100] # Limit text
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
| 69 |
+
except:
|
| 70 |
+
font = ImageFont.load_default()
|
| 71 |
+
|
| 72 |
+
# Center text
|
| 73 |
+
bbox = draw.textbbox((0, 0), text_wrapped, font=font)
|
| 74 |
+
text_width = bbox[2] - bbox[0]
|
| 75 |
+
text_height = bbox[3] - bbox[1]
|
| 76 |
+
|
| 77 |
+
position = ((size[0] - text_width) // 2, (size[1] - text_height) // 2)
|
| 78 |
+
draw.text(position, text_wrapped, fill='white', font=font)
|
| 79 |
+
|
| 80 |
+
# Save to bytes
|
| 81 |
+
buffered = BytesIO()
|
| 82 |
+
img.save(buffered, format="PNG")
|
| 83 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 84 |
+
|
| 85 |
+
return f"data:image/png;base64,{img_str}"
|
| 86 |
+
|
| 87 |
+
@task
|
| 88 |
+
def select_image_generator(prompt: str) -> str:
|
| 89 |
+
"""Select and use available image generation service"""
|
| 90 |
+
# Try Canva first
|
| 91 |
+
image_url = generate_image_with_canva(prompt)
|
| 92 |
+
|
| 93 |
+
if not image_url:
|
| 94 |
+
# Fallback to placeholder
|
| 95 |
+
image_url = generate_placeholder_image(prompt)
|
| 96 |
+
|
| 97 |
+
return image_url
|