File size: 13,921 Bytes
55a3c9a e12ecfd 55a3c9a 1e6854d 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e5bd268 55a3c9a 1938a04 55a3c9a ebb8e2f 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a ebb8e2f 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a e12ecfd 55a3c9a |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
import os
import numpy as np
import io
# os.system("pip install gradio==4.29.0")
os.system("pip install opencv-python")
import cv2
import gradio as gr
import random
import warnings
import spaces
from PIL import Image
from S2I import Sketch2ImageController, css, scripts
dark_mode_theme = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'dark') {
url.searchParams.set('__theme', 'dark');
window.location.href = url.href;
}
}
"""
os.environ["TOKENIZERS_PARALLELISM"] = "false"
warnings.filterwarnings("ignore")
controller = Sketch2ImageController(gr)
def run_gpu(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag):
return controller.artwork(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag)
def run_cpu(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag):
return controller.artwork(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag)
def get_dark_mode():
return """
() => {
document.body.classList.toggle('dark');
}
"""
def clear_session():
return gr.update(value=None), gr.update(value=None)
def assign_gpu(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag):
if options == 'GPU':
decorated_run = spaces.GPU(run_gpu)
return decorated_run(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag)
else:
return run_cpu(options, img_init, text_init, prompt_template_init, style_name_init, seeds_init, val_r_values_init, faster_init, model_name_init, clear_flag)
def read_temp_file(temp_file_wrapper):
name = temp_file_wrapper.name
with open(temp_file_wrapper.name, 'rb') as f:
# Read the content of the file
file_content = f.read()
return file_content, name
def convert_to_pencil_sketch(image):
if image is None:
raise ValueError(f"Image at path {image} could not be loaded.")
# Converting it into grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Inverting the image
inverted_image = 255 - gray_image
# Blurring the image
blurred = cv2.GaussianBlur(inverted_image, (25, 25), 0)
inverted_blurred = 255 - blurred
# Creating the pencil sketch
pencil_sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
return pencil_sketch
def get_meta_from_image(input_img, type_image):
if input_img is None:
return gr.update(value=None)
file_content, _ = read_temp_file(input_img)
# Read the image using Pillow
img = Image.open(io.BytesIO(file_content)).convert("RGB")
img_np = np.array(img)
if type_image == 'RGB':
sketch = convert_to_pencil_sketch(img_np)
processed_img = 255 - sketch
elif type_image == 'SKETCH':
processed_img = 255 - img_np
# Convert the processed image back to PIL Image
img_pil = Image.fromarray(processed_img.astype('uint8'))
return img_pil
with gr.Blocks(css=css, theme="NoCrypt/miku@1.2.1") as demo:
gr.HTML(
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>S2I-Artwork Animation</title>
<style>
@keyframes blinkCursor {
from { border-right-color: rgba(255, 255, 255, 0.75); }
to { border-right-color: transparent; }
}
@keyframes fadeIn {
0% { opacity: 0; transform: translateY(-10px); }
100% { opacity: 1; transform: translateY(0); }
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-10px);
}
60% {
transform: translateY(-5px);
}
}
.typewriter h1 {
overflow: hidden;
border-right: .15em solid rgba(255, 255, 255, 0.75);
white-space: nowrap;
margin: 0 auto;
letter-spacing: .15em;
animation:
zoomInOut 4s infinite;
}
.animated-heading {
animation: fadeIn 2s ease-in-out;
}
.animated-link {
display: inline-block;
animation: bounce 3s infinite;
}
</style>
</head>
<body>
<div>
<div class="typewriter">
<h1 style="display: flex; align-items: center; justify-content: center; margin-bottom: 10px; text-align: center;">
<img src="https://imgur.com/H2SLps2.png" alt="icon" style="margin-left: 10px; height: 30px;">
S2I-Artwork
<img src="https://imgur.com/cNMKSAy.png" alt="icon" style="margin-left: 10px; height: 30px;">:
Personalized Sketch-to-Art 🧨 Diffusion Models
<img src="https://imgur.com/yDnDd1p.png" alt="icon" style="margin-left: 10px; height: 30px;">
</h1>
</div>
<h3 class="animated-heading" style="text-align: center; margin-bottom: 10px;">Authors: Vo Nguyen An Tin, Nguyen Thiet Su</h3>
<h4 class="animated-heading" style="margin-bottom: 10px;">*This project is the fine-tuning task with LorA on large datasets included: COCO-2017, LHQ, Danbooru, LandScape and Mid-Journey V6</h4>
<h4 class="animated-heading" style="margin-bottom: 10px;">* We public 2 sketch2image-models-lora training on 30K and 60K steps with skip-connection and Transformers Super-Resolution variables</h4>
<h4 class="animated-heading" style="margin-bottom: 10px;">* The inference and demo time of model is faster, you can slowly in the first runtime, but after that, the time process over 1.5 ~ 2s</h4>
<h4 class="animated-heading" style="margin-bottom: 10px;">* View the full code project:
<a class="animated-link" href="https://github.com/aihacker111/S2I-Artwork-Sketch-to-Image/" target="_blank">GitHub Repository</a>
</h4>
<h4 class="animated-heading" style="margin-bottom: 10px;">
<a class="animated-link" href="https://github.com/aihacker111/S2I-Artwork-Sketch-to-Image/" target="_blank">
<img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="100">
</a>
</h4>
</div>
</body>
</html>
"""
)
with gr.Row(elem_id="main_row"):
with gr.Column(elem_id="column_input"):
gr.Markdown("## SKETCH", elem_id="input_header")
image = gr.Sketchpad(
type="pil",
height=512,
width=512,
min_width=512,
image_mode="RGBA",
show_label=False,
mirror_webcam=False,
show_download_button=True,
elem_id='input_image',
brush=gr.Brush(colors=["#000000"], color_mode="fixed", default_size=4),
canvas_size=(1024, 1024),
layers=False
)
input_image = gr.File(label='Input image')
download_sketch = gr.Button(
"Download sketch", scale=1, elem_id="download_sketch"
)
with gr.Column(elem_id="column_output"):
gr.Markdown("## IMAGE GENERATE", elem_id="output_header")
result = gr.Image(
label="Result",
height=440,
width=440,
elem_id="output_image",
show_label=False,
show_download_button=True,
)
with gr.Row():
run_button = gr.Button("Generate 🪄", min_width=5, variant='primary')
randomize_seed = gr.Button(value='\U0001F3B2', variant='primary')
clear_button = gr.Button("Reset Sketch Session", min_width=10, variant='primary')
prompt = gr.Textbox(label="Personalized Text", value="", show_label=True)
with gr.Accordion("S2I Advances Option", open=True):
with gr.Row():
ui_mode = gr.Radio(
choices=["Light Mode", "Dark Mode"],
value="Light Mode",
label="Switch Light/Dark Mode UI",
interactive=True)
type_image = gr.Radio(
choices=["RGB", "SKETCH"],
value="SKETCH",
label="Type of Image (Color Image or Sketch Image)",
interactive=True)
input_type = gr.Radio(
choices=["live-sketch", "upload"],
value="live-sketch",
label="Type Sketch2Image models",
interactive=True)
style = gr.Dropdown(
label="Style",
choices=controller.STYLE_NAMES,
value=controller.DEFAULT_STYLE_NAME,
scale=1,
)
prompt_temp = gr.Textbox(
label="Prompt Style Template",
value=controller.styles[controller.DEFAULT_STYLE_NAME],
scale=2,
max_lines=1,
)
seed = gr.Textbox(label="Seed", value='42', scale=1, min_width=50)
zero_gpu_options = gr.Radio(
choices=["GPU", "CPU"],
value="GPU",
label="GPU & CPU Options Spaces",
interactive=True)
half_model = gr.Radio(
choices=["float32", "float16"],
value="float16",
label="Demo Speed",
interactive=True)
model_options = gr.Radio(
choices=["100k", "350k"],
value="350k",
label="Type Sketch2Image models",
interactive=True)
val_r = gr.Slider(
label="Sketch guidance: ",
show_label=True,
minimum=0,
maximum=1,
value=0.4,
step=0.01,
scale=3,
)
demo.load(None, None, None, js=scripts)
ui_mode.change(None, [], [], js=get_dark_mode())
randomize_seed.click(
lambda x: random.randint(0, controller.MAX_SEED),
inputs=[],
outputs=seed,
queue=False,
api_name=False,
)
inputs = [zero_gpu_options, image, prompt, prompt_temp, style, seed, val_r, half_model, model_options, input_type]
outputs = [result, download_sketch]
prompt.submit(fn=assign_gpu, inputs=inputs, outputs=outputs, api_name=False)
input_image.change(
fn=get_meta_from_image,
inputs=[
input_image, type_image
],
outputs=[
image
]
)
style.change(
lambda x: controller.styles[x],
inputs=[style],
outputs=[prompt_temp],
queue=False,
api_name=False,
).then(
fn=assign_gpu,
inputs=inputs,
outputs=outputs,
api_name=False,
)
clear_button.click(fn=clear_session, inputs=[], outputs=[image, result]).then(
fn=assign_gpu,
inputs=inputs,
outputs=outputs,
api_name=False,
)
val_r.change(assign_gpu, inputs=inputs, outputs=outputs, queue=False, api_name=False)
run_button.click(fn=assign_gpu, inputs=inputs, outputs=outputs, api_name=False)
image.change(assign_gpu, inputs=inputs, outputs=outputs, queue=False, api_name=False)
if __name__ == '__main__':
demo.queue()
demo.launch(debug=True, share=False)
|