Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,98 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
torch.cuda.max_memory_allocated(device=device)
|
11 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
|
19 |
-
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
seed = random.randint(0, MAX_SEED)
|
25 |
-
|
26 |
-
generator = torch.Generator().manual_seed(seed)
|
27 |
-
|
28 |
-
image = pipe(
|
29 |
-
prompt = prompt,
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
]
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
|
58 |
-
with gr.Blocks(
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
)
|
88 |
-
|
89 |
-
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
)
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
with gr.Row():
|
100 |
-
|
101 |
-
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
with gr.Row():
|
118 |
-
|
119 |
-
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
|
140 |
-
|
141 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
-
)
|
145 |
|
146 |
-
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from omegaconf import OmegaConf
|
4 |
+
from gligen.task_grounded_generation import grounded_generation_box, load_ckpt, load_common_ckpt
|
5 |
+
|
6 |
+
import json
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image, ImageDraw, ImageFont
|
9 |
+
from functools import partial
|
10 |
+
from collections import Counter
|
11 |
+
import math
|
12 |
+
import gc
|
13 |
|
14 |
+
from gradio import processing_utils
|
15 |
+
from typing import Optional
|
16 |
|
17 |
+
import warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
from datetime import datetime
|
|
|
20 |
|
21 |
+
from huggingface_hub import hf_hub_download
|
22 |
+
hf_hub_download = partial(hf_hub_download, library_name="gligen_demo")
|
23 |
|
24 |
+
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
class ImageMask(gr.components.Image):
|
27 |
+
is_template = True
|
28 |
+
def __init__(self, **kwargs):
|
29 |
+
super().__init__(source="upload", tool="sketch", interactive=True, **kwargs)
|
|
|
30 |
|
31 |
+
def preprocess(self, x):
|
32 |
+
if x is None:
|
33 |
+
return x
|
34 |
+
if self.tool == "sketch" and self.source in ["upload", "webcam"] and type(x) != dict:
|
35 |
+
decode_image = processing_utils.decode_base64_to_image(x)
|
36 |
+
width, height = decode_image.size
|
37 |
+
mask = np.zeros((height, width, 4), dtype=np.uint8)
|
38 |
+
mask[..., -1] = 255
|
39 |
+
mask = self.postprocess(mask)
|
40 |
+
x = {'image': x, 'mask': mask}
|
41 |
+
return super().preprocess(x)
|
42 |
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
with gr.Blocks(
|
45 |
+
analytics_enabled=False,
|
46 |
+
title="GLIGen demo",
|
47 |
+
) as main:
|
48 |
+
with gr.Row():
|
49 |
+
with gr.Column(scale=4):
|
50 |
+
sketch_pad_trigger = gr.Number(value=0, visible=False)
|
51 |
+
sketch_pad_resize_trigger = gr.Number(value=0, visible=False)
|
52 |
+
init_white_trigger = gr.Number(value=0, visible=False)
|
53 |
+
image_scale = gr.Number(value=0, elem_id="image_scale", visible=False)
|
54 |
+
new_image_trigger = gr.Number(value=0, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
task = gr.Radio(
|
57 |
+
choices=["Grounded Generation", 'Grounded Inpainting'],
|
58 |
+
type="value",
|
59 |
+
value="Grounded Generation",
|
60 |
+
label="Task",
|
|
|
|
|
61 |
)
|
62 |
+
language_instruction = gr.Textbox(
|
63 |
+
label="Language instruction",
|
|
|
|
|
|
|
|
|
|
|
64 |
)
|
65 |
+
grounding_instruction = gr.Textbox(
|
66 |
+
label="Grounding instruction (Separated by semicolon)",
|
67 |
+
)
|
68 |
+
with gr.Row():
|
69 |
+
sketch_pad = ImageMask(label="Sketch Pad", elem_id="img2img_image")
|
70 |
+
out_imagebox = gr.Image(type="pil", label="Parsed Sketch Pad")
|
71 |
+
with gr.Row():
|
72 |
+
clear_btn = gr.Button(value='Clear')
|
73 |
+
gen_btn = gr.Button(value='Generate')
|
74 |
+
with gr.Accordion("Advanced Options", open=False):
|
75 |
+
with gr.Column():
|
76 |
+
alpha_sample = gr.Slider(minimum=0, maximum=1.0, step=0.1, value=0.3, label="Scheduled Sampling (τ)")
|
77 |
+
guidance_scale = gr.Slider(minimum=0, maximum=50, step=0.5, value=7.5, label="Guidance Scale")
|
78 |
+
batch_size = gr.Slider(minimum=1, maximum=4, step=1, value=2, label="Number of Samples")
|
79 |
+
append_grounding = gr.Checkbox(value=True, label="Append grounding instructions to the caption")
|
80 |
+
use_actual_mask = gr.Checkbox(value=False, label="Use actual mask for inpainting", visible=False)
|
81 |
+
with gr.Row():
|
82 |
+
fix_seed = gr.Checkbox(value=True, label="Fixed seed")
|
83 |
+
rand_seed = gr.Slider(minimum=0, maximum=1000, step=1, value=0, label="Seed")
|
84 |
+
with gr.Row(visible=False):
|
85 |
+
use_style_cond = gr.Checkbox(value=False, label="Enable Style Condition")
|
86 |
+
style_cond_image = gr.Image(type="pil", label="Style Condition", visible=False, interactive=True)
|
87 |
+
with gr.Column(scale=4):
|
88 |
+
gr.HTML('<span style="font-size: 20px; font-weight: bold">Generated Images</span>')
|
89 |
with gr.Row():
|
90 |
+
out_gen_1 = gr.Image(type="pil", visible=True, show_label=False)
|
91 |
+
out_gen_2 = gr.Image(type="pil", visible=True, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
with gr.Row():
|
93 |
+
out_gen_3 = gr.Image(type="pil", visible=False, show_label=False)
|
94 |
+
out_gen_4 = gr.Image(type="pil", visible=False, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
state = gr.State({})
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
main.queue(concurrency_count=1, api_open=False).launch(share=False, show_api=False, show_error=True)
|