Merge pull request #25 from thisjam/dev

v1.63
pull/36/merge
Six_GodK 2023-09-09 00:23:44 +08:00 committed by GitHub
commit 69ee6d1d43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 574 additions and 61 deletions

View File

@ -1,6 +1,4 @@

# sd-webui-oldsix_prompt v1.60
# sd-webui-oldsix_prompt v1.63
### 老版词库 @路过银河&网上收集
### 新词库由群里小伙伴 @麻瓜 @万碎爷 @元一斤 整理
### 100%完全自制插件不是魔改别人最近有个别up未均本人允许在多个平台打着nsfw词库的旗号拿本插件吸粉入群别人安装插件出问题又无法提供解决方案 ###
@ -28,6 +26,7 @@
* 左键输入正向提示框、 右键输入负向提示框
* 支持自定义词库
* 支持随机抽卡 【灵感模式、分类随机模式】
* 支持翻译、权重调整、位置调整。Alt键呼出隐藏面板
* 支持动态随机提示词 本插件专属语法示例:#[red#blue#yellow] 或#[日常服饰]、 #[红色类#黄色类]
---
##### 展示1

View File

@ -1,4 +1,5 @@
let Elements;
const selectPrompts={}
function loadNodes() {
let Elements = {
prompt: getEle('#oldsix-prompt1'),
@ -16,6 +17,11 @@ function loadNodes() {
btnSends:getEleAll('.oldsix-btnSend'),
txtStart:getEleAll('.oldsix-txt-start textarea'),
txtEnd:getEleAll('.oldsix-txt-end textarea'),
trans:getEleAll('.old-six-traninput'),
tabtxt:getEle("#tab_txt2img"),
tabimg:getEle("#tab_img2img"),
autoComs:[],
btnReload:[],
btnClearP:[],
@ -23,6 +29,8 @@ function loadNodes() {
pClasses:[],
txtLeftLayout:getEle('#txt2img_results'),
imgLeftLayout:getEle('#img2img_results'),
txtul:null,
imgul:null
@ -60,9 +68,8 @@ function CreateEle(type,parentDom,css,html){
function addPrompt(e) {
let dom=e.target;
let str= e.target.dataset.sixoldtit
let elementprompt =e.target.dataset.pageindex==1 ? Elements.imgpromt : Elements.txtpromt
let str= e.target.dataset.sixoldtit
let elementprompt =e.target.dataset.pageindex==1 ? Elements.imgpromt : Elements.txtpromt
dom.classList.toggle("active")
toggleNavCss(dom)
ishas=false;
@ -72,7 +79,7 @@ function CreateEle(type,parentDom,css,html){
}
}
if(!ishas){
//删除带权重
if(elementprompt.value.includes(str+':')){
const teststr=`${str},|\\(${str}:\\d+\\.\\d+\\),`
const regex =new RegExp(teststr);
@ -80,13 +87,26 @@ function CreateEle(type,parentDom,css,html){
elementprompt.value= elementprompt.value.replace(regex,'');
}
else{
//删除
else{
elementprompt.value= elementprompt.value.replace(str+',','');
}
return
let selet=selectPrompts[dom.textContent]
if(selet){
selet.li.parentNode.removeChild(selet.li)
delete selectPrompts[dom.textContent]
}
return //
}
updatatextToTextArea(elementprompt,str)
//添加
let ul =e.target.dataset.pageindex==1 ? Elements.imgul : Elements.txtul
let cn=dom.innerHTML
let en=str
addLi(ul,cn,en,dom)
updatatextToTextArea(elementprompt,en)
}
@ -95,12 +115,29 @@ function addNPrompt(e) {
let elementprompt = e.target.dataset.pageindex==1 ? Elements.imgnpromt : Elements.txtnpromt
elementprompt.focus();
document.execCommand('insertText', false, e.target.dataset.sixoldtit + ',')
closeAotuCom()
}
function updatatextToTextArea(inputelem,val){
inputelem.value+=val+','
updateInput(inputelem)
closeAotuCom()
}
function closeAotuCom(){
if(Elements.autoComs.length<2){
Elements.autoComs=getEleAll(".autocompleteParent.p")
}
setTimeout(() => {
Elements.autoComs.forEach(element => {
element.style.display = "none";
})
}, 50)
}
@ -198,7 +235,7 @@ function addDynamicToTextArea(btnele,pageindex){
let elementprompt =pageindex==1 ? Elements.imgpromt : Elements.txtpromt
elementprompt.focus();
document.execCommand('insertText', false, text + ',')
closeAotuCom()
}
}
@ -357,17 +394,61 @@ function move(){
checkboxParents.forEach(item=>{
item.parentElement.classList.add('oldsix-inline')
})
Elements.trans.forEach((item,index)=>{
item.classList.remove('block')
let ul=CreateEle('ul',item,'oldsix-ul','')
if(!index){
Elements.txtul=ul
}
else{
Elements.imgul=ul
}
item.onmousedown = function(event) {
let istriggel=event.target.classList.contains("old-six-traninput");
if(!istriggel)return
// 获取div当前的x和y坐标
var x = event.clientX - item.offsetLeft;
var y = event.clientY - item.offsetTop;
item.style.cursor = 'grabbing';
document.onmousemove = function(event) {
// 获取鼠标当前的位置
var newX = event.clientX - x;
var newY = event.clientY - y;
// 设置div的新位置
item.style.left = newX + 'px';
item.style.top = newY + 'px';
};
document.onmouseup = function() {
// 当鼠标松开时移除mousemove和mouseup事件以防止继续拖动
item.style.cursor = 'grab';
document.onmousemove = null;
document.onmouseup = null;
};
};
})
getEle("#tab_txt2img").appendChild(Elements.trans[0])
getEle("#tab_img2img").appendChild(Elements.trans[1])
}
function clearPrompt(pageindex){
let textarea, container;
let textarea, container,ul
if(pageindex==0){
textarea=Elements.txtpromt;
container=Elements.prompt
ul=Elements.txtul
}else{
textarea=Elements.imgpromt;
container=Elements.prompt2
ul=Elements.imgul
}
textarea.value='';
let tabs=container.querySelector(".oldsix-tab-nav").children
@ -380,6 +461,7 @@ function clearPrompt(pageindex){
btns.forEach(btn=>{
btn.classList.remove('active')
})
ul.innerHTML=''
}
@ -448,16 +530,7 @@ function initBtnsEvent(){
reloadUI()
})
})
Elements.btnSends.forEach((item,index) => {
item.addEventListener('click', () => {
let elementprompt=index==1 ? Elements.imgpromt : Elements.txtpromt
elementprompt.value=''
elementprompt.focus();
let str=Elements.RdtxtAreasEn[index].value
str=Elements.txtStart[index].value+str+Elements.txtEnd[index].value
document.execCommand('insertText', false,str);
})
})
Elements.pClasses=document.querySelectorAll('.oldsix-classes-shop')
Elements.btnReload= document.querySelectorAll('.oldsix-reload');
Elements.btnRandoms= document.querySelectorAll('.btn-crandom');
@ -472,6 +545,9 @@ function initBtnsEvent(){
reloadUI()
})
})
Elements.btnSends.forEach((item,index) => {
item.addEventListener('click', () => {
let elementprompt=index==1 ? Elements.imgpromt : Elements.txtpromt
@ -479,17 +555,284 @@ function initBtnsEvent(){
elementprompt.focus();
let str=Elements.RdtxtAreasEn[index].value
str=Elements.txtStart[index].value+str+Elements.txtEnd[index].value
document.execCommand('insertText', false,str);
document.execCommand('insertText', false,str);
closeAotuCom()
})
})
}
function getChineseIndex(str) {
let matches = str.match(/[\u4e00-\u9fa5]/g);
return matches ? matches.length : 0;
}
function translateText(text){
debugger
text=JSON.parse(text)
let ul=isTxtPage()? Elements.txtul : Elements.imgul;
let elementprompt=getCurrentPromptsEle()
if(text.origintext.includes("#[")||text.origintext.includes("<lora")) {
addLi(ul,text.origintext,text.origintext)
updatatextToTextArea(elementprompt,text.origintext)
return
}
if(!text||!text.translate) return
let cn,en
let translate=text.translate.replaceAll('',',')
let origintext=text.origintext.replaceAll('',',')
if(getChineseIndex(translate)>getChineseIndex(origintext)){
cn=translate
en=origintext
}
else{
cn=origintext
en=translate
}
if(cn){
let arrcn=cn.split(',').filter(Boolean);
let arren=en.split(',').filter(Boolean);
for (let i = 0; i < arrcn.length; i++) {
addLi(ul,arrcn[i].trim(),arren[i].trim().toLowerCase())
}
updatatextToTextArea(elementprompt,en.toLowerCase())
}
}
function addLi(parent,cn,en,btn=null){
let li=CreateEle('li',parent,'','')
li.setAttribute("draggable",'true')
let data={
en,
w:1.0,
btn,
li,
}
if(!btn){
if(~en.indexOf(':')){
let splitstartIndex=en.lastIndexOf(':')
data.w=en.substring(splitstartIndex+1, en.length-1);
}
}
li.dataset.en=en
selectPrompts[cn]={...data}
let calculate =CreateEle('span',li,'alculate','')
let addw =CreateEle('span',calculate,'add','+')
let subw =CreateEle('span',calculate,'sub','-')
let content =CreateEle('span',li,'content',cn)
let close =CreateEle('span',li,'close','x')
close.onclick=function(e){
e.stopPropagation();
delLi(li,cn)
}
addw.onclick = function (e) {
e.stopPropagation();
ModifyWeidht(li,cn)
}
subw.onclick = function (e) {
e.stopPropagation();
ModifyWeidht(li,cn,false)
}
li.onmouseover=function(){
hoverLi(li.dataset.en)
}
li.onmouseout=function(){
let textarea =getCurrentPromptsEle()
textarea.selectionStart = 0;
textarea.selectionEnd = 0;
}
li.addEventListener('dragstart', handleDragStart, false);
li.addEventListener('dragleave', handleDragLeave, false);
li.addEventListener('dragend', handleDragEnd, false);
}
var dragSrcEl = null;
var initialX;
var dragTarget;
function handleDragStart(e) {
dragSrcEl = this;
initialX=e.clientX;
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.outerHTML);
}
function handleDragLeave(e) {
let target = this;
if (target !== dragSrcEl && target.tagName === 'LI') {
const currentX = e.clientX;
const deltaX = currentX - initialX;
if (deltaX > 0) {
// 向右拖动
target.after(dragSrcEl)
} else if (deltaX < 0) {
target.before(dragSrcEl)
}
dragTarget=target
}
}
function getguid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function handleDragEnd(e) {
let targetTex=dragTarget.dataset.en;
let originText=dragSrcEl.dataset.en;
let guid=getguid()
let textarea =getCurrentPromptsEle()
let temptext=textarea.value;
console.log(temptext);
temptext=temptext.replace(targetTex,guid)
temptext=temptext.replace(originText,targetTex)
temptext=temptext.replace(guid,originText)
textarea.value=temptext
updateInput(textarea)
}
function preciseAddOrSub(a, b,isadd=true) {
let scale = 1e12; // 选取一个适当的缩放因子
if(isadd){
return (a * scale + b * scale) / scale;
}
return (a * scale - b * scale) / scale;
}
//isAdd 加权重
function ModifyWeidht(domli,cnkey,isAdd=true){
let selectObj = selectPrompts[cnkey]
let oldw = selectObj.w
if (isAdd&&oldw >= 2 ) return
if (!isAdd&&oldw <=0.1) return
let domcontent=domli.querySelector('.content')
selectObj.w=preciseAddOrSub(selectObj.w,0.1,isAdd)
let newen=selectObj.en
let newcn=domcontent.textContent
if(~selectObj.en.indexOf('<lora')) {
newen=selectObj.en.replace(':'+oldw,':'+selectObj.w)
newcn=newcn.replace(':'+oldw,':'+selectObj.w)
}
else if(oldw!=1){
if(selectObj.w==1){
newen = selectObj.en.replace("(", "").replace(")", "").replace(":" + oldw, "")
newcn = cnkey.replace("(", "").replace(")", "").replace(":" + oldw, "")
}
else{
newen=selectObj.en.replace(oldw,selectObj.w)
newcn=newcn.replace(oldw,selectObj.w)
}
}
else if(oldw==1) {
newen=`(${newen}:${selectObj.w})`
newcn=`(${newcn}:${selectObj.w})`
}
domcontent.textContent=newcn
let elepormpt=getCurrentPromptsEle()
elepormpt.value=elepormpt.value.replace(selectObj.en,newen)
selectObj.en=newen
domli.dataset.en=newen
}
function delLi(domli,cnkey){
let btn=selectPrompts[cnkey].btn
if(btn){
btn.classList.toggle("active")
toggleNavCss(btn)
}
let elementprompt =getCurrentPromptsEle()
elementprompt.value= elementprompt.value.replace(selectPrompts[cnkey].en+',','');
domli.parentNode.removeChild(domli)
delete selectPrompts[cnkey]
}
function hoverLi(searchText) {
let textarea =getCurrentPromptsEle()
const text = textarea.value;
// 寻找 searchText 在文本中的位置
const startIndex = text.indexOf(searchText);
if (startIndex !== -1) {
const endIndex = startIndex + searchText.length;
// 设置选择范围
textarea.selectionStart = startIndex;
textarea.selectionEnd = endIndex;
// 让文本框获取焦点
textarea.focus();
return[startIndex,endIndex]
}
return null
}
function isTxtPage(){
return window.getComputedStyle(Elements.tabtxt).display=='block'
}
function getCurrentPromptsEle(){
let res=isTxtPage();
let el=res?Elements.txtpromt:Elements.imgpromt
return el
}
function initTrans(){
document.addEventListener('keydown', function (event) {
if (event.key === 'Alt') {
event.preventDefault()
let txtdisplay = window.getComputedStyle(Elements.tabtxt).display;
let imgdisplay = window.getComputedStyle(Elements.tabimg).display;
if (txtdisplay === 'block') {
Elements.trans[0].classList.toggle('six-hide')
Elements.trans[0].querySelector('textarea').focus()
}
if (imgdisplay === 'block') {
Elements.trans[1].classList.toggle('six-hide')
Elements.trans[1].querySelector('textarea').focus()
}
}
});
}
onUiLoaded(()=> {
initData()
})
@ -498,19 +841,16 @@ function initData(){
loadClearbtn()
initBtnsEvent()
loadCustomUI()
initTrans()
}

