mikonvergence commited on
Commit
81223b7
·
1 Parent(s): 1832e37

major app update

Browse files
Files changed (1) hide show
  1. app.py +87 -54
app.py CHANGED
@@ -55,11 +55,28 @@ if torch.cuda.is_available():
55
 
56
  # Functions
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def get_guide(image):
59
- return hed(image,scribble=True)
60
 
61
  def create_demo(max_images=12, default_num_images=3):
62
- with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"])) as demo:
63
 
64
  gr.Markdown('# Cut and Sketch ✂️▶️✏️')
65
  with gr.Accordion('Instructions', open=False):
@@ -71,59 +88,70 @@ def create_demo(max_images=12, default_num_images=3):
71
  gr.Markdown('4. Now, you can **sketch a replacement** object! (Sketch ✏️)')
72
  gr.Markdown('5. (You can also provide a **text prompt** if you want)')
73
  gr.Markdown('6. 🔮 Click `Generate` when ready! ')
 
74
 
75
- with gr.Row() as main_blocks:
76
- with gr.Column() as step_1:
77
- gr.Markdown('### Mask Input')
78
- input_image = gr.Image(source='upload',
79
- shape=[HEIGHT,WIDTH],
80
- type='numpy',
81
- elem_id="image-upload",
82
- label='Mask Draw (Cut!)',
83
- tool='sketch',
84
- brush_radius=80)
85
- mask_button = gr.Button(label='Set Mask', value='Set Mask',full_width=False)
86
- with gr.Column(visible=False) as step_2:
87
- gr.Markdown('### Sketch Input')
88
- sketch_image = gr.Image(source='upload',
89
- shape=[HEIGHT,WIDTH],
90
- type='numpy',
91
- label='Fill Draw (Sketch!)',
92
- tool='sketch',
93
- brush_radius=15)
94
- run_button = gr.Button(label='Generate', value='Generate')
95
- prompt = gr.Textbox(label='Prompt')
96
-
97
- with gr.Column() as output_step:
98
- gr.Markdown('### Output')
99
- output_image = gr.Gallery(
100
- label="Generated images",
101
- show_label=False,
102
- elem_id="gallery",
103
- )
104
-
105
- with gr.Accordion('Advanced options', open=False):
106
- num_steps = gr.Slider(label='Steps',
107
- minimum=1,
108
- maximum=100,
109
- value=20,
110
- step=1)
111
- text_scale = gr.Slider(label='Text Guidance Scale',
112
- minimum=0.1,
113
- maximum=30.0,
114
- value=7.5,
115
- step=0.1)
116
- seed = gr.Slider(label='Seed',
117
- minimum=-1,
118
- maximum=2147483647,
119
- step=1,
120
- randomize=True)
121
-
122
- sketch_scale = gr.Slider(label='Sketch Guidance Scale',
123
- minimum=0.0,
124
- maximum=1.0,
125
- value=1.0,
126
- step=0.05)
 
 
 
 
 
 
 
 
 
 
127
 
128
  inputs = [
129
  sketch_image,
@@ -187,7 +215,12 @@ def create_demo(max_images=12, default_num_images=3):
187
  step_1: gr.update(visible=True),
188
  step_2: gr.update(visible=False)
189
  }
 
 
 
 
190
 
 
191
  mask_button.click(fn=set_mask, inputs=[input_image], outputs=[input_image, sketch_image, step_1,step_2])
192
  run_button.click(fn=generate, inputs=inputs, outputs=[output_image, step_1,step_2])
193
  return demo
 
55
 
56
  # Functions
57
 
58
+ css='''
59
+ .container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
60
+ .image_upload{min-height:500px}
61
+ .image_upload [data-testid="image"], .image_upload [data-testid="image"] > div{min-height: 500px}
62
+ .image_upload [data-testid="sketch"], .image_upload [data-testid="sketch"] > div{min-height: 500px}
63
+ .image_upload .touch-none{display: flex}
64
+ #output_image{min-height:500px;max-height=500px;}
65
+ @keyframes spin {
66
+ from {
67
+ transform: rotate(0deg);
68
+ }
69
+ to {
70
+ transform: rotate(360deg);
71
+ }
72
+ }
73
+ '''
74
+
75
  def get_guide(image):
