|
| 1 | +from beam import Image, asgi, Output |
| 2 | +import requests |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +image = ( |
| 6 | + Image() |
| 7 | + .add_commands(["apt update && apt install git -y"]) |
| 8 | + .add_python_packages( |
| 9 | + [ |
| 10 | + "fastapi[standard]==0.115.4", |
| 11 | + "comfy-cli", |
| 12 | + "huggingface_hub[hf_transfer]==0.26.2", |
| 13 | + ] |
| 14 | + ) |
| 15 | + .add_commands( |
| 16 | + [ |
| 17 | + "yes | comfy install --nvidia --version 0.3.10", |
| 18 | + "comfy node install was-node-suite-comfyui@1.0.2", |
| 19 | + "mkdir -p /root/comfy/ComfyUI/models/checkpoints/", |
| 20 | + "huggingface-cli download Comfy-Org/flux1-schnell flux1-schnell-fp8.safetensors --cache-dir /comfy-cache", |
| 21 | + "ln -s /comfy-cache/models--Comfy-Org--flux1-schnell/snapshots/f2808ab17fe9ff81dcf89ed0301cf644c281be0a/flux1-schnell-fp8.safetensors /root/comfy/ComfyUI/models/checkpoints/flux1-schnell-fp8.safetensors", |
| 22 | + ] |
| 23 | + ) |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def download_image_from_url(url: str, filename: str = "input_image.png"): |
| 28 | + target_path = Path("/root/comfy/ComfyUI/input") / filename |
| 29 | + target_path.parent.mkdir(parents=True, exist_ok=True) |
| 30 | + |
| 31 | + response = requests.get(url) |
| 32 | + if response.status_code != 200: |
| 33 | + raise Exception(f"Failed to download image: {response.status_code}") |
| 34 | + |
| 35 | + with open(target_path, "wb") as f: |
| 36 | + f.write(response.content) |
| 37 | + |
| 38 | + return target_path |
| 39 | + |
| 40 | + |
| 41 | +def hf_download(): |
| 42 | + import subprocess |
| 43 | + from huggingface_hub import hf_hub_download |
| 44 | + |
| 45 | + dream_model = hf_hub_download( |
| 46 | + repo_id="Bruhn/Lab_merge", |
| 47 | + filename="dreamCreationVirtual3DECommerce_v10.safetensors", |
| 48 | + cache_dir="/comfy-cache", |
| 49 | + ) |
| 50 | + |
| 51 | + vae_model = hf_hub_download( |
| 52 | + repo_id="stabilityai/sd-vae-ft-mse-original", |
| 53 | + filename="vae-ft-mse-840000-ema-pruned.safetensors", |
| 54 | + cache_dir="/comfy-cache", |
| 55 | + ) |
| 56 | + |
| 57 | + controlnet_model = hf_hub_download( |
| 58 | + repo_id="comfyanonymous/ControlNet-v1-1_fp16_safetensors", |
| 59 | + filename="control_v11p_sd15_scribble_fp16.safetensors", |
| 60 | + cache_dir="/comfy-cache", |
| 61 | + ) |
| 62 | + |
| 63 | + subprocess.run( |
| 64 | + "mkdir -p /root/comfy/ComfyUI/models/checkpoints/", |
| 65 | + shell=True, |
| 66 | + check=True, |
| 67 | + ) |
| 68 | + subprocess.run( |
| 69 | + "mkdir -p /root/comfy/ComfyUI/models/vae/", |
| 70 | + shell=True, |
| 71 | + check=True, |
| 72 | + ) |
| 73 | + subprocess.run( |
| 74 | + "mkdir -p /root/comfy/ComfyUI/models/controlnet/", |
| 75 | + shell=True, |
| 76 | + check=True, |
| 77 | + ) |
| 78 | + |
| 79 | + subprocess.run( |
| 80 | + f"ln -s {dream_model} /root/comfy/ComfyUI/models/checkpoints/dreamCreationVirtual3DECommerce_v10.safetensors", |
| 81 | + shell=True, |
| 82 | + check=True, |
| 83 | + ) |
| 84 | + subprocess.run( |
| 85 | + f"ln -s {vae_model} /root/comfy/ComfyUI/models/vae/vae-ft-mse-840000-ema-pruned.safetensors", |
| 86 | + shell=True, |
| 87 | + check=True, |
| 88 | + ) |
| 89 | + subprocess.run( |
| 90 | + f"ln -s {controlnet_model} /root/comfy/ComfyUI/models/controlnet/control_v11p_sd15_scribble_fp16.safetensors", |
| 91 | + shell=True, |
| 92 | + check=True, |
| 93 | + ) |
| 94 | + |
| 95 | + cmd = "comfy launch --background" |
| 96 | + subprocess.run(cmd, shell=True, check=True) |
| 97 | + |
| 98 | + |
| 99 | +@asgi( |
| 100 | + name="comfy", |
| 101 | + image=image, |
| 102 | + on_start=hf_download, |
| 103 | + cpu=8, |
| 104 | + memory="32Gi", |
| 105 | + gpu="A100-40", |
| 106 | + timeout=-1, |
| 107 | +) |
| 108 | +def handler(): |
| 109 | + from fastapi import FastAPI, HTTPException |
| 110 | + import subprocess |
| 111 | + import json |
| 112 | + from pathlib import Path |
| 113 | + import uuid |
| 114 | + from typing import Dict |
| 115 | + |
| 116 | + app = FastAPI() |
| 117 | + |
| 118 | + # This is where you specify the path to your workflow file. |
| 119 | + # Make sure "workflow_api.json" exists in the same directory as this script. |
| 120 | + WORKFLOW_FILE = Path(__file__).parent / "workflow_api.json" |
| 121 | + OUTPUT_DIR = Path("/root/comfy/ComfyUI/output") |
| 122 | + |
| 123 | + @app.post("/generate") |
| 124 | + async def generate(item: Dict): |
| 125 | + if not WORKFLOW_FILE.exists(): |
| 126 | + raise HTTPException(status_code=500, detail="Workflow file not found.") |
| 127 | + |
| 128 | + image_url = item.get("image_url") |
| 129 | + prompt = item.get("prompt") |
| 130 | + |
| 131 | + if not image_url or not prompt: |
| 132 | + raise HTTPException(status_code=400, detail="Missing image_url or prompt") |
| 133 | + |
| 134 | + downloaded_image_path = download_image_from_url(image_url, "input.jpg") |
| 135 | + request_id = uuid.uuid4().hex |
| 136 | + |
| 137 | + workflow_data = json.loads(WORKFLOW_FILE.read_text()) |
| 138 | + workflow_data["6"]["inputs"]["text"] = item["prompt"] |
| 139 | + workflow_data["11"]["inputs"]["image"] = str(downloaded_image_path) |
| 140 | + |
| 141 | + new_workflow_file = Path(f"{request_id}.json") |
| 142 | + new_workflow_file.write_text(json.dumps(workflow_data, indent=4)) |
| 143 | + |
| 144 | + # Run inference |
| 145 | + cmd = ( |
| 146 | + f"comfy run --workflow {new_workflow_file} --wait --timeout 1200 --verbose" |
| 147 | + ) |
| 148 | + subprocess.run(cmd, shell=True, check=True) |
| 149 | + |
| 150 | + image_files = list(OUTPUT_DIR.glob("*")) |
| 151 | + |
| 152 | + # Find the latest image |
| 153 | + latest_image = max( |
| 154 | + (f for f in image_files if f.suffix.lower() in {".png", ".jpg", ".jpeg"}), |
| 155 | + key=lambda f: f.stat().st_mtime, |
| 156 | + default=None, |
| 157 | + ) |
| 158 | + |
| 159 | + if not latest_image: |
| 160 | + raise HTTPException(status_code=404, detail="No output image found.") |
| 161 | + |
| 162 | + output_file = Output(path=latest_image) |
| 163 | + output_file.save() |
| 164 | + public_url = output_file.public_url(expires=-1) |
| 165 | + print(public_url) |
| 166 | + return {"output_url": public_url} |
| 167 | + |
| 168 | + return app |
0 commit comments