This commit is contained in:
Vladimir Mandic
2023-05-16 18:05:29 -04:00
parent bce16a1db5
commit ff4f94fc2a
7 changed files with 50 additions and 95 deletions
+21 -41
View File
@@ -3,31 +3,16 @@ let currentHeight = null;
let arFrameTimeout = setTimeout(() => {}, 0);
function dimensionChange(e, is_width, is_height) {
if (is_width) {
currentWidth = e.target.value * 1.0;
}
if (is_height) {
currentHeight = e.target.value * 1.0;
}
if (is_width) currentWidth = e.target.value * 1.0;
if (is_height) currentHeight = e.target.value * 1.0;
const inImg2img = gradioApp().querySelector('#tab_img2img').style.display === 'block';
if (!inImg2img) {
return;
}
if (!inImg2img) return;
let targetElement = null;
const tabIndex = get_tab_index('mode_img2img');
if (tabIndex === 0) { // img2img
targetElement = gradioApp().querySelector('#img2img_image div[data-testid=image] img');
} else if (tabIndex === 1) { // Sketch
targetElement = gradioApp().querySelector('#img2img_sketch div[data-testid=image] img');
} else if (tabIndex === 2) { // Inpaint
targetElement = gradioApp().querySelector('#img2maskimg div[data-testid=image] img');
} else if (tabIndex === 3) { // Inpaint sketch
targetElement = gradioApp().querySelector('#inpaint_sketch div[data-testid=image] img');
}
if (tabIndex === 0) targetElement = gradioApp().querySelector('#img2img_image div[data-testid=image] img'); // img2img
else if (tabIndex === 1) targetElement = gradioApp().querySelector('#img2img_sketch div[data-testid=image] img'); // Sketch
else if (tabIndex === 2) targetElement = gradioApp().querySelector('#img2maskimg div[data-testid=image] img'); // Inpaint
else if (tabIndex === 3) targetElement = gradioApp().querySelector('#inpaint_sketch div[data-testid=image] img'); // Inpaint sketch
if (targetElement) {
let arPreviewRect = gradioApp().querySelector('#imageARPreview');
@@ -39,29 +24,24 @@ function dimensionChange(e, is_width, is_height) {
const viewportOffset = targetElement.getBoundingClientRect();
viewportscale = Math.min(targetElement.clientWidth / targetElement.naturalWidth, targetElement.clientHeight / targetElement.naturalHeight);
const viewportscale = Math.min(targetElement.clientWidth / targetElement.naturalWidth, targetElement.clientHeight / targetElement.naturalHeight);
scaledx = targetElement.naturalWidth * viewportscale;
scaledy = targetElement.naturalHeight * viewportscale;
const scaledx = targetElement.naturalWidth * viewportscale;
const scaledy = targetElement.naturalHeight * viewportscale;
cleintRectTop = (viewportOffset.top + window.scrollY);
cleintRectLeft = (viewportOffset.left + window.scrollX);
cleintRectCentreY = cleintRectTop + (targetElement.clientHeight / 2);
cleintRectCentreX = cleintRectLeft + (targetElement.clientWidth / 2);
const cleintRectTop = (viewportOffset.top + window.scrollY);
const cleintRectLeft = (viewportOffset.left + window.scrollX);
const cleintRectCentreY = cleintRectTop + (targetElement.clientHeight / 2);
const cleintRectCentreX = cleintRectLeft + (targetElement.clientWidth / 2);
viewRectTop = cleintRectCentreY - (scaledy / 2);
viewRectLeft = cleintRectCentreX - (scaledx / 2);
arRectWidth = scaledx;
arRectHeight = scaledy;
const arscale = Math.min(scaledx / currentWidth, scaledy / currentHeight);
const arscaledx = currentWidth * arscale;
const arscaledy = currentHeight * arscale;
arscale = Math.min(arRectWidth / currentWidth, arRectHeight / currentHeight);
arscaledx = currentWidth * arscale;
arscaledy = currentHeight * arscale;
arRectTop = cleintRectCentreY - (arscaledy / 2);
arRectLeft = cleintRectCentreX - (arscaledx / 2);
arRectWidth = arscaledx;
arRectHeight = arscaledy;
const arRectTop = cleintRectCentreY - (arscaledy / 2);
const arRectLeft = cleintRectCentreX - (arscaledx / 2);
const arRectWidth = arscaledx;
const arRectHeight = arscaledy;
arPreviewRect.style.top = `${arRectTop}px`;
arPreviewRect.style.left = `${arRectLeft}px`;
+3 -9
View File
@@ -3,7 +3,7 @@ contextMenuInit = function () {
const menuSpecs = new Map();
const uid = function () {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
return Date.now().toString(36) + Math.random().toString(36).substring(2);
};
function showContextMenu(event, element, menuEntries) {
@@ -85,15 +85,9 @@ contextMenuInit = function () {
}
function addContextMenuEventListener() {
if (eventListenerApplied) {
return;
}
if (eventListenerApplied) return;
gradioApp().addEventListener('click', (e) => {
const source = e.composedPath()[0];
if (source.id && source.id.indexOf('check_progress') > -1) {
return;
}
if (!e.isTrusted) return;
const oldMenu = gradioApp().querySelector('#context-menu');
if (oldMenu) {
oldMenu.remove();
+20 -37
View File
@@ -2,11 +2,9 @@ function keyupEditAttention(event) {
const target = event.originalTarget || event.composedPath()[0];
if (!target.matches("[id*='_toprow'] [id*='_prompt'] textarea")) return;
if (!(event.metaKey || event.ctrlKey)) return;
const isPlus = event.key === 'ArrowUp';
const isMinus = event.key === 'ArrowDown';
if (!isPlus && !isMinus) return;
let { selectionStart } = target;
let { selectionEnd } = target;
let text = target.value;
@@ -15,7 +13,7 @@ function keyupEditAttention(event) {
if (selectionStart !== selectionEnd) return false;
// Find opening parenthesis around current cursor
const before = text.substring(0, selectionStart);
const before = text.substringing(0, selectionStart);
let beforeParen = before.lastIndexOf(OPEN);
if (beforeParen === -1) return false;
let beforeParenClose = before.lastIndexOf(CLOSE);
@@ -25,7 +23,7 @@ function keyupEditAttention(event) {
}
// Find closing parenthesis around current cursor
const after = text.substring(selectionStart);
const after = text.substringing(selectionStart);
let afterParen = after.indexOf(CLOSE);
if (afterParen === -1) return false;
let afterParenOpen = after.indexOf(OPEN);
@@ -36,7 +34,7 @@ function keyupEditAttention(event) {
if (beforeParen === -1 || afterParen === -1) return false;
// Set the selection to the text between the parenthesis
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
const parenContent = text.substringing(beforeParen + 1, selectionStart + afterParen);
const lastColon = parenContent.lastIndexOf(':');
selectionStart = beforeParen + 1;
selectionEnd = selectionStart + lastColon;
@@ -47,63 +45,50 @@ function keyupEditAttention(event) {
function selectCurrentWord() {
if (selectionStart !== selectionEnd) return false;
const delimiters = `${opts.keyedit_delimiters} \r\n\t`;
// seek backward until to find beggining
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
selectionStart--;
}
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) selectionStart--;
// seek forward to find end
while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) {
selectionEnd++;
}
while (!delimiters.includes(text[selectionEnd]) && selectionEnd < text.length) selectionEnd++;
target.setSelectionRange(selectionStart, selectionEnd);
return true;
}
// If the user hasn't selected anything, let's select their current parenthesis block or word
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
selectCurrentWord();
}
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) selectCurrentWord();
event.preventDefault();
closeCharacter = ')';
delta = opts.keyedit_precision_attention;
let closeCharacter = ')';
let delta = opts.keyedit_precision_attention;
if (selectionStart > 0 && text[selectionStart - 1] === '<') {
closeCharacter = '>';
delta = opts.keyedit_precision_extra;
} else if (selectionStart === 0 || text[selectionStart - 1] != '(') {
// do not include spaces at the end
while (selectionEnd > selectionStart && text[selectionEnd - 1] === ' ') {
selectionEnd -= 1;
}
if (selectionStart === selectionEnd) {
return;
}
} else if (selectionStart === 0 || text[selectionStart - 1] !== '(') {
while (selectionEnd > selectionStart && text[selectionEnd - 1] === ' ') selectionEnd -= 1;
if (selectionStart === selectionEnd) return;
text = `${text.slice(0, selectionStart)}(${text.slice(selectionStart, selectionEnd)}:1.0)${text.slice(selectionEnd)}`;
selectionStart += 1;
selectionEnd += 1;
}
end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
if (isNaN(weight)) return;
const end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
let weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
if (Number.isNaN(weight)) return;
weight += isPlus ? delta : -delta;
weight = parseFloat(weight.toPrecision(12));
if (String(weight).length === 1) weight += '.0';
if (closeCharacter === ')' && weight === 1) {
console.log('HERE', closeCharacter, weight);
if (closeCharacter == ')' && weight == 1) {
console.log('HERE2');
text = text.slice(0, selectionStart - 1) + text.slice(selectionStart, selectionEnd) + text.slice(selectionEnd + 5);
selectionStart--;
selectionEnd--;
console.log('HERE2', text);
} else {
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + 1 + end - 1);
console.log('HERE3', text);
}
target.focus();
@@ -114,6 +99,4 @@ function keyupEditAttention(event) {
updateInput(target);
}
addEventListener('keydown', (event) => {
keyupEditAttention(event);
});
addEventListener('keydown', (event) => keyupEditAttention(event));
+3 -3
View File
@@ -3,8 +3,8 @@ function extensions_apply(extensions_disabled_list, extensions_update_list, disa
const disable = [];
const update = [];
gradioApp().querySelectorAll('#extensions input[type="checkbox"]').forEach((x) => {
if (x.name.startsWith('enable_') && !x.checked) disable.push(x.name.substr(7));
if (x.name.startsWith('update_') && x.checked) update.push(x.name.substr(7));
if (x.name.startsWith('enable_') && !x.checked) disable.push(x.name.substring(7));
if (x.name.startsWith('update_') && x.checked) update.push(x.name.substring(7));
});
restart_reload();
return [JSON.stringify(disable), JSON.stringify(update), disable_all];
@@ -14,7 +14,7 @@ function extensions_check(info, extensions_disabled_list, search_text, sort_colu
console.log('Extensions check:', info, extensions_disabled_list);
const disable = [];
gradioApp().querySelectorAll('#extensions input[type="checkbox"]').forEach((x) => {
if (x.name.startsWith('enable_') && !x.checked) disable.push(x.name.substr(7));
if (x.name.startsWith('enable_') && !x.checked) disable.push(x.name.substring(7));
});
// gradioApp().querySelectorAll('#extensions .extension_status').forEach((x) => {
// x.innerHTML = 'Loading...';
+1 -1
View File
@@ -247,7 +247,7 @@ onUiUpdate(() => {
onOptionsChanged(() => {
const elem = gradioApp().getElementById('sd_checkpoint_hash');
const sd_checkpoint_hash = opts.sd_checkpoint_hash || '';
const shorthash = sd_checkpoint_hash.substr(0, 10);
const shorthash = sd_checkpoint_hash.substring(0, 10);
if (elem && elem.textContent !== shorthash) {
elem.textContent = shorthash;
+2 -1
View File
@@ -323,7 +323,8 @@ if __name__ == "__main__":
# import os
# import sys
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
input_text = "(upzero) (upone:1.1), ((uptwo:1.2)), [downzero], [downone:0.9], [[downtwo:0.8]], this is a test"
# input_text = "(upzero) (upone:1.1), ((uptwo:1.2)), [downzero], [downone:0.9], [[downtwo:0.8]], this is a test"
input_text = 'a (white (lion:1.4)), cat [mouse] [tiger:0.8], (high) in a jungle'
output_list = parse_prompt_attention(input_text)
print('INPUT', input_text)
print('OUTPUT', output_list)
-3
View File
@@ -368,9 +368,6 @@ def create_ui():
search_text = gr.Text(label="Search")
info = gr.HTML('Note: After any operation such as install/uninstall or enable/disable, please restart the server')
with gr.Column(scale=1):
print('HERE1', sort_ordering)
print('HERE2', list(sort_ordering.keys()))
print('HERE2', sort_ordering.items())
sort_column = gr.Dropdown(value="default", label="Sort by", choices=list(sort_ordering.keys()), multiselect=False)
with gr.Column(scale=1):
refresh_extensions_button = gr.Button(value="Refresh extension list", variant="primary")