AP123 commited on
Commit
453ed2e
1 Parent(s): 1992e2d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import os
3
+ import gradio as gr
4
+ from PIL import Image
5
+ from diffusers import (
6
+ StableDiffusionPipeline,
7
+ StableDiffusionControlNetImg2ImgPipeline,
8
+ ControlNetModel,
9
+ DDIMScheduler,
10
+ DPMSolverMultistepScheduler,
11
+ DEISMultistepScheduler,
12
+ HeunDiscreteScheduler,
13
+ EulerDiscreteScheduler,
14
+ )
15
+
16
+ # Initialize ControlNet model
17
+ controlnet = ControlNetModel.from_pretrained(
18
+ "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
19
+ )
20
+
21
+ # Initialize pipeline
22
+ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
23
+ "XpucT/Deliberate",
24
+ controlnet=controlnet,
25
+ safety_checker=None,
26
+ torch_dtype=torch.float16,
27
+ ).to("cuda")
28
+ pipe.enable_xformers_memory_efficient_attention()
29
+
30
+ # Sampler configurations
31
+ SAMPLER_MAP = {
32
+ "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
33
+ "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
34
+ }
35
+
36
+ # Inference function
37
+ def inference(
38
+ input_image: Image.Image,
39
+ prompt: str,
40
+ negative_prompt: str,
41
+ guidance_scale: float = 10.0,
42
+ controlnet_conditioning_scale: float = 1.0,
43
+ strength: float = 0.8,
44
+ seed: int = -1,
45
+ sampler = "DPM++ Karras SDE",
46
+ ):
47
+ if prompt is None or prompt == "":
48
+ raise gr.Error("Prompt is required")
49
+
50
+ pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
51
+ generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
52
+
53
+ out = pipe(
54
+ prompt=prompt,
55
+ negative_prompt=negative_prompt,
56
+ image=input_image,
57
+ control_image=input_image, # type: ignore
58
+ width=512, # type: ignore
59
+ height=512, # type: ignore
60
+ guidance_scale=float(guidance_scale),
61
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
62
+ generator=generator,
63
+ strength=float(strength),
64
+ num_inference_steps=40,
65
+ )
66
+ return out.images[0] # type: ignore
67
+
68
+ # Gradio UI
69
+ with gr.Blocks() as app:
70
+ gr.Markdown(
71
+ '''
72
+ # Illusion Diffusion
73
+ ## A simple UI for generating beatiful illusion art with Stable Diffusion 1.5
74
+ '''
75
+ )
76
+
77
+ with gr.Row():
78
+ with gr.Column():
79
+ input_image = gr.Image(label="Input Illusion", type="pil")
80
+ prompt = gr.Textbox(label="Prompt", info="Prompt that guides the generation towards")
81
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="ugly, disfigured, low quality, blurry, nsfw")
82
+ with gr.Accordion(label="Advanced Options", open=False):
83
+ controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=5.0, step=0.01, value=1.1, label="Controlnet Conditioning Scale")
84
+ strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength")
85
+ guidance_scale = gr.Slider(minimum=0.0, maximum=50.0, step=0.25, value=7.5, label="Guidance Scale")
86
+ sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE")
87
+ seed = gr.Slider(minimum=-1, maximum=9999999999, step=1, value=2313123, label="Seed", randomize=True)
88
+ run_btn = gr.Button("Run")
89
+ with gr.Column():
90
+ result_image = gr.Image(label="Illusion Diffusion Output")
91
+
92
+ run_btn.click(
93
+ inference,
94
+ inputs=[input_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, strength, seed, sampler],
95
+ outputs=[result_image]
96
+ )
97
+
98
+ app.queue(concurrency_count=4, max_size=20)
99
+
100
+ if __name__ == "__main__":
101
+ app.launch(debug=True)