76
+ return hed(image,scribble=True)
77
 
78
  def create_demo(max_images=12, default_num_images=3):
79
+ with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"]), css=css) as demo:
80
 
81
  gr.Markdown('# Cut and Sketch ✂️▶️✏️')
82
  with gr.Accordion('Instructions', open=False):
 
88
  gr.Markdown('4. Now, you can **sketch a replacement** object! (Sketch ✏️)')
89
  gr.Markdown('5. (You can also provide a **text prompt** if you want)')
90
  gr.Markdown('6. 🔮 Click `Generate` when ready! ')
91
+ example_button=gr.Button(label='example',value='Try example image!').style(full_width=False, size='sm')
92
 
93
+ with gr.Group():
94
+ with gr.Box():
95
+ with gr.Column():
96
+ with gr.Row() as main_blocks:
97
+ with gr.Column() as step_1:
98
+ gr.Markdown('### Mask Input')
99
+ image = gr.Image(source='upload',
100
+ shape=[HEIGHT,WIDTH],
101
+ type='numpy',
102
+ elem_classes="image_upload",
103
+ label='Mask Draw (Cut!)',
104
+ tool='sketch',
105
+ brush_radius=60).style(height=500)
106
+ input_image=image
107
+ mask_button = gr.Button(label='Set Mask', value='Set Mask')
108
+ with gr.Column(visible=False) as step_2:
109
+ gr.Markdown('### Sketch Input')
110
+ sketch = gr.Image(source='upload',
111
+ shape=[HEIGHT,WIDTH],
112
+ type='numpy',
113
+ elem_classes="image_upload",
114
+ label='Fill Draw (Sketch!)',
115
+ tool='sketch',
116
+ brush_radius=10).style(height=500)
117
+ sketch_image=sketch
118
+ run_button = gr.Button(label='Generate', value='Generate')
119
+ prompt = gr.Textbox(label='Prompt')
120
+
121
+ with gr.Column() as output_step:
122
+ gr.Markdown('### Output')
123
+ output_image = gr.Gallery(
124
+ label="Generated images",
125
+ show_label=False,
126
+ elem_id="output_image",
127
+ ).style(height=500,containter=True)
128
+ with gr.Accordion('Advanced options', open=False):
129
+ num_steps = gr.Slider(label='Steps',
130
+ minimum=1,
131
+ maximum=100,
132
+ value=20,
133
+ step=1)
134
+ text_scale = gr.Slider(label='Text Guidance Scale',
135
+ minimum=0.1,
136
+ maximum=30.0,
137
+ value=7.5,
138
+ step=0.1)
139
+ seed = gr.Slider(label='Seed',
140
+ minimum=-1,
141
+ maximum=2147483647,
142
+ step=1,
143
+ randomize=True)
144
+
145
+ sketch_scale = gr.Slider(label='Sketch Guidance Scale',
146
+ minimum=0.0,
147
+ maximum=1.0,
148
+ value=1.0,
149
+ step=0.05)
150
+
151
+ with gr.Accordion('More Info', open=False):
152
+ gr.Markdown('This demo was created by Mikolaj Czerkawski [@mikonvergence](https://twitter.com/mikonvergence) based on the 🌱 open-source implementation of [ControlNetInpaint](https://github.com/mikonvergence/ControlNetInpaint) (diffusers-friendly!).')
153
+ gr.Markdown('The tool currently only works with image resolution of 512px.')
154
+ gr.Markdown('💡 To learn more about diffusion with interactive code, check out my open-source ⏩[DiffusionFastForward](https://github.com/mikonvergence/DiffusionFastForward) course. It contains example code, executable notebooks, videos, notes, and a few use cases for training from scratch!')
155
 
156
  inputs = [
157
  sketch_image,
 
215
  step_1: gr.update(visible=True),
216
  step_2: gr.update(visible=False)
217
  }
218
+
219
+ def example_fill():
220
+
221
+ return Image.open('data/xp-love.jpg')
222
 
223
+ example_button.click(fn=example_fill, outputs=[input_image])
224
  mask_button.click(fn=set_mask, inputs=[input_image], outputs=[input_image, sketch_image, step_1,step_2])
225
  run_button.click(fn=generate, inputs=inputs, outputs=[output_image, step_1,step_2])
226
  return demo