Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 68d8ce4bfd | |||
| 138fc3ac47 | |||
| 222ac2a0dd | |||
| 40aed9b5f8 | |||
| d4d36bf86e | |||
| e839cfc6e8 | |||
| f04138e13b | |||
| de3aa87c8f |
+1
-1
@@ -66,7 +66,7 @@ class AuthData:
|
|||||||
"""data used to authenticate requester"""
|
"""data used to authenticate requester"""
|
||||||
|
|
||||||
cost: str
|
cost: str
|
||||||
endpoint: str
|
endpoint_id: int
|
||||||
reqnum: int
|
reqnum: int
|
||||||
request_idx: int
|
request_idx: int
|
||||||
signature: str
|
signature: str
|
||||||
|
|||||||
+7
-3
@@ -75,6 +75,7 @@ def print_truncate_res(res: str):
|
|||||||
@dataclass
|
@dataclass
|
||||||
class ClientState:
|
class ClientState:
|
||||||
endpoint_group_name: str
|
endpoint_group_name: str
|
||||||
|
endpoint_id: int
|
||||||
api_key: str
|
api_key: str
|
||||||
server_url: str
|
server_url: str
|
||||||
worker_endpoint: str
|
worker_endpoint: str
|
||||||
@@ -95,7 +96,7 @@ class ClientState:
|
|||||||
self.status = ClientStatus.Error
|
self.status = ClientStatus.Error
|
||||||
return
|
return
|
||||||
route_payload = {
|
route_payload = {
|
||||||
"endpoint": self.endpoint_group_name,
|
"endpoint_id": self.endpoint_id,
|
||||||
"api_key": self.api_key,
|
"api_key": self.api_key,
|
||||||
"cost": self.payload.count_workload(),
|
"cost": self.payload.count_workload(),
|
||||||
}
|
}
|
||||||
@@ -244,16 +245,19 @@ def run_test(
|
|||||||
print_thread = threading.Thread(target=print_state, args=(clients, num_requests))
|
print_thread = threading.Thread(target=print_state, args=(clients, num_requests))
|
||||||
print_thread.daemon = True # makes threads get killed on program exit
|
print_thread.daemon = True # makes threads get killed on program exit
|
||||||
print_thread.start()
|
print_thread.start()
|
||||||
endpoint_api_key = Endpoint.get_endpoint_api_key(
|
endpoint_info = Endpoint.get_endpoint_info(
|
||||||
endpoint_name=endpoint_group_name, account_api_key=api_key, instance=instance
|
endpoint_name=endpoint_group_name, account_api_key=api_key, instance=instance
|
||||||
)
|
)
|
||||||
if not endpoint_api_key:
|
if not endpoint_info:
|
||||||
log.debug(f"Endpoint {endpoint_group_name} not found for API key")
|
log.debug(f"Endpoint {endpoint_group_name} not found for API key")
|
||||||
return
|
return
|
||||||
|
endpoint_id = endpoint_info["id"]
|
||||||
|
endpoint_api_key = endpoint_info["api_key"]
|
||||||
try:
|
try:
|
||||||
for _ in range(num_requests):
|
for _ in range(num_requests):
|
||||||
client = ClientState(
|
client = ClientState(
|
||||||
endpoint_group_name=endpoint_group_name,
|
endpoint_group_name=endpoint_group_name,
|
||||||
|
endpoint_id=endpoint_id,
|
||||||
api_key=endpoint_api_key,
|
api_key=endpoint_api_key,
|
||||||
server_url=server_url,
|
server_url=server_url,
|
||||||
worker_endpoint=worker_endpoint,
|
worker_endpoint=worker_endpoint,
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
# ComfyUI PyWorker
|
# ComfyUI PyWorker
|
||||||
|
|
||||||
This is the base PyWorker for ComfyUI. It provides a unified interface for running any ComfyUI workflow through a proxy-based architecture.
|
This is the base PyWorker for ComfyUI. It provides a unified interface for running any ComfyUI workflow through a proxy-based architecture. See the [Serverless documentation](https://docs.vast.ai/serverless) for guides and how-to's.
|
||||||
|
|
||||||
The cost for each request has a static value of `1`. ComfyUI does not handle concurrent workloads and there is no current provision to load multiple instances of ComfyUI per worker node.
|
The cost for each request has a static value of `1`. ComfyUI does not handle concurrent workloads and there is no current provision to load multiple instances of ComfyUI per worker node.
|
||||||
|
|
||||||
|
## Instance Setup
|
||||||
|
|
||||||
|
1. Pick a template
|
||||||
|
|
||||||
|
- [ComfyUI (Serverless)](https://cloud.vast.ai/?ref_id=62897&creator_id=62897&name=ComfyUI%20(Serverless))
|
||||||
|
|
||||||
|
2. Follow the [getting started guide](https://docs.vast.ai/documentation/serverless/quickstart) for help with configuring your serverless setup. For testing, we recommend that you use the default options presented by the web interface.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
@@ -10,6 +18,88 @@ This worker requires both [ComfyUI](https://github.com/comfyanonymous/ComfyUI) a
|
|||||||
|
|
||||||
A docker image is provided but you may use any if the above requirements are met.
|
A docker image is provided but you may use any if the above requirements are met.
|
||||||
|
|
||||||
|
## Client
|
||||||
|
|
||||||
|
The client demonstrates how to use the Vast Serverless SDK to generate images, save them locally, and optionally upload to S3-compatible storage.
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
1. Clone the PyWorker repository to your local machine and install the necessary requirements for running the test client.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/vast-ai/pyworker
|
||||||
|
cd pyworker
|
||||||
|
pip install uv
|
||||||
|
uv venv -p 3.12
|
||||||
|
source .venv/bin/activate
|
||||||
|
uv pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Set your API key:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export VAST_API_KEY=<your_api_key>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Default prompt
|
||||||
|
python -m workers.comfyui-json.client
|
||||||
|
|
||||||
|
# Custom prompt
|
||||||
|
python -m workers.comfyui-json.client --prompt "a cat sitting on a rainbow"
|
||||||
|
|
||||||
|
# With options
|
||||||
|
python -m workers.comfyui-json.client --prompt "sunset" --width 1024 --height 1024 --steps 30
|
||||||
|
|
||||||
|
# Using a custom workflow file
|
||||||
|
python -m workers.comfyui-json.client --workflow my_workflow.json
|
||||||
|
|
||||||
|
# With S3 upload
|
||||||
|
python -m workers.comfyui-json.client --s3
|
||||||
|
```
|
||||||
|
|
||||||
|
### CLI Flags
|
||||||
|
|
||||||
|
| Flag | Default | Description |
|
||||||
|
|------|---------|-------------|
|
||||||
|
| `--endpoint` | `my-comfyui-endpoint` | Vast endpoint name |
|
||||||
|
| `--prompt` | (default) | Text prompt for image generation |
|
||||||
|
| `--workflow` | (none) | Path to custom workflow JSON file |
|
||||||
|
| `--width` | 512 | Image width in pixels |
|
||||||
|
| `--height` | 512 | Image height in pixels |
|
||||||
|
| `--steps` | 20 | Number of denoising steps |
|
||||||
|
| `--seed` | (random) | Random seed for reproducibility |
|
||||||
|
| `--s3` | (disabled) | Upload generated images to S3 |
|
||||||
|
|
||||||
|
### Output
|
||||||
|
|
||||||
|
Images are saved to `./generated_images/comfy_{seed}.png`.
|
||||||
|
|
||||||
|
### S3 Upload (Optional)
|
||||||
|
|
||||||
|
You can optionally upload generated images to an S3-compatible storage service (AWS S3, Cloudflare R2, Backblaze B2, etc.) by using the `--s3` flag.
|
||||||
|
|
||||||
|
**1. Set environment variables:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export S3_ENDPOINT_URL="https://your-account.r2.cloudflarestorage.com"
|
||||||
|
export S3_BUCKET_NAME="my-bucket"
|
||||||
|
export S3_ACCESS_KEY_ID="your-access-key-id"
|
||||||
|
export S3_SECRET_ACCESS_KEY="your-secret-access-key"
|
||||||
|
```
|
||||||
|
|
||||||
|
**2. Run with S3 upload enabled:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m workers.comfyui-json.client --prompt "a beautiful landscape" --s3
|
||||||
|
```
|
||||||
|
|
||||||
|
Images will be saved locally AND uploaded to `s3://{bucket}/comfyui/{filename}`.
|
||||||
|
|
||||||
|
**Note:** Requires `boto3` (`pip install boto3`).
|
||||||
|
|
||||||
## Benchmarking
|
## Benchmarking
|
||||||
|
|
||||||
### Custom Benchmark Workflows
|
### Custom Benchmark Workflows
|
||||||
@@ -212,11 +302,3 @@ WEBHOOK_TIMEOUT=30 # Webhook timeout in seconds
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Client Libraries
|
|
||||||
|
|
||||||
See the test client examples for implementation details on how to integrate with the ComfyUI worker.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
See Vast's serverless documentation for more details on how to use ComfyUI with autoscaler.
|
|
||||||
+301
-24
@@ -1,35 +1,312 @@
|
|||||||
from .data_types import count_workload
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
import random
|
import random
|
||||||
import asyncio
|
import asyncio
|
||||||
import random
|
import logging
|
||||||
|
import argparse
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
from vastai import Serverless
|
from vastai import Serverless
|
||||||
|
|
||||||
async def main():
|
# ---------------------- Config ----------------------
|
||||||
async with Serverless() as client:
|
DEFAULT_PROMPT = "a beautiful sunset over mountains, digital art, highly detailed"
|
||||||
endpoint = await client.get_endpoint(name="my-comfy-endpoint") # Change this to your endpoint name
|
ENDPOINT_NAME = "my-comfyui-endpoint"
|
||||||
|
DEFAULT_WIDTH = 512
|
||||||
|
DEFAULT_HEIGHT = 512
|
||||||
|
DEFAULT_STEPS = 20
|
||||||
|
COST = 100 # Fixed cost for ComfyUI requests
|
||||||
|
|
||||||
payload = {
|
# Optional S3 Configuration (from environment variables)
|
||||||
"input": {
|
S3_ENDPOINT_URL = os.getenv("S3_ENDPOINT_URL")
|
||||||
"request_id": str(uuid.uuid4()),
|
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
|
||||||
"modifier": "Text2Image",
|
S3_ACCESS_KEY_ID = os.getenv("S3_ACCESS_KEY_ID")
|
||||||
"modifications": {
|
S3_SECRET_ACCESS_KEY = os.getenv("S3_SECRET_ACCESS_KEY")
|
||||||
"prompt": "a beautiful landscape with mountains and lakes",
|
|
||||||
"width": 1024,
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s")
|
||||||
"height": 1024,
|
log = logging.getLogger(__name__)
|
||||||
"steps": 20,
|
|
||||||
"seed": random.randint(0, 2**32 - 1)
|
|
||||||
},
|
def get_s3_client():
|
||||||
"workflow_json": {} # Empty since using modifier approach
|
"""Create and return an S3 client configured for the S3-compatible endpoint"""
|
||||||
}
|
try:
|
||||||
|
import boto3
|
||||||
|
from botocore.config import Config
|
||||||
|
except ImportError:
|
||||||
|
log.error("boto3 is required for S3 uploads. Install with: pip install boto3")
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not all([S3_ENDPOINT_URL, S3_BUCKET_NAME, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY]):
|
||||||
|
log.error("S3 environment variables not fully configured. Required:")
|
||||||
|
log.error(" S3_ENDPOINT_URL, S3_BUCKET_NAME, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return boto3.client(
|
||||||
|
"s3",
|
||||||
|
endpoint_url=S3_ENDPOINT_URL,
|
||||||
|
aws_access_key_id=S3_ACCESS_KEY_ID,
|
||||||
|
aws_secret_access_key=S3_SECRET_ACCESS_KEY,
|
||||||
|
config=Config(signature_version="s3v4"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------- API Functions ----------------------
|
||||||
|
async def call_generate(
|
||||||
|
client: Serverless,
|
||||||
|
*,
|
||||||
|
endpoint_name: str,
|
||||||
|
prompt: str,
|
||||||
|
width: int,
|
||||||
|
height: int,
|
||||||
|
steps: int,
|
||||||
|
seed: int,
|
||||||
|
) -> dict:
|
||||||
|
"""Generate image using Text2Image modifier"""
|
||||||
|
endpoint = await client.get_endpoint(name=endpoint_name)
|
||||||
|
payload = {
|
||||||
|
"input": {
|
||||||
|
"request_id": str(uuid.uuid4()),
|
||||||
|
"modifier": "Text2Image",
|
||||||
|
"modifications": {
|
||||||
|
"prompt": prompt,
|
||||||
|
"width": width,
|
||||||
|
"height": height,
|
||||||
|
"steps": steps,
|
||||||
|
"seed": seed,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
response = await endpoint.request("/generate/sync", payload, cost=count_workload())
|
return await endpoint.request("/generate/sync", payload, cost=COST)
|
||||||
|
|
||||||
|
|
||||||
|
async def call_generate_workflow(
|
||||||
|
client: Serverless,
|
||||||
|
*,
|
||||||
|
endpoint_name: str,
|
||||||
|
workflow_json: dict,
|
||||||
|
) -> dict:
|
||||||
|
"""Generate using custom workflow JSON"""
|
||||||
|
endpoint = await client.get_endpoint(name=endpoint_name)
|
||||||
|
payload = {
|
||||||
|
"input": {
|
||||||
|
"request_id": str(uuid.uuid4()),
|
||||||
|
"workflow_json": workflow_json,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return await endpoint.request("/generate/sync", payload, cost=COST)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------- Demo Class ----------------------
|
||||||
|
class APIDemo:
|
||||||
|
def __init__(self, client: Serverless, endpoint_name: str, upload_s3: bool = False):
|
||||||
|
self.client = client
|
||||||
|
self.endpoint_name = endpoint_name
|
||||||
|
self.upload_s3 = upload_s3
|
||||||
|
self.s3_client = get_s3_client() if upload_s3 else None
|
||||||
|
|
||||||
|
if upload_s3 and not self.s3_client:
|
||||||
|
log.warning("S3 upload requested but client creation failed. Images will only be saved locally.")
|
||||||
|
|
||||||
|
def extract_filename(self, response: dict) -> str | None:
|
||||||
|
"""Extract the generated image filename from ComfyUI response"""
|
||||||
|
if "comfyui_response" in response:
|
||||||
|
for data in response["comfyui_response"].values():
|
||||||
|
if isinstance(data, dict) and "outputs" in data:
|
||||||
|
for node_output in data["outputs"].values():
|
||||||
|
if "images" in node_output and node_output["images"]:
|
||||||
|
return node_output["images"][0].get("filename")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def save_image(self, worker_url: str, filename: str, local_name: str) -> str | None:
|
||||||
|
"""Fetch and save image locally from the worker, optionally upload to S3"""
|
||||||
|
os.makedirs("generated_images", exist_ok=True)
|
||||||
|
return await self._fetch_image(worker_url, filename, local_name)
|
||||||
|
|
||||||
|
def _upload_to_s3(self, local_path: str, s3_key: str) -> str | None:
|
||||||
|
"""Upload a local file to S3 and return the S3 URL"""
|
||||||
|
if not self.s3_client:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.s3_client.upload_file(
|
||||||
|
local_path,
|
||||||
|
S3_BUCKET_NAME,
|
||||||
|
s3_key,
|
||||||
|
ExtraArgs={"ContentType": "image/png"}
|
||||||
|
)
|
||||||
|
s3_url = f"{S3_ENDPOINT_URL}/{S3_BUCKET_NAME}/{s3_key}"
|
||||||
|
print(f" ☁️ Uploaded to S3: {s3_key}")
|
||||||
|
return s3_url
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Failed to upload to S3: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _fetch_image(self, worker_url: str, filename: str, local_name: str) -> str | None:
|
||||||
|
"""Fetch image from worker's /view endpoint and save locally"""
|
||||||
|
if not worker_url:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
url = f"{worker_url}/view"
|
||||||
|
params = {"filename": filename, "type": "output"}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, params=params, ssl=False) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
path = f"generated_images/{local_name}"
|
||||||
|
image_data = await resp.read()
|
||||||
|
with open(path, "wb") as f:
|
||||||
|
f.write(image_data)
|
||||||
|
print(f" 💾 Saved: {path}")
|
||||||
|
|
||||||
|
# Upload to S3 if enabled
|
||||||
|
if self.upload_s3 and self.s3_client:
|
||||||
|
s3_key = f"comfyui/{local_name}"
|
||||||
|
self._upload_to_s3(path, s3_key)
|
||||||
|
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def demo_prompt(
|
||||||
|
self,
|
||||||
|
prompt: str,
|
||||||
|
width: int,
|
||||||
|
height: int,
|
||||||
|
steps: int,
|
||||||
|
seed: int | None,
|
||||||
|
):
|
||||||
|
"""Demo: Generate image from text prompt"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("COMFYUI TEXT-TO-IMAGE DEMO")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
if seed is None:
|
||||||
|
seed = random.randint(0, 2**32 - 1)
|
||||||
|
|
||||||
|
print(f"Prompt: {prompt[:100]}..." if len(prompt) > 100 else f"Prompt: {prompt}")
|
||||||
|
print(f"Size: {width}x{height}, Steps: {steps}, Seed: {seed}")
|
||||||
|
print("\n🎨 Generating image...")
|
||||||
|
|
||||||
|
response = await call_generate(
|
||||||
|
self.client,
|
||||||
|
endpoint_name=self.endpoint_name,
|
||||||
|
prompt=prompt,
|
||||||
|
width=width,
|
||||||
|
height=height,
|
||||||
|
steps=steps,
|
||||||
|
seed=seed,
|
||||||
|
)
|
||||||
|
|
||||||
|
print("\n✅ Generation complete!")
|
||||||
|
|
||||||
|
# Get worker URL for fetching images
|
||||||
|
worker_url = response.get("url", "")
|
||||||
|
print(f"Worker URL: {worker_url}")
|
||||||
|
|
||||||
|
# Fetch and save image
|
||||||
|
if "response" in response:
|
||||||
|
filename = self.extract_filename(response["response"])
|
||||||
|
if filename:
|
||||||
|
path = await self.save_image(worker_url, filename, f"comfy_{seed}.png")
|
||||||
|
if not path:
|
||||||
|
print(f"❌ Failed to fetch image")
|
||||||
|
else:
|
||||||
|
print("❌ No image in response")
|
||||||
|
else:
|
||||||
|
print("❌ Unexpected response format")
|
||||||
|
|
||||||
|
async def demo_workflow(self, workflow_file: str):
|
||||||
|
"""Demo: Generate using custom workflow file"""
|
||||||
|
print("=" * 60)
|
||||||
|
print("COMFYUI CUSTOM WORKFLOW DEMO")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
if not os.path.exists(workflow_file):
|
||||||
|
log.error(f"Workflow file not found: {workflow_file}")
|
||||||
|
return
|
||||||
|
|
||||||
|
with open(workflow_file, "r") as f:
|
||||||
|
workflow_json = json.load(f)
|
||||||
|
|
||||||
|
print(f"Workflow: {workflow_file}")
|
||||||
|
print("\n🎨 Generating...")
|
||||||
|
|
||||||
|
response = await call_generate_workflow(
|
||||||
|
self.client,
|
||||||
|
endpoint_name=self.endpoint_name,
|
||||||
|
workflow_json=workflow_json,
|
||||||
|
)
|
||||||
|
|
||||||
|
print("\n✅ Generation complete!")
|
||||||
|
|
||||||
|
worker_url = response.get("url", "")
|
||||||
|
|
||||||
|
if "response" in response:
|
||||||
|
filename = self.extract_filename(response["response"])
|
||||||
|
if filename:
|
||||||
|
path = await self.save_image(worker_url, filename, "workflow.png")
|
||||||
|
if not path:
|
||||||
|
print(f"❌ Failed to fetch image")
|
||||||
|
else:
|
||||||
|
print("❌ No image in response")
|
||||||
|
else:
|
||||||
|
print("❌ Unexpected response format")
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------- CLI ----------------------
|
||||||
|
def build_arg_parser() -> argparse.ArgumentParser:
|
||||||
|
p = argparse.ArgumentParser(description="Vast ComfyUI-JSON Demo (Serverless SDK)")
|
||||||
|
p.add_argument("--endpoint", default=ENDPOINT_NAME, help=f"Vast endpoint name (default: {ENDPOINT_NAME})")
|
||||||
|
p.add_argument("--prompt", type=str, default=DEFAULT_PROMPT, metavar="TEXT",
|
||||||
|
help=f"Prompt text (default: '{DEFAULT_PROMPT[:30]}...')")
|
||||||
|
p.add_argument("--workflow", type=str, metavar="FILE", help="Use custom workflow JSON file instead")
|
||||||
|
p.add_argument("--width", type=int, default=DEFAULT_WIDTH, help=f"Image width (default: {DEFAULT_WIDTH})")
|
||||||
|
p.add_argument("--height", type=int, default=DEFAULT_HEIGHT, help=f"Image height (default: {DEFAULT_HEIGHT})")
|
||||||
|
p.add_argument("--steps", type=int, default=DEFAULT_STEPS, help=f"Steps (default: {DEFAULT_STEPS})")
|
||||||
|
p.add_argument("--seed", type=int, default=None, help="Seed (default: random)")
|
||||||
|
p.add_argument("--s3", action="store_true",
|
||||||
|
help="Upload generated images to S3 (requires S3_ENDPOINT_URL, S3_BUCKET_NAME, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY env vars)")
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
async def main_async():
|
||||||
|
args = build_arg_parser().parse_args()
|
||||||
|
|
||||||
|
print("=" * 60)
|
||||||
|
print(f"Using endpoint: {args.endpoint}")
|
||||||
|
if args.s3:
|
||||||
|
print(f"S3 upload: enabled (bucket: {S3_BUCKET_NAME})")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with Serverless() as client:
|
||||||
|
demo = APIDemo(client, args.endpoint, upload_s3=args.s3)
|
||||||
|
|
||||||
|
if args.workflow:
|
||||||
|
await demo.demo_workflow(workflow_file=args.workflow)
|
||||||
|
else:
|
||||||
|
await demo.demo_prompt(
|
||||||
|
prompt=args.prompt,
|
||||||
|
width=args.width,
|
||||||
|
height=args.height,
|
||||||
|
steps=args.steps,
|
||||||
|
seed=args.seed,
|
||||||
|
)
|
||||||
|
|
||||||
|
except AttributeError as e:
|
||||||
|
if "API key" in str(e):
|
||||||
|
log.error("API key missing. Set VAST_API_KEY environment variable.")
|
||||||
|
else:
|
||||||
|
log.error(f"Error: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Get the file from the path on the local machine using SCP or SFTP
|
|
||||||
# or configure S3 to upload to cloud storage.
|
|
||||||
print(response["response"]["output"][0]["local_path"])
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main_async())
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import dataclasses
|
|||||||
import base64
|
import base64
|
||||||
from typing import Optional, Union, Type
|
from typing import Optional, Union, Type
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from aiohttp import web, ClientResponse
|
from aiohttp import web, ClientResponse
|
||||||
|
|
||||||
from lib.backend import Backend, LogAction
|
from lib.backend import Backend, LogAction
|
||||||
@@ -13,6 +14,7 @@ from .data_types import ComfyWorkflowData
|
|||||||
|
|
||||||
|
|
||||||
MODEL_SERVER_URL = os.getenv("MODEL_SERVER_URL", "http://127.0.0.1:18288")
|
MODEL_SERVER_URL = os.getenv("MODEL_SERVER_URL", "http://127.0.0.1:18288")
|
||||||
|
COMFYUI_URL = os.getenv("COMFYUI_URL", "http://127.0.0.1:18188") # Raw ComfyUI server
|
||||||
|
|
||||||
# This is the last log line that gets emitted once comfyui+extensions have been fully loaded
|
# This is the last log line that gets emitted once comfyui+extensions have been fully loaded
|
||||||
MODEL_SERVER_START_LOG_MSG = "To see the GUI go to: "
|
MODEL_SERVER_START_LOG_MSG = "To see the GUI go to: "
|
||||||
@@ -108,8 +110,39 @@ async def handle_ping(_):
|
|||||||
return web.Response(body="pong")
|
return web.Response(body="pong")
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_view(request: web.Request) -> web.Response:
|
||||||
|
"""Proxy /view requests to raw ComfyUI server to fetch generated images"""
|
||||||
|
# Forward query params to raw ComfyUI (not the API wrapper)
|
||||||
|
query_string = request.query_string
|
||||||
|
url = f"{COMFYUI_URL}/view?{query_string}"
|
||||||
|
|
||||||
|
log.debug(f"Proxying /view request to: {url}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
content = await resp.read()
|
||||||
|
return web.Response(
|
||||||
|
body=content,
|
||||||
|
status=200,
|
||||||
|
content_type=resp.content_type or "image/png"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
text = await resp.text()
|
||||||
|
return web.Response(
|
||||||
|
text=text,
|
||||||
|
status=resp.status,
|
||||||
|
content_type="text/plain"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Error proxying /view: {e}")
|
||||||
|
return web.Response(text=str(e), status=500)
|
||||||
|
|
||||||
|
|
||||||
routes = [
|
routes = [
|
||||||
web.post("/generate/sync", backend.create_handler(ComfyWorkflowHandler())),
|
web.post("/generate/sync", backend.create_handler(ComfyWorkflowHandler())),
|
||||||
|
web.get("/view", handle_view),
|
||||||
web.get("/ping", handle_ping),
|
web.get("/ping", handle_ping),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
+14
-11
@@ -13,11 +13,11 @@ from vastai import Serverless
|
|||||||
ENDPOINT_NAME = "my-comfyui-endpoint"
|
ENDPOINT_NAME = "my-comfyui-endpoint"
|
||||||
COST = 100 # Use a constant cost for image generation
|
COST = 100 # Use a constant cost for image generation
|
||||||
|
|
||||||
def call_default_workflow(client: Serverless) -> None:
|
def call_default_workflow(endpoint_id: int, api_key: str, server_url: str) -> None:
|
||||||
WORKER_ENDPOINT = "/prompt"
|
WORKER_ENDPOINT = "/prompt"
|
||||||
COST = 100
|
COST = 100
|
||||||
route_payload = {
|
route_payload = {
|
||||||
"endpoint": endpoint_group_name,
|
"endpoint_id": endpoint_id,
|
||||||
"api_key": api_key,
|
"api_key": api_key,
|
||||||
"cost": COST,
|
"cost": COST,
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ def call_default_workflow(client: Serverless) -> None:
|
|||||||
auth_data = dict(
|
auth_data = dict(
|
||||||
signature=message["signature"],
|
signature=message["signature"],
|
||||||
cost=message["cost"],
|
cost=message["cost"],
|
||||||
endpoint=message["endpoint"],
|
endpoint_id=message["endpoint_id"],
|
||||||
reqnum=message["reqnum"],
|
reqnum=message["reqnum"],
|
||||||
url=message["url"],
|
url=message["url"],
|
||||||
)
|
)
|
||||||
@@ -52,12 +52,12 @@ def call_default_workflow(client: Serverless) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def call_custom_workflow_for_sd3(
|
def call_custom_workflow_for_sd3(
|
||||||
endpoint_group_name: str, api_key: str, server_url: str
|
endpoint_id: int, api_key: str, server_url: str
|
||||||
) -> None:
|
) -> None:
|
||||||
WORKER_ENDPOINT = "/custom-workflow"
|
WORKER_ENDPOINT = "/custom-workflow"
|
||||||
COST = 100
|
COST = 100
|
||||||
route_payload = {
|
route_payload = {
|
||||||
"endpoint": endpoint_group_name,
|
"endpoint_id": endpoint_id,
|
||||||
"api_key": api_key,
|
"api_key": api_key,
|
||||||
"cost": COST,
|
"cost": COST,
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ def call_custom_workflow_for_sd3(
|
|||||||
auth_data = dict(
|
auth_data = dict(
|
||||||
signature=message["signature"],
|
signature=message["signature"],
|
||||||
cost=message["cost"],
|
cost=message["cost"],
|
||||||
endpoint=message["endpoint"],
|
endpoint_id=message["endpoint_id"],
|
||||||
reqnum=message["reqnum"],
|
reqnum=message["reqnum"],
|
||||||
url=message["url"],
|
url=message["url"],
|
||||||
request_idx=message["request_idx"],
|
request_idx=message["request_idx"],
|
||||||
@@ -146,25 +146,28 @@ def call_custom_workflow_for_sd3(
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from lib.test_utils import test_args
|
from lib.test_utils import test_args
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
args = test_args.parse_args()
|
args = test_args.parse_args()
|
||||||
endpoint_api_key = Endpoint.get_endpoint_api_key(
|
endpoint_info = Endpoint.get_endpoint_info(
|
||||||
endpoint_name=args.endpoint_group_name,
|
endpoint_name=args.endpoint_group_name,
|
||||||
account_api_key=args.api_key,
|
account_api_key=args.api_key,
|
||||||
instance=args.instance,
|
instance=args.instance,
|
||||||
)
|
)
|
||||||
if endpoint_api_key:
|
if endpoint_info:
|
||||||
|
endpoint_id = endpoint_info["id"]
|
||||||
|
endpoint_api_key = endpoint_info["api_key"]
|
||||||
try:
|
try:
|
||||||
call_default_workflow(
|
call_default_workflow(
|
||||||
|
endpoint_id=endpoint_id,
|
||||||
api_key=endpoint_api_key,
|
api_key=endpoint_api_key,
|
||||||
endpoint_group_name=args.endpoint_group_name,
|
|
||||||
server_url=args.server_url,
|
server_url=args.server_url,
|
||||||
)
|
)
|
||||||
call_custom_workflow_for_sd3(
|
call_custom_workflow_for_sd3(
|
||||||
|
endpoint_id=endpoint_id,
|
||||||
api_key=endpoint_api_key,
|
api_key=endpoint_api_key,
|
||||||
endpoint_group_name=args.endpoint_group_name,
|
|
||||||
server_url=args.server_url,
|
server_url=args.server_url,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Error during API call: {e}")
|
log.error(f"Error during API call: {e}")
|
||||||
else:
|
else:
|
||||||
log.error(f"Failed to get API key for endpoint {args.endpoint_group_name} ")
|
log.error(f"Failed to get endpoint info for {args.endpoint_group_name}")
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ def do_one(endpoint_name: str,
|
|||||||
worker_session):
|
worker_session):
|
||||||
try:
|
try:
|
||||||
workload = payload.count_workload()
|
workload = payload.count_workload()
|
||||||
route_payload = {"endpoint": endpoint_name, "api_key": endpoint_api_key, "cost": workload}
|
route_payload = {"endpoint_id": endpoint_id, "api_key": endpoint_api_key, "cost": workload}
|
||||||
headers = {"Authorization": f"Bearer {endpoint_api_key}"}
|
headers = {"Authorization": f"Bearer {endpoint_api_key}"}
|
||||||
start = time.time()
|
start = time.time()
|
||||||
r0 = route_session.post(urljoin(server_url, "/route/"), json=route_payload, headers=headers, timeout=4)
|
r0 = route_session.post(urljoin(server_url, "/route/"), json=route_payload, headers=headers, timeout=4)
|
||||||
|
|||||||
Reference in New Issue
Block a user