Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc of use js front end to get loras list #156

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions js/share_lora_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { api } from "../../../scripts/api.js";
import { app } from "../../scripts/app.js";
app.registerExtension({
name: "bizyair.siliconcloud.share.lora.loader",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (nodeData.name === "BizyAir_SharedLoraLoader") {
async function onTextChange(share_id, canvas, comfynode) {
if(share_id.length === 25 && share_id.startsWith("clx")){
const response = await api.fetchApi("/bizyair/shareloras", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ share_id: share_id }),
});
const loras_list = await response.json()
const lora_name_widget = comfynode.widgets.find(widget => widget.name === "lora_name");
lora_name_widget.value = loras_list[0]
lora_name_widget.options.values = loras_list
}
}

function setWigetCallback(){
const shareid_widget = this.widgets.find(widget => widget.name === "share_id");

if (shareid_widget) {
shareid_widget.callback = onTextChange;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前端的主要作用,是通过设置 shareid 那个文本框的回调,监控文本改变事件。

然后在文本改变的事件里,像后端发请求,得到 loras list

} else {
console.log("share_id widget not found");
}

}
const onNodeCreated = nodeType.prototype.onNodeCreated
nodeType.prototype.onNodeCreated = function () {
onNodeCreated?.apply(this, arguments);
setWigetCallback.call(this, arguments);
};
}
},
})
2 changes: 1 addition & 1 deletion nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def INPUT_TYPES(s):
return {
"required": {
"share_id": ("STRING", {"default": "share_id"}),
"lora_name": ("STRING", {"default": "lora_name"}),
"lora_name": (folder_paths.get_filename_list("loras"),),
"model": (data_types.MODEL,),
"clip": (data_types.CLIP,),
"strength_model": (
Expand Down
20 changes: 20 additions & 0 deletions showcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ async def get_file_content(request):
status=500,
content_type="application/json",
)


@PromptServer.instance.routes.post("/bizyair/shareloras")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段添加后端的代码,是临时放在这的。请根据需要挪到合适的地方。

async def get_share_loras(request):
try:
data = await request.json()
except json.JSONDecodeError:
return web.Response(
text=json.dumps({"error": "Invalid JSON body"}),
status=400,
content_type="application/json",
)

share_id = data.get("share_id")
print(f"use {share_id} to get the loras_list...")
loras_list = ["flux/lora1.safetensors", "flux/lora2.safetensors"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 loras_list 应该来自网络请求。 loras_list 建议放上一个默认的美甲模型,使得 loras_list 的长度一定是 >=1 的,这样前端的逻辑就完全不用改了

return web.Response(
text=json.dumps(loras_list, ensure_ascii=False),
content_type="application/json",
)
Loading