Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,996 Bytes
6678b47 11c0865 6678b47 4b2ec41 6678b47 722e880 6678b47 722e880 6678b47 722e880 6678b47 11c0865 722e880 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 722e880 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 11c0865 6678b47 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import gradio as gr
import tempfile
import os
from utils.ui_utils import *
CANVAS_SIZE = 400
DEFAULT_GEN_SIZE = 512
def create_interface():
with gr.Blocks() as app:
# Add main title and project link
gr.Markdown("# A simplified implementation of Inpaint4Drag")
gr.Markdown("## [Visit our project page for more examples](https://visual-ai.github.io/inpaint4drag)")
# State variables
state = {
'canvas_size': gr.Number(value=CANVAS_SIZE, visible=False, precision=0),
'gen_size': gr.Number(value=DEFAULT_GEN_SIZE, visible=False, precision=0),
'points_list': gr.State(value=[]),
'inpaint_mask': gr.State(value=None)
}
with gr.Tab(label='Inpaint4Drag'):
with gr.Row():
# Draw Region Column
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">1. Draw Regions</p>""")
# Use ImageEditor for newer Gradio versions, fallback to Image with brush
try:
canvas = gr.ImageEditor(
label=" ",
height=CANVAS_SIZE,
width=CANVAS_SIZE,
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed")
)
except:
# Fallback for older Gradio versions
canvas = gr.Image(
type="numpy",
label=" ",
height=CANVAS_SIZE,
width=CANVAS_SIZE,
sources=["upload", "webcam", "clipboard"]
)
with gr.Row():
fit_btn = gr.Button("Resize Image")
# Control Points Column
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">2. Control Points</p>""")
input_img = gr.Image(
type="numpy",
label=" ",
height=CANVAS_SIZE,
width=CANVAS_SIZE,
interactive=True
)
with gr.Row():
undo_btn = gr.Button("Undo Point")
clear_btn = gr.Button("Clear Points")
# Results Column
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Results</p>""")
output_img = gr.Image(
type="numpy",
label=" ",
height=CANVAS_SIZE,
width=CANVAS_SIZE,
interactive=False
)
with gr.Row():
run_btn = gr.Button("Inpaint")
reset_btn = gr.Button("Reset All")
# Generation Parameters
with gr.Row():
inpaint_ks = gr.Slider(
minimum=0,
maximum=25,
value=5,
step=1,
label='How much to expand inpainting mask',
interactive=True
)
setup_events(
components={
'canvas': canvas,
'input_img': input_img,
'output_img': output_img,
'inpaint_ks': inpaint_ks,
},
state=state,
buttons={
'fit': fit_btn,
'undo': undo_btn,
'clear': clear_btn,
'run': run_btn,
'reset': reset_btn
}
)
return app
def setup_events(components, state, buttons):
# Reset and clear events
def setup_reset_events():
buttons['reset'].click(
clear_all,
[state['canvas_size']],
[components['canvas'], components['input_img'], components['output_img'],
state['points_list'], components['inpaint_ks'], state['inpaint_mask']]
)
components['canvas'].clear(
clear_all,
[state['canvas_size']],
[components['canvas'], components['input_img'], components['output_img'],
state['points_list'], components['inpaint_ks'], state['inpaint_mask']]
)
# Image manipulation events
def setup_image_events():
buttons['fit'].click(
clear_point,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['input_img']]
).then(
resize,
[components['canvas'], state['gen_size'], state['canvas_size']],
[components['canvas'], components['input_img'], components['output_img']]
)
# Canvas interaction events
def setup_canvas_events():
# Handle both ImageEditor and Image events
canvas_event = components['canvas'].change if hasattr(components['canvas'], 'change') else components['canvas'].edit
canvas_event(
visualize_user_drag,
[components['canvas'], state['points_list']],
[components['input_img']]
).then(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
)
components['inpaint_ks'].change(
visualize_user_drag,
[components['canvas'], state['points_list']],
[components['input_img']]
).then(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
)
# Input image events
def setup_input_events():
components['input_img'].select(
add_point,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['input_img']]
).then(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
)
# Point manipulation events
def setup_point_events():
buttons['undo'].click(
undo_point,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['input_img']]
).then(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
)
buttons['clear'].click(
clear_point,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['input_img']]
).then(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
)
# Processing events
def setup_processing_events():
buttons['run'].click(
preview_out_image,
[components['canvas'], state['points_list'], components['inpaint_ks']],
[components['output_img'], state['inpaint_mask']]
).then(
inpaint,
[components['output_img'], state['inpaint_mask']],
[components['output_img']]
)
# Setup all events
setup_reset_events()
setup_image_events()
setup_canvas_events()
setup_input_events()
setup_point_events()
setup_processing_events()
def main():
app = create_interface()
# HF Space compatible launch
app.queue().launch()
if __name__ == '__main__':
main() |