Tesaan commited on
Commit
7cfda54
·
verified ·
1 Parent(s): c4a83ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -119
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from datasets import load_dataset
3
- from PIL import Image
4
  import re
5
  import os
6
  import requests
@@ -14,6 +14,7 @@ word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt",
14
  word_list = word_list_dataset["train"]['text']
15
 
16
  is_gpu_busy = False
 
17
  def infer(prompt):
18
  global is_gpu_busy
19
  samples = 4
@@ -26,30 +27,44 @@ def infer(prompt):
26
  images = []
27
  url = os.getenv('JAX_BACKEND_URL')
28
  payload = {'prompt': prompt}
29
- images_request = requests.post(url, json = payload)
30
  for image in images_request.json()["images"]:
31
  image_b64 = (f"data:image/jpeg;base64,{image}")
32
  images.append(image_b64)
33
 
34
  return images
35
 
36
- def query(payload):
37
- response = requests.post(API_URL, headers=headers, json=payload)
38
- return response.content
39
-
40
- output = query({
41
- "inputs": "Astronaut riding a horse",
42
- "parameters": {
43
- "num_inference_steps": 25,
44
- "negative_prompt": "deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)",
45
- "num_images_per_prompt": 4
46
-
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
 
48
  css = """
49
- .gradio-container {
50
  font-family: 'IBM Plex Sans', sans-serif;
51
  }
52
- .gr-button {
53
  color: white;
54
  border-color: black;
55
  background: black;
@@ -57,10 +72,10 @@ css = """
57
  input[type='range'] {
58
  accent-color: black;
59
  }
60
- .dark input[type='range'] {
61
  accent-color: #dfdfdf;
62
  }
63
- .container {
64
  max-width: 730px;
65
  margin: auto;
66
  padding-top: 1.5rem;
@@ -70,19 +85,19 @@ css = """
70
  margin-bottom: 15px;
71
  margin-left: auto;
72
  margin-right: auto;
73
- border-bottom-right-radius: .5rem !important;
74
- border-bottom-left-radius: .5rem !important;
75
  }
76
  #gallery>div>.h-full {
77
  min-height: 20rem;
78
  }
79
- .details:hover {
80
  text-decoration: underline;
81
  }
82
- .gr-button {
83
  white-space: nowrap;
84
  }
85
- .gr-button:focus {
86
  border-color: rgb(147 197 253 / var(--tw-border-opacity));
87
  outline: none;
88
  box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
@@ -90,41 +105,41 @@ css = """
90
  --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
91
  --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
92
  --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
93
- --tw-ring-opacity: .5;
94
  }
95
  #advanced-btn {
96
- font-size: .7rem !important;
97
  line-height: 19px;
98
  margin-top: 12px;
99
  margin-bottom: 12px;
100
  padding: 2px 8px;
101
- border-radius: 14px !important;
102
  }
103
  #advanced-options {
104
  display: none;
105
  margin-bottom: 20px;
106
  }
107
- .footer {
108
  margin-bottom: 45px;
109
  margin-top: 35px;
110
  text-align: center;
111
  border-bottom: 1px solid #e5e5e5;
112
  }
113
- .footer>p {
114
- font-size: .8rem;
115
  display: inline-block;
116
  padding: 0 10px;
117
  transform: translateY(10px);
118
  background: white;
119
  }
120
- .dark .footer {
121
  border-color: #303030;
122
  }
123
- .dark .footer>p {
124
  background: #0b0f19;
125
  }
126
- .acknowledgments h4{
127
- margin: 1.25em 0 .25em 0;
128
  font-weight: bold;
129
  font-size: 115%;
130
  }
@@ -134,7 +149,7 @@ css = """
134
  justify-content: space-between;
135
  align-items: center;
136
  }
137
- .animate-spin {
138
  animation: spin 1s linear infinite;
139
  }
140
  @keyframes spin {
@@ -146,23 +161,23 @@ css = """
146
  }
147
  }
148
  #share-btn-container {
