Compare commits

..

12 Commits
0.3.3 ... main

Author SHA1 Message Date
918e37e077 Bump version: 0.3.7 → 0.3.8 2025-08-12 15:21:26 +01:00
f5427d18ed revert blocking image gen 2025-08-12 15:21:23 +01:00
34f8a05035 Bump version: 0.3.6 → 0.3.7 2025-08-12 15:17:34 +01:00
82f29a4fde block generation if image in queue 2025-08-12 15:17:02 +01:00
ad814855ab Bump version: 0.3.5 → 0.3.6 2025-08-12 15:03:57 +01:00
1b75417360 update the queue 2025-08-12 15:03:50 +01:00
9f3cbf736a Bump version: 0.3.4 → 0.3.5 2025-08-12 14:45:40 +01:00
3e46b3363b working queue logic 2025-08-12 14:15:23 +01:00
ff5dfbcbce show queue count 2025-08-12 13:02:38 +01:00
14e69f7608 initial qwen support 2025-08-12 12:08:12 +01:00
1468ac4bbe Bump version: 0.3.3 → 0.3.4 2025-08-09 09:38:52 +01:00
2e13ecfa2f feat(prompt): implement robust error handling and fallback mechanism
Add retry logic and fallback mechanism to prompt generation. When OpenWebUI
fails, the system now attempts a second try before falling back to OpenRouter.
Proper error handling and logging have been added throughout the prompt
generation flow to ensure more reliable operation.
2025-08-09 09:38:46 +01:00
9 changed files with 460 additions and 35 deletions

View File

@ -1,5 +1,5 @@
[tool.bumpversion] [tool.bumpversion]
current_version = "0.3.3" current_version = "0.3.8"
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)" parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
serialize = ["{major}.{minor}.{patch}"] serialize = ["{major}.{minor}.{patch}"]
replace = "{new_version}" replace = "{new_version}"

View File

@ -4,7 +4,7 @@ FROM python:3.11-slim
# Set the working directory in the container # Set the working directory in the container
WORKDIR /app WORKDIR /app
# Set version label # Set version label
ARG VERSION="0.3.3" ARG VERSION="0.3.8"
LABEL version=$VERSION LABEL version=$VERSION
# Copy project files into the container # Copy project files into the container

View File

