Johannes commited on
Commit
9c281ac
1 Parent(s): e95d2f0

1st version

Browse files
Files changed (1) hide show
  1. app.py +30 -111
app.py CHANGED
@@ -3,6 +3,9 @@ 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"
@@ -24,127 +27,48 @@ if disable_safety:
24
 
25
 
26
  def infer(prompt, n_samples, steps, scale):
27
-
 
28
  with context("cuda"):
29
- images = pipe(n_samples*[prompt], guidance_scale=scale, num_inference_steps=steps).images
30
 
31
  return images
32
 
33
- css = """
34
- a {
35
- color: inherit;
36
- text-decoration: underline;
37
- }
38
- .gradio-container {
39
- font-family: 'IBM Plex Sans', sans-serif;
40
- }
41
- .gr-button {
42
- color: white;
43
- border-color: #9d66e5;
44
- background: #9d66e5;
45
- }
46
- input[type='range'] {
47
- accent-color: #9d66e5;
48
- }
49
- .dark input[type='range'] {
50
- accent-color: #dfdfdf;
51
- }
52
- .container {
53
- max-width: 730px;
54
- margin: auto;
55
- padding-top: 1.5rem;
56
- }
57
- #gallery {
58
- min-height: 22rem;
59
- margin-bottom: 15px;
60
- margin-left: auto;
61
- margin-right: auto;
62
- border-bottom-right-radius: .5rem !important;
63
- border-bottom-left-radius: .5rem !important;
64
- }
65
- #gallery>div>.h-full {
66
- min-height: 20rem;
67
- }
68
- .details:hover {
69
- text-decoration: underline;
70
- }
71
- .gr-button {
72
- white-space: nowrap;
73
- }
74
- .gr-button:focus {
75
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
76
- outline: none;
77
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
78
- --tw-border-opacity: 1;
79
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
80
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
81
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
82
- --tw-ring-opacity: .5;
83
- }
84
- #advanced-options {
85
- margin-bottom: 20px;
86
- }
87
- .footer {
88
- margin-bottom: 45px;
89
- margin-top: 35px;
90
- text-align: center;
91
- border-bottom: 1px solid #e5e5e5;
92
- }
93
- .footer>p {
94
- font-size: .8rem;
95
- display: inline-block;
96
- padding: 0 10px;
97
- transform: translateY(10px);
98
- background: white;
99
- }
100
- .dark .logo{ filter: invert(1); }
101
- .dark .footer {
102
- border-color: #303030;
103
- }
104
- .dark .footer>p {
105
- background: #0b0f19;
106
- }
107
- .acknowledgments h4{
108
- margin: 1.25em 0 .25em 0;
109
- font-weight: bold;
110
- font-size: 115%;
111
- }
112
- """
113
-
114
- block = gr.Blocks(css=css)
115
 
116
  examples = [
117
  [
118
- 'Yoda',
119
- 2,
120
- 7.5,
121
- ],
122
- [
123
- 'Abraham Lincoln',
124
  2,
125
  7.5,
126
  ],
127
- [
128
- 'George Washington',
129
- 2,
130
- 7,
131
- ],
132
  ]
133
 
134
  with block:
135
  gr.HTML(
136
  """
137
- <div style="text-align: center; max-width: 650px; margin: 0 auto;">
138
  <div>
139
- <img class="logo" src="https://lambdalabs.com/hubfs/logos/lambda-logo.svg" alt="Lambda Logo"
140
- style="margin: auto; max-width: 7rem;">
141
  <h1 style="font-weight: 900; font-size: 3rem;">
142
- Pokémon text to image
143
  </h1>
144
  </div>
145
- <p style="margin-bottom: 10px; font-size: 94%">
146
- Generate new Pokémon from a text description,
147
- <a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">created by Lambda Labs</a>.
148
  </p>
149
  </div>
150
  """
@@ -188,15 +112,10 @@ with block:
188
  btn.click(infer, inputs=[text, samples, steps, scale], outputs=gallery)
189
  gr.HTML(
190
  """
191
- <div class="footer">
192
- <p> Gradio Demo by 🤗 Hugging Face and Lambda Labs
193
- </p>
194
- </div>
195
- <div class="acknowledgments">
196
- <p> Put in a text prompt and generate your own Pokémon character, no "prompt engineering" required!
197
- <p>If you want to find out how we made this model read about it in <a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">this blog post</a>.
198
- <p>And if you want to train your own Stable Diffusion variants, see our <a href="https://github.com/LambdaLabsML/examples/tree/main/stable-diffusion-finetuning">Examples Repo</a>!
199
- <p>Trained by <a href="justinpinkney.com">Justin Pinkney</a> (<a href="https://twitter.com/Buntworthy">@Buntworthy</a>) at <a href="https://lambdalabs.com/">Lambda Labs</a>.</p>
200
  </div>
201
  """
202
  )
 
3
  import torch
4
  from torch import autocast
5
  from diffusers import StableDiffusionPipeline
6
+ import urllib, urllib.request
7
+ import os
8
+ from xml.etree import ElementTree
9
 
10
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
27
 
28
 
29
  def infer(prompt, n_samples, steps, scale):
30
+ paper_title = get_paper_name(prompt)
31
+
32
  with context("cuda"):
33
+ images = pipe(n_samples*[paper_title], guidance_scale=scale, num_inference_steps=steps).images
34
 
35
  return images
36
 
37
+ def get_paper_name(url: str):
38
+ paper_id = os.path.basename(url)
39
+ paper_id = paper_id.split(".pdf")[0]
40
+ query_url = f"http://export.arxiv.org/api/query?id_list={paper_id}"
41
+ hdr = { 'Content-Type' : 'application/atom+xml' }
42
+ req = urllib.request.Request(query_url, headers=hdr)
43
+ response = urllib.request.urlopen(req)
44
+ tree = ElementTree.fromstring(response.read().decode('utf-8'))
45
+ paper_title = tree.find('{http://www.w3.org/2005/Atom}entry').find('{http://www.w3.org/2005/Atom}title').text
46
+
47
+ return paper_title
48
+
49
+
50
+
51
+ block = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  examples = [
54
  [
55
+ 'https://arxiv.org/abs/1706.03762',
 
 
 
 
 
56
  2,
57
  7.5,
58
  ],
 
 
 
 
 
59
  ]
60
 
61
  with block:
62
  gr.HTML(
63
  """
64
+ <div style="text-align: center; max-width: 650px; margin: 50px auto;">
65
  <div>
 
 
66
  <h1 style="font-weight: 900; font-size: 3rem;">
67
+ Paper to Pokémon
68
  </h1>
69
  </div>
70
+ <p style="margin-bottom: 10px; margin-top: 30px; font-size: 94%">
71
+ Generate new Pokémon from an arXiv link. Just paste the link to the overview, the pdf or just give the id of the paper.
 
72
  </p>
73
  </div>
74
  """
 
112
  btn.click(infer, inputs=[text, samples, steps, scale], outputs=gallery)
113
  gr.HTML(
114
  """
115
+ <div class="footer" style="text-align: center; max-width: 650px; margin: 50px auto;">
116
+ <p>Inspired by and cloned from the great <a href="https://huggingface.co/spaces/lambdalabs/text-to-pokemon">
117
+ Text-to-Pokémon</a> space by Lambda labs</p>
118
+ <p> Gradio Demo by johko</p>
 
 
 
 
 
119
  </div>
120
  """
121
  )