DGSpitzer commited on
Commit
e81fe92
β€’
1 Parent(s): 835a5bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -10
app.py CHANGED
@@ -1,6 +1,15 @@
1
  from diffusers import StableDiffusionPipeline
2
  import gradio as gr
3
  import torch
 
 
 
 
 
 
 
 
 
4
 
5
  models = [
6
  "DGSpitzer/Cyberpunk-Anime-Diffusion"
@@ -11,9 +20,14 @@ prompt_prefixes = {
11
  }
12
 
13
  current_model = models[0]
14
- pipe = StableDiffusionPipeline.from_pretrained(current_model, torch_dtype=torch.float16)
 
 
 
15
  if torch.cuda.is_available():
16
  pipe = pipe.to("cuda")
 
 
17
 
18
  device = "GPU πŸ”₯" if torch.cuda.is_available() else "CPU πŸ₯Ά"
19
 
@@ -23,17 +37,55 @@ def on_model_change(model):
23
  global pipe
24
  if model != current_model:
25
  current_model = model
26
- pipe = StableDiffusionPipeline.from_pretrained(current_model, torch_dtype=torch.float16)
27
  if torch.cuda.is_available():
28
  pipe = pipe.to("cuda")
29
 
30
  def inference(prompt, guidance, steps):
31
 
 
 
 
 
 
 
32
  prompt = prompt_prefixes[current_model] + prompt
33
  image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance, width=512, height=512).images[0]
34
  return image
35
 
36
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  gr.HTML(
38
  """
39
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
@@ -50,40 +102,49 @@ with gr.Blocks() as demo:
50
  </h1>
51
  </div>
52
  <p style="margin-bottom: 10px; font-size: 94%">
53
- Demo for Cyberpunk Anime Diffusion. Based of Finetuned Diffusion by anzorq <a href="https://twitter.com/hahahahohohe">
54
  </p>
55
  </div>
56
  """
57
  )
58
  gr.Markdown('''
59
- πŸ‘‡ Buy me a coffee if you like β™₯ this project~ πŸ‘‡
60
  [![Buy me a coffee](https://badgen.net/badge/icon/Buy%20Me%20A%20Coffee?icon=buymeacoffee&label)](https://www.buymeacoffee.com/dgspitzer)
61
  ''')
62
  with gr.Row():
63
 
64
  with gr.Column():
65
  model = gr.Dropdown(label="Model", choices=models, value=models[0])
66
- prompt = gr.Textbox(label="Prompt", placeholder="{} is added automatically".format(prompt_prefixes[current_model]))
67
  guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
68
  steps = gr.Slider(label="Steps", value=27, maximum=100, minimum=2)
69
  run = gr.Button(value="Run")
70
  gr.Markdown(f"Running on: {device}")
71
  with gr.Column():
72
- image_out = gr.Image(height=512)
73
 
 
 
 
 
 
 
74
  model.change(on_model_change, inputs=model, outputs=[])
