ngoctuanai commited on
Commit
3279038
1 Parent(s): 6bb218c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -293
app.py CHANGED
@@ -1,295 +1,3 @@
1
  import gradio as gr
2
 
3
- gr.load("models/runwayml/stable-diffusion-v1-5").launch()
4
-
5
- model_id = "runwayml/stable-diffusion-v1-5"
6
- device = "cuda"
7
-
8
- word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)
9
- word_list = word_list_dataset["train"]['text']
10
-
11
- is_gpu_busy = False
12
- def infer(prompt):
13
- global is_gpu_busy
14
- samples = 4
15
- steps = 50
16
- scale = 7.5
17
- for filter in word_list:
18
- if re.search(rf"\b{filter}\b", prompt):
19
- raise gr.Error("Unsafe content found. Please try again with different prompts.")
20
-
21
- images = []
22
- url = os.getenv('JAX_BACKEND_URL')
23
- payload = {'prompt': prompt}
24
- images_request = requests.post(url, json = payload)
25
- for image in images_request.json()["images"]:
26
- image_b64 = (f"data:image/jpeg;base64,{image}")
27
- images.append(image_b64)
28
-
29
- return images
30
-
31
-
32
- css = """
33
- .gradio-container {
34
- font-family: 'IBM Plex Sans', sans-serif;
35
- }
36
- .gr-button {
37
- color: white;
38
- border-color: black;
39
- background: black;
40
- }
41
- input[type='range'] {
42
- accent-color: black;
43
- }
44
- .dark input[type='range'] {
45
- accent-color: #dfdfdf;
46
- }
47
- .container {
48
- max-width: 730px;
49
- margin: auto;
50
- padding-top: 1.5rem;
51
- }
52
- #gallery {
53
- min-height: 22rem;
54
- margin-bottom: 15px;
55
- margin-left: auto;
56
- margin-right: auto;
57
- border-bottom-right-radius: .5rem !important;
58
- border-bottom-left-radius: .5rem !important;
59
- }
60
- #gallery>div>.h-full {
61
- min-height: 20rem;
62
- }
63
- .details:hover {
64
- text-decoration: underline;
65
- }
66
- .gr-button {
67
- white-space: nowrap;
68
- }
69
- .gr-button:focus {
70
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
71
- outline: none;
72
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
73
- --tw-border-opacity: 1;
74
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
75
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
76
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
77
- --tw-ring-opacity: .5;
78
- }
79
- #advanced-btn {
80
- font-size: .7rem !important;
81
- line-height: 19px;
82
- margin-top: 12px;
83
- margin-bottom: 12px;
84
- padding: 2px 8px;
85
- border-radius: 14px !important;
86
- }
87
- #advanced-options {
88
- display: none;
89
- margin-bottom: 20px;
90
- }
91
- .footer {
92
- margin-bottom: 45px;
93
- margin-top: 35px;
94
- text-align: center;
95
- border-bottom: 1px solid #e5e5e5;
96
- }
97
- .footer>p {
98
- font-size: .8rem;
99
- display: inline-block;
100
- padding: 0 10px;
101
- transform: translateY(10px);
102
- background: white;
103
- }
104
- .dark .footer {
105
- border-color: #303030;
106
- }
107
- .dark .footer>p {
108
- background: #0b0f19;
109
- }
110
- .acknowledgments h4{
111
- margin: 1.25em 0 .25em 0;
112
- font-weight: bold;
113
- font-size: 115%;
114
- }
115
- #container-advanced-btns{
116
- display: flex;
117
- flex-wrap: wrap;
118
- justify-content: space-between;
119
- align-items: center;
120
- }
121
- .animate-spin {
122
- animation: spin 1s linear infinite;
123
- }
124
- @keyframes spin {
125
- from {
126
- transform: rotate(0deg);
127
- }
128
- to {
129
- transform: rotate(360deg);
130
- }
131
- }
132
- #share-btn-container {
133
- display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
134
- }
135
- #share-btn {
136
- all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
137
- }
138
- #share-btn * {
139
- all: unset;
140
- }
141
- .gr-form{
142
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
143
- }
144
- #prompt-container{
145
- gap: 0;
146
- }
147
- #share-btn-container div:nth-child(-n+2){
148
- width: auto !important;
149
- min-height: 0px !important;
150
- }
151
- """
152
-
153
- block = gr.Blocks(css=css)
154
-
155
- examples = [
156
- [
157
- 'The spirit of a tamagotchi wandering in the city of Paris',
158
- # 4,
159
- # 45,
160
- # 7.5,
161
- # 1024,
162
- ],
163
- [
164
- 'A delicious ceviche cheesecake slice',
165
- # 4,
166
- # 45,
167
- # 7,
168
- # 1024,
169
- ],
170
- [
171
- 'A pao de queijo foodcart in front of a japanese castle',
172
- # 4,
173
- # 45,
174
- # 7,
175
- # 1024,
176
- ],
177
- [
178
- 'alone in the amusement park by Edward Hopper',
179
- # 4,
180
- # 45,
181
- # 7,
182
- # 1024,
183
- ],
184
- [
185
- "A large cabin on top of a sunny mountain in the style of Dreamworks, artstation",
186
- # 4,
187
- # 45,
188
- # 7,
189
- # 1024,
190
- ],
191
- ]
192
-
193
-
194
- with block:
195
- gr.HTML(
196
- """
197
- <div style="text-align: center; max-width: 650px; margin: 0 auto; padding-top: 7px;">
198
- <div
199
- style="
200
- display: inline-flex;
201
- align-items: center;
202
- gap: 0.8rem;
203
- font-size: 1.75rem;
204
- "
205
- >
206
- <h1 style="font-weight: 900; margin-bottom: 7px;">
207
- Stable Diffusion v1.5
208
- </h1>
209
- </div>
210
- <p style="margin-bottom: 10px; font-size: 94%">
211
- Stable Diffusion v1-5 is the latest version of the state of the art text-to-image model.
212
- </p>
213
- </div>
214
- """
215
- )
216
- with gr.Group():
217
- with gr.Box():
218
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
219
- text = gr.Textbox(
220
- label="Enter your prompt",
221
- show_label=False,
222
- max_lines=1,
223
- placeholder="Enter your prompt",
224
- elem_id="prompt-text-input",
225
- ).style(
226
- border=(True, False, True, True),
227
- rounded=(True, False, False, True),
228
- container=False,
229
- )
230
- btn = gr.Button("Generate image").style(
231
- margin=False,
232
- rounded=(False, True, True, False),
233
- full_width=False,
234
- )
235
-
236
- gallery = gr.Gallery(
237
- label="Generated images", show_label=False, elem_id="gallery"
238
- ).style(grid=[2], height="auto")
239
-
240
- with gr.Group(elem_id="container-advanced-btns"):
241
- advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
242
- with gr.Group(elem_id="share-btn-container"):
243
- community_icon = gr.HTML(community_icon_html)
244
- loading_icon = gr.HTML(loading_icon_html)
245
- share_button = gr.Button("Share to community", elem_id="share-btn")
246
-
247
- with gr.Row(elem_id="advanced-options"):
248
- gr.Markdown("Advanced settings are temporarily unavailable")
249
- samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
250
- steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
251
- scale = gr.Slider(
252
- label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
253
- )
254
- seed = gr.Slider(
255
- label="Seed",
256
- minimum=0,
257
- maximum=2147483647,
258
- step=1,
259
- randomize=True,
260
- )
261
-
262
- ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery], cache_examples=True, postprocess=False)
263
- ex.dataset.headers = [""]
264
-
265
- text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
266
- btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
267
-
268
- advanced_button.click(
269
- None,
270
- [],
271
- text,
272
- _js="""
273
- () => {
274
- const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
275
- options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
276
- }""",
277
- )
278
- share_button.click(
279
- None,
280
- [],
281
- [],
282
- _js=share_js,
283
- )
284
- gr.HTML(
285
- """
286
- <div class="footer">
287
- <p>Model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://runwayml.com/" style="text-decoration: underline;" target="_blank">Runway</a> supported by NgocTuanAI" style="text-decoration: underline;" target="_blank">Stability AI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a>
288
- </p>
289
- </div>
290
- <div class="acknowledgments">
291
- </div>
292
- """
293
- )
294
-
295
- block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)
 
1
  import gradio as gr
2
 
3
+ gr.load("models/runwayml/stable-diffusion-v1-5").launch()