Update app.py
Browse files
app.py
CHANGED
@@ -19,23 +19,30 @@ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
img_width, img_height = 512, 512
|
25 |
r_image = np.zeros((img_height, img_width, 3), dtype=np.uint8)
|
26 |
list_cond_image = []
|
27 |
|
28 |
-
|
|
|
29 |
x1, y1, x2, y2 = map(int, bbox.split(","))
|
30 |
cond_image = np.zeros_like(r_image, dtype=np.uint8)
|
31 |
cond_image[y1:y2, x1:x2] = 255
|
32 |
list_cond_image.append(Image.fromarray(cond_image).convert('RGB'))
|
33 |
|
34 |
-
return object_classes.split(","), list_cond_image
|
35 |
-
|
36 |
-
# Inference function
|
37 |
-
def infer(prompt, guidance_scale, num_inference_steps, randomize_seed, seed, object_classes, object_bboxes):
|
38 |
-
obj_classes, list_cond_image_pil = generate_user_data(object_classes, object_bboxes)
|
39 |
if randomize_seed or seed is None:
|
40 |
seed = np.random.randint(0, MAX_SEED)
|
41 |
|
@@ -43,11 +50,11 @@ def infer(prompt, guidance_scale, num_inference_steps, randomize_seed, seed, obj
|
|
43 |
|
44 |
image = pipe(
|
45 |
prompt=prompt,
|
46 |
-
layo_prompt=
|
47 |
guess_mode=False,
|
48 |
guidance_scale=guidance_scale,
|
49 |
num_inference_steps=num_inference_steps,
|
50 |
-
image=
|
51 |
fuse_type="avg",
|
52 |
width=512,
|
53 |
height=512
|
@@ -57,12 +64,17 @@ def infer(prompt, guidance_scale, num_inference_steps, randomize_seed, seed, obj
|
|
57 |
|
58 |
# Gradio UI
|
59 |
with gr.Blocks() as demo:
|
60 |
-
gr.Markdown("# Text-to-Image Generator with
|
61 |
|
62 |
with gr.Row():
|
63 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
with gr.Row():
|
68 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
|
@@ -71,12 +83,20 @@ with gr.Blocks() as demo:
|
|
71 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
72 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
73 |
|
74 |
-
|
75 |
result = gr.Image(label="Generated Image")
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
outputs=[result, seed]
|
81 |
)
|
82 |
|
|
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
|
22 |
+
# Store objects
|
23 |
+
object_classes_list = []
|
24 |
+
object_bboxes_list = []
|
25 |
+
|
26 |
+
# Function to add a new object
|
27 |
+
def add_object(object_class, bbox):
|
28 |
+
object_classes_list.append(object_class)
|
29 |
+
object_bboxes_list.append(bbox)
|
30 |
+
# Return updated list of objects
|
31 |
+
return object_classes_list, object_bboxes_list
|
32 |
+
|
33 |
+
# Function to generate images based on added objects
|
34 |
+
def generate_image(prompt, guidance_scale, num_inference_steps, randomize_seed, seed):
|
35 |
img_width, img_height = 512, 512
|
36 |
r_image = np.zeros((img_height, img_width, 3), dtype=np.uint8)
|
37 |
list_cond_image = []
|
38 |
|
39 |
+
# Process bounding boxes and create conditional images
|
40 |
+
for bbox in object_bboxes_list:
|
41 |
x1, y1, x2, y2 = map(int, bbox.split(","))
|
42 |
cond_image = np.zeros_like(r_image, dtype=np.uint8)
|
43 |
cond_image[y1:y2, x1:x2] = 255
|
44 |
list_cond_image.append(Image.fromarray(cond_image).convert('RGB'))
|
45 |
|
|
|
|
|
|
|
|
|
|
|
46 |
if randomize_seed or seed is None:
|
47 |
seed = np.random.randint(0, MAX_SEED)
|
48 |
|
|
|
50 |
|
51 |
image = pipe(
|
52 |
prompt=prompt,
|
53 |
+
layo_prompt=object_classes_list,
|
54 |
guess_mode=False,
|
55 |
guidance_scale=guidance_scale,
|
56 |
num_inference_steps=num_inference_steps,
|
57 |
+
image=list_cond_image,
|
58 |
fuse_type="avg",
|
59 |
width=512,
|
60 |
height=512
|
|
|
64 |
|
65 |
# Gradio UI
|
66 |
with gr.Blocks() as demo:
|
67 |
+
gr.Markdown("# Text-to-Image Generator with Object Addition")
|
68 |
|
69 |
with gr.Row():
|
70 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
|
71 |
+
|
72 |
+
object_class_input = gr.Textbox(label="Object Class", placeholder="Enter object class (e.g., Object_1)")
|
73 |
+
bbox_input = gr.Textbox(label="Bounding Box (x1,y1,x2,y2)", placeholder="Enter bounding box coordinates")
|
74 |
+
add_button = gr.Button("Add Object")
|
75 |
+
|
76 |
+
# Display list of added objects
|
77 |
+
objects_display = gr.Dataframe(headers=["Object Class", "Bounding Box"], datatype=["str", "str"], value=[])
|
78 |
|
79 |
with gr.Row():
|
80 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
|
|
|
83 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
84 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
85 |
|
86 |
+
generate_button = gr.Button("Generate Image")
|
87 |
result = gr.Image(label="Generated Image")
|
88 |
|
89 |
+
# Add object and update display
|
90 |
+
add_button.click(
|
91 |
+
fn=add_object,
|
92 |
+
inputs=[object_class_input, bbox_input],
|
93 |
+
outputs=[objects_display]
|
94 |
+
)
|
95 |
+
|
96 |
+
# Generate image based on added objects
|
97 |
+
generate_button.click(
|
98 |
+
fn=generate_image,
|
99 |
+
inputs=[prompt, guidance_scale, num_inference_steps, randomize_seed, seed],
|
100 |
outputs=[result, seed]
|
101 |
)
|
102 |
|