alfredplpl commited on
Commit
a6ac31b
1 Parent(s): c14628b

Update app.py

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