KumaPower's picture
Improve mask editing and add a sample gallery
fd40e27 verified
Raw History Blame Contribute Delete
2.43 kB
"""Hugging Face Spaces entry point for the published CSA release."""
import hashlib
import io
import json
import os
import sys
from pathlib import Path
from zipfile import ZipFile
if os.environ.get("SPACE_ID") and os.environ.get("CSA_DEVICE", "cuda") == "cuda":
import spaces
else:
spaces = None
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
# Spaces installs dependencies before copying the repository into the image.
sys.path.insert(0, str(Path(__file__).with_name("csa_inpainting-0.3.0-py3-none-any.whl")))
from csa_inpainting.inference import load_inference_generator, predict_stages
from csa_inpainting.web_demo import CSS, Source, build_demo
MODEL = "KumaPower/CSA"
REVISION = "bf82a33eb70a6a0182180ae9f880bb7d1a08c439"
FILENAME = "csa-stage2-step97397.pt"
SHA256 = "605b80c18dcb0856367e6276813df0b54ec42aabea782af1631052aedb7abdfe"
def download(filename):
return Path(hf_hub_download(MODEL, filename, revision=REVISION, token=False))
checkpoint = Path(os.environ["CSA_CHECKPOINT"]) if "CSA_CHECKPOINT" in os.environ else download(FILENAME)
with checkpoint.open("rb") as stream:
if hashlib.file_digest(stream, "sha256").hexdigest() != SHA256:
raise ValueError("CSA release checkpoint checksum mismatch")
torch.set_num_threads(2)
model, config = load_inference_generator(checkpoint, torch.device("cuda" if spaces else "cpu"))
examples = {}
with ZipFile(Path(__file__).with_name("examples.zip")) as archive:
for example in json.loads(archive.read("index.json")):
label = example["label"]
with Image.open(io.BytesIO(archive.read(example["image"]))) as image:
source = Source(image.convert("RGB"), label)
with Image.open(io.BytesIO(archive.read(example["mask"]))) as mask:
examples[label] = source, mask.convert("L")
def run_stages(image, mask):
rough, refined = predict_stages(model, image, mask)
return rough.cpu(), refined.cpu()
if spaces is not None:
run_stages = spaces.GPU(duration=10)(run_stages)
demo, theme = build_demo(model, config, examples, FILENAME, run_stages=run_stages)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0" if os.environ.get("SPACE_ID") else "127.0.0.1",
server_port=int(os.environ.get("CSA_PORT", "7860")),
share=False,
theme=theme,
css=CSS,
max_file_size="16mb",
show_error=False,
)