File size: 4,531 Bytes
3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda 722243d 3f6feda |
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 |
import gradio as gr
import numpy as np
import random
from PIL import Image
import torch
from diffusers import ControlNetModel, UniPCMultistepScheduler
from hico_pipeline import StableDiffusionControlNetMultiLayoutPipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize model
controlnet = ControlNetModel.from_pretrained("qihoo360/HiCo_T2I", torch_dtype=torch.float16)
pipe = StableDiffusionControlNetMultiLayoutPipeline.from_pretrained(
"krnl/realisticVisionV51_v51VAE", controlnet=[controlnet], torch_dtype=torch.float16
)
pipe = pipe.to(device)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
MAX_SEED = np.iinfo(np.int32).max
# Function for generating dummy bounding box and label data
def generate_dummy_data():
# Generate random image size
img_width, img_height = 512, 512
r_image = np.zeros((img_height, img_width, 3), dtype=np.uint8)
# Generate random bounding boxes and labels
num_objects = random.randint(1, 5)
r_obj_bbox = []
r_obj_class = ["Object"]
list_cond_image = []
for _ in range(num_objects):
x1, y1 = random.randint(0, img_width // 2), random.randint(0, img_height // 2)
x2, y2 = random.randint(x1, img_width), random.randint(y1, img_height)
r_obj_bbox.append([x1, y1, x2, y2])
cond_image = np.zeros_like(r_image, dtype=np.uint8)
cond_image[y1:y2, x1:x2] = 255
list_cond_image.append(cond_image)
r_obj_bbox.insert(0, [0, 0, img_width, img_height]) # Add background
r_obj_class.insert(0, "Background")
list_cond_image.insert(0, np.zeros_like(r_image, dtype=np.uint8)) # Add full background
obj_cond_image = np.stack(list_cond_image, axis=0)
list_cond_image_pil = [Image.fromarray(img).convert('RGB') for img in list_cond_image]
return r_obj_class, r_obj_bbox, list_cond_image_pil, obj_cond_image
# Inference function
def infer(
prompt, guidance_scale, num_inference_steps, randomize_seed, seed=None
):
# Generate dummy data for demonstration
r_obj_class, r_obj_bbox, list_cond_image_pil, _ = generate_dummy_data()
if randomize_seed or seed is None:
seed = random.randint(0, MAX_SEED)
generator = torch.manual_seed(seed)
# Run inference
image = pipe(
prompt=prompt,
layo_prompt=r_obj_class,
guess_mode=False,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
image=list_cond_image_pil,
fuse_type="avg",
width=512,
height=512
).images[0]
return image, seed
examples = [
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
"An astronaut riding a green horse",
"A delicious ceviche cheesecake slice",
]
css = """
#col-container {
margin: 0 auto;
max-width: 640px;
}
"""
# Gradio UI
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(" # Text-to-Image Gradio Template")
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, variant="primary")
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():
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.0,
maximum=10.0,
step=0.1,
value=7.5,
)
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=50,
step=1,
value=50,
)
gr.Examples(examples=examples, inputs=[prompt])
run_button.click(
fn=infer,
inputs=[
prompt,
guidance_scale,
num_inference_steps,
randomize_seed,
seed,
],
outputs=[result, seed],
)
if __name__ == "__main__":
demo.launch()
|