Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "stabilityai/sdxl-turbo"
|
| 11 |
|
| 12 |
if torch.cuda.is_available():
|
| 13 |
torch_dtype = torch.float16
|
|
@@ -20,8 +18,6 @@ pipe = pipe.to(device)
|
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
| 23 |
-
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
prompt,
|
| 27 |
negative_prompt,
|
|
@@ -31,25 +27,35 @@ def infer(
|
|
| 31 |
height,
|
| 32 |
guidance_scale,
|
| 33 |
num_inference_steps,
|
|
|
|
| 34 |
progress=gr.Progress(track_tqdm=True),
|
| 35 |
):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
@@ -62,11 +68,16 @@ css = """
|
|
| 62 |
margin: 0 auto;
|
| 63 |
max-width: 640px;
|
| 64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"""
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
|
@@ -76,12 +87,24 @@ with gr.Blocks(css=css) as demo:
|
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
-
|
| 80 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
label="Negative prompt",
|
| 87 |
max_lines=1,
|
|
@@ -105,7 +128,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
-
value=1024,
|
| 109 |
)
|
| 110 |
|
| 111 |
height = gr.Slider(
|
|
@@ -113,7 +136,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 113 |
minimum=256,
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
step=32,
|
| 116 |
-
value=1024,
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Row():
|
|
@@ -122,7 +145,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 122 |
minimum=0.0,
|
| 123 |
maximum=10.0,
|
| 124 |
step=0.1,
|
| 125 |
-
value=0.0,
|
| 126 |
)
|
| 127 |
|
| 128 |
num_inference_steps = gr.Slider(
|
|
@@ -130,10 +153,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 130 |
minimum=1,
|
| 131 |
maximum=50,
|
| 132 |
step=1,
|
| 133 |
-
value=2,
|
| 134 |
)
|
| 135 |
|
| 136 |
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
|
@@ -146,9 +170,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 146 |
height,
|
| 147 |
guidance_scale,
|
| 148 |
num_inference_steps,
|
|
|
|
| 149 |
],
|
| 150 |
-
outputs=[
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model_repo_id = "stabilityai/sdxl-turbo"
|
| 9 |
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
torch_dtype = torch.float16
|
|
|
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
|
|
|
|
|
|
| 21 |
def infer(
|
| 22 |
prompt,
|
| 23 |
negative_prompt,
|
|
|
|
| 27 |
height,
|
| 28 |
guidance_scale,
|
| 29 |
num_inference_steps,
|
| 30 |
+
num_images, # নতুন প্যারামিটার: কতগুলো ইমেজ জেনারেট করতে হবে
|
| 31 |
progress=gr.Progress(track_tqdm=True),
|
| 32 |
):
|
| 33 |
+
# ৪টি ইমেজ জেনারেট করার জন্য
|
| 34 |
+
images = []
|
| 35 |
+
seeds_used = []
|
| 36 |
+
|
| 37 |
+
for i in range(num_images):
|
| 38 |
+
if randomize_seed:
|
| 39 |
+
current_seed = random.randint(0, MAX_SEED)
|
| 40 |
+
else:
|
| 41 |
+
current_seed = seed + i # প্রতিটি ইমেজের জন্য আলাদা সিড
|
| 42 |
+
|
| 43 |
+
generator = torch.Generator().manual_seed(current_seed)
|
| 44 |
+
|
| 45 |
+
image = pipe(
|
| 46 |
+
prompt=prompt,
|
| 47 |
+
negative_prompt=negative_prompt,
|
| 48 |
+
guidance_scale=guidance_scale,
|
| 49 |
+
num_inference_steps=num_inference_steps,
|
| 50 |
+
width=width,
|
| 51 |
+
height=height,
|
| 52 |
+
generator=generator,
|
| 53 |
+
).images[0]
|
| 54 |
+
|
| 55 |
+
images.append(image)
|
| 56 |
+
seeds_used.append(current_seed)
|
| 57 |
+
|
| 58 |
+
return images, seeds_used
|
| 59 |
|
| 60 |
examples = [
|
| 61 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
|
|
| 68 |
margin: 0 auto;
|
| 69 |
max-width: 640px;
|
| 70 |
}
|
| 71 |
+
.gallery {
|
| 72 |
+
display: grid;
|
| 73 |
+
grid-template-columns: repeat(2, 1fr);
|
| 74 |
+
gap: 10px;
|
| 75 |
+
}
|
| 76 |
"""
|
| 77 |
|
| 78 |
with gr.Blocks(css=css) as demo:
|
| 79 |
with gr.Column(elem_id="col-container"):
|
| 80 |
+
gr.Markdown(" # Text-to-Image Gradio Template (4 Images)")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
prompt = gr.Text(
|
|
|
|
| 87 |
placeholder="Enter your prompt",
|
| 88 |
container=False,
|
| 89 |
)
|
|
|
|
| 90 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 91 |
|
| 92 |
+
# ৪টি ইমেজ দেখানোর জন্য Gallery কম্পোনেন্ট
|
| 93 |
+
gallery = gr.Gallery(label="Generated Images", elem_classes="gallery")
|
| 94 |
+
|
| 95 |
+
# ব্যবহৃত সিডগুলো দেখানোর জন্য
|
| 96 |
+
seed_info = gr.Textbox(label="Seeds Used")
|
| 97 |
|
| 98 |
with gr.Accordion("Advanced Settings", open=False):
|
| 99 |
+
# নতুন অপশন: কতগুলো ইমেজ জেনারেট করতে চান
|
| 100 |
+
num_images = gr.Slider(
|
| 101 |
+
label="Number of Images",
|
| 102 |
+
minimum=1,
|
| 103 |
+
maximum=10,
|
| 104 |
+
step=1,
|
| 105 |
+
value=4, # ডিফল্ট ৪টি ইমেজ
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
negative_prompt = gr.Text(
|
| 109 |
label="Negative prompt",
|
| 110 |
max_lines=1,
|
|
|
|
| 128 |
minimum=256,
|
| 129 |
maximum=MAX_IMAGE_SIZE,
|
| 130 |
step=32,
|
| 131 |
+
value=1024,
|
| 132 |
)
|
| 133 |
|
| 134 |
height = gr.Slider(
|
|
|
|
| 136 |
minimum=256,
|
| 137 |
maximum=MAX_IMAGE_SIZE,
|
| 138 |
step=32,
|
| 139 |
+
value=1024,
|
| 140 |
)
|
| 141 |
|
| 142 |
with gr.Row():
|
|
|
|
| 145 |
minimum=0.0,
|
| 146 |
maximum=10.0,
|
| 147 |
step=0.1,
|
| 148 |
+
value=0.0,
|
| 149 |
)
|
| 150 |
|
| 151 |
num_inference_steps = gr.Slider(
|
|
|
|
| 153 |
minimum=1,
|
| 154 |
maximum=50,
|
| 155 |
step=1,
|
| 156 |
+
value=2,
|
| 157 |
)
|
| 158 |
|
| 159 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 160 |
+
|
| 161 |
gr.on(
|
| 162 |
triggers=[run_button.click, prompt.submit],
|
| 163 |
fn=infer,
|
|
|
|
| 170 |
height,
|
| 171 |
guidance_scale,
|
| 172 |
num_inference_steps,
|
| 173 |
+
num_images, # নতুন ইনপুট যোগ করা হয়েছে
|
| 174 |
],
|
| 175 |
+
outputs=[gallery, seed_info], # আউটপুট হিসেবে Gallery এবং seed_info
|
| 176 |
)
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
demo.launch()
|