zhiweili commited on
Commit
518257b
1 Parent(s): f04f727

add app_haircolor_pix2pix_sdxl

Browse files
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from app_haircolor_img2img import create_demo as create_demo_haircolor
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
 
1
  import gradio as gr
2
 
3
+ from app_haircolor_pix2pix_sdxl import create_demo as create_demo_haircolor
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
app_haircolor_pix2pix.py CHANGED
@@ -26,7 +26,7 @@ BASE_MODEL = "timbrooks/instruct-pix2pix"
26
 
27
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
28
 
29
- DEFAULT_EDIT_PROMPT = "turn hair into blue"
30
  DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face"
31
 
32
  DEFAULT_CATEGORY = "hair"
 
26
 
27
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
28
 
29
+ DEFAULT_EDIT_PROMPT = "change hair to blue"
30
  DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face"
31
 
32
  DEFAULT_CATEGORY = "hair"
app_haircolor_pix2pix_sdxl.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import time
4
+ import torch
5
+ import numpy as np
6
+ import cv2
7
+
8
+ from PIL import Image
9
+ from segment_utils import(
10
+ segment_image,
11
+ restore_result,
12
+ )
13
+ from diffusers import (
14
+ StableDiffusionXLInstructPix2PixPipeline,
15
+ EulerAncestralDiscreteScheduler,
16
+ )
17
+
18
+ from controlnet_aux import (
19
+ CannyDetector,
20
+ LineartDetector,
21
+ PidiNetDetector,
22
+ HEDdetector,
23
+ )
24
+
25
+ BASE_MODEL = "diffusers/sdxl-instructpix2pix-768"
26
+
27
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
28
+
29
+ DEFAULT_EDIT_PROMPT = "change hair to blue"
30
+ DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face"
31
+
32
+ DEFAULT_CATEGORY = "hair"
33
+
34
+ basepipeline = StableDiffusionXLInstructPix2PixPipeline.from_pretrained(
35
+ BASE_MODEL,
36
+ torch_dtype=torch.float16,
37
+ use_safetensors=True,
38
+ )
39
+
40
+ basepipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(basepipeline.scheduler.config)
41
+
42
+ basepipeline = basepipeline.to(DEVICE)
43
+
44
+ basepipeline.enable_model_cpu_offload()
45
+
46
+ @spaces.GPU(duration=30)
47
+ def image_to_image(
48
+ input_image: Image,
49
+ edit_prompt: str,
50
+ seed: int,
51
+ num_steps: int,
52
+ guidance_scale: float,
53
+ image_guidance_scale: float,
54
+ generate_size: int,
55
+ ):
56
+ run_task_time = 0
57
+ time_cost_str = ''
58
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
59
+
60
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
61
+ generated_image = basepipeline(
62
+ generator=generator,
63
+ prompt=edit_prompt,
64
+ image=input_image,
65
+ guidance_scale=guidance_scale,
66
+ image_guidance_scale=image_guidance_scale,
67
+ num_inference_steps=num_steps,
68
+ ).images[0]
69
+
70
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
71
+
72
+ return generated_image, time_cost_str
73
+
74
+ def get_time_cost(run_task_time, time_cost_str):
75
+ now_time = int(time.time()*1000)
76
+ if run_task_time == 0:
77
+ time_cost_str = 'start'
78
+ else:
79
+ if time_cost_str != '':
80
+ time_cost_str += f'-->'
81
+ time_cost_str += f'{now_time - run_task_time}'
82
+ run_task_time = now_time
83
+ return run_task_time, time_cost_str
84
+
85
+ def create_demo() -> gr.Blocks:
86
+ with gr.Blocks() as demo:
87
+ croper = gr.State()
88
+ with gr.Row():
89
+ with gr.Column():
90
+ edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
91
+ generate_size = gr.Number(label="Generate Size", value=768)
92
+ with gr.Column():
93
+ num_steps = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Num Steps")
94
+ guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
95
+ with gr.Column():
96
+ image_guidance_scale = gr.Slider(minimum=0, maximum=30, value=1.5, step=0.1, label="Image Guidance Scale")
97
+ with gr.Accordion("Advanced Options", open=False):
98
+ mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
99
+ mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
100
+ seed = gr.Number(label="Seed", value=8)
101
+ category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
102
+ g_btn = gr.Button("Edit Image")
103
+
104
+ with gr.Row():
105
+ with gr.Column():
106
+ input_image = gr.Image(label="Input Image", type="pil")
107
+ with gr.Column():
108
+ restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
109
+ with gr.Column():
110
+ origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
111
+ generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
112
+ generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
113
+
114
+ g_btn.click(
115
+ fn=segment_image,
116
+ inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
117
+ outputs=[origin_area_image, croper],
118
+ ).success(
119
+ fn=image_to_image,
120
+ inputs=[origin_area_image, edit_prompt,seed, num_steps, guidance_scale, image_guidance_scale, generate_size],
121
+ outputs=[generated_image, generated_cost],
122
+ ).success(
123
+ fn=restore_result,
124
+ inputs=[croper, category, generated_image],
125
+ outputs=[restored_image],
126
+ )
127
+
128
+ return demo