privacyzer0's picture
Upload folder using huggingface_hub
2cbdbd8 verified
Raw
History Blame Contribute Delete
4.28 kB
import gradio as gr
import numpy as np
import torch
import spaces
from PIL import Image
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
import os
hf_token = os.environ.get("HF_TOKEN")
PIPE = None
DEPTH_PROC = None
DEPTH_MODEL = None
def load_models():
global PIPE, DEPTH_PROC, DEPTH_MODEL
if PIPE is not None:
return
from diffusers import FluxControlPipeline
from transformers import AutoImageProcessor, DepthAnythingForDepthEstimation
print("Loading FLUX.1-Depth-dev ...", flush=True)
PIPE = FluxControlPipeline.from_pretrained(
"black-forest-labs/FLUX.1-Depth-dev",
torch_dtype=torch.bfloat16,
token=hf_token,
low_cpu_mem_usage=True,
).to("cuda")
PIPE.enable_sequential_cpu_offload()
PIPE.enable_vae_tiling()
PIPE.enable_vae_slicing()
print("FLUX loaded.", flush=True)
depth_id = "LiheYoung/depth-anything-large-hf"
print("Loading DepthAnything ...", flush=True)
DEPTH_PROC = AutoImageProcessor.from_pretrained(depth_id, token=hf_token)
DEPTH_MODEL = DepthAnythingForDepthEstimation.from_pretrained(depth_id, token=hf_token).to("cuda")
print("DepthAnything loaded.", flush=True)
def to_depth(image_pil):
inputs = DEPTH_PROC(images=image_pil, return_tensors="pt").to("cuda")
with torch.no_grad():
out = DEPTH_MODEL(**inputs).predicted_depth
depth = out.squeeze().cpu().float().numpy()
depth = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
depth = (depth * 255.0).astype("uint8")
return Image.fromarray(depth).convert("RGB")
@spaces.GPU
def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
try:
load_models()
if randomize_seed:
seed = int(np.random.randint(0, MAX_SEED))
control_image = to_depth(control_image.convert("RGB"))
image = PIPE(
prompt=prompt,
control_image=control_image,
height=height,
width=width,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=torch.Generator().manual_seed(seed),
).images[0]
return image, seed
except Exception as e:
import traceback
traceback.print_exc()
raise
examples = [
"a tiny astronaut hatching from an egg on the moon",
"a cat holding a sign that says hello world",
"an anime illustration of a wiener schnitzel",
]
css = """#col-container { margin: 0 auto; max-width: 520px; }"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(
"# FLUX.1 Depth [dev]\n"
"12B param rectified flow transformer structural conditioning, guidance-distilled. "
"Non-commercial license."
)
control_image = gr.Image(label="Upload the image for control", type="pil")
with gr.Row():
prompt = gr.Text(label="Prompt", show_label=False, max_lines=1,
placeholder="Enter your prompt", container=False)
run_button = gr.Button("Run", scale=0)
result = gr.Image(label="Result", show_label=False)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
with gr.Row():
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=30, step=0.5, value=10)
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[control_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
outputs=[result, seed],
)
demo.launch()