Update app.py
Browse files
app.py
CHANGED
@@ -14,27 +14,36 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
14 |
MAX_IMAGE_SIZE = 2048
|
15 |
|
16 |
@spaces.GPU()
|
17 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
18 |
if randomize_seed:
|
19 |
seed = random.randint(0, MAX_SEED)
|
20 |
generator = torch.Generator().manual_seed(seed)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
examples = [
|
32 |
"a tiny astronaut hatching from an egg on the moon",
|
33 |
"a cat holding a sign that says hello world",
|
34 |
"an anime illustration of a wiener schnitzel",
|
|
|
35 |
]
|
36 |
|
37 |
-
css="""
|
38 |
#col-container {
|
39 |
margin: 0 auto;
|
40 |
max-width: 520px;
|
@@ -50,21 +59,18 @@ with gr.Blocks(css=css) as demo:
|
|
50 |
""")
|
51 |
|
52 |
with gr.Row():
|
53 |
-
|
54 |
-
prompt = gr.Text(
|
55 |
label="Prompt",
|
56 |
show_label=False,
|
57 |
max_lines=1,
|
58 |
placeholder="Enter your prompt",
|
59 |
container=False,
|
60 |
)
|
61 |
-
|
62 |
run_button = gr.Button("Run", scale=0)
|
63 |
|
64 |
result = gr.Image(label="Result", show_label=False)
|
65 |
|
66 |
with gr.Accordion("Advanced Settings", open=False):
|
67 |
-
|
68 |
seed = gr.Slider(
|
69 |
label="Seed",
|
70 |
minimum=0,
|
@@ -72,11 +78,9 @@ with gr.Blocks(css=css) as demo:
|
|
72 |
step=1,
|
73 |
value=0,
|
74 |
)
|
75 |
-
|
76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
77 |
|
78 |
with gr.Row():
|
79 |
-
|
80 |
width = gr.Slider(
|
81 |
label="Width",
|
82 |
minimum=256,
|
@@ -84,7 +88,6 @@ with gr.Blocks(css=css) as demo:
|
|
84 |
step=32,
|
85 |
value=1024,
|
86 |
)
|
87 |
-
|
88 |
height = gr.Slider(
|
89 |
label="Height",
|
90 |
minimum=256,
|
@@ -94,8 +97,6 @@ with gr.Blocks(css=css) as demo:
|
|
94 |
)
|
95 |
|
96 |
with gr.Row():
|
97 |
-
|
98 |
-
|
99 |
num_inference_steps = gr.Slider(
|
100 |
label="Number of inference steps",
|
101 |
minimum=1,
|
@@ -103,20 +104,31 @@ with gr.Blocks(css=css) as demo:
|
|
103 |
step=1,
|
104 |
value=4,
|
105 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
gr.Examples(
|
108 |
-
examples
|
109 |
-
fn
|
110 |
-
inputs
|
111 |
-
outputs
|
112 |
cache_examples="lazy"
|
113 |
)
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
outputs = [result, seed]
|
120 |
)
|
121 |
|
|
|
|
|
|
|
|
|
|
|
122 |
demo.launch()
|
|
|
14 |
MAX_IMAGE_SIZE = 2048
|
15 |
|
16 |
@spaces.GPU()
|
17 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=7.5, progress=gr.Progress(track_tqdm=True)):
|
18 |
+
if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
|
19 |
+
raise ValueError("Image size exceeds the maximum allowed dimensions.")
|
20 |
+
|
21 |
if randomize_seed:
|
22 |
seed = random.randint(0, MAX_SEED)
|
23 |
generator = torch.Generator().manual_seed(seed)
|
24 |
+
|
25 |
+
try:
|
26 |
+
image = pipe(
|
27 |
+
prompt=prompt,
|
28 |
+
width=width,
|
29 |
+
height=height,
|
30 |
+
num_inference_steps=num_inference_steps,
|
31 |
+
generator=generator,
|
32 |
+
guidance_scale=guidance_scale
|
33 |
+
).images[0]
|
34 |
+
except Exception as e:
|
35 |
+
return None, seed, f"Error: {str(e)}"
|
36 |
+
|
37 |
+
return image, seed, None
|
38 |
|
39 |
examples = [
|
40 |
"a tiny astronaut hatching from an egg on the moon",
|
41 |
"a cat holding a sign that says hello world",
|
42 |
"an anime illustration of a wiener schnitzel",
|
43 |
+
# Add more diverse examples
|
44 |
]
|
45 |
|
46 |
+
css = """
|
47 |
#col-container {
|
48 |
margin: 0 auto;
|
49 |
max-width: 520px;
|
|
|
59 |
""")
|
60 |
|
61 |
with gr.Row():
|
62 |
+
prompt = gr.Textbox(
|
|
|
63 |
label="Prompt",
|
64 |
show_label=False,
|
65 |
max_lines=1,
|
66 |
placeholder="Enter your prompt",
|
67 |
container=False,
|
68 |
)
|
|
|
69 |
run_button = gr.Button("Run", scale=0)
|
70 |
|
71 |
result = gr.Image(label="Result", show_label=False)
|
72 |
|
73 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
74 |
seed = gr.Slider(
|
75 |
label="Seed",
|
76 |
minimum=0,
|
|
|
78 |
step=1,
|
79 |
value=0,
|
80 |
)
|
|
|
81 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
82 |
|
83 |
with gr.Row():
|
|
|
84 |
width = gr.Slider(
|
85 |
label="Width",
|
86 |
minimum=256,
|
|
|
88 |
step=32,
|
89 |
value=1024,
|
90 |
)
|
|
|
91 |
height = gr.Slider(
|
92 |
label="Height",
|
93 |
minimum=256,
|
|
|
97 |
)
|
98 |
|
99 |
with gr.Row():
|
|
|
|
|
100 |
num_inference_steps = gr.Slider(
|
101 |
label="Number of inference steps",
|
102 |
minimum=1,
|
|
|
104 |
step=1,
|
105 |
value=4,
|
106 |
)
|
107 |
+
guidance_scale = gr.Slider(
|
108 |
+
label="Guidance Scale",
|
109 |
+
minimum=0.0,
|
110 |
+
maximum=20.0,
|
111 |
+
step=0.5,
|
112 |
+
value=7.5,
|
113 |
+
)
|
114 |
|
115 |
gr.Examples(
|
116 |
+
examples=examples,
|
117 |
+
fn=infer,
|
118 |
+
inputs=[prompt],
|
119 |
+
outputs=[result, seed],
|
120 |
cache_examples="lazy"
|
121 |
)
|
122 |
|
123 |
+
run_button.click(
|
124 |
+
fn=infer,
|
125 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
|
126 |
+
outputs=[result, seed],
|
|
|
127 |
)
|
128 |
|
129 |
+
gr.Markdown("""
|
130 |
+
## Save Your Image
|
131 |
+
Right-click on the image and select 'Save As' to download the generated image.
|
132 |
+
""")
|
133 |
+
|
134 |
demo.launch()
|