mirror of
https://github.com/easydiffusion/easydiffusion.git
synced 2025-08-16 11:09:00 +02:00
Lora Manager
This commit is contained in:
@ -511,10 +511,10 @@ function showImages(reqBody, res, outputContainer, livePreview) {
|
||||
{ text: "Upscale", on_click: onUpscaleClick },
|
||||
{ text: "Fix Faces", on_click: onFixFacesClick },
|
||||
],
|
||||
{
|
||||
{
|
||||
text: "Use as Thumbnail",
|
||||
on_click: onUseAsThumbnailClick,
|
||||
filter: (req, img) => "use_embeddings_model" in req,
|
||||
filter: (req, img) => "use_embeddings_model" in req || "use_lora_model" in req
|
||||
},
|
||||
]
|
||||
|
||||
@ -748,25 +748,45 @@ function onUseAsThumbnailClick(req, img) {
|
||||
onUseAsThumbnailClick.croppr.setImage(img.src)
|
||||
}
|
||||
|
||||
let embeddings = req.use_embeddings_model.map((e) => e.split("/").pop())
|
||||
let LORA = []
|
||||
useAsThumbSelect.innerHTML=""
|
||||
|
||||
if ("use_lora_model" in req) {
|
||||
LORA = req.use_lora_model
|
||||
if ("use_embeddings_model" in req) {
|
||||
let embeddings = req.use_embeddings_model.map((e) => e.split("/").pop())
|
||||
|
||||
let embOptions = document.createElement("optgroup")
|
||||
embOptions.label = "Embeddings"
|
||||
embOptions.replaceChildren(
|
||||
...embeddings.map((e) => {
|
||||
let option = document.createElement("option")
|
||||
option.innerText = e
|
||||
option.dataset["type"] = "embeddings"
|
||||
return option
|
||||
})
|
||||
)
|
||||
useAsThumbSelect.appendChild(embOptions)
|
||||
}
|
||||
|
||||
let optgroup = document.createElement("optgroup")
|
||||
optgroup.label = "Embeddings"
|
||||
optgroup.replaceChildren(
|
||||
...embeddings.map((e) => {
|
||||
let option = document.createElement("option")
|
||||
option.innerText = e
|
||||
option.dataset["type"] = "embeddings"
|
||||
return option
|
||||
})
|
||||
)
|
||||
|
||||
useAsThumbSelect.replaceChildren(optgroup)
|
||||
if ("use_lora_model" in req) {
|
||||
let LORA = req.use_lora_model
|
||||
if (typeof LORA == "string") {
|
||||
LORA = [LORA]
|
||||
}
|
||||
LORA = LORA.map((e) => e.split("/").pop())
|
||||
|
||||
let loraOptions = document.createElement("optgroup")
|
||||
loraOptions.label = "LORA"
|
||||
loraOptions.replaceChildren(
|
||||
...LORA.map((e) => {
|
||||
let option = document.createElement("option")
|
||||
option.innerText = e
|
||||
option.dataset["type"] = "lora"
|
||||
return option
|
||||
})
|
||||
)
|
||||
useAsThumbSelect.appendChild(loraOptions)
|
||||
}
|
||||
|
||||
useAsThumbDialog.showModal()
|
||||
onUseAsThumbnailClick.scale = scale
|
||||
}
|
||||
@ -782,6 +802,50 @@ useAsThumbCancelBtn.addEventListener("click", () => {
|
||||
useAsThumbDialog.close()
|
||||
})
|
||||
|
||||
const Bucket = {
|
||||
upload(path, blob) {
|
||||
const formData = new FormData()
|
||||
formData.append("file", blob)
|
||||
return fetch(`bucket/${path}`, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
})
|
||||
},
|
||||
|
||||
getImageAsDataURL(path) {
|
||||
return fetch(`bucket/${path}`)
|
||||
.then((response) => {
|
||||
if (response.status == 200) {
|
||||
return response.blob()
|
||||
} else {
|
||||
throw new Error("Bucket error")
|
||||
}
|
||||
})
|
||||
.then((blob) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => resolve(reader.result)
|
||||
reader.onerror = reject
|
||||
reader.readAsDataURL(blob)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
getList(path) {
|
||||
return fetch(`bucket/${path}`)
|
||||
.then((response) => (response.status == 200 ? response.json() : []))
|
||||
},
|
||||
|
||||
store(path, data) {
|
||||
return Bucket.upload(`${path}.json`, JSON.stringify(data))
|
||||
},
|
||||
|
||||
retrieve(path) {
|
||||
return fetch(`bucket/${path}.json`)
|
||||
.then((response) => (response.status == 200 ? response.json() : null))
|
||||
},
|
||||
}
|
||||
|
||||
useAsThumbSaveBtn.addEventListener("click", (e) => {
|
||||
let scale = 1 / onUseAsThumbnailClick.scale
|
||||
let crop = onUseAsThumbnailClick.croppr.getValue()
|
||||
@ -793,16 +857,11 @@ useAsThumbSaveBtn.addEventListener("click", (e) => {
|
||||
.then((thumb) => fetch(thumb))
|
||||
.then((response) => response.blob())
|
||||
.then(async function(blob) {
|
||||
const formData = new FormData()
|
||||
formData.append("file", blob)
|
||||
let options = useAsThumbSelect.selectedOptions
|
||||
let promises = []
|
||||
for (let embedding of options) {
|
||||
promises.push(
|
||||
fetch(`bucket/${profileName}/${embedding.dataset["type"]}/${embedding.value}.png`, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
})
|
||||
Bucket.upload(`${profileName}/${embedding.dataset["type"]}/${embedding.value}.png`, blob)
|
||||
)
|
||||
}
|
||||
return Promise.all(promises)
|
||||
@ -2362,20 +2421,10 @@ function loadThumbnailImageFromFile() {
|
||||
}
|
||||
|
||||
function updateEmbeddingsList(filter = "") {
|
||||
function html(model, iconlist = [], prefix = "", filter = "") {
|
||||
function html(model, iconMap = {}, prefix = "", filter = "") {
|
||||
filter = filter.toLowerCase()
|
||||
let toplevel = document.createElement("div")
|
||||
let folders = document.createElement("div")
|
||||
let embIcon = Object.assign(
|
||||
{},
|
||||
...iconlist.map((x) => ({
|
||||
[x
|
||||
.toLowerCase()
|
||||
.split(".")
|
||||
.slice(0, -1)
|
||||
.join(".")]: x,
|
||||
}))
|
||||
)
|
||||
|
||||
let profileName = profileNameField.value
|
||||
model?.forEach((m) => {
|
||||
@ -2383,13 +2432,9 @@ function updateEmbeddingsList(filter = "") {
|
||||
let token = m.toLowerCase()
|
||||
if (token.search(filter) != -1) {
|
||||
let button
|
||||
// if (iconlist.length==0) {
|
||||
// button = document.createElement("button")
|
||||
// button.innerText = m
|
||||
// } else {
|
||||
let img = "/media/images/noimg.png"
|
||||
if (token in embIcon) {
|
||||
img = `/bucket/${profileName}/embeddings/${embIcon[token]}`
|
||||
if (token in iconMap) {
|
||||
img = `/bucket/${profileName}/${iconMap[token]}`
|
||||
}
|
||||
button = createModifierCard(m, [img, img], true)
|
||||
// }
|
||||
@ -2398,7 +2443,7 @@ function updateEmbeddingsList(filter = "") {
|
||||
toplevel.appendChild(button)
|
||||
}
|
||||
} else {
|
||||
let subdir = html(m[1], iconlist, prefix + m[0] + "/", filter)
|
||||
let subdir = html(m[1], iconMap, prefix + m[0] + "/", filter)
|
||||
if (typeof subdir == "object") {
|
||||
let div1 = document.createElement("div")
|
||||
let div2 = document.createElement("div")
|
||||
@ -2457,11 +2502,44 @@ function updateEmbeddingsList(filter = "") {
|
||||
</div>
|
||||
`
|
||||
|
||||
let loraTokens = []
|
||||
let profileName = profileNameField.value
|
||||
fetch(`/bucket/${profileName}/embeddings/`)
|
||||
.then((response) => (response.status == 200 ? response.json() : []))
|
||||
.then(async function(iconlist) {
|
||||
embeddingsList.replaceChildren(html(modelsOptions.embeddings, iconlist, "", filter))
|
||||
let iconMap = {}
|
||||
|
||||
Bucket.getList(`${profileName}/embeddings/`)
|
||||
.then((icons) => {
|
||||
iconMap = Object.assign(
|
||||
{},
|
||||
...icons.map((x) => ({
|
||||
[x
|
||||
.toLowerCase()
|
||||
.split(".")
|
||||
.slice(0, -1)
|
||||
.join(".")]: `embeddings/${x}`,
|
||||
}))
|
||||
)
|
||||
|
||||
return Bucket.getList(`${profileName}/lora/`)
|
||||
})
|
||||
.then(async function (icons) {
|
||||
for (let lora of loraModelField.value.modelNames) {
|
||||
let keywords = await getLoraKeywords(lora)
|
||||
loraTokens = loraTokens.concat(keywords)
|
||||
let loraname = lora.split("/").pop()
|
||||
|
||||
if (icons.includes(`${loraname}.png`)) {
|
||||
keywords.forEach((kw) => {
|
||||
iconMap[kw.toLowerCase()] = `lora/${loraname}.png`
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let tokenList = [...modelsOptions.embeddings]
|
||||
if (loraTokens.length != 0) {
|
||||
tokenList.unshift(['LORA Keywords', loraTokens])
|
||||
}
|
||||
embeddingsList.replaceChildren(html(tokenList, iconMap, "", filter))
|
||||
createCollapsibles(embeddingsList)
|
||||
if (filter != "") {
|
||||
embeddingsExpandAll()
|
||||
|
Reference in New Issue
Block a user