Add semantic tagging, search, favourite/NSFW filtering, and LLM job queue
Replaces old list-format tags (which duplicated prompt content) with structured dict tags per category (origin_series, outfit_type, participants, style_type, scene_type, etc.). Tags are now purely organizational metadata — removed from the prompt pipeline entirely. Adds is_favourite and is_nsfw columns to all 8 resource models. Favourite is DB-only (user preference); NSFW is mirrored in JSON tags for rescan persistence. All library pages get filter controls and favourites-first sorting. Introduces a parallel LLM job queue (_enqueue_task + _llm_queue_worker) for background tag regeneration, with the same status polling UI as ComfyUI jobs. Fixes call_llm() to use has_request_context() fallback for background threads. Adds global search (/search) across resources and gallery images, with navbar search bar. Adds gallery image sidecar JSON for per-image favourite/NSFW metadata. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
<div class="col-md-5">
|
||||
<div id="progress-container" class="mb-3 d-none">
|
||||
<label id="progress-label" class="form-label">Generating...</label>
|
||||
<div class="progress" role="progressbar" aria-label="Generation Progress" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
|
||||
<div class="progress" role="progressbar">
|
||||
<div id="progress-bar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%">0%</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mb-4">
|
||||
<div class="card-header bg-primary text-white">Generator Settings</div>
|
||||
<div class="card-header bg-primary text-white">Generator</div>
|
||||
<div class="card-body">
|
||||
<form id="generator-form" action="{{ url_for('generator') }}" method="post">
|
||||
<form id="generator-form" action="{{ url_for('generator_generate') }}" method="post">
|
||||
|
||||
<!-- Controls bar -->
|
||||
<div class="d-flex align-items-center gap-2 flex-wrap mb-3 pb-3 border-bottom">
|
||||
@@ -34,184 +34,94 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Character -->
|
||||
<!-- Preset selector -->
|
||||
<div class="mb-3">
|
||||
<div class="d-flex justify-content-between align-items-center mb-1">
|
||||
<label for="character" class="form-label mb-0">Character</label>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" id="random-char-btn">Random</button>
|
||||
<label for="preset-select" class="form-label mb-0 fw-semibold">Preset</label>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" id="random-preset-btn">Random</button>
|
||||
</div>
|
||||
<select class="form-select" id="character" name="character" required>
|
||||
<option value="" disabled {% if not selected_char %}selected{% endif %}>Select a character...</option>
|
||||
{% for char in characters %}
|
||||
<option value="{{ char.slug }}" {% if selected_char == char.slug %}selected{% endif %}>{{ char.name }}</option>
|
||||
<select class="form-select" id="preset-select" name="preset_slug" required>
|
||||
<option value="" disabled {% if not preset_slug %}selected{% endif %}>Select a preset...</option>
|
||||
{% for p in presets %}
|
||||
<option value="{{ p.slug }}" {% if preset_slug == p.slug %}selected{% endif %}>{{ p.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Checkpoint -->
|
||||
<!-- Preset summary (populated via AJAX) -->
|
||||
<div class="mb-3" id="preset-summary-container" style="display:none">
|
||||
<label class="form-label mb-1 small fw-semibold text-muted">Preset Configuration</label>
|
||||
<div class="d-flex flex-wrap gap-1" id="preset-summary"></div>
|
||||
</div>
|
||||
|
||||
<!-- Checkpoint override -->
|
||||
<div class="mb-3">
|
||||
<div class="d-flex justify-content-between align-items-center mb-1">
|
||||
<label for="checkpoint" class="form-label mb-0">Checkpoint Model</label>
|
||||
<label for="checkpoint" class="form-label mb-0">Checkpoint Override</label>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" id="random-ckpt-btn">Random</button>
|
||||
</div>
|
||||
<select class="form-select" id="checkpoint" name="checkpoint" required>
|
||||
<select class="form-select form-select-sm" id="checkpoint" name="checkpoint">
|
||||
<option value="">Use preset default</option>
|
||||
{% for ckpt in checkpoints %}
|
||||
<option value="{{ ckpt }}" {% if selected_ckpt == ckpt %}selected{% endif %}>{{ ckpt }}</option>
|
||||
<option value="{{ ckpt }}" {% if selected_ckpt == ckpt %}selected{% endif %}>{{ ckpt.split('/')[-1] }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<div class="form-text">Listing models from Illustrious/ folder</div>
|
||||
</div>
|
||||
|
||||
<!-- Mix & Match -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Mix & Match
|
||||
<small class="text-muted fw-normal ms-1">— first checked per category applies its LoRA</small>
|
||||
</label>
|
||||
<div class="accordion" id="mixAccordion">
|
||||
{% set mix_categories = [
|
||||
('Actions', 'action', actions, 'action_slugs'),
|
||||
('Outfits', 'outfit', outfits, 'outfit_slugs'),
|
||||
('Scenes', 'scene', scenes, 'scene_slugs'),
|
||||
('Styles', 'style', styles, 'style_slugs'),
|
||||
('Detailers', 'detailer', detailers, 'detailer_slugs'),
|
||||
] %}
|
||||
{% for cat_label, cat_key, cat_items, field_name in mix_categories %}
|
||||
<div class="accordion-item">
|
||||
<h2 class="accordion-header">
|
||||
<button class="accordion-button collapsed py-2" type="button"
|
||||
data-bs-toggle="collapse" data-bs-target="#mix-{{ cat_key }}">
|
||||
{{ cat_label }}
|
||||
<span class="badge bg-secondary rounded-pill ms-2" id="badge-{{ cat_key }}">0</span>
|
||||
<span class="badge bg-light text-secondary border ms-2 px-2 py-1"
|
||||
style="cursor:pointer;font-size:.7rem;font-weight:normal"
|
||||
onclick="event.stopPropagation(); randomizeCategory('{{ field_name }}', '{{ cat_key }}')">Random</span>
|
||||
</button>
|
||||
</h2>
|
||||
<div id="mix-{{ cat_key }}" class="accordion-collapse collapse">
|
||||
<div class="accordion-body p-2">
|
||||
<input type="text" class="form-control form-control-sm mb-2"
|
||||
placeholder="Search {{ cat_label | lower }}..."
|
||||
oninput="filterMixCategory(this, 'mixlist-{{ cat_key }}')">
|
||||
<div id="mixlist-{{ cat_key }}" style="max-height:220px;overflow-y:auto;">
|
||||
{% for item in cat_items %}
|
||||
<label class="mix-item d-flex align-items-center gap-2 px-2 py-1 rounded"
|
||||
data-name="{{ item.name | lower }}" style="cursor:pointer;">
|
||||
<input type="checkbox" class="form-check-input flex-shrink-0"
|
||||
name="{{ field_name }}" value="{{ item.slug }}"
|
||||
onchange="updateMixBadge('{{ cat_key }}', '{{ field_name }}')">
|
||||
{% if item.image_path %}
|
||||
<img src="{{ url_for('static', filename='uploads/' + item.image_path) }}"
|
||||
class="rounded flex-shrink-0" style="width:32px;height:32px;object-fit:cover">
|
||||
{% else %}
|
||||
<span class="rounded bg-light flex-shrink-0 d-inline-flex align-items-center justify-content-center text-muted"
|
||||
style="width:32px;height:32px;font-size:9px;">N/A</span>
|
||||
{% endif %}
|
||||
<span class="small text-truncate">{{ item.name }}</span>
|
||||
</label>
|
||||
{% else %}
|
||||
<p class="text-muted small p-2 mb-0">No {{ cat_label | lower }} found.</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Resolution -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Resolution</label>
|
||||
<label class="form-label">Resolution Override</label>
|
||||
<div class="d-flex flex-wrap gap-1 mb-2">
|
||||
<button type="button" class="btn btn-sm btn-secondary preset-btn" data-w="1024" data-h="1024">1:1</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="1152" data-h="896">4:3 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="896" data-h="1152">4:3 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="1344" data-h="768">16:9 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="768" data-h="1344">16:9 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="1280" data-h="800">16:10 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="800" data-h="1280">16:10 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="1792" data-h="768">21:9 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary preset-btn" data-w="768" data-h="1792">21:9 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="" data-h="">Preset default</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="1024" data-h="1024">1:1</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="1152" data-h="896">4:3 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="896" data-h="1152">4:3 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="1344" data-h="768">16:9 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="768" data-h="1344">16:9 P</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="1280" data-h="800">16:10 L</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary res-btn" data-w="800" data-h="1280">16:10 P</button>
|
||||
</div>
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<label class="form-label mb-0 small fw-semibold">W</label>
|
||||
<input type="number" class="form-control form-control-sm" name="width" id="res-width"
|
||||
value="1024" min="64" max="4096" step="64" style="width:88px">
|
||||
<span class="text-muted">×</span>
|
||||
value="" min="64" max="4096" step="64" style="width:88px" placeholder="Auto">
|
||||
<span class="text-muted">×</span>
|
||||
<label class="form-label mb-0 small fw-semibold">H</label>
|
||||
<input type="number" class="form-control form-control-sm" name="height" id="res-height"
|
||||
value="1024" min="64" max="4096" step="64" style="width:88px">
|
||||
value="" min="64" max="4096" step="64" style="width:88px" placeholder="Auto">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Prompt Preview -->
|
||||
<!-- Extra prompts -->
|
||||
<div class="mb-3">
|
||||
<div class="d-flex justify-content-between align-items-center mb-1">
|
||||
<label class="form-label mb-0">Prompt Preview</label>
|
||||
<div class="d-flex gap-1">
|
||||
<button type="button" class="btn btn-sm btn-outline-primary" id="build-preview-btn">Build</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" id="clear-preview-btn">Clear</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tag-widget-container d-none" id="prompt-tags"></div>
|
||||
<textarea class="form-control form-control-sm font-monospace" id="prompt-preview"
|
||||
name="override_prompt" rows="5"
|
||||
placeholder="Click Build to preview the auto-generated prompt — edit freely before generating."></textarea>
|
||||
<div class="form-text" id="preview-status"></div>
|
||||
</div>
|
||||
|
||||
<!-- ADetailer Prompt Previews -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label mb-0">Face Detailer Prompt</label>
|
||||
<div class="tag-widget-container d-none" id="face-tags"></div>
|
||||
<textarea class="form-control form-control-sm font-monospace" id="face-preview"
|
||||
name="override_face_prompt" rows="2"
|
||||
placeholder="Auto-populated on Build — edit to override face detailer prompt."></textarea>
|
||||
<label for="extra_positive" class="form-label">Extra Positive Prompt</label>
|
||||
<textarea class="form-control form-control-sm" id="extra_positive" name="extra_positive" rows="2"
|
||||
placeholder="Additional tags appended to the preset prompt"></textarea>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label mb-0">Hand Detailer Prompt</label>
|
||||
<div class="tag-widget-container d-none" id="hand-tags"></div>
|
||||
<textarea class="form-control form-control-sm font-monospace" id="hand-preview"
|
||||
name="override_hand_prompt" rows="2"
|
||||
placeholder="Auto-populated on Build — edit to override hand detailer prompt."></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Additional prompts -->
|
||||
<div class="mb-3">
|
||||
<label for="positive_prompt" class="form-label">Additional Positive Prompt</label>
|
||||
<textarea class="form-control" id="positive_prompt" name="positive_prompt" rows="2" placeholder="e.g. sitting in a cafe, drinking coffee, daylight"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="negative_prompt" class="form-label">Additional Negative Prompt</label>
|
||||
<textarea class="form-control" id="negative_prompt" name="negative_prompt" rows="2" placeholder="e.g. bad hands, extra digits"></textarea>
|
||||
<label for="extra_negative" class="form-label">Extra Negative Prompt</label>
|
||||
<textarea class="form-control form-control-sm" id="extra_negative" name="extra_negative" rows="2"
|
||||
placeholder="Additional negative tags"></textarea>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-md-7">
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark text-white">Result</div>
|
||||
<div class="card-body p-0 d-flex align-items-center justify-content-center" style="min-height: 500px;" id="result-container">
|
||||
{% if generated_image %}
|
||||
<div class="img-container w-100 h-100">
|
||||
<img src="{{ url_for('static', filename='uploads/' + generated_image) }}" alt="Generated Result" class="img-fluid w-100" id="result-img">
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="text-center text-muted" id="placeholder-text">
|
||||
<p>Select settings and click Generate</p>
|
||||
<p>Select a preset and click Generate</p>
|
||||
</div>
|
||||
<div class="img-container w-100 h-100 d-none">
|
||||
<img src="" alt="Generated Result" class="img-fluid w-100" id="result-img">
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="card-footer d-none" id="result-footer">
|
||||
<small class="text-muted">Saved to character gallery</small>
|
||||
<small class="text-muted">Saved to generator gallery</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -221,297 +131,194 @@
|
||||
|
||||
{% block scripts %}
|
||||
<script>
|
||||
// --- Filtering ---
|
||||
function filterMixCategory(input, listId) {
|
||||
const query = input.value.toLowerCase();
|
||||
document.querySelectorAll(`#${listId} .mix-item`).forEach(el => {
|
||||
el.style.display = el.dataset.name.includes(query) ? '' : 'none';
|
||||
});
|
||||
}
|
||||
// --- Preset summary ---
|
||||
async function loadPresetInfo(slug) {
|
||||
const container = document.getElementById('preset-summary-container');
|
||||
const summary = document.getElementById('preset-summary');
|
||||
if (!slug) { container.style.display = 'none'; return; }
|
||||
|
||||
function updateMixBadge(key, fieldName) {
|
||||
const count = document.querySelectorAll(`input[name="${fieldName}"]:checked`).length;
|
||||
const badge = document.getElementById(`badge-${key}`);
|
||||
badge.textContent = count;
|
||||
badge.className = count > 0
|
||||
? 'badge bg-primary rounded-pill ms-2'
|
||||
: 'badge bg-secondary rounded-pill ms-2';
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`/generator/preset_info?slug=${encodeURIComponent(slug)}`);
|
||||
const data = await res.json();
|
||||
summary.innerHTML = '';
|
||||
|
||||
// --- Randomizers (global so inline onclick can call them) ---
|
||||
function randomizeCategory(fieldName, catKey) {
|
||||
const cbs = Array.from(document.querySelectorAll(`input[name="${fieldName}"]`));
|
||||
cbs.forEach(cb => cb.checked = false);
|
||||
if (cbs.length) cbs[Math.floor(Math.random() * cbs.length)].checked = true;
|
||||
updateMixBadge(catKey, fieldName);
|
||||
}
|
||||
|
||||
function applyLuckyDip() {
|
||||
const charOpts = Array.from(document.getElementById('character').options).filter(o => o.value);
|
||||
if (charOpts.length)
|
||||
document.getElementById('character').value = charOpts[Math.floor(Math.random() * charOpts.length)].value;
|
||||
|
||||
const ckptOpts = Array.from(document.getElementById('checkpoint').options).filter(o => o.value);
|
||||
if (ckptOpts.length)
|
||||
document.getElementById('checkpoint').value = ckptOpts[Math.floor(Math.random() * ckptOpts.length)].value;
|
||||
|
||||
const presets = Array.from(document.querySelectorAll('.preset-btn'));
|
||||
if (presets.length) presets[Math.floor(Math.random() * presets.length)].click();
|
||||
|
||||
[['action_slugs', 'action'], ['outfit_slugs', 'outfit'], ['scene_slugs', 'scene'],
|
||||
['style_slugs', 'style'], ['detailer_slugs', 'detailer']].forEach(([field, key]) => {
|
||||
randomizeCategory(field, key);
|
||||
});
|
||||
|
||||
clearTagWidgets('prompt-tags', 'prompt-preview');
|
||||
clearTagWidgets('face-tags', 'face-preview');
|
||||
clearTagWidgets('hand-tags', 'hand-preview');
|
||||
document.getElementById('preview-status').textContent = '';
|
||||
}
|
||||
|
||||
// --- Resolution presets ---
|
||||
document.querySelectorAll('.preset-btn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
document.getElementById('res-width').value = btn.dataset.w;
|
||||
document.getElementById('res-height').value = btn.dataset.h;
|
||||
document.querySelectorAll('.preset-btn').forEach(b => {
|
||||
b.classList.remove('btn-secondary');
|
||||
b.classList.add('btn-outline-secondary');
|
||||
});
|
||||
btn.classList.remove('btn-outline-secondary');
|
||||
btn.classList.add('btn-secondary');
|
||||
});
|
||||
});
|
||||
|
||||
['res-width', 'res-height'].forEach(id => {
|
||||
document.getElementById(id).addEventListener('input', () => {
|
||||
document.querySelectorAll('.preset-btn').forEach(b => {
|
||||
b.classList.remove('btn-secondary');
|
||||
b.classList.add('btn-outline-secondary');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// --- Tag Widget System ---
|
||||
function populateTagWidgets(containerId, textareaId, promptStr) {
|
||||
const container = document.getElementById(containerId);
|
||||
const textarea = document.getElementById(textareaId);
|
||||
container.innerHTML = '';
|
||||
|
||||
if (!promptStr || !promptStr.trim()) {
|
||||
container.classList.add('d-none');
|
||||
return;
|
||||
}
|
||||
|
||||
const tags = promptStr.split(',').map(t => t.trim()).filter(Boolean);
|
||||
tags.forEach(tag => {
|
||||
const el = document.createElement('span');
|
||||
el.className = 'tag-widget active';
|
||||
el.textContent = tag;
|
||||
el.dataset.tag = tag;
|
||||
el.addEventListener('click', () => {
|
||||
el.classList.toggle('active');
|
||||
el.classList.toggle('inactive');
|
||||
rebuildFromTags(containerId, textareaId);
|
||||
});
|
||||
container.appendChild(el);
|
||||
});
|
||||
container.classList.remove('d-none');
|
||||
textarea.classList.add('d-none');
|
||||
}
|
||||
|
||||
function rebuildFromTags(containerId, textareaId) {
|
||||
const container = document.getElementById(containerId);
|
||||
const textarea = document.getElementById(textareaId);
|
||||
const activeTags = Array.from(container.querySelectorAll('.tag-widget.active'))
|
||||
.map(el => el.dataset.tag);
|
||||
textarea.value = activeTags.join(', ');
|
||||
}
|
||||
|
||||
function clearTagWidgets(containerId, textareaId) {
|
||||
const container = document.getElementById(containerId);
|
||||
const textarea = document.getElementById(textareaId);
|
||||
container.innerHTML = '';
|
||||
container.classList.add('d-none');
|
||||
textarea.classList.remove('d-none');
|
||||
textarea.value = '';
|
||||
}
|
||||
|
||||
// --- Prompt preview ---
|
||||
async function buildPromptPreview() {
|
||||
const charVal = document.getElementById('character').value;
|
||||
const status = document.getElementById('preview-status');
|
||||
if (!charVal) { status.textContent = 'Select a character first.'; return; }
|
||||
|
||||
status.textContent = 'Building...';
|
||||
const formData = new FormData(document.getElementById('generator-form'));
|
||||
try {
|
||||
const resp = await fetch('/generator/preview_prompt', { method: 'POST', body: formData });
|
||||
const data = await resp.json();
|
||||
if (data.error) {
|
||||
status.textContent = 'Error: ' + data.error;
|
||||
} else {
|
||||
document.getElementById('prompt-preview').value = data.prompt;
|
||||
document.getElementById('face-preview').value = data.face || '';
|
||||
document.getElementById('hand-preview').value = data.hand || '';
|
||||
populateTagWidgets('prompt-tags', 'prompt-preview', data.prompt);
|
||||
populateTagWidgets('face-tags', 'face-preview', data.face || '');
|
||||
populateTagWidgets('hand-tags', 'hand-preview', data.hand || '');
|
||||
status.textContent = 'Click tags to toggle — Clear to reset.';
|
||||
}
|
||||
} catch (err) {
|
||||
status.textContent = 'Request failed.';
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('build-preview-btn').addEventListener('click', buildPromptPreview);
|
||||
document.getElementById('clear-preview-btn').addEventListener('click', () => {
|
||||
clearTagWidgets('prompt-tags', 'prompt-preview');
|
||||
clearTagWidgets('face-tags', 'face-preview');
|
||||
clearTagWidgets('hand-tags', 'hand-preview');
|
||||
document.getElementById('preview-status').textContent = '';
|
||||
});
|
||||
|
||||
// --- Main generation logic ---
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('generator-form');
|
||||
const progressBar = document.getElementById('progress-bar');
|
||||
const progressCont = document.getElementById('progress-container');
|
||||
const progressLbl = document.getElementById('progress-label');
|
||||
const generateBtn = document.getElementById('generate-btn');
|
||||
const endlessBtn = document.getElementById('endless-btn');
|
||||
const stopBtn = document.getElementById('stop-btn');
|
||||
const numInput = document.getElementById('num-images');
|
||||
const resultImg = document.getElementById('result-img');
|
||||
const placeholder = document.getElementById('placeholder-text');
|
||||
const resultFooter = document.getElementById('result-footer');
|
||||
|
||||
let currentJobId = null;
|
||||
let stopRequested = false;
|
||||
|
||||
async function waitForJob(jobId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const poll = setInterval(async () => {
|
||||
try {
|
||||
const resp = await fetch(`/api/queue/${jobId}/status`);
|
||||
const data = await resp.json();
|
||||
if (data.status === 'done') { clearInterval(poll); resolve(data); }
|
||||
else if (data.status === 'failed' || data.status === 'removed') { clearInterval(poll); reject(new Error(data.error || 'Job failed')); }
|
||||
else if (data.status === 'processing') progressLbl.textContent = 'Generating…';
|
||||
else progressLbl.textContent = 'Queued…';
|
||||
} catch (err) {}
|
||||
}, 1500);
|
||||
});
|
||||
}
|
||||
|
||||
function setGeneratingState(active) {
|
||||
generateBtn.disabled = active;
|
||||
endlessBtn.disabled = active;
|
||||
stopBtn.classList.toggle('d-none', !active);
|
||||
if (!active) progressCont.classList.add('d-none');
|
||||
}
|
||||
|
||||
async function runOne(label) {
|
||||
if (document.getElementById('lucky-dip').checked) applyLuckyDip();
|
||||
|
||||
progressCont.classList.remove('d-none');
|
||||
progressBar.style.width = '100%'; progressBar.textContent = '';
|
||||
progressBar.classList.add('progress-bar-striped', 'progress-bar-animated');
|
||||
progressLbl.textContent = label;
|
||||
|
||||
const fd = new FormData(form);
|
||||
|
||||
const resp = await fetch(form.action, {
|
||||
method: 'POST', body: fd,
|
||||
headers: { 'X-Requested-With': 'XMLHttpRequest' }
|
||||
});
|
||||
const data = await resp.json();
|
||||
if (data.error) throw new Error(data.error);
|
||||
|
||||
currentJobId = data.job_id;
|
||||
progressLbl.textContent = 'Queued…';
|
||||
|
||||
const jobResult = await waitForJob(currentJobId);
|
||||
currentJobId = null;
|
||||
|
||||
if (jobResult.result && jobResult.result.image_url) {
|
||||
resultImg.src = jobResult.result.image_url;
|
||||
resultImg.parentElement.classList.remove('d-none');
|
||||
if (placeholder) placeholder.classList.add('d-none');
|
||||
resultFooter.classList.remove('d-none');
|
||||
}
|
||||
updateSeedFromResult(jobResult.result);
|
||||
progressBar.classList.remove('progress-bar-striped', 'progress-bar-animated');
|
||||
}
|
||||
|
||||
async function runLoop(endless) {
|
||||
const total = endless ? Infinity : (parseInt(numInput.value) || 1);
|
||||
stopRequested = false;
|
||||
setGeneratingState(true);
|
||||
let n = 0;
|
||||
try {
|
||||
while (!stopRequested && n < total) {
|
||||
n++;
|
||||
const lbl = endless ? `Generating #${n} (endless)...`
|
||||
: total === 1 ? 'Starting...'
|
||||
: `Generating ${n} / ${total}...`;
|
||||
await runOne(lbl);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
alert('Generation failed: ' + err.message);
|
||||
} finally {
|
||||
setGeneratingState(false);
|
||||
}
|
||||
}
|
||||
|
||||
form.addEventListener('submit', (e) => { e.preventDefault(); runLoop(false); });
|
||||
endlessBtn.addEventListener('click', () => runLoop(true));
|
||||
stopBtn.addEventListener('click', () => {
|
||||
stopRequested = true;
|
||||
progressLbl.textContent = 'Stopping after current image...';
|
||||
});
|
||||
|
||||
document.getElementById('random-char-btn').addEventListener('click', () => {
|
||||
const opts = Array.from(document.getElementById('character').options).filter(o => o.value);
|
||||
if (opts.length) {
|
||||
document.getElementById('character').value = opts[Math.floor(Math.random() * opts.length)].value;
|
||||
buildPromptPreview();
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('random-ckpt-btn').addEventListener('click', () => {
|
||||
const opts = Array.from(document.getElementById('checkpoint').options).filter(o => o.value);
|
||||
if (opts.length)
|
||||
document.getElementById('checkpoint').value = opts[Math.floor(Math.random() * opts.length)].value;
|
||||
});
|
||||
|
||||
document.getElementById('character').addEventListener('change', buildPromptPreview);
|
||||
|
||||
// Pre-populate from gallery URL params (?action=slug, ?outfit=slug, etc.)
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const catMap = {
|
||||
action: { field: 'action_slugs', catKey: 'action' },
|
||||
outfit: { field: 'outfit_slugs', catKey: 'outfit' },
|
||||
scene: { field: 'scene_slugs', catKey: 'scene' },
|
||||
style: { field: 'style_slugs', catKey: 'style' },
|
||||
detailer: { field: 'detailer_slugs', catKey: 'detailer' },
|
||||
const colors = {
|
||||
character: 'primary', outfit: 'success', action: 'danger',
|
||||
style: 'warning', scene: 'info', detailer: 'secondary',
|
||||
look: 'primary', checkpoint: 'dark', resolution: 'light text-dark'
|
||||
};
|
||||
let preselected = false;
|
||||
for (const [param, { field, catKey }] of Object.entries(catMap)) {
|
||||
const val = urlParams.get(param);
|
||||
if (!val) continue;
|
||||
const cb = document.querySelector(`input[name="${field}"][value="${CSS.escape(val)}"]`);
|
||||
if (cb) {
|
||||
cb.checked = true;
|
||||
updateMixBadge(catKey, field);
|
||||
// Expand the accordion panel
|
||||
const panel = document.getElementById(`mix-${catKey}`);
|
||||
if (panel) new bootstrap.Collapse(panel, { toggle: false }).show();
|
||||
preselected = true;
|
||||
}
|
||||
|
||||
for (const [key, val] of Object.entries(data)) {
|
||||
if (val === null) continue;
|
||||
const color = colors[key] || 'secondary';
|
||||
const chip = document.createElement('span');
|
||||
chip.className = `badge bg-${color} me-1 mb-1`;
|
||||
chip.innerHTML = `<small class="opacity-75">${key}:</small> ${val}`;
|
||||
summary.appendChild(chip);
|
||||
}
|
||||
if (preselected) buildPromptPreview();
|
||||
container.style.display = '';
|
||||
} catch (e) {
|
||||
container.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// --- Resolution presets ---
|
||||
document.querySelectorAll('.res-btn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
document.getElementById('res-width').value = btn.dataset.w;
|
||||
document.getElementById('res-height').value = btn.dataset.h;
|
||||
document.querySelectorAll('.res-btn').forEach(b => {
|
||||
b.classList.remove('btn-secondary');
|
||||
b.classList.add('btn-outline-secondary');
|
||||
});
|
||||
btn.classList.remove('btn-outline-secondary');
|
||||
btn.classList.add('btn-secondary');
|
||||
});
|
||||
});
|
||||
|
||||
// Deselect res presets when manual input changes
|
||||
['res-width', 'res-height'].forEach(id => {
|
||||
document.getElementById(id).addEventListener('input', () => {
|
||||
document.querySelectorAll('.res-btn').forEach(b => {
|
||||
b.classList.remove('btn-secondary');
|
||||
b.classList.add('btn-outline-secondary');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// --- Seed ---
|
||||
document.getElementById('seed-clear-btn').addEventListener('click', () => {
|
||||
document.getElementById('seed-input').value = '';
|
||||
});
|
||||
|
||||
// --- Random buttons ---
|
||||
document.getElementById('random-preset-btn').addEventListener('click', () => {
|
||||
const opts = Array.from(document.getElementById('preset-select').options).filter(o => o.value);
|
||||
if (opts.length) {
|
||||
const pick = opts[Math.floor(Math.random() * opts.length)];
|
||||
document.getElementById('preset-select').value = pick.value;
|
||||
loadPresetInfo(pick.value);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('random-ckpt-btn').addEventListener('click', () => {
|
||||
const opts = Array.from(document.getElementById('checkpoint').options).filter(o => o.value);
|
||||
if (opts.length) {
|
||||
document.getElementById('checkpoint').value = opts[Math.floor(Math.random() * opts.length)].value;
|
||||
}
|
||||
});
|
||||
|
||||
// --- Lucky Dip ---
|
||||
function applyLuckyDip() {
|
||||
document.getElementById('random-preset-btn').click();
|
||||
const ckptOpts = Array.from(document.getElementById('checkpoint').options).filter(o => o.value);
|
||||
if (ckptOpts.length)
|
||||
document.getElementById('checkpoint').value = ckptOpts[Math.floor(Math.random() * ckptOpts.length)].value;
|
||||
const resBtns = Array.from(document.querySelectorAll('.res-btn'));
|
||||
if (resBtns.length) resBtns[Math.floor(Math.random() * resBtns.length)].click();
|
||||
}
|
||||
|
||||
// --- Preset select change ---
|
||||
document.getElementById('preset-select').addEventListener('change', (e) => {
|
||||
loadPresetInfo(e.target.value);
|
||||
});
|
||||
|
||||
// --- Generation loop ---
|
||||
let stopRequested = false;
|
||||
|
||||
async function waitForJob(jobId) {
|
||||
while (true) {
|
||||
const res = await fetch(`/api/queue/${jobId}/status`);
|
||||
const data = await res.json();
|
||||
if (data.status === 'done') return data;
|
||||
if (data.status === 'failed') throw new Error(data.error || 'Generation failed');
|
||||
await new Promise(r => setTimeout(r, 1500));
|
||||
}
|
||||
}
|
||||
|
||||
function setGeneratingState(active) {
|
||||
document.getElementById('generate-btn').disabled = active;
|
||||
document.getElementById('endless-btn').classList.toggle('d-none', active);
|
||||
document.getElementById('stop-btn').classList.toggle('d-none', !active);
|
||||
document.getElementById('progress-container').classList.toggle('d-none', !active);
|
||||
}
|
||||
|
||||
function updateSeedFromResult(result) {
|
||||
if (result && result.result && result.result.seed != null) {
|
||||
document.getElementById('seed-input').value = result.result.seed;
|
||||
}
|
||||
}
|
||||
|
||||
async function runOne(label) {
|
||||
if (document.getElementById('lucky-dip').checked) applyLuckyDip();
|
||||
|
||||
const progressBar = document.getElementById('progress-bar');
|
||||
progressBar.style.width = '100%';
|
||||
progressBar.textContent = label;
|
||||
document.getElementById('progress-label').textContent = label;
|
||||
|
||||
const form = document.getElementById('generator-form');
|
||||
const formData = new FormData(form);
|
||||
|
||||
const res = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
headers: {'X-Requested-With': 'XMLHttpRequest'},
|
||||
body: formData,
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.error) throw new Error(data.error);
|
||||
|
||||
const result = await waitForJob(data.job_id);
|
||||
|
||||
if (result.result && result.result.image_url) {
|
||||
const img = document.getElementById('result-img');
|
||||
img.src = result.result.image_url + '?t=' + Date.now();
|
||||
img.parentElement.classList.remove('d-none');
|
||||
document.getElementById('placeholder-text')?.classList.add('d-none');
|
||||
document.getElementById('result-footer').classList.remove('d-none');
|
||||
}
|
||||
|
||||
updateSeedFromResult(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function runLoop(endless) {
|
||||
stopRequested = false;
|
||||
setGeneratingState(true);
|
||||
|
||||
const total = endless ? Infinity : parseInt(document.getElementById('num-images').value) || 1;
|
||||
let i = 0;
|
||||
|
||||
try {
|
||||
while (i < total && !stopRequested) {
|
||||
i++;
|
||||
const label = endless
|
||||
? `Generating (endless) #${i}...`
|
||||
: total > 1
|
||||
? `Generating ${i} / ${total}...`
|
||||
: 'Generating...';
|
||||
await runOne(label);
|
||||
}
|
||||
} catch (e) {
|
||||
alert('Generation error: ' + e.message);
|
||||
} finally {
|
||||
setGeneratingState(false);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Event listeners ---
|
||||
document.getElementById('generator-form').addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
runLoop(false);
|
||||
});
|
||||
|
||||
document.getElementById('endless-btn').addEventListener('click', () => runLoop(true));
|
||||
document.getElementById('stop-btn').addEventListener('click', () => { stopRequested = true; });
|
||||
|
||||
// --- Init: load preset info if pre-selected ---
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const presetSelect = document.getElementById('preset-select');
|
||||
if (presetSelect.value) loadPresetInfo(presetSelect.value);
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user