zhiweili commited on
Commit
88b6d97
1 Parent(s): b611dc3

change base model

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. app_jugger.py +110 -0
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from app_haircolor_inpainting 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_jugger import create_demo as create_demo_haircolor
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
app_jugger.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ StableDiffusionXLImg2ImgPipeline
13
+ )
14
+
15
+ BASE_MODEL = "RunDiffusion/Juggernaut-XI-v11"
16
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
+
18
+ DEFAULT_EDIT_PROMPT = "a woman with blue hair"
19
+ 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"
20
+
21
+ DEFAULT_CATEGORY = "hair"
22
+
23
+ basepipeline = StableDiffusionXLImg2ImgPipeline.from_pretrained(
24
+ BASE_MODEL,
25
+ torch_dtype=torch.float16,
26
+ variant="fp16",
27
+ use_safetensors=True,
28
+ )
29
+
30
+ basepipeline = basepipeline.to(DEVICE)
31
+
32
+
33
+ @spaces.GPU(duration=15)
34
+ def image_to_image(
35
+ input_image: Image,
36
+ edit_prompt: str,
37
+ seed: int,
38
+ num_steps: int,
39
+ guidance_scale: float,
40
+ ):
41
+ run_task_time = 0
42
+ time_cost_str = ''
43
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
44
+ generator = torch.Generator(device=DEVICE).manual_seed(seed)
45
+ generated_image = basepipeline(
46
+ generator=generator,
47
+ prompt=edit_prompt,
48
+ negative_prompt=DEFAULT_NEGATIVE_PROMPT,
49
+ image=input_image,
50
+ guidance_scale=guidance_scale,
51
+ num_inference_steps = num_steps,
52
+ ).images[0]
53
+
54
+ run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
55
+
56
+ return generated_image, time_cost_str
57
+
58
+ def get_time_cost(run_task_time, time_cost_str):
59
+ now_time = int(time.time()*1000)
60
+ if run_task_time == 0:
61
+ time_cost_str = 'start'
62
+ else:
63
+ if time_cost_str != '':
64
+ time_cost_str += f'-->'
65
+ time_cost_str += f'{now_time - run_task_time}'
66
+ run_task_time = now_time
67
+ return run_task_time, time_cost_str
68
+
69
+ def create_demo() -> gr.Blocks:
70
+ with gr.Blocks() as demo:
71
+ croper = gr.State()
72
+ with gr.Row():
73
+ with gr.Column():
74
+ edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
75
+ generate_size = gr.Number(label="Generate Size", value=1024)
76
+ category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
77
+ with gr.Column():
78
+ num_steps = gr.Slider(minimum=1, maximum=100, value=15, step=1, label="Num Steps")
79
+ guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
80
+ mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
81
+ with gr.Column():
82
+ mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
83
+ seed = gr.Number(label="Seed", value=8)
84
+ g_btn = gr.Button("Edit Image")
85
+
86
+ with gr.Row():
87
+ with gr.Column():
88
+ input_image = gr.Image(label="Input Image", type="pil")
89
+ with gr.Column():
90
+ restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
91
+ with gr.Column():
92
+ origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
93
+ generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
94
+ generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
95
+
96
+ g_btn.click(
97
+ fn=segment_image,
98
+ inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
99
+ outputs=[origin_area_image, croper],
100
+ ).success(
101
+ fn=image_to_image,
102
+ inputs=[origin_area_image, edit_prompt,seed, num_steps, guidance_scale],
103
+ outputs=[generated_image, generated_cost],
104
+ ).success(
105
+ fn=restore_result,
106
+ inputs=[croper, category, generated_image],
107
+ outputs=[restored_image],
108
+ )
109
+
110
+ return demo