From 82ef4c9ae190be908484fc59267a9e1beb9534c8 Mon Sep 17 00:00:00 2001 From: Ruixi Fan Date: Mon, 15 Apr 2024 12:20:49 -0700 Subject: [PATCH] add downloading features --- gcs_utils.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ server.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 gcs_utils.py diff --git a/gcs_utils.py b/gcs_utils.py new file mode 100644 index 000000000..99f3d94d8 --- /dev/null +++ b/gcs_utils.py @@ -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}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e7d8c0e9c..d4e3aa731 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,6 @@ scipy tqdm psutil kornia>=0.7.1 + +# treat-ai specific terms +google-cloud-storage \ No newline at end of file diff --git a/server.py b/server.py index 5642bd5e2..f09338472 100644 --- a/server.py +++ b/server.py @@ -14,6 +14,8 @@ import struct from PIL import Image, ImageOps from PIL.PngImagePlugin import PngInfo from io import BytesIO +from google.cloud import storage + import aiohttp from aiohttp import web @@ -25,6 +27,7 @@ import comfy.utils import comfy.model_management from app.user_manager import UserManager +from gcs_utils import download_gcs_file class BinaryEventTypes: PREVIEW_IMAGE = 1 @@ -89,6 +92,13 @@ class PromptServer(): self.client_id = None self.on_prompt_handlers = [] + self.gcs_client = storage.Client() + self.weight_type2path = { + "lora": "./models/loras", + "base": "./models/checkpoints", + "controlnet": "./models/controlnet", + } + @routes.get('/ws') async def websocket_handler(request): @@ -196,6 +206,46 @@ class PromptServer(): else: 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") async def upload_image(request): post = await request.post() @@ -356,6 +406,11 @@ class PromptServer(): return web.Response(status=404) 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") async def get_queue(request): device = comfy.model_management.get_torch_device()