mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-21 23:57:02 +08:00
add file conversion
This commit is contained in:
parent
0f0340c0ca
commit
6eb236df56
12
conversion_utils.py
Normal file
12
conversion_utils.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from safetensors.torch import load_file, save_file
|
||||||
|
|
||||||
|
from diffusers.utils import convert_all_state_dict_to_peft, convert_state_dict_to_kohya
|
||||||
|
|
||||||
|
|
||||||
|
def lora_convert_and_save(input_lora, output_lora):
|
||||||
|
diffusers_state_dict = load_file(input_lora)
|
||||||
|
peft_state_dict = convert_all_state_dict_to_peft(diffusers_state_dict)
|
||||||
|
kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
|
||||||
|
save_file(kohya_state_dict, output_lora)
|
||||||
25
server.py
25
server.py
@ -1,6 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import tempfile
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
import nodes
|
import nodes
|
||||||
@ -27,6 +29,7 @@ import comfy.utils
|
|||||||
import comfy.model_management
|
import comfy.model_management
|
||||||
|
|
||||||
from app.user_manager import UserManager
|
from app.user_manager import UserManager
|
||||||
|
from conversion_utils import lora_convert_and_save
|
||||||
from gcs_utils import download_gcs_file
|
from gcs_utils import download_gcs_file
|
||||||
|
|
||||||
class BinaryEventTypes:
|
class BinaryEventTypes:
|
||||||
@ -210,20 +213,39 @@ class PromptServer():
|
|||||||
weight_url = post.get("weight_url")
|
weight_url = post.get("weight_url")
|
||||||
weight_type = post.get("weight_type")
|
weight_type = post.get("weight_type")
|
||||||
local_file_name = post.get("local_file_name", "treat_weight.safetensors")
|
local_file_name = post.get("local_file_name", "treat_weight.safetensors")
|
||||||
|
convert_weight = post.get("convert_weight", True)
|
||||||
|
|
||||||
|
|
||||||
if weight_type in self.weight_type2path:
|
if weight_type in self.weight_type2path:
|
||||||
target_path = self.weight_type2path[weight_type]
|
target_path = self.weight_type2path[weight_type]
|
||||||
try:
|
try:
|
||||||
|
with tempfile.TemporaryDirectory as temp_dir:
|
||||||
|
temp_save_path = os.path.join(temp_dir, local_file_name)
|
||||||
|
final_save_path = os.path.join(target_path, local_file_name)
|
||||||
|
logging.info(f"Downloading file to {temp_save_path}.")
|
||||||
|
logging.info(f"Final save path to {final_save_path}")
|
||||||
download_gcs_file(
|
download_gcs_file(
|
||||||
gcs_client=self.gcs_client,
|
gcs_client=self.gcs_client,
|
||||||
uri=weight_url,
|
uri=weight_url,
|
||||||
target_file_path=os.path.join(target_path, local_file_name),
|
target_file_path=temp_save_path,
|
||||||
)
|
)
|
||||||
|
if weight_type and convert_weight == "lora":
|
||||||
|
lora_convert_and_save(
|
||||||
|
input_lora=temp_save_path,
|
||||||
|
output_lora=final_save_path,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
shutil.move(
|
||||||
|
src=temp_save_path,
|
||||||
|
dst=final_save_path,
|
||||||
|
)
|
||||||
|
|
||||||
return web.json_response(
|
return web.json_response(
|
||||||
data = {
|
data = {
|
||||||
"weight_type": weight_type,
|
"weight_type": weight_type,
|
||||||
"weight_url": weight_url,
|
"weight_url": weight_url,
|
||||||
"target_path": target_path,
|
"target_path": target_path,
|
||||||
|
"convert_weight": convert_weight,
|
||||||
"download_status": "success",
|
"download_status": "success",
|
||||||
},
|
},
|
||||||
status=200,
|
status=200,
|
||||||
@ -234,6 +256,7 @@ class PromptServer():
|
|||||||
"weight_type": weight_type,
|
"weight_type": weight_type,
|
||||||
"weight_url": weight_url,
|
"weight_url": weight_url,
|
||||||
"target_path": target_path,
|
"target_path": target_path,
|
||||||
|
"convert_weight": convert_weight,
|
||||||
"download_status": f"Failed with exception: {e}",
|
"download_status": f"Failed with exception: {e}",
|
||||||
},
|
},
|
||||||
status=400,
|
status=400,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user