Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,708 Bytes
7e1fa02 99738e0 7e1fa02 99738e0 7e1fa02 99738e0 7e1fa02 99738e0 7e1fa02 99738e0 7e1fa02 99738e0 7e1fa02 99738e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import gradio as gr
import torch
import spaces
import os
import numpy as np
from PIL import Image
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from omegaconf import OmegaConf
from src.flux.util import load_ae, load_clip, load_flow_model2, load_t5, tensor_to_pil_image
from src.flux.xflux_pipeline import XFluxSampler
from image_datasets.dataset import image_resize
# ===== No CUDA/model initialization globally =====
args = OmegaConf.load("inference_configs/inference.yaml")
is_schnell = args.model_name == "flux-schnell"
# sampler = None
device = torch.device("cuda")
dtype = torch.bfloat16
dit = load_flow_model2(args.model_name, device="cpu").to(device, dtype=dtype)
vae = load_ae(args.model_name, device="cpu").to(device, dtype=dtype)
t5 = load_t5(device="cpu", max_length=256 if is_schnell else 512).to(device, dtype=dtype)
clip = load_clip("cpu").to(device, dtype=dtype)
vae.requires_grad_(False)
t5.requires_grad_(False)
clip.requires_grad_(False)
model_path = hf_hub_download(
repo_id="Boese0601/ByteMorpher",
filename="dit.safetensors",
use_auth_token=os.getenv("HF_TOKEN")
)
state_dict = load_file(model_path)
dit.load_state_dict(state_dict)
dit.eval()
dit.to(device, dtype=dtype)
sampler = XFluxSampler(
clip=clip,
t5=t5,
ae=vae,
model=dit,
device=device,
ip_loaded=False,
spatial_condition=False,
clip_image_processor=None,
image_encoder=None,
improj=None
)
#test push
@spaces.GPU
def generate(image: Image.Image, edit_prompt: str):
# global sampler
# device = torch.device("cuda")
# dtype = torch.bfloat16
# if sampler is None:
# dit = load_flow_model2(args.model_name, device="cpu").to(device, dtype=dtype)
# vae = load_ae(args.model_name, device="cpu").to(device, dtype=dtype)
# t5 = load_t5(device="cpu", max_length=256 if is_schnell else 512).to(device, dtype=dtype)
# clip = load_clip("cpu").to(device, dtype=dtype)
# vae.requires_grad_(False)
# t5.requires_grad_(False)
# clip.requires_grad_(False)
# model_path = hf_hub_download(
# repo_id="Boese0601/ByteMorpher",
# filename="dit.safetensors",
# use_auth_token=os.getenv("HF_TOKEN")
# )
# state_dict = load_file(model_path)
# dit.load_state_dict(state_dict)
# dit.eval()
# sampler = XFluxSampler(
# clip=clip,
# t5=t5,
# ae=vae,
# model=dit,
# device=device,
# ip_loaded=False,
# spatial_condition=False,
# clip_image_processor=None,
# image_encoder=None,
# improj=None
# )
img = image_resize(image, 512)
w, h = img.size
img = img.resize(((w // 32) * 32, (h // 32) * 32))
img = torch.from_numpy((np.array(img) / 127.5) - 1)
img = img.permute(2, 0, 1).unsqueeze(0).to(device, dtype=dtype)
with torch.no_grad():
result = sampler(
prompt=edit_prompt,
width=args.sample_width,
height=args.sample_height,
num_steps=args.sample_steps,
image_prompt=None,
true_gs=args.cfg_scale,
seed=args.seed,
ip_scale=args.ip_scale if args.use_ip else 1.0,
source_image=img if args.use_spatial_condition else None,
)
return tensor_to_pil_image(result)
def get_samples():
sample_list = [
{
"image": "assets/0_camera_zoom/20486354.png",
"edit_prompt": "Zoom in on the coral and add a small blue fish in the background.",
},
]
return [
[
Image.open(sample["image"]).resize((512, 512)),
sample["edit_prompt"],
]
for sample in sample_list
]
header = """
# ByteMorph
<div style="text-align: center; display: flex; justify-content: left; gap: 5px;">
<a href=""><img src="https://img.shields.io/badge/ariXv-Paper-A42C25.svg" alt="arXiv"></a>
<a href="https://huggingface.co/datasets/Boese0601/ByteMorph-Bench"><img src="https://img.shields.io/badge/🤗-Model-ffbd45.svg" alt="HuggingFace"></a>
<a href="https://github.com/Boese0601/ByteMorph"><img src="https://img.shields.io/badge/GitHub-Code-blue.svg?logo=github&" alt="GitHub"></a>
</div>
"""
def create_app():
with gr.Blocks() as app:
gr.Markdown(header, elem_id="header")
with gr.Row(equal_height=False):
with gr.Column(variant="panel", elem_classes="inputPanel"):
original_image = gr.Image(
type="pil", label="Condition Image", width=300, elem_id="input"
)
edit_prompt = gr.Textbox(lines=2, label="Edit Prompt", elem_id="edit_prompt")
submit_btn = gr.Button("Run", elem_id="submit_btn")
with gr.Column(variant="panel", elem_classes="outputPanel"):
output_image = gr.Image(type="pil", elem_id="output")
with gr.Row():
examples = gr.Examples(
examples=get_samples(),
inputs=[original_image, edit_prompt],
label="Examples",
)
submit_btn.click(
fn=generate,
inputs=[original_image, edit_prompt],
outputs=output_image,
)
gr.HTML(
"""
<div style="text-align: center;">
* This demo's template was modified from <a href="https://arxiv.org/abs/2411.15098" target="_blank">OminiControl</a>.
</div>
"""
)
return app
if __name__ == "__main__":
create_app().launch(debug=False, share=False, ssr_mode=False)
|