@ -122,6 +122,7 @@ def generate_image(
def select_model(model: str) -> tuple[str, str]: def select_model(model: str) -> tuple[str, str]:
use_flux = json.loads(user_config["comfyui"].get("FLUX", "false").lower()) use_flux = json.loads(user_config["comfyui"].get("FLUX", "false").lower())
only_flux = json.loads(user_config["comfyui"].get("ONLY_FLUX", "false").lower()) only_flux = json.loads(user_config["comfyui"].get("ONLY_FLUX", "false").lower())
use_qwen = json.loads(user_config["comfyui"].get("Qwen", "false").lower())
if model == "Random Image Model": if model == "Random Image Model":
selected_workflow = "FLUX" if (use_flux and (only_flux or random.choice([True, False]))) else "SDXL" selected_workflow = "FLUX" if (use_flux and (only_flux or random.choice([True, False]))) else "SDXL"
@ -133,6 +134,8 @@ def select_model(model: str) -> tuple[str, str]:
if model == "Random Image Model": if model == "Random Image Model":
if selected_workflow == "FLUX": if selected_workflow == "FLUX":
valid_models = user_config["comfyui:flux"]["models"].split(",") valid_models = user_config["comfyui:flux"]["models"].split(",")
elif selected_workflow == "Qwen":
valid_models = user_config["comfyui:qwen"]["models"].split(",")
else: # SDXL else: # SDXL
available_model_list = user_config["comfyui"]["models"].split(",") available_model_list = user_config["comfyui"]["models"].split(",")
valid_models = list(set(get_available_models()) & set(available_model_list)) valid_models = list(set(get_available_models()) & set(available_model_list))
@ -173,7 +176,86 @@ def create_image(prompt: str | None = None, model: str = "Random Image Model") -
model_param="unet_name", model_param="unet_name",
model=model model=model
) )
elif selected_workflow == "Qwen":
generate_image(
file_name="image",
comfy_prompt=prompt,
workflow_path="./workflow_qwen.json",
prompt_node="Positive",
seed_node="KSampler",
seed_param="seed",
save_node="Save Image",
save_param="filename_prefix",
model_node="Load Checkpoint",
model_param="ckpt_name",
model=model
)
else: # SDXL else: # SDXL
generate_image("image", comfy_prompt=prompt, model=model) generate_image("image", comfy_prompt=prompt, model=model)
logging.info(f"{selected_workflow} generation started with prompt: {prompt}") logging.info(f"{selected_workflow} generation started with prompt: {prompt}")
def get_queue_count() -> int:
"""Fetches the current queue count from ComfyUI (pending + running jobs)."""
url = user_config["comfyui"]["comfyui_url"] + "/queue"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
pending = len(data.get("queue_pending", []))
running = len(data.get("queue_running", []))
return pending + running
except Exception as e:
logging.error(f"Error fetching queue count: {e}")
return 0
def get_queue_details() -> list:
"""Fetches detailed queue information including model names and prompts."""
url = user_config["comfyui"]["comfyui_url"] + "/queue"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
jobs = []
for job_list in [data.get("queue_running", []), data.get("queue_pending", [])]:
for job in job_list:
# Extract prompt data (format: [priority, time, prompt])
prompt_data = job[2]
model = "Unknown"
prompt = "No prompt"
# Find model loader node (works for SDXL/FLUX/Qwen workflows)
for node in prompt_data.values():
if node.get("class_type") in ["CheckpointLoaderSimple", "UnetLoaderGGUFAdvancedDisTorchMultiGPU"]:
model = node["inputs"].get("ckpt_name", "Unknown")
break
# Find prompt node using class_type pattern and title matching
for node in prompt_data.values():
class_type = node.get("class_type", "")
if "CLIPTextEncode" in class_type and "text" in node["inputs"]:
meta = node.get('_meta', {})
title = meta.get('title', '').lower()
if 'positive' in title or 'prompt' in title:
prompt = node["inputs"]["text"]
break
jobs.append({
"id": job[0],
"model": model.split(".")[0] if model != "Unknown" else model,
"prompt": prompt
})
return jobs
except Exception as e:
logging.error(f"Error fetching queue details: {e}")
return []
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
pending = len(data.get("queue_pending", []))
running = len(data.get("queue_running", []))
return pending + running
except Exception as e:
logging.error(f"Error fetching queue count: {e}")
return 0

View File