149
- 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;
150
  }
151
  #share-btn {
152
- 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;
153
  }
154
  #share-btn * {
155
  all: unset;
156
  }
157
- .gr-form{
158
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
159
  }
160
  #prompt-container{
161
  gap: 0;
162
  }
163
  #share-btn-container div:nth-child(-n+2){
164
- width: auto !important;
165
- min-height: 0px !important;
166
  }
167
  """
168
 
@@ -234,84 +249,4 @@ with block:
234
  with gr.Group():
235
  with gr.Box():
236
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
237
- text = gr.Textbox(
238
- label="Enter your prompt",
239
- show_label=False,
240
- max_lines=1,
241
- placeholder="Enter your prompt",
242
- elem_id="prompt-text-input",
243
- ).style(
244
- border=(True, False, True, True),
245
- rounded=(True, False, False, True),
246
- container=False,
247
- )
248
- btn = gr.Button("Generate image").style(
249
- margin=False,
250
- rounded=(False, True, True, False),
251
- full_width=False,
252
- )
253
-
254
- gallery = gr.Gallery(
255
- label="Generated images", show_label=False, elem_id="gallery"
256
- ).style(grid=[2], height="auto")
257
-
258
- with gr.Group(elem_id="container-advanced-btns"):
259
- advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
260
- with gr.Group(elem_id="share-btn-container"):
261
- community_icon = gr.HTML(community_icon_html)
262
- loading_icon = gr.HTML(loading_icon_html)
263
- share_button = gr.Button("Share to community", elem_id="share-btn")
264
-
265
- with gr.Row(elem_id="advanced-options"):
266
- gr.Markdown("Advanced settings are temporarily unavailable")
267
- samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
268
- steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
269
- scale = gr.Slider(
270
- label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
271
- )
272
- seed = gr.Slider(
273
- label="Seed",
274
- minimum=0,
275
- maximum=2147483647,
276
- step=1,
277
- randomize=True,
278
- )
279
-
280
- ex = gr.Examples(examples=examples, fn=infer, inputs=text, outputs=[gallery], cache_examples=True, postprocess=False)
281
- ex.dataset.headers = [""]
282
-
283
- text.submit(infer, inputs=text, outputs=[gallery], postprocess=False)
284
- btn.click(infer, inputs=text, outputs=[gallery], postprocess=False)
285
-
286
- advanced_button.click(
287
- None,
288
- [],
289
- text,
290
- _js="""
291
- () => {
292
- const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
293
- options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
294
- }""",
295
- )
296
- share_button.click(
297
- None,
298
- [],
299
- [],
300
- _js=share_js,
301
- )
302
- gr.HTML(
303
- """
304
- <div class="footer">
305
- <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 <a href="https://huggingface.co/stabilityai" 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> - Gradio Demo by 🤗 Hugging Face
306
- </p>
307
- </div>
308
- <div class="acknowledgments">
309
- <p><h4>LICENSE</h4>
310
- The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
311
- <p><h4>Biases and content acknowledgment</h4>
312
- Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/runwayml/stable-diffusion-v1-5" style="text-decoration: underline;" target="_blank">model card</a></p>
313
- </div>
314
- """
315
- )
316
-
317
- block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)
 
1
  import gradio as gr
2
  from datasets import load_dataset
3
+ from PIL import Image
4
  import re
5
  import os
6
  import requests
 
14
  word_list = word_list_dataset["train"]['text']
15
 
16
  is_gpu_busy = False
17
+
18
  def infer(prompt):
19
  global is_gpu_busy
20
  samples = 4
 
27
  images = []
28
  url = os.getenv('JAX_BACKEND_URL')
29
  payload = {'prompt': prompt}
30
+ images_request = requests.post(url, json=payload)
31
  for image in images_request.json()["images"]:
32
  image_b64 = (f"data:image/jpeg;base64,{image}")
33
  images.append(image_b64)
34
 
35
  return images
36
 
37
+ API_URL = "https://edmx2y4mrvq3tal8.us-east-1.aws.endpoints.huggingface.cloud" # Replace with your actual API URL
38
+ headers = {"Content-Type": "application/json"}
39
+
40
+ def query(payload):
41
+ response = requests.post(API_URL, headers=headers, json=payload)
42
+ return response.content
43
+
44
+ def generate(prompt):
45
+ payload = {
46
+ "inputs": prompt,
47
+ "parameters": {
48
+ "height": 1024,
49
+ "width": 1024,
50
+ "num_inference_steps": 25,
51
+ "negative_prompt": "deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)",
52
+ "num_images_per_prompt": 4
53
+ }
54
+ }
55
+ output = query(payload)
56
+ images = []
57
+ for i in range(4):
58
+ image = Image.open(io.BytesIO(output))
59
+ images.append(image)
60
+ return images
61
 
62
+
63
  css = """
