akhaliq HF staff commited on
Commit
0225348
1 Parent(s): 7d10989

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
2
+ import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+
6
+ model_id = 'Norod78/sd2-dreambooth-ClaymationXmas'
7
+ prefix = ''
8
+
9
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
10
+
11
+ pipe = StableDiffusionPipeline.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
14
+ scheduler=scheduler)
15
+
16
+ pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
19
+ scheduler=scheduler)
20
+
21
+ if torch.cuda.is_available():
22
+ pipe = pipe.to("cuda")
23
+ pipe_i2i = pipe_i2i.to("cuda")
24
+
25
+ def error_str(error, title="Error"):
26
+ return f"""#### {title}
27
+ {error}""" if error else ""
28
+
29
+ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
30
+
31
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
32
+ prompt = f"{prefix} {prompt}" if auto_prefix else prompt
33
+
34
+ try:
35
+ if img is not None:
36
+ return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
37
+ else:
38
+ return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
39
+ except Exception as e:
40
+ return None, error_str(e)
41
+
42
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
43
+
44
+ result = pipe(
45
+ prompt,
46
+ negative_prompt = neg_prompt,
47
+ num_inference_steps = int(steps),
48
+ guidance_scale = guidance,
49
+ width = width,
50
+ height = height,
51
+ generator = generator)
52
+
53
+ return result.images[0]
54
+
55
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
56
+
57
+ ratio = min(height / img.height, width / img.width)
58
+ img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
59
+ result = pipe_i2i(
60
+ prompt,
61
+ negative_prompt = neg_prompt,
62
+ init_image = img,
63
+ num_inference_steps = int(steps),
64
+ strength = strength,
65
+ guidance_scale = guidance,
66
+ width = width,
67
+ height = height,
68
+ generator = generator)
69
+
70
+ return result.images[0]
71
+
72
+ css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
73
+ """
74
+ with gr.Blocks(css=css) as demo:
75
+ gr.HTML(
76
+ f"""
77
+ <div class="main-div">
78
+ <div>
79
+ <h1>Sd2 Dreambooth Claymationxmas</h1>
80
+ </div>
81
+ <p>
82
+ Demo for <a href="https://huggingface.co/Norod78/sd2-dreambooth-ClaymationXmas">Sd2 Dreambooth Claymationxmas</a> Stable Diffusion model.<br>
83
+ {"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
84
+ </p>
85
+ Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/akhaliq/sd2-dreambooth-ClaymationXmas/settings'>Settings</a></b>"}<br><br>
86
+ <a style="display:inline-block" href="https://huggingface.co/spaces/akhaliq/sd2-dreambooth-ClaymationXmas?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
87
+ </div>
88
+ """
89
+ )
90
+ with gr.Row():
91
+
92
+ with gr.Column(scale=55):
93
+ with gr.Group():
94
+ with gr.Row():
95
+ prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
96
+ generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
97
+
98
+ image_out = gr.Image(height=512)
99
+ error_output = gr.Markdown()
100
+
101
+ with gr.Column(scale=45):
102
+ with gr.Tab("Options"):
103
+ with gr.Group():
104
+ neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
105
+ auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically ()", value=prefix, visible=prefix)
106
+
107
+ with gr.Row():
108
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
109
+ steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
110
+
111
+ with gr.Row():
112
+ width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
113
+ height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
114
+
115
+ seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
116
+
117
+ with gr.Tab("Image to image"):
118
+ with gr.Group():
119
+ image = gr.Image(label="Image", height=256, tool="editor", type="pil")
120
+ strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
121
+
122
+ auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
123
+
124
+ inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, auto_prefix]
125
+ outputs = [image_out, error_output]
126
+ prompt.submit(inference, inputs=inputs, outputs=outputs)
127
+ generate.click(inference, inputs=inputs, outputs=outputs)
128
+
129
+ gr.HTML("""
130
+ <div style="border-top: 1px solid #303030;">
131
+ <br>
132
+ <p>This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p>
133
+ </div>
134
+ """)
135
+
136
+ demo.queue(concurrency_count=1)
137
+ demo.launch()