75
- run.click(inference, inputs=[prompt, guidance, steps], outputs=image_out)
 
 
 
 
76
  gr.Examples([
77
  ["portrait of anime girl", 7.5, 27],
78
  ["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],
79
  ["cyberpunk city landscape with fancy car", 7.5, 27],
80
  ["portrait of liu yifei girl, soldier working in a cyberpunk city, cleavage, intricate, 8k, highly detailed, digital painting, intense, sharp focus", 7.5, 27],
81
  ["portrait of a muscular beard male in dgs illustration style, half-body, holding robot arms, strong chest", 7.5, 27],
82
- ], [prompt, guidance, steps], image_out, inference, cache_examples=torch.cuda.is_available())
83
  gr.Markdown('''
84
  Models and Space by [@DGSpitzer](https://huggingface.co/DGSpitzer)❀️<br>
85
  [![Twitter Follow](https://img.shields.io/twitter/follow/DGSpitzer?label=%40DGSpitzer&style=social)](https://twitter.com/DGSpitzer)
86
-
87
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=dgspitzer_DGS_Diffusion_Space)
88
 
89
  ![Model Views](https://visitor-badge.glitch.me/badge?page_id=Cyberpunk_Anime_Diffusion)
 
1
  from diffusers import StableDiffusionPipeline
2
  import gradio as gr
3
  import torch
4
+ import os
5
+
6
+ from share_btn import community_icon_html, loading_icon_html, share_js
7
+
8
+ if torch.cuda.is_available():
9
+ torchfloat = torch.float16
10
+ else:
11
+ torchfloat = torch.float32
12
+
13
 
14
  models = [
15
  "DGSpitzer/Cyberpunk-Anime-Diffusion"
 
20
  }
21
 
22
  current_model = models[0]
23
+
24
+ auth_token = os.environ.get("adminToken") or True
25
+ pipe = StableDiffusionPipeline.from_pretrained(current_model, use_auth_token=auth_token, torch_dtype=torchfloat, revision="fp16")
26
+
27
  if torch.cuda.is_available():
28
  pipe = pipe.to("cuda")
29
+ else:
30
+ pipe = pipe.to("cpu")
31
 
32
  device = "GPU πŸ”₯" if torch.cuda.is_available() else "CPU πŸ₯Ά"
33
 
 
37
  global pipe
38
  if model != current_model:
39
  current_model = model
40
+ pipe = StableDiffusionPipeline.from_pretrained(current_model, torch_dtype=torchfloat)
41
  if torch.cuda.is_available():
42
  pipe = pipe.to("cuda")
43
 
44
  def inference(prompt, guidance, steps):
45
 
46
+ prompt = prompt_prefixes[current_model] + prompt
47
+ image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance, width=512, height=512).images[0]
48
+ return image, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
49
+
50
+ def inference_example(prompt, guidance, steps):
51
+
52
  prompt = prompt_prefixes[current_model] + prompt
53
  image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance, width=512, height=512).images[0]
54
  return image
55
 
56
+ css = """
57
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
58
+ a {text-decoration-line: underline; font-weight: 600;}
59
+ .animate-spin {
60
+ animation: spin 1s linear infinite;
61
+ }
62
+ @keyframes spin {
63
+ from {
64
+ transform: rotate(0deg);
65
+ }
66
+ to {
67
+ transform: rotate(360deg);
68
+ }
69
+ }
70
+ #share-btn-container {
71
+ 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;
72
+ }
73
+ #share-btn {
74
+ 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;
75
+ }
76
+ #share-btn * {
77
+ all: unset;
78
+ }
79
+ #share-btn-container div:nth-child(-n+2){
80
+ width: auto !important;
81
+ min-height: 0px !important;
82
+ }
83
+ #share-btn-container .wrap {
84
+ display: none !important;
85
+ }
86
+ """
87
+
88
+ with gr.Blocks(css=css) as demo:
89
  gr.HTML(
90
  """
91
  <div style="text-align: center; max-width: 700px; margin: 0 auto;">
 
102
  </h1>
103
  </div>
104
  <p style="margin-bottom: 10px; font-size: 94%">
105
+ Demo for Cyberpunk Anime Diffusion. Based of the projects by anzorq and fffiloni <a href="https://twitter.com/hahahahohohe">
106
  </p>
107
  </div>
108
  """
109
  )
110
  gr.Markdown('''
111
+ πŸ‘‡ Buy me a coffee if you like β™₯ this project~ πŸ‘‡ Running this server costs me $100 per week, any help would be much appreciated!
112
  [![Buy me a coffee](https://badgen.net/badge/icon/Buy%20Me%20A%20Coffee?icon=buymeacoffee&label)](https://www.buymeacoffee.com/dgspitzer)
113
  ''')
114
  with gr.Row():
115
 
116
  with gr.Column():
117
  model = gr.Dropdown(label="Model", choices=models, value=models[0])
118
+ prompt = gr.Textbox(label="Prompt", placeholder="{} is added automatically".format(prompt_prefixes[current_model]), elem_id="input-prompt")
119
  guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
120
  steps = gr.Slider(label="Steps", value=27, maximum=100, minimum=2)
121
  run = gr.Button(value="Run")
122
  gr.Markdown(f"Running on: {device}")
123
  with gr.Column():
124
+ image_out = gr.Image(height=512, type="filepath", elem_id="output-img")
125
 
126
+ with gr.Column(elem_id="col-container"):
127
+ with gr.Group(elem_id="share-btn-container"):
128
+ community_icon = gr.HTML(community_icon_html, visible=False)
129
+ loading_icon = gr.HTML(loading_icon_html, visible=False)
130
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
131
+
132
  model.change(on_model_change, inputs=model, outputs=[])
133
+ run.click(inference, inputs=[prompt, guidance, steps], outputs=[image_out, share_button, community_icon, loading_icon])
134
+
135
+ share_button.click(None, [], [], _js=share_js)
136
+
137
+
138
  gr.Examples([
139
  ["portrait of anime girl", 7.5, 27],
140
  ["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],
141
  ["cyberpunk city landscape with fancy car", 7.5, 27],
142
  ["portrait of liu yifei girl, soldier working in a cyberpunk city, cleavage, intricate, 8k, highly detailed, digital painting, intense, sharp focus", 7.5, 27],
143
  ["portrait of a muscular beard male in dgs illustration style, half-body, holding robot arms, strong chest", 7.5, 27],
144
+ ], [prompt, guidance, steps], image_out, inference_example, cache_examples=torch.cuda.is_available())
145
  gr.Markdown('''
146
  Models and Space by [@DGSpitzer](https://huggingface.co/DGSpitzer)❀️<br>
147
  [![Twitter Follow](https://img.shields.io/twitter/follow/DGSpitzer?label=%40DGSpitzer&style=social)](https://twitter.com/DGSpitzer)
 
148
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=dgspitzer_DGS_Diffusion_Space)
149
 
150
  ![Model Views](https://visitor-badge.glitch.me/badge?page_id=Cyberpunk_Anime_Diffusion)