aflyrt commited on
Commit
d79e56f
1 Parent(s): 2329d0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -128
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
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
8
 
9
- if torch.cuda.is_available():
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
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
22
 
23
- if randomize_seed:
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
- examples = [
41
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
- "An astronaut riding a green horse",
43
- "A delicious ceviche cheesecake slice",
44
- ]
45
 
46
- css="""
47
- #col-container {
48
- margin: 0 auto;
49
- max-width: 520px;
50
- }
51
- """
 
 
 
 
 
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
57
 
58
- with gr.Blocks(css=css) as demo:
59
-
60
- with gr.Column(elem_id="col-container"):
61
- gr.Markdown(f"""
62
- # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
- """)
65
-
66
- with gr.Row():
67
-
68
- prompt = gr.Text(
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
- with gr.Accordion("Advanced Settings", open=False):
81
-
82
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
  )
88
-
89
- seed = gr.Slider(
90
- label="Seed",
91
- minimum=0,
92
- maximum=MAX_SEED,
93
- step=1,
94
- value=0,
95
  )
96
-
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  with gr.Row():
100
-
101
- width = gr.Slider(
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
- guidance_scale = gr.Slider(
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
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
144
- )
145
 
146
- demo.queue().launch()
 
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)