64
+ .gradio-container {
65
  font-family: 'IBM Plex Sans', sans-serif;
66
  }
67
+ .gr-button {
68
  color: white;
69
  border-color: black;
70
  background: black;
 
72
  input[type='range'] {
73
  accent-color: black;
74
  }
75
+ .dark input[type='range'] {
76
  accent-color: #dfdfdf;
77
  }
78
+ .container {
79
  max-width: 730px;
80
  margin: auto;
81
  padding-top: 1.5rem;
 
85
  margin-bottom: 15px;
86
  margin-left: auto;
87
  margin-right: auto;
88
+ border-bottom-right-radius:.5rem!important;
89
+ border-bottom-left-radius:.5rem!important;
90
  }
91
  #gallery>div>.h-full {
92
  min-height: 20rem;
93
  }
94
+ .details:hover {
95
  text-decoration: underline;
96
  }
97
+ .gr-button {
98
  white-space: nowrap;
99
  }
100
+ .gr-button:focus {
101
  border-color: rgb(147 197 253 / var(--tw-border-opacity));
102
  outline: none;
103
  box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
 
105
  --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
106
  --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
107
  --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
108
+ --tw-ring-opacity:.5;
109
  }
110
  #advanced-btn {
111
+ font-size:.7rem!important;
112
  line-height: 19px;
113
  margin-top: 12px;
114
  margin-bottom: 12px;
115
  padding: 2px 8px;
116
+ border-radius: 14px!important;
117
  }
118
  #advanced-options {
119
  display: none;
120
  margin-bottom: 20px;
121
  }
122
+ .footer {
123
  margin-bottom: 45px;
124
  margin-top: 35px;
125
  text-align: center;
126
  border-bottom: 1px solid #e5e5e5;
127
  }
128
+ .footer>p {
129
+ font-size:.8rem;
130
  display: inline-block;
131
  padding: 0 10px;
132
  transform: translateY(10px);
133
  background: white;
134
  }
135
+ .dark.footer {
136
  border-color: #303030;
137
  }
138
+ .dark.footer>p {
139
  background: #0b0f19;
140
  }
141
+ .acknowledgments h4{
142
+ margin: 1.25em 0.25em 0;
143
  font-weight: bold;
144
  font-size: 115%;
145
  }
 
149
  justify-content: space-between;
150
  align-items: center;
151
  }
152
+ .animate-spin {
153
  animation: spin 1s linear infinite;
154
  }
155
  @keyframes spin {
 
161
  }
162
  }
163
  #share-btn-container {
164
+ 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;
165
  }
166
  #share-btn {
167
+ 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;
168
  }
169
  #share-btn * {
170
  all: unset;
171
  }
172
+ .gr-form{
173
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
174
  }
175
  #prompt-container{
176
  gap: 0;
177
  }
178
  #share-btn-container div:nth-child(-n+2){
179
+ width: auto!important;
180
+ min-height: 0px!important;
181
  }
182
  """
183
 
 
249
  with gr.Group():
250
  with gr.Box():
251
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
252
+ text = gr.Textbox(