Additional inputs for Describe functionality
This commit is contained in:
parent
efaa5c4306
commit
ad184d778d
5 changed files with 239 additions and 38 deletions
|
|
@ -45,6 +45,134 @@
|
|||
setDescribeButtonsDisabled(false);
|
||||
}
|
||||
|
||||
function showDescribePromptModal(job, articleId){
|
||||
var existing = document.getElementById('mvlog-describe-prompt-modal');
|
||||
if (existing) existing.remove();
|
||||
var overlay = document.createElement('div');
|
||||
overlay.id = 'mvlog-describe-prompt-modal';
|
||||
overlay.style.cssText = 'position:fixed;left:0;top:0;width:100%;height:100%;background:rgba(0,0,0,0.5);display:flex;align-items:center;justify-content:center;z-index:9999;';
|
||||
var box = document.createElement('div');
|
||||
box.style.cssText = 'background:white;color:#111;padding:20px;max-width:800px;width:min(800px,92vw);max-height:80vh;overflow:auto;border-radius:6px;font-family:inherit;';
|
||||
var h = document.createElement('h3');
|
||||
h.textContent = 'Additional infos/guidelines';
|
||||
var p = document.createElement('div');
|
||||
p.style.cssText = 'margin:0 0 10px 0;color:#444;';
|
||||
p.textContent = 'Optional extra guidance for the description prompt.';
|
||||
var label = document.createElement('div');
|
||||
label.style.cssText = 'font-weight:600;margin:10px 0 6px;';
|
||||
label.textContent = 'Infos/guidelines';
|
||||
var textarea = document.createElement('textarea');
|
||||
textarea.rows = 8;
|
||||
textarea.style.cssText = 'width:100%;box-sizing:border-box;font-family:inherit;padding:10px;border:1px solid #ccc;border-radius:4px;resize:vertical;';
|
||||
textarea.placeholder = 'Loading saved prompt…';
|
||||
textarea.disabled = true;
|
||||
var btnBar = document.createElement('div');
|
||||
btnBar.style.cssText = 'margin-top:12px;text-align:right;';
|
||||
var submitBtn = document.createElement('button');
|
||||
submitBtn.className = 'button';
|
||||
submitBtn.textContent = 'Describe';
|
||||
submitBtn.disabled = true;
|
||||
var cancelBtn = document.createElement('button');
|
||||
cancelBtn.className = 'button';
|
||||
cancelBtn.textContent = 'Cancel';
|
||||
cancelBtn.style.marginLeft = '8px';
|
||||
btnBar.appendChild(submitBtn);
|
||||
btnBar.appendChild(cancelBtn);
|
||||
box.appendChild(h);
|
||||
box.appendChild(p);
|
||||
box.appendChild(label);
|
||||
box.appendChild(textarea);
|
||||
box.appendChild(btnBar);
|
||||
overlay.appendChild(box);
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
function closeModal(){
|
||||
if (overlay && overlay.parentNode) overlay.parentNode.removeChild(overlay);
|
||||
document.removeEventListener('keydown', onKeyDown);
|
||||
}
|
||||
function submit(){
|
||||
var promptText = textarea.value || '';
|
||||
closeModal();
|
||||
runDescribeRequest(job, articleId, promptText);
|
||||
}
|
||||
function onKeyDown(ev){
|
||||
if (ev.key === 'Escape') {
|
||||
ev.preventDefault();
|
||||
closeModal();
|
||||
} else if ((ev.ctrlKey || ev.metaKey) && ev.key === 'Enter') {
|
||||
ev.preventDefault();
|
||||
submit();
|
||||
}
|
||||
}
|
||||
function loadSavedPrompt(){
|
||||
var body = 'id=' + encodeURIComponent(articleId) + '&job=' + encodeURIComponent(job);
|
||||
fetch('lib/describe_prompt.php', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {'Content-Type':'application/x-www-form-urlencoded'},
|
||||
body: body
|
||||
}).then(function(r){ return r.json().catch(function(){ return {}; }); })
|
||||
.then(function(data){
|
||||
if (!overlay || !overlay.parentNode) return;
|
||||
if (data && data.status === 'ok' && typeof data.additional_prompt === 'string') {
|
||||
textarea.value = data.additional_prompt;
|
||||
}
|
||||
}).catch(function(){}).finally(function(){
|
||||
if (!overlay || !overlay.parentNode) return;
|
||||
textarea.disabled = false;
|
||||
submitBtn.disabled = false;
|
||||
textarea.placeholder = 'Optional details about the images/videos/trip…';
|
||||
setTimeout(function(){ textarea.focus(); }, 0);
|
||||
});
|
||||
}
|
||||
|
||||
cancelBtn.addEventListener('click', function(){ closeModal(); });
|
||||
submitBtn.addEventListener('click', submit);
|
||||
overlay.addEventListener('click', function(ev){ if (ev.target === overlay) closeModal(); });
|
||||
document.addEventListener('keydown', onKeyDown);
|
||||
loadSavedPrompt();
|
||||
}
|
||||
|
||||
function runDescribeRequest(job, articleId, additionalPrompt){
|
||||
showDescribeBusy();
|
||||
var body = 'id=' + encodeURIComponent(articleId) + '&job=' + encodeURIComponent(job) + '&max_frames=16';
|
||||
if (additionalPrompt && additionalPrompt.trim()) {
|
||||
body += '&additional_prompt=' + encodeURIComponent(additionalPrompt);
|
||||
}
|
||||
fetch('lib/generate_data_sync.php', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {'Content-Type':'application/x-www-form-urlencoded'},
|
||||
body: body
|
||||
}).then(function(r){ return r.json().catch(()=>({})); })
|
||||
.then(function(data){
|
||||
if (data && data.status === 'ok'){
|
||||
var desc = data.description || (data.raw_json && JSON.stringify(data.raw_json)) || '';
|
||||
var teaser = data.teaser || '';
|
||||
var rawFirst = (Array.isArray(data.raw_json) && data.raw_json.length) ? data.raw_json[0] : data.raw_json;
|
||||
var quoteDa = data.quote_da || (rawFirst && rawFirst.quote_da) || '';
|
||||
showModal(job, articleId, desc, teaser, quoteDa, data.raw_json || null);
|
||||
} else {
|
||||
var msg = (data && data.message) ? data.message : 'No response';
|
||||
if (data && data.log) {
|
||||
try {
|
||||
var log = String(data.log);
|
||||
var lines = log.split('\n').map(function(l){ return l.trim(); }).filter(function(l){ return l.length>0; });
|
||||
var keywords = ['quota','rate limit','rate-limit','429','503','overloaded','high demand','busy','unavailable','error','failed','retry'];
|
||||
var found = lines.find(function(l){ var ll=l.toLowerCase(); return keywords.some(function(k){ return ll.indexOf(k) !== -1; }); });
|
||||
if (found) msg += ' — ' + found;
|
||||
else if (lines.length) msg += ' — ' + lines[0].slice(0,200);
|
||||
} catch(e){}
|
||||
}
|
||||
showToast('Error: ' + msg, false, (data && data.log) ? (typeof data.log === 'string' ? data.log : JSON.stringify(data.log)) : null);
|
||||
}
|
||||
}).catch(function(err){
|
||||
showToast('Request failed: ' + err, false);
|
||||
}).finally(function(){
|
||||
hideDescribeBusy();
|
||||
});
|
||||
}
|
||||
|
||||
function showModal(job, articleId, description, teaser, quoteDa, rawJson){
|
||||
var existing = document.getElementById('mvlog-gemini-modal');
|
||||
if (existing) existing.remove();
|
||||
|
|
@ -127,39 +255,7 @@
|
|||
var job = el.dataset.job || '';
|
||||
var articleId = el.dataset.id || '';
|
||||
if (!job && !articleId) return;
|
||||
showDescribeBusy();
|
||||
fetch('lib/generate_data_sync.php', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {'Content-Type':'application/x-www-form-urlencoded'},
|
||||
body: 'id=' + encodeURIComponent(articleId) + '&job=' + encodeURIComponent(job) + '&max_frames=16'
|
||||
}).then(function(r){ return r.json().catch(()=>({})); })
|
||||
.then(function(data){
|
||||
if (data && data.status === 'ok'){
|
||||
var desc = data.description || (data.raw_json && JSON.stringify(data.raw_json)) || '';
|
||||
var teaser = data.teaser || '';
|
||||
var rawFirst = (Array.isArray(data.raw_json) && data.raw_json.length) ? data.raw_json[0] : data.raw_json;
|
||||
var quoteDa = data.quote_da || (rawFirst && rawFirst.quote_da) || '';
|
||||
showModal(job, articleId, desc, teaser, quoteDa, data.raw_json || null);
|
||||
} else {
|
||||
var msg = (data && data.message) ? data.message : 'No response';
|
||||
if (data && data.log) {
|
||||
try {
|
||||
var log = String(data.log);
|
||||
var lines = log.split('\n').map(function(l){ return l.trim(); }).filter(function(l){ return l.length>0; });
|
||||
var keywords = ['quota','rate limit','rate-limit','429','503','overloaded','high demand','busy','unavailable','error','failed','retry'];
|
||||
var found = lines.find(function(l){ var ll=l.toLowerCase(); return keywords.some(function(k){ return ll.indexOf(k) !== -1; }); });
|
||||
if (found) msg += ' — ' + found;
|
||||
else if (lines.length) msg += ' — ' + lines[0].slice(0,200);
|
||||
} catch(e){}
|
||||
}
|
||||
showToast('Error: ' + msg, false, (data && data.log) ? (typeof data.log === 'string' ? data.log : JSON.stringify(data.log)) : null);
|
||||
}
|
||||
}).catch(function(err){
|
||||
showToast('Request failed: ' + err, false);
|
||||
}).finally(function(){
|
||||
hideDescribeBusy();
|
||||
});
|
||||
showDescribePromptModal(job, articleId);
|
||||
});
|
||||
|
||||
attachDescribeButtons();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue