mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-22 06:37:00 +08:00
add downloading features
This commit is contained in:
parent
4f8ebfa9e3
commit
82ef4c9ae1
51
gcs_utils.py
Normal file
51
gcs_utils.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from google.cloud import storage
|
||||||
|
|
||||||
|
|
||||||
|
def extract_bucket_name_and_path(uri: str) -> tuple[str, str]:
|
||||||
|
if not uri.startswith("gs://"):
|
||||||
|
raise ValueError("The uri must follow gs://{bucket-name}/{path}")
|
||||||
|
try:
|
||||||
|
bucket_name, path = uri.split("gs://")[1].split("/", 1)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError("The uri must follow gs://{bucket-name}/{path}")
|
||||||
|
return bucket_name, path
|
||||||
|
|
||||||
|
|
||||||
|
def get_bucket_and_path(gcs_client: storage.Client, uri: str) -> (storage.Bucket, str, bool):
|
||||||
|
"""
|
||||||
|
Get the bucket and path from the uri.
|
||||||
|
:return: The bucket, path and a boolean indicating which is true if the path is a file
|
||||||
|
"""
|
||||||
|
bucket_name, path = extract_bucket_name_and_path(uri)
|
||||||
|
bucket = gcs_client.bucket(bucket_name)
|
||||||
|
blob = bucket.blob(path)
|
||||||
|
if blob.exists():
|
||||||
|
return bucket, path, True
|
||||||
|
|
||||||
|
prefix = path if path.endswith("/") else path + "/"
|
||||||
|
iterator = gcs_client.list_blobs(bucket, prefix=prefix, delimiter="/")
|
||||||
|
blobs = list(iterator)
|
||||||
|
prefixes = list(iterator.prefixes)
|
||||||
|
|
||||||
|
if blobs or prefixes:
|
||||||
|
return bucket, path, False
|
||||||
|
|
||||||
|
raise RuntimeError(f"The uri '{uri}' does not exist")
|
||||||
|
|
||||||
|
|
||||||
|
def get_bucket_and_file_path(gcs_client: storage.Client, uri: str):
|
||||||
|
bucket, file_path, is_file = get_bucket_and_path(gcs_client, uri)
|
||||||
|
if is_file:
|
||||||
|
return bucket, file_path
|
||||||
|
else:
|
||||||
|
raise ValueError(f"The uri '{uri}' is a directory, not a file")
|
||||||
|
|
||||||
|
def download_gcs_file(gcs_client: storage.Client, uri: str, target_file_path: str):
|
||||||
|
bucket, file_path = get_bucket_and_file_path(gcs_client, uri)
|
||||||
|
blob = bucket.blob(file_path)
|
||||||
|
blob.download_to_filename(target_file_path)
|
||||||
|
|
||||||
|
if not os.path.exists(target_file_path):
|
||||||
|
raise ValueError(f"File not downloaded to {target_file_path}")
|
||||||
@ -11,3 +11,6 @@ scipy
|
|||||||
tqdm
|
tqdm
|
||||||
psutil
|
psutil
|
||||||
kornia>=0.7.1
|
kornia>=0.7.1
|
||||||
|
|
||||||
|
# treat-ai specific terms
|
||||||
|
google-cloud-storage
|
||||||
55
server.py
55
server.py
@ -14,6 +14,8 @@ import struct
|
|||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
from PIL.PngImagePlugin import PngInfo
|
from PIL.PngImagePlugin import PngInfo
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from google.cloud import storage
|
||||||
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -25,6 +27,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 gcs_utils import download_gcs_file
|
||||||
|
|
||||||
class BinaryEventTypes:
|
class BinaryEventTypes:
|
||||||
PREVIEW_IMAGE = 1
|
PREVIEW_IMAGE = 1
|
||||||
@ -89,6 +92,13 @@ class PromptServer():
|
|||||||
self.client_id = None
|
self.client_id = None
|
||||||
|
|
||||||
self.on_prompt_handlers = []
|
self.on_prompt_handlers = []
|
||||||
|
self.gcs_client = storage.Client()
|
||||||
|
self.weight_type2path = {
|
||||||
|
"lora": "./models/loras",
|
||||||
|
"base": "./models/checkpoints",
|
||||||
|
"controlnet": "./models/controlnet",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@routes.get('/ws')
|
@routes.get('/ws')
|
||||||
async def websocket_handler(request):
|
async def websocket_handler(request):
|
||||||
@ -196,6 +206,46 @@ class PromptServer():
|
|||||||
else:
|
else:
|
||||||
return web.Response(status=400)
|
return web.Response(status=400)
|
||||||
|
|
||||||
|
def fetch_weight(post):
|
||||||
|
weight_url = post.get("weight_url")
|
||||||
|
weight_type = post.get("weight_type")
|
||||||
|
|
||||||
|
if weight_type in self.weight_type2path:
|
||||||
|
target_path = self.weight_type2path[weight_type]
|
||||||
|
try:
|
||||||
|
download_gcs_file(
|
||||||
|
gcs_client=self.gcs_client,
|
||||||
|
uri=weight_url,
|
||||||
|
target_file_path=target_path,
|
||||||
|
)
|
||||||
|
return web.json_response(
|
||||||
|
data = {
|
||||||
|
"weight_type": weight_type,
|
||||||
|
"weight_url": weight_url,
|
||||||
|
"target_path": target_path,
|
||||||
|
"download_status": "success",
|
||||||
|
},
|
||||||
|
status=200,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return web.json_response(
|
||||||
|
data = {
|
||||||
|
"weight_type": weight_type,
|
||||||
|
"weight_url": weight_url,
|
||||||
|
"target_path": target_path,
|
||||||
|
"download_status": f"Failed with exception: {e}",
|
||||||
|
},
|
||||||
|
status=400,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return web.Response(
|
||||||
|
status=400,
|
||||||
|
text=f"Invalid weight_type: {weight_type}."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@routes.post("/upload/image")
|
@routes.post("/upload/image")
|
||||||
async def upload_image(request):
|
async def upload_image(request):
|
||||||
post = await request.post()
|
post = await request.post()
|
||||||
@ -356,6 +406,11 @@ class PromptServer():
|
|||||||
return web.Response(status=404)
|
return web.Response(status=404)
|
||||||
return web.json_response(dt["__metadata__"])
|
return web.json_response(dt["__metadata__"])
|
||||||
|
|
||||||
|
@routes.post("/fetch_weight/{}")
|
||||||
|
async def upload_weight(request):
|
||||||
|
post = await request.post()
|
||||||
|
return fetch_weight(post)
|
||||||
|
|
||||||
@routes.get("/system_stats")
|
@routes.get("/system_stats")
|
||||||
async def get_queue(request):
|
async def get_queue(request):
|
||||||
device = comfy.model_management.get_torch_device()
|
device = comfy.model_management.get_torch_device()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user