Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,164 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import torch
|
3 |
-
from transformers import DiffusionModel, DiffusionImageProcessor
|
4 |
-
from threading import Thread
|
5 |
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
processor = DiffusionImageProcessor.from_model(model)
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
11 |
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
21 |
with torch.no_grad():
|
22 |
result = processor.generate(**inputs)
|
23 |
-
|
24 |
-
# Return the generated image
|
25 |
return result[0]
|
26 |
|
27 |
-
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import uuid
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
import torch
|
9 |
+
from transformers import DiffusionModel, DiffusionImageProcessor
|
|
|
10 |
|
11 |
+
DESCRIPTION = """
|
12 |
+
# Image Generator
|
13 |
+
"""
|
|
|
|
|
14 |
|
15 |
+
def save_image(img):
|
16 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
17 |
+
img.save(unique_name)
|
18 |
+
return unique_name
|
19 |
|
20 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
21 |
+
if randomize_seed:
|
22 |
+
seed = random.randint(0, MAX_SEED)
|
23 |
+
return seed
|
24 |
+
|
25 |
+
MAX_SEED = np.iinfo(np.int32).max
|
26 |
+
|
27 |
+
if not torch.cuda.is_available():
|
28 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
29 |
|
30 |
+
def generate_image(text):
|
31 |
+
model = DiffusionModel.from_pretrained("stability/stable-diffusion-xl-base-1.0")
|
32 |
+
processor = DiffusionImageProcessor.from_model(model)
|
33 |
+
inputs = processor(text, return_tensors="pt")
|
34 |
with torch.no_grad():
|
35 |
result = processor.generate(**inputs)
|
|
|
|
|
36 |
return result[0]
|
37 |
|
38 |
+
@gr.output_hook
|
39 |
+
def post_process_images(paths):
|
40 |
+
return [gr.outputs.Image(path) for path in paths]
|
41 |
+
|
42 |
+
@gr.output_hook
|
43 |
+
def post_process_seed(seed):
|
44 |
+
return seed
|
45 |
+
|
46 |
+
examples = [
|
47 |
+
"Newton and Einstein sitting together and thinking about gravity and space",
|
48 |
+
"an astronaut riding a horse in space",
|
49 |
+
"a cartoon of a boy playing with a tiger",
|
50 |
+
"neon holography crystal cat",
|
51 |
+
"a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone",
|
52 |
+
"a cute robot artist painting on an easel,concept art",
|
53 |
+
"a cat eating a piece of cheese",
|
54 |
+
]
|
55 |
+
|
56 |
+
css = '''
|
57 |
+
.gradio-container{max-width: 560px !important}
|
58 |
+
h1{text-align:center}
|
59 |
+
footer {
|
60 |
+
visibility: hidden
|
61 |
+
}
|
62 |
+
'''
|
63 |
+
with gr.Blocks(css=css, theme="pseudolab/huggingface-korea-theme") as demo:
|
64 |
+
gr.Markdown(DESCRIPTION)
|
65 |
+
gr.DuplicateButton(
|
66 |
+
value="Duplicate Space for private use",
|
67 |
+
elem_id="duplicate-button",
|
68 |
+
visible=False,
|
69 |
+
)
|
70 |
+
|
71 |
+
with gr.Group():
|
72 |
+
with gr.Row():
|
73 |
+
prompt = gr.Text(
|
74 |
+
label="Prompt",
|
75 |
+
show_label=False,
|
76 |
+
max_lines=1,
|
77 |
+
placeholder="Enter your prompt",
|
78 |
+
container=False,
|
79 |
+
)
|
80 |
+
run_button = gr.Button("Run", scale=0)
|
81 |
+
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
|
82 |
+
with gr.Accordion("Advanced options", open=False):
|
83 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
84 |
+
negative_prompt = gr.Text(
|
85 |
+
label="Negative prompt",
|
86 |
+
lines=4,
|
87 |
+
max_lines=6,
|
88 |
+
value="""(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)""",
|
89 |
+
placeholder="Enter a negative prompt",
|
90 |
+
visible=True,
|
91 |
+
)
|
92 |
+
seed = gr.Slider(
|
93 |
+
label="Seed",
|
94 |
+
minimum=0,
|
95 |
+
maximum=MAX_SEED,
|
96 |
+
step=1,
|
97 |
+
value=0,
|
98 |
+
visible=True
|
99 |
+
)
|
100 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
+
with gr.Row(visible=True):
|
102 |
+
width = gr.Slider(
|
103 |
+
label="Width",
|
104 |
+
minimum=512,
|
105 |
+
maximum=2048,
|
106 |
+
step=8,
|
107 |
+
value=1024,
|
108 |
+
)
|
109 |
+
height = gr.Slider(
|
110 |
+
label="Height",
|
111 |
+
minimum=512,
|
112 |
+
maximum=2048,
|
113 |
+
step=8,
|
114 |
+
value=1024,
|
115 |
+
)
|
116 |
+
with gr.Row():
|
117 |
+
guidance_scale = gr.Slider(
|
118 |
+
label="Guidance Scale",
|
119 |
+
minimum=0.1,
|
120 |
+
maximum=20.0,
|
121 |
+
step=0.1,
|
122 |
+
value=6,
|
123 |
+
)
|
124 |
+
|
125 |
+
gr.Examples(
|
126 |
+
examples=examples,
|
127 |
+
inputs=prompt,
|
128 |
+
outputs=[result, seed],
|
129 |
+
fn=generate_image,
|
130 |
+
cache_examples=False,
|
131 |
+
output_hooks=[post_process_images, post_process_seed]
|
132 |
+
)
|
133 |
+
|
134 |
+
use_negative_prompt.change(
|
135 |
+
fn=lambda x: gr.update(visible=x),
|
136 |
+
inputs=use_negative_prompt,
|
137 |
+
outputs=negative_prompt,
|
138 |
+
api_name=False,
|
139 |
+
)
|
140 |
+
|
141 |
+
|
142 |
+
gr.on(
|
143 |
+
triggers=[
|
144 |
+
prompt.submit,
|
145 |
+
negative_prompt.submit,
|
146 |
+
run_button.click,
|
147 |
+
],
|
148 |
+
fn=generate_image,
|
149 |
+
inputs=[
|
150 |
+
prompt,
|
151 |
+
negative_prompt,
|
152 |
+
use_negative_prompt,
|
153 |
+
seed,
|
154 |
+
width,
|
155 |
+
height,
|
156 |
+
guidance_scale,
|
157 |
+
randomize_seed,
|
158 |
+
],
|
159 |
+
outputs=[result, seed],
|
160 |
+
api_name="run",
|
161 |
+
)
|
162 |
+
|
163 |
+
if __name__ == "__main__":
|
164 |
+
demo.queue(max_size=20).launch(show_api=False, debug=False)
|