AdamOswald1 commited on
Commit
50186c5
·
1 Parent(s): 8966da4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -1
app.py CHANGED
@@ -209,4 +209,161 @@ Despite how impressive being able to turn text into image is, beware to the fact
209
  </div>
210
  """
211
  )
212
- demo.queue(max_size=25).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  </div>
210
  """
211
  )
212
+ demo.queue(max_size=25).launch()
213
+
214
+ from diffusers import StableDiffusionPipeline
215
+ import gradio as gr
216
+ import torch
217
+ import os
218
+
219
+ from share_btn import community_icon_html, loading_icon_html, share_js
220
+
221
+ if torch.cuda.is_available():
222
+ torchfloat = torch.float16
223
+ else:
224
+ torchfloat = torch.float32
225
+
226
+
227
+ models = [
228
+ "DGSpitzer/Cyberpunk-Anime-Diffusion"
229
+ ]
230
+
231
+ prompt_prefixes = {
232
+ models[0]: "dgs illustration style "
233
+ }
234
+
235
+ current_model = models[0]
236
+
237
+ #auth_token = os.environ.get("test") or True
238
+ #pipe = StableDiffusionPipeline.from_pretrained(current_model, use_auth_token=auth_token, torch_dtype=torchfloat, revision="fp16")
239
+ pipe = StableDiffusionPipeline.from_pretrained(current_model, torch_dtype=torchfloat, revision="fp16")
240
+
241
+ if torch.cuda.is_available():
242
+ pipe = pipe.to("cuda")
243
+ else:
244
+ pipe = pipe.to("cpu")
245
+
246
+ device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
247
+
248
+ def on_model_change(model):
249
+
250
+ global current_model
251
+ global pipe
252
+ if model != current_model:
253
+ current_model = model
254
+ pipe = StableDiffusionPipeline.from_pretrained(current_model, torch_dtype=torchfloat)
255
+ if torch.cuda.is_available():
256
+ pipe = pipe.to("cuda")
257
+
258
+ def inference(prompt, guidance, steps):
259
+
260
+ promptPrev = prompt
261
+ prompt = prompt_prefixes[current_model] + prompt
262
+ image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance, width=512, height=512).images[0]
263
+ return image, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(placeholder=promptPrev)
264
+
265
+ def inference_example(prompt, guidance, steps):
266
+
267
+ prompt = prompt_prefixes[current_model] + prompt
268
+ image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance, width=512, height=512).images[0]
269
+ return image
270
+
271
+ css = """
272
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
273
+ a {text-decoration-line: underline; font-weight: 600;}
274
+ .animate-spin {
275
+ animation: spin 1s linear infinite;
276
+ }
277
+ @keyframes spin {
278
+ from {
279
+ transform: rotate(0deg);
280
+ }
281
+ to {
282
+ transform: rotate(360deg);
283
+ }
284
+ }
285
+ #share-btn-container {
286
+ 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;
287
+ }
288
+ #share-btn {
289
+ 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;right:0;
290
+ }
291
+ #share-btn * {
292
+ all: unset;
293
+ }
294
+ #share-btn-container div:nth-child(-n+2){
295
+ width: auto !important;
296
+ min-height: 0px !important;
297
+ }
298
+ #share-btn-container .wrap {
299
+ display: none !important;
300
+ }
301
+ """
302
+
303
+ with gr.Blocks(css=css) as demo:
304
+ gr.HTML(
305
+ """
306
+ <div style="text-align: center; max-width: 700px; margin: 0 auto;">
307
+ <div
308
+ style="
309
+ display: inline-flex;
310
+ align-items: center;
311
+ gap: 0.8rem;
312
+ font-size: 1.75rem;
313
+ "
314
+ >
315
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
316
+ DGS Diffusion Space
317
+ </h1>
318
+ </div>
319
+ <p style="margin-bottom: 10px; font-size: 94%">
320
+ Demo for Cyberpunk Anime Diffusion. Based of the projects by anzorq and fffiloni <a href="https://twitter.com/hahahahohohe">
321
+ </p>
322
+ </div>
323
+ """
324
+ )
325
+ gr.Markdown('''
326
+ 👇 Buy me a coffee if you like ♥ this project~ 👇 Running this server costs me $100 per week, any help would be much appreciated!
327
+ [![Buy me a coffee](https://badgen.net/badge/icon/Buy%20Me%20A%20Coffee?icon=buymeacoffee&label)](https://www.buymeacoffee.com/dgspitzer)
328
+ ''')
329
+ with gr.Row():
330
+
331
+ with gr.Column():
332
+ model = gr.Dropdown(label="Model", choices=models, value=models[0])
333
+ prompt = gr.Textbox(label="Prompt", placeholder="{} is added automatically".format(prompt_prefixes[current_model]), elem_id="input-prompt")
334
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
335
+ steps = gr.Slider(label="Steps", value=27, maximum=100, minimum=2)
336
+ run = gr.Button(value="Run")
337
+ gr.Markdown(f"Running on: {device}")
338
+ with gr.Column():
339
+ image_out = gr.Image(height=512, type="filepath", elem_id="output-img")
340
+
341
+ with gr.Column(elem_id="col-container"):
342
+ with gr.Group(elem_id="share-btn-container"):
343
+ community_icon = gr.HTML(community_icon_html, visible=False)
344
+ loading_icon = gr.HTML(loading_icon_html, visible=False)
345
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
346
+
347
+ model.change(on_model_change, inputs=model, outputs=[])
348
+ run.click(inference, inputs=[prompt, guidance, steps], outputs=[image_out, share_button, community_icon, loading_icon, prompt])
349
+
350
+ share_button.click(None, [], [], _js=share_js)
351
+
352
+ gr.Examples([
353
+ ["portrait of anime girl", 7.5, 27],
354
+ ["a beautiful perfect face girl, Anime fine details portrait of school girl in front of modern tokyo city landscape on the background deep bokeh, anime masterpiece by studio ghibli, 8k, sharp high quality anime, artstation", 7.5, 27],
355
+ ["cyberpunk city landscape with fancy car", 7.5, 27],
356
+ ["portrait of liu yifei girl, soldier working in a cyberpunk city, cleavage, intricate, 8k, highly detailed, digital painting, intense, sharp focus", 7.5, 27],
357
+ ["portrait of a muscular beard male in dgs illustration style, half-body, holding robot arms, strong chest", 7.5, 27],
358
+ ], [prompt, guidance, steps], image_out, inference_example, cache_examples=torch.cuda.is_available())
359
+ gr.Markdown('''
360
+ Models and Space by [@DGSpitzer](https://huggingface.co/DGSpitzer)❤️<br>
361
+ [![Twitter Follow](https://img.shields.io/twitter/follow/DGSpitzer?label=%40DGSpitzer&style=social)](https://twitter.com/DGSpitzer)
362
+ ![visitors](https://visitor-badge.glitch.me/badge?page_id=dgspitzer_DGS_Diffusion_Space)
363
+
364
+ ![Model Views](https://visitor-badge.glitch.me/badge?page_id=Cyberpunk_Anime_Diffusion)
365
+
366
+ ''')
367
+
368
+ demo.queue()
369
+ demo.launch()