zhiweili commited on
Commit
2a3a686
1 Parent(s): 3a87f75

change app

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. app_text2img.py +158 -0
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from app_haircolor 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_text2img import create_demo as create_demo_haircolor
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
app_text2img.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import time
4
+ import torch
5
+
6
+ from PIL import Image
7
+ from segment_utils import(
8
+ segment_image,
9
+ restore_result,
10
+ )
11
+ from diffusers import (
12
+ StableDiffusionXLAdapterPipeline,
13
+ DiffusionPipeline,
14
+ T2IAdapter,
15
+ MultiAdapter,
16
+ )
17
+
18
+ from controlnet_aux import (
19
+ LineartDetector,
20
+ CannyDetector,
21
+ )
22
+
23
+ BASE_MODEL = "SG161222/RealVisXL_V5.0_Lightning"
24
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
25
+
26
+ DEFAULT_EDIT_PROMPT = "a woman, blue hair, high detailed"
27
+ 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"
28
+
29
+ DEFAULT_CATEGORY = "hair"
30
+
31
+ lineart_detector = LineartDetector.from_pretrained("lllyasviel/Annotators")
32
+ lineart_detector = lineart_detector.to(DEVICE)
33
+
34
+ canndy_detector = CannyDetector()
35
+
36
+ adapters = MultiAdapter(
37
+ [
38
+ T2IAdapter.from_pretrained(
39
+ "TencentARC/t2i-adapter-lineart-sdxl-1.0",
40
+ torch_dtype=torch.float16,
41
+ varient="fp16",
42
+ ),
43
+ T2IAdapter.from_pretrained(
44
+ "TencentARC/t2i-adapter-canny-sdxl-1.0",
45
+ torch_dtype=torch.float16,
46
+ varient="fp16",
47
+ ),
48
+ ]
49
+ )
50
+ adapters = adapters.to(torch.float16)
51
+
52
+
53
+ basepipeline = StableDiffusionXLAdapterPipeline.from_pretrained(
54
+ BASE_MODEL,
55
+ torch_dtype=torch.float16,
56
+ use_safetensors=True,
57
+ adapter=adapters,
58
+ )
59
+
60
+ basepipeline = basepipeline.to(DEVICE)
61
+
62
+ basepipeline.enable_model_cpu_offload()
63
+
64
+ @spaces.GPU(duration=30)
65
+ def image_to_image(
66
+ input_image: Image,
67
+ edit_prompt: str,
68
+ seed: int,
69
+ num_steps: int,
70
+ guidance_scale: float,
71
+ generate_size: int,
72
+ lineart_scale: float = 1.0,
73
+ canny_scale: float = 0.5,
74
+ ):
75
+ run_task_time = 0
76
+ time_cost_str = ''
77
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
78
+ lineart_image = lineart_detector(input_image, 384, generate_size)
79
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
80
+ canny_image = canndy_detector(input_image, 384, generate_size)
81
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
82
+
83
+ cond_image = [lineart_image, canny_image]
84
+ cond_scale = [lineart_scale, canny_scale]
85
+
86
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
87
+ generated_image = basepipeline(
88
+ generator=generator,
89
+ prompt=edit_prompt,
90
+ negative_prompt=DEFAULT_NEGATIVE_PROMPT,
91
+ image=cond_image,
92
+ height=generate_size,
93
+ width=generate_size,
94
+ guidance_scale=guidance_scale,
95
+ num_inference_steps=num_steps,
96
+ # adapter_image=cond_image,
97
+ adapter_conditioning_scale=cond_scale,
98
+ ).images[0]
99
+
100
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
101
+
102
+ return generated_image, time_cost_str
103
+
104
+ def get_time_cost(run_task_time, time_cost_str):
105
+ now_time = int(time.time()*1000)
106
+ if run_task_time == 0:
107
+ time_cost_str = 'start'
108
+ else:
109
+ if time_cost_str != '':
110
+ time_cost_str += f'-->'
111
+ time_cost_str += f'{now_time - run_task_time}'
112
+ run_task_time = now_time
113
+ return run_task_time, time_cost_str
114
+
115
+ def create_demo() -> gr.Blocks:
116
+ with gr.Blocks() as demo:
117
+ croper = gr.State()
118
+ with gr.Row():
119
+ with gr.Column():
120
+ edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
121
+ generate_size = gr.Number(label="Generate Size", value=512)
122
+ seed = gr.Number(label="Seed", value=8)
123
+ category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
124
+ with gr.Column():
125
+ num_steps = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Num Steps")
126
+ guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
127
+ mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
128
+ with gr.Column():
129
+ mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
130
+ lineart_scale = gr.Slider(minimum=0, maximum=2, value=0.3, step=0.1, label="Lineart Scale")
131
+ canny_scale = gr.Slider(minimum=0, maximum=2, value=0.7, step=0.1, label="Canny Scale")
132
+ g_btn = gr.Button("Edit Image")
133
+
134
+ with gr.Row():
135
+ with gr.Column():
136
+ input_image = gr.Image(label="Input Image", type="pil")
137
+ with gr.Column():
138
+ restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
139
+ with gr.Column():
140
+ origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
141
+ generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
142
+ generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
143
+
144
+ g_btn.click(
145
+ fn=segment_image,
146
+ inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
147
+ outputs=[origin_area_image, croper],
148
+ ).success(
149
+ fn=image_to_image,
150
+ inputs=[origin_area_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, lineart_scale, canny_scale],
151
+ outputs=[generated_image, generated_cost],
152
+ ).success(
153
+ fn=restore_result,
154
+ inputs=[croper, category, generated_image],
155
+ outputs=[restored_image],
156
+ )
157
+
158
+ return demo