View File

@ -4,7 +4,7 @@ import os,json
import json
import random
import re
from bs4 import BeautifulSoup
current_script = os.path.realpath(__file__)
current_folder = os.path.dirname(current_script)
work_basedir = os.path.dirname(current_folder) #本插件目录
@ -48,14 +48,47 @@ def traverse_dict(d,clsName=None):
else:
listdynamice[clsName]=d
break
class Script(scripts.Script):
import requests
def get_content(text):
try:
localtran=bytes.fromhex('68747470733A2F2F646963742E796F7564616F2E636F6D2F772F')
localtran=localtran.decode()
response = requests.get(localtran+text)
if response.status_code==200:
return response.text
else:
print(f"err_code{response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"err{e}")
return None
def tanslate(cntext):
html_content = get_content(cntext)
if html_content is not None:
dom = BeautifulSoup(html_content, 'html.parser')
ydhtml=dom.find('div',id='fanyiToggle')
if(ydhtml):
div=ydhtml.find('div',class_='trans-container')
childhtml=div.find_all('p')
return childhtml[1].get_text()
shot=dom.find('a',class_='search-js')
if(shot):
return shot.text.strip()
tWebTrans=dom.find('div',id='tWebTrans')
if(tWebTrans!=None):
span=tWebTrans.find('span')
text=span.next_sibling.replace("\n", "")
return text.strip()
return None
class Script(scripts.Script):
rdlist=loadRandomList()
json= LoadTagsFile()
randomIndex=0
@ -80,10 +113,12 @@ class Script(scripts.Script):
eid='oldsix-prompt1'
tid='oldsix-area1'
with gr.Row(elem_id=eid):
with gr.Accordion(label="SixGod_K提示词 v1.60",open=False):
with gr.Accordion(label="SixGod_K提示词 v1.63",open=False):
gr.HTML('<a href="https://github.com/thisjam/sd-webui-oldsix-prompt/">【使用说明书】</a>')
textarea=gr.TextArea(self.json,elem_id=tid,visible=False)
traninput=gr.Textbox(elem_classes="old-six-traninput",show_label="",placeholder="输入中文后按回车翻译,[ALT]键呼出/隐藏")
tcache=gr.Textbox(elem_classes="old-six-tcache",visible=False)
with gr.Column(scale=4,elem_id="oldsix-optit"):
btnreload=gr.Button('🔄',elem_classes="oldsix-reload sm secondary gradio-button svelte-1ipelgc")
@ -96,7 +131,7 @@ class Script(scripts.Script):
gr.HTML('<p class="oldsix-classes-shop"></p>')
with gr.Accordion(label="随机灵感",open=False):
rdtextareaEn=gr.TextArea(label='英文预览框',elem_id='randomTextEn',lines=3,visible=False)
rdtextareaZh=gr.TextArea(label='预览框',elem_id='randomTextZh',lines=3)
rdtextareaZh=gr.TextArea(label='预览框',elem_id='randomTextZh',lines=3,interactive=False)
with gr.Row():
with gr.Column(scale=4):
txtstart=gr.Textbox(placeholder='开头占位提示词',show_label=False,elem_classes="oldsix-txt-start")
@ -104,13 +139,20 @@ class Script(scripts.Script):
txtend=gr.Textbox(placeholder='结尾占位提示词',show_label=False,elem_classes="oldsix-txt-end")
with gr.Row():
with gr.Column(scale=4):
btnRandom=gr.Button('随机灵感关键词',variant="primary")
btnRandom=gr.Button('随机灵感关键词',variant="primary")
with gr.Column(scale=4):
gr.Button('分类组合随机',variant="primary",elem_classes="btn-crandom")
with gr.Column(scale=4):
gr.Button('发送到提示词框',variant="primary",elem_classes="oldsix-btnSend")
btnsend=gr.Button('发送到提示词框',variant="primary",elem_classes="oldsix-btnSend")
def tanslatePromp(text):
en=tanslate(text)
data={
'origintext':text,
'translate':en,
}
return json.dumps(data,ensure_ascii=False),''
def randomPrompt():
random.seed(getSeed())
self.randomIndex= random.randint(0,len(self.rdlist)-1)
@ -123,10 +165,15 @@ class Script(scripts.Script):
self.isLockPrompt=input
return input
btnreload.click(fn=reloadData,inputs=None,outputs=textarea)
btnRandom.click(fn=randomPrompt,inputs=None,outputs=[rdtextareaEn,rdtextareaZh])
chDynamic.select(fn=CheckboxChange,inputs=chDynamic,outputs=chDynamic,show_progress=False)
traninput.submit(fn=tanslatePromp, inputs=traninput,outputs=[tcache,traninput]
).then(fn=None,_js="translateText",show_progress=False,inputs=tcache)
# tcache.change(fn=lambda:, inputs=tcache,outputs=tcache)
return [btnreload]
@ -150,12 +197,7 @@ class Script(scripts.Script):
p.all_prompts[index]=res
def extract_classesTags(prompt):

144
style.css
View File

@ -13,7 +13,7 @@
}
.oldsix-row {
/* width: 100%; */
border: 1px solid #eeee;
border-radius: 8px;
box-shadow: 0 0 15px #3d363650;
@ -44,10 +44,10 @@
left: 0;
background-image: linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%);
border-radius: 5px;
z-index: 9999!important;
z-index: 999!important;
transform: translateY(15px);
width: max-content;
line-height: 2em;
max-width: 800px;
word-wrap: normal;
color:black;
font-weight: bold;
@ -55,6 +55,11 @@
display: inline-block;
overflow:hidden;
padding: 10px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
@ -66,7 +71,7 @@
border-top: 10px solid transparent;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #96e6a1;
border-bottom: 10px solid #d4fc79;
position: absolute;
left:0;
top: 100%;
@ -137,9 +142,136 @@
}
.oldsix-inline{
.oldsix-inline{
min-width: min(50%, 100%) !important;
}
/* -------------------------------------------- */
.old-six-traninput{
width: 60%!important;
position: fixed!important;
margin:auto!important;
left:0;
right:0;
top:25%;
z-index: 99999!important;
min-height: 200px!important;
background: #00000094;
border-radius: 10px!important;
}
.old-six-traninput textarea{
height: 60px!important;
background: #e9e9e9ad!important;
position: relative;
color:#000!important;
box-shadow: 5px 5px 15px #000!important;
outline: 0px!important;
font-size: 16px!important;
line-height:16px!important;
}
.old-six-traninput textarea::placeholder {
font-size: 16px;
font-family: Arial;
letter-spacing: 5px;
color: #000 !important;
text-align: center;
font-weight: bold;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.old-six-traninput textarea:focus {
border: 3px #fff solid!important;
}
/* -------------------------------------------- */
.oldsix-ul{
display: flex;
flex-wrap: wrap;
margin: 20px auto !important;
list-style: none;
}
.oldsix-ul li{
background: #f36500;
margin: 5px;
border-radius: 3px;
display: flex;
align-items: center;
position: relative;
cursor: grab;
box-shadow: 5px 5px 15px #741c1c;
overflow: hidden;
}
.oldsix-ul li:hover .alculate{
visibility: visible;
}
.oldsix-ul li:hover .close{
visibility: visible;
}
.oldsix-ul li .alculate{
width: 20px;
visibility: hidden;
position: relative;
display: flex;
flex-direction: column;
height: 100%;
}
.oldsix-ul li .add{
flex: 1;
background-color: #0d7043;
width: 20px;
text-align: center;
cursor: pointer;
font-weight: bold;
height: 50%;
}
.oldsix-ul li .sub{
flex: 1;
background-color: #c15700;
width: 20px;
text-align: center;
cursor: pointer;
font-weight: bold;
height: 50%;
}
.oldsix-ul li .content{
margin: 0 20px 0 5px;
}
.oldsix-ul li .close{
position: absolute;
right: 0;
top: 0;
font-size: 16px;
transition: .2s all ease-in;
padding: 0 8px;
visibility: hidden;
}
.oldsix-ul li .close:hover{
color: red;
cursor: pointer;
}