add downloading features

This commit is contained in:
Ruixi Fan 2024-04-15 12:20:49 -07:00
parent 4f8ebfa9e3
commit 82ef4c9ae1
No known key found for this signature in database
GPG Key ID: C6112CB66F7DED6C
3 changed files with 109 additions and 0 deletions

51
gcs_utils.py Normal file
View 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}")

View File

@ -11,3 +11,6 @@ scipy
tqdm
psutil
kornia>=0.7.1
# treat-ai specific terms
google-cloud-storage

View File

@ -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()