File size: 5,353 Bytes
883fe51
2d91882
 
 
 
883fe51
2d91882
 
 
883fe51
2d91882
 
 
 
 
 
 
 
883fe51
2d91882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac30f8
2d91882
 
883fe51
2d91882
 
 
 
 
 
 
 
 
 
 
 
 
883fe51
2d91882
883fe51
3ac30f8
883fe51
2d91882
 
 
 
 
883fe51
 
2d91882
 
 
 
 
 
 
 
 
 
 
 
3ac30f8
 
 
 
2d91882
 
 
 
 
3ac30f8
2d91882
 
 
3ac30f8
 
2d91882
 
3ac30f8
 
2d91882
 
3ac30f8
2d91882
 
883fe51
3ac30f8
2d91882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac30f8
 
 
2d91882
 
3ac30f8
 
 
 
2d91882
 
 
 
 
 
 
 
 
 
 
 
 
3ac30f8
 
2d91882
 
 
3ac30f8
 
883fe51
3ac30f8
2d91882
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import gradio as gr
import random
import numpy as np
from diffusers import DiffusionPipeline
import torch

# ডিভাইস সেটআপ
device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "stabilityai/sdxl-turbo"

# মডেল লোড করুন
pipe = DiffusionPipeline.from_pretrained(
    model_repo_id,
    torch_dtype=torch.float16 if device == "cuda" else torch.float32,
    variant="fp16" if device == "cuda" else None,
    use_safetensors=True
)
pipe = pipe.to(device)

# সেটিংস
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
DEFAULT_NUM_IMAGES = 4

def generate_images(
    prompt,
    negative_prompt="",
    seed=0,
    randomize_seed=True,
    width=768,
    height=768,
    guidance_scale=0.0,
    num_inference_steps=2,
    num_images=DEFAULT_NUM_IMAGES,
    progress=gr.Progress(track_tqdm=True)
):
    # র‍্যান্ডম সিড জেনারেট করুন
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    
    # জেনারেটর তৈরি করুন
    generator = torch.Generator(device=device).manual_seed(seed)
    
    # একাধিক ইমেজ জেনারেট করুন
    images = []
    for _ in range(num_images):
        image = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            guidance_scale=guidance_scale,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator,
        ).images[0]
        images.append(image)
    
    return images, seed

# উদাহরণ প্রম্পট
examples = [
    "A cyberpunk cityscape at night, neon lights, rain",
    "Majestic waterfall in a tropical forest, cinematic lighting",
    "An astronaut floating in space, earth in background",
    "A futuristic robot playing guitar in a jazz club",
    "A magical forest with glowing mushrooms and fireflies"
]

# UI থিম
theme = gr.themes.Glass(
    primary_hue="purple",
    secondary_hue="blue",
    font=[gr.themes.GoogleFont("Poppins")]
)

# UI তৈরি
with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo:
    gr.Markdown("""
    # 🚀 SDXL-Turbo Image Generator
    **Create 4 stunning AI images in seconds!** No API key required.
    """)
    
    with gr.Row():
        with gr.Column(scale=3):
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Describe your image...",
                lines=2,
                max_lines=3
            )
            negative_prompt = gr.Textbox(
                label="Negative Prompt",
                placeholder="What to exclude from the image...",
                lines=1
            )
        with gr.Column(scale=1):
            generate_btn = gr.Button("✨ Generate 4 Images", variant="primary")
    
    gallery = gr.Gallery(
        label="Generated Images",
        columns=4,
        height="auto",
        preview=True,
        object_fit="contain"
    )
    
    with gr.Accordion("Advanced Settings", open=False):
        with gr.Row():
            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0
            )
            randomize_seed = gr.Checkbox(
                label="Random Seed",
                value=True
            )
        with gr.Row():
            width = gr.Slider(
                label="Width",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=768
            )
            height = gr.Slider(
                label="Height",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=32,
                value=768
            )
        with gr.Row():
            guidance_scale = gr.Slider(
                label="Guidance Scale",
                minimum=0.0,
                maximum=10.0,
                step=0.1,
                value=0.0
            )
            num_inference_steps = gr.Slider(
                label="Inference Steps",
                minimum=1,
                maximum=50,
                step=1,
                value=2
            )
            num_images = gr.Slider(
                label="Number of Images",
                minimum=1,
                maximum=10,
                step=1,
                value=DEFAULT_NUM_IMAGES
            )
    
    gr.Examples(
        examples=examples,
        inputs=prompt,
        label="Example Prompts"
    )
    
    # ইভেন্ট হ্যান্ডলার
    generate_btn.click(
        fn=generate_images,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
            num_images
        ],
        outputs=[gallery, seed]
    )
    
    gr.Markdown("""
    <div style="text-align: center; margin-top: 20px; color: #666;">
        Powered by SDXL-Turbo • No API key required • Runs on Hugging Face Spaces
    </div>
    """)

# অ্যাপ্লিকেশন চালু করুন
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)