@ -113,11 +113,28 @@ def get_current_version():
return "unknown" return "unknown"
def load_models_from_config(): def load_models_from_config():
flux_models = load_config()["comfyui:flux"]["models"].split(",") config = load_config()
sdxl_models = load_config()["comfyui"]["models"].split(",")
# Only load FLUX models if FLUX feature is enabled
use_flux = config["comfyui"].get("flux", "False").lower() == "true"
if use_flux and "comfyui:flux" in config and "models" in config["comfyui:flux"]:
flux_models = config["comfyui:flux"]["models"].split(",")
else:
flux_models = []
sdxl_models = config["comfyui"]["models"].split(",")
# Only load Qwen models if Qwen feature is enabled
use_qwen = config["comfyui"].get("qwen", "False").lower() == "true"
if use_qwen and "comfyui:qwen" in config and "models" in config["comfyui:qwen"]:
qwen_models = config["comfyui:qwen"]["models"].split(",")
else:
qwen_models = []
sorted_flux_models = sorted(flux_models, key=str.lower) sorted_flux_models = sorted(flux_models, key=str.lower)
sorted_sdxl_models = sorted(sdxl_models, key=str.lower) sorted_sdxl_models = sorted(sdxl_models, key=str.lower)
return sorted_sdxl_models, sorted_flux_models sorted_qwen_models = sorted(qwen_models, key=str.lower)
return sorted_sdxl_models, sorted_flux_models, sorted_qwen_models
def load_topics_from_config(): def load_topics_from_config():
@ -158,7 +175,10 @@ def load_prompt_models_from_config():
def create_prompt_with_random_model(base_prompt: str, topic: str = "random"): def create_prompt_with_random_model(base_prompt: str, topic: str = "random"):
"""Create a prompt using a randomly selected model from OpenWebUI or OpenRouter.""" """Create a prompt using a randomly selected model from OpenWebUI or OpenRouter.
If OpenWebUI fails, it will retry once. If it fails again, it will fallback to OpenRouter.
"""
prompt_models = load_prompt_models_from_config() prompt_models = load_prompt_models_from_config()
if not prompt_models: if not prompt_models:
@ -168,16 +188,59 @@ def create_prompt_with_random_model(base_prompt: str, topic: str = "random"):
# Randomly select a model # Randomly select a model
service, model = random.choice(prompt_models) service, model = random.choice(prompt_models)
if service == "openwebui": # Import here to avoid circular imports
# Import here to avoid circular imports from libs.openwebui import create_prompt_on_openwebui
from libs.openwebui import create_prompt_on_openwebui from libs.openrouter import create_prompt_on_openrouter
return create_prompt_on_openwebui(base_prompt, topic)
elif service == "openrouter":
# Import here to avoid circular imports
from libs.openrouter import create_prompt_on_openrouter
return create_prompt_on_openrouter(base_prompt, topic)
return None if service == "openwebui":
try:
# First attempt with OpenWebUI
logging.info(f"Attempting to generate prompt with OpenWebUI using model: {model}")
result = create_prompt_on_openwebui(base_prompt, topic, model)
if result:
return result
# If first attempt returns None, try again
logging.warning("First OpenWebUI attempt failed. Retrying...")
result = create_prompt_on_openwebui(base_prompt, topic, model)
if result:
return result
# If second attempt fails, fallback to OpenRouter
logging.warning("Second OpenWebUI attempt failed. Falling back to OpenRouter...")
openrouter_models = [m for m in prompt_models if m[0] == "openrouter"]
if openrouter_models:
_, openrouter_model = random.choice(openrouter_models)
return create_prompt_on_openrouter(base_prompt, topic, openrouter_model)
else:
logging.error("No OpenRouter models configured for fallback.")
return "A colorful abstract composition" # Default fallback prompt
except Exception as e:
logging.error(f"Error with OpenWebUI: {e}")
# Fallback to OpenRouter on exception
logging.warning("OpenWebUI exception. Falling back to OpenRouter...")
openrouter_models = [m for m in prompt_models if m[0] == "openrouter"]
if openrouter_models:
_, openrouter_model = random.choice(openrouter_models)
try:
return create_prompt_on_openrouter(base_prompt, topic, openrouter_model)
except Exception as e2:
logging.error(f"Error with OpenRouter fallback: {e2}")
return "A colorful abstract composition" # Default fallback prompt
else:
logging.error("No OpenRouter models configured for fallback.")
return "A colorful abstract composition" # Default fallback prompt
elif service == "openrouter":
try:
# Use OpenRouter
return create_prompt_on_openrouter(base_prompt, topic, model)
except Exception as e:
logging.error(f"Error with OpenRouter: {e}")
return "A colorful abstract composition" # Default fallback prompt
return "A colorful abstract composition" # Default fallback prompt
user_config = load_config() user_config = load_config()
output_folder = user_config["comfyui"]["output_dir"] output_folder = user_config["comfyui"]["output_dir"]

View File

