Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions src/plugins/ai_image/ai_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from PIL import Image
from io import BytesIO
import base64
from utils.http_client import get_http_session
import logging

logger = logging.getLogger(__name__)

IMAGE_MODELS = ["dall-e-3", "dall-e-2", "gpt-image-1"]
DEFAULT_IMAGE_MODEL = "dall-e-3"
DEFAULT_IMAGE_QUALITY = "standard"
IMAGE_MODELS = ["gpt-image-1", "gpt-image-2"]
DEFAULT_IMAGE_MODEL = "gpt-image-1"
DEFAULT_IMAGE_QUALITY = "medium"

class AIImage(BasePlugin):
def generate_settings_template(self):
Expand All @@ -37,7 +36,7 @@ def generate_image(self, settings, device_config):
logger.error(f"Invalid image model: {image_model}")
raise RuntimeError("Invalid Image Model provided.")

image_quality = settings.get('quality', "medium" if image_model == "gpt-image-1" else "standard")
image_quality = settings.get('quality', DEFAULT_IMAGE_QUALITY)
randomize_prompt = settings.get('randomizePrompt') == 'true'
orientation = device_config.get_config("orientation")

Expand Down Expand Up @@ -73,7 +72,7 @@ def generate_image(self, settings, device_config):
logger.info("=== AI Image Plugin: Image generation complete ===")
return image

def fetch_image(self, ai_client, prompt, model="dall-e-3", quality="standard", orientation="horizontal"):
def fetch_image(self, ai_client, prompt, model=DEFAULT_IMAGE_MODEL, quality=DEFAULT_IMAGE_QUALITY, orientation="horizontal"):
"""
Fetch image from OpenAI API. Now an instance method to access image_loader.
"""
Expand All @@ -90,27 +89,14 @@ def fetch_image(self, ai_client, prompt, model="dall-e-3", quality="standard", o
args = {
"model": model,
"prompt": prompt,
"size": "1024x1024",
"size": "1536x1024" if orientation == "horizontal" else "1024x1536",
"quality": quality,
}
if model == "dall-e-3":
args["size"] = "1792x1024" if orientation == "horizontal" else "1024x1792"
args["quality"] = quality
elif model == "gpt-image-1":
args["size"] = "1536x1024" if orientation == "horizontal" else "1024x1536"
args["quality"] = quality

response = ai_client.images.generate(**args)
if model in ["dall-e-3", "dall-e-2"]:
image_url = response.data[0].url
# Use adaptive loader for memory-efficient processing
# AI images are pre-sized, but still benefit from optimized loading
session = get_http_session()
response = session.get(image_url)
img = Image.open(BytesIO(response.content))
elif model == "gpt-image-1":
image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
img = Image.open(BytesIO(image_bytes))
image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
img = Image.open(BytesIO(image_bytes))
return img

@staticmethod
Expand Down
18 changes: 5 additions & 13 deletions src/plugins/ai_image/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<div class="form-group">
<label for="imageModel" class="form-label">Image Model:</label>
<select id="imageModel" name="imageModel" class="form-input" onchange="toggleQualityDropdown()">
<option value="dall-e-2">DALL·E 2</option>
<option value="dall-e-3" selected>DALL·E 3</option>
<option value="gpt-image-1">GPT Image 1</option>
<option value="gpt-image-1" selected>GPT Image 1</option>
<option value="gpt-image-2">GPT Image 2</option>
</select>
</div>

Expand Down Expand Up @@ -42,14 +41,7 @@
// Clear out old options
qualityDropdown.innerHTML = "";

if (selectedModel === "dall-e-3") {
qualityDropdown.add(new Option("HD", "hd"));
qualityDropdown.add(new Option("Standard", "standard"));
}
else if (selectedModel === "dall-e-2") {
qualityDropdown.add(new Option("Standard", "standard"));
}
else if (selectedModel === "gpt-image-1") {
if (selectedModel === "gpt-image-1" || selectedModel === "gpt-image-2") {
qualityDropdown.add(new Option("High", "high"));
qualityDropdown.add(new Option("Medium", "medium"));
qualityDropdown.add(new Option("Low", "low"));
Expand All @@ -63,8 +55,8 @@
// populate form values from plugin settings
document.addEventListener('DOMContentLoaded', () => {
// defaults
var imageModel = 'dall-e-2'
var quality = 'standard'
var imageModel = 'gpt-image-1'
var quality = 'medium'

if (loadPluginSettings) {
document.getElementById('textPrompt').value = pluginSettings.textPrompt || '';
Expand Down