// various functions for interation with ui.py not large enough to warrant putting them in separate files function selected_gallery_index(){ var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem .gallery-item') var button = gradioApp().querySelector('[style="display: block;"].tabitem .gallery-item.\\!ring-2') var result = -1 buttons.forEach(function(v, i){ if(v==button) { result = i } }) return result } function extract_image_from_gallery(gallery){ if(gallery.length == 1){ return gallery[0] } index = selected_gallery_index() if (index < 0 || index >= gallery.length){ return [null] } return gallery[index]; } function args_to_array(args){ res = [] for(var i=0;i label > textarea") txt2img_textarea?.addEventListener("input", debounce(submit_prompt_text.bind(null, "txt2img"), debounce_time)) } if (!img2img_textarea) { img2img_textarea = gradioApp().querySelector("#img2img_prompt > label > textarea") img2img_textarea?.addEventListener("input", debounce(submit_prompt_text.bind(null, "img2img"), debounce_time)) } }) let txt2img_textarea, img2img_textarea = undefined; function submit_prompt_text(source, e) { let prompt_text; if (source == "txt2img") prompt_text = txt2img_textarea.value; else if (source == "img2img") prompt_text = img2img_textarea.value; if (!prompt_text) return; params = { method: "POST", headers: { "Accept": "application/json", "Content-type": "application/json" }, body: JSON.stringify({data:[prompt_text]}) } fetch('http://127.0.0.1:7860/api/tokenize/', params) .then((response) => response.json()) .then((data) => { if (data?.data.length) { let response_json = data.data[0] if (elem = gradioApp().getElementById(source+"_token_counter")) { if (response_json.token_count > response_json.max_length) elem.classList.add("red"); else elem.classList.remove("red"); elem.innerText = response_json.token_count + "/" + response_json.max_length; } } }) .catch((error) => { console.error('Error:', error); }); }