@ -73,17 +73,22 @@ def create_prompt_on_openwebui(prompt: str, topic: str = "random", model: str =
] ]
# Send the chat request # Send the chat request
result = client.chat( try:
question=user_content, result = client.chat(
chat_title=datetime.now().strftime("%Y-%m-%d %H:%M"), question=user_content,
folder_name="ai-frame-image-server" chat_title=datetime.now().strftime("%Y-%m-%d %H:%M"),
) folder_name="ai-frame-image-server"
)
if result: if result:
prompt = result["response"].strip('"') prompt = result["response"].strip('"')
else: else:
# Fallback if the request fails # Return None if the request fails
prompt = "A vibrant landscape" logging.warning(f"OpenWebUI request failed with model: {model}")
return None
except Exception as e:
logging.error(f"Error in OpenWebUI request with model {model}: {e}")
return None
match = re.search(r'"([^"]+)"', prompt) match = re.search(r'"([^"]+)"', prompt)
if not match: if not match:

View File

@ -1,6 +1,6 @@
from flask import Blueprint, request, render_template, redirect, url_for, session from flask import Blueprint, request, render_template, redirect, url_for, session
import threading import threading
from libs.comfyui import create_image, select_model, get_available_models from libs.comfyui import create_image, select_model, get_available_models, get_queue_count
from libs.openwebui import create_prompt_on_openwebui from libs.openwebui import create_prompt_on_openwebui
from libs.generic import load_models_from_config, load_topics_from_config, load_openrouter_models_from_config, load_openwebui_models_from_config, create_prompt_with_random_model from libs.generic import load_models_from_config, load_topics_from_config, load_openrouter_models_from_config, load_openwebui_models_from_config, create_prompt_with_random_model
import os import os
@ -35,17 +35,20 @@ def create():
threading.Thread(target=lambda: create_image(prompt, model)).start() threading.Thread(target=lambda: create_image(prompt, model)).start()
return redirect(url_for("create_routes.image_queued", prompt=prompt, model=model.split(".")[0])) return redirect(url_for("create_routes.image_queued", prompt=prompt, model=model.split(".")[0]))
# Load all models (SDXL and FLUX only) # Load all models (SDXL, FLUX, and Qwen)
sdxl_models, flux_models = load_models_from_config() sdxl_models, flux_models, qwen_models = load_models_from_config()
openwebui_models = load_openwebui_models_from_config() openwebui_models = load_openwebui_models_from_config()
openrouter_models = load_openrouter_models_from_config() openrouter_models = load_openrouter_models_from_config()
queue_count = get_queue_count()
return render_template("create_image.html", return render_template("create_image.html",
sdxl_models=sdxl_models, sdxx_models=sdxl_models,
flux_models=flux_models, flux_models=flux_models,
qwen_models=qwen_models,
openwebui_models=openwebui_models, openwebui_models=openwebui_models,
openrouter_models=openrouter_models, openrouter_models=openrouter_models,
topics=load_topics_from_config()) topics=load_topics_from_config(),
queue_count=queue_count)
@bp.route("/image_queued") @bp.route("/image_queued")
def image_queued(): def image_queued():
@ -62,17 +65,20 @@ def create_image_page():
if user_config["frame"]["create_requires_auth"] == "True" and not session.get("authenticated"): if user_config["frame"]["create_requires_auth"] == "True" and not session.get("authenticated"):
return redirect(url_for("auth_routes.login", next=request.path)) return redirect(url_for("auth_routes.login", next=request.path))
# Load all models (SDXL and FLUX only) # Load all models (SDXL, FLUX, and Qwen)
sdxl_models, flux_models = load_models_from_config() sdxl_models, flux_models, qwen_models = load_models_from_config()
openwebui_models = load_openwebui_models_from_config() openwebui_models = load_openwebui_models_from_config()
openrouter_models = load_openrouter_models_from_config() openrouter_models = load_openrouter_models_from_config()
queue_count = get_queue_count()
return render_template("create_image.html", return render_template("create_image.html",
sdxl_models=sdxl_models, sdxl_models=sdxl_models,
flux_models=flux_models, flux_models=flux_models,
qwen_models=qwen_models,
openwebui_models=openwebui_models, openwebui_models=openwebui_models,
openrouter_models=openrouter_models, openrouter_models=openrouter_models,
topics=load_topics_from_config()) topics=load_topics_from_config(),
queue_count=queue_count)
def init_app(config): def init_app(config):

View File

@ -1,8 +1,12 @@
from flask import Blueprint from flask import Blueprint, jsonify
from libs.comfyui import cancel_current_job from libs.comfyui import cancel_current_job, get_queue_details
bp = Blueprint("job_routes", __name__) bp = Blueprint("job_routes", __name__)
@bp.route("/cancel", methods=["GET"]) @bp.route("/cancel", methods=["GET"])
def cancel_job(): def cancel_job():
return cancel_current_job() return cancel_current_job()
@bp.route("/api/queue", methods=["GET"])
def api_queue():
return jsonify(get_queue_details())

View File

@ -73,6 +73,7 @@
background: #555; background: #555;
} }
#spinner-overlay { #spinner-overlay {
position: fixed; position: fixed;
inset: 0; inset: 0;
@ -131,10 +132,66 @@
height: 150px; height: 150px;
} }
} }
.queue-dropdown {
position: absolute;
top: 100%;
right: 0;
background: #222;
border: 1px solid #444;
border-radius: 5px;
padding: 10px;
z-index: 1001;
display: none;
max-height: 300px;
overflow-y: auto;
width: 400px;
}
.queue-item {
margin-bottom: 5px;
padding: 5px;
border-bottom: 1px solid #333;
}
.queue-item:last-child {
border-bottom: none;
}
.queue-item .prompt {
font-size: 0.9em;
color: #aaa;
white-space: normal;
word-wrap: break-word;
position: relative;
cursor: pointer;
}
.queue-item .prompt:hover::after {
content: "Model: " attr(data-model);
position: absolute;
bottom: 100%;
left: 0;
background: #333;
color: #00aaff;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.8em;
white-space: nowrap;
z-index: 1002;
box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
</style> </style>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div class="queue-container" style="position: fixed; top: 20px; right: 20px; z-index: 1000;">
<button id="queue-btn" style="background: #333; color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;">
Queue: <span id="queue-count">{{ queue_count | default(0) }}</span>
</button>
<div id="queue-dropdown" class="queue-dropdown">
<!-- Queue items will be populated here -->
</div>
</div>
<h1 style="margin-bottom: 20px;">Create An Image</h1> <h1 style="margin-bottom: 20px;">Create An Image</h1>
<textarea id="prompt-box" placeholder="Enter your custom prompt here..."></textarea> <textarea id="prompt-box" placeholder="Enter your custom prompt here..."></textarea>
@ -157,6 +214,13 @@
{% endfor %} {% endfor %}
</optgroup> </optgroup>
{% endif %} {% endif %}
{% if qwen_models %}
<optgroup label="Qwen">
{% for m in qwen_models %}
<option value="{{ m }}">{{ m.rsplit('.', 1)[0] if '.' in m else m }}</option>
{% endfor %}
</optgroup>
{% endif %}
{% if sdxl_models %} {% if sdxl_models %}
<optgroup label="SDXL"> <optgroup label="SDXL">
{% for m in sdxl_models %} {% for m in sdxl_models %}
@ -262,5 +326,59 @@
alert("Error requesting random prompt: " + error); alert("Error requesting random prompt: " + error);
}); });
} }
document.addEventListener('DOMContentLoaded', function() {
const queueBtn = document.getElementById('queue-btn');
const queueDropdown = document.getElementById('queue-dropdown');
const queueCountSpan = document.getElementById('queue-count');
// Toggle dropdown visibility
queueBtn.addEventListener('click', function(e) {
e.stopPropagation();
if (queueDropdown.style.display === 'block') {
queueDropdown.style.display = 'none';
} else {
fetchQueueDetails();
queueDropdown.style.display = 'block';
}
});
// Close dropdown when clicking outside
document.addEventListener('click', function() {
queueDropdown.style.display = 'none';
});
// Prevent dropdown from closing when clicking inside it
queueDropdown.addEventListener('click', function(e) {
e.stopPropagation();
});
function fetchQueueDetails() {
fetch('/api/queue')
.then(response => response.json())
.then(jobs => {
queueCountSpan.textContent = jobs.length;
const container = queueDropdown;
container.innerHTML = '';
if (jobs.length === 0) {
container.innerHTML = '<div class="queue-item">No jobs in queue</div>';
return;
}
jobs.forEach(job => {
const item = document.createElement('div');
item.className = 'queue-item';
item.innerHTML = `
<div class="prompt" data-model="${job.model}">${job.prompt}</div>
`;
container.appendChild(item);
});
})
.catch(error => {
console.error('Error fetching queue:', error);
queueDropdown.innerHTML = '<div class="queue-item">Error loading queue</div>';
});
}
});
</script> </script>
{% endblock %} {% endblock %}

147
workflow_qwen.json Normal file
View File

@ -0,0 +1,147 @@
{
"93": {
"inputs": {
"text": "jpeg compression",
"speak_and_recognation": {
"__value__": [
false,
true
]
},
"clip": [
"126",
0
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"95": {
"inputs": {
"seed": 22,
"steps": 10,
"cfg": 4.5,
"sampler_name": "euler",
"scheduler": "normal",
"denoise": 1,
"model": [
"127",
0
],
"positive": [
"100",
0
],
"negative": [
"93",
0
],
"latent_image": [
"97",
0
]
},
"class_type": "KSampler",
"_meta": {
"title": "KSampler"
}
},
"97": {
"inputs": {
"width": 1280,
"height": 768,
"length": 1,
"batch_size": 1
},
"class_type": "EmptyHunyuanLatentVideo",
"_meta": {
"title": "EmptyHunyuanLatentVideo"
}
},
"98": {
"inputs": {
"samples": [
"95",
0
],
"vae": [
"128",
0
]
},
"class_type": "VAEDecode",
"_meta": {
"title": "VAE Decode"
}
},
"100": {
"inputs": {
"text": "Terminator riding a push bike",
"speak_and_recognation": {
"__value__": [
false,
true
]
},
"clip": [
"126",
0
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"102": {
"inputs": {
"images": [
"98",
0
]
},
"class_type": "PreviewImage",
"_meta": {
"title": "Preview Image"
}
},
"126": {
"inputs": {
"clip_name": "Qwen2.5-VL-7B-Instruct-Q3_K_M.gguf",
"type": "qwen_image",
"device": "cuda:1",
"virtual_vram_gb": 6,
"use_other_vram": true,
"expert_mode_allocations": ""
},
"class_type": "CLIPLoaderGGUFDisTorchMultiGPU",
"_meta": {
"title": "CLIPLoaderGGUFDisTorchMultiGPU"
}
},
"127": {
"inputs": {
"unet_name": "qwen-image-Q2_K.gguf",
"device": "cuda:0",
"virtual_vram_gb": 6,
"use_other_vram": true,
"expert_mode_allocations": ""
},
"class_type": "UnetLoaderGGUFDisTorchMultiGPU",
"_meta": {
"title": "UnetLoaderGGUFDisTorchMultiGPU"
}
},
"128": {
"inputs": {
"vae_name": "qwen_image_vae.safetensors",
"device": "cuda:1"
},
"class_type": "VAELoaderMultiGPU",
"_meta": {
"title": "VAELoaderMultiGPU"
}
}
}