karanam commited on
Commit
533501f
Β·
1 Parent(s): 586614c

Add application file

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from contextlib import nullcontext
2
+ import gradio as gr
3
+ import torch
4
+ from torch import autocast
5
+ from diffusers import StableDiffusionPipeline
6
+
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ context = autocast if device == "cuda" else nullcontext
10
+ dtype = torch.float16 if device == "cuda" else torch.float32
11
+
12
+ pipe = StableDiffusionPipeline.from_pretrained(
13
+ "arattinger/diffusion.model", torch_dtype=dtype)
14
+ pipe = pipe.to(device)
15
+
16
+ disable_safety = True
17
+
18
+ if disable_safety:
19
+ def null_safety(images, **kwargs):
20
+ return images, False
21
+ pipe.safety_checker = null_safety
22
+
23
+ def infer(prompt, n_samples, steps, scale):
24
+
25
+ with context("cuda"):
26
+ images = pipe(n_samples*[prompt], guidance_scale=scale, num_inference_steps=steps).images
27
+ return images
28
+
29
+ css = """
30
+ a {
31
+ color: inherit;
32
+ text-decoration: underline;
33
+ }
34
+ .gradio-container {
35
+ font-family: 'IBM Plex Sans', sans-serif;
36
+ }
37
+ .gr-button {
38
+ color: white;
39
+ border-color: #9d66e5;
40
+ background: #9d66e5;
41
+ }
42
+ input[type='range'] {
43
+ accent-color: #9d66e5;
44
+ }
45
+ .dark input[type='range'] {
46
+ accent-color: #dfdfdf;
47
+ }
48
+ .container {
49
+ max-width: 730px;
50
+ margin: auto;
51
+ padding-top: 1.5rem;
52
+ }
53
+ #gallery {
54
+ min-height: 22rem;
55
+ margin-bottom: 15px;
56
+ margin-left: auto;
57
+ margin-right: auto;
58
+ border-bottom-right-radius: .5rem !important;
59
+ border-bottom-left-radius: .5rem !important;
60
+ }
61
+ #gallery>div>.h-full {
62
+ min-height: 20rem;
63
+ }
64
+ .details:hover {
65
+ text-decoration: underline;
66
+ }
67
+ .gr-button {
68
+ white-space: nowrap;
69
+ }
70
+ .gr-button:focus {
71
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
72
+ outline: none;
73
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
74
+ --tw-border-opacity: 1;
75
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
76
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
77
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
78
+ --tw-ring-opacity: .5;
79
+ }
80
+ #advanced-options {
81
+ margin-bottom: 20px;
82
+ }
83
+ .footer {
84
+ margin-bottom: 45px;
85
+ margin-top: 35px;
86
+ text-align: center;
87
+ border-bottom: 1px solid #e5e5e5;
88
+ }
89
+ .footer>p {
90
+ font-size: .8rem;
91
+ display: inline-block;
92
+ padding: 0 10px;
93
+ transform: translateY(10px);
94
+ background: white;
95
+ }
96
+ .dark .logo{ filter: invert(1); }
97
+ .dark .footer {
98
+ border-color: #303030;
99
+ }
100
+ .dark .footer>p {
101
+ background: #0b0f19;
102
+ }
103
+ .acknowledgments h4{
104
+ margin: 1.25em 0 .25em 0;
105
+ font-weight: bold;
106
+ font-size: 115%;
107
+ }
108
+ """
109
+
110
+ block = gr.Blocks(css=css)
111
+
112
+ examples = [
113
+ [
114
+ 'Climber',
115
+ 2,
116
+ 7.5,
117
+ ],
118
+ ]
119
+
120
+ with block:
121
+ gr.HTML(
122
+ """
123
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
124
+ <div>
125
+ <h1 style="font-weight: 900; font-size: 3rem;">
126
+ Emoji text to image
127
+ </h1>
128
+ </div>
129
+ </div>
130
+ """
131
+ )
132
+ with gr.Group():
133
+ with gr.Box():
134
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
135
+ text = gr.Textbox(
136
+ label="Enter your prompt",
137
+ show_label=False,
138
+ max_lines=1,
139
+ placeholder="Enter your prompt",
140
+ ).style(
141
+ border=(True, False, True, True),
142
+ rounded=(True, False, False, True),
143
+ container=False,
144
+ )
145
+ btn = gr.Button("Generate image").style(
146
+ margin=False,
147
+ rounded=(False, True, True, False),
148
+ )
149
+
150
+ gallery = gr.Gallery(
151
+ label="Generated images", show_label=False, elem_id="gallery"
152
+ ).style(grid=[2], height="auto")
153
+
154
+
155
+ with gr.Row(elem_id="advanced-options"):
156
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=2, step=1)
157
+ steps = gr.Slider(label="Steps", minimum=5, maximum=50, value=25, step=5)
158
+ scale = gr.Slider(
159
+ label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
160
+ )
161
+
162
+
163
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, scale], outputs=gallery, cache_examples=False)
164
+ ex.dataset.headers = [""]
165
+
166
+
167
+ text.submit(infer, inputs=[text, samples, steps, scale], outputs=gallery)
168
+ btn.click(infer, inputs=[text, samples, steps, scale], outputs=gallery)
169
+ gr.HTML(
170
+ """
171
+ <div class="footer">
172
+ </div>
173
+ <div class="acknowledgments">
174
+ <p>Source and Inspiration from the lambda labs pokemon model</p>
175
+ </div>
176
+ """
177
+ )
178
+
179
+ block.launch()