File size: 4,622 Bytes
80d002f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# main.py
import gradio as gr
import numpy as np
import random
import torch
import os
from diffusers import SanaSprintPipeline
from PIL import Image

# Initialize device and dtype
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"

# Load models
pipe = SanaSprintPipeline.from_pretrained(
    "Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers",
    torch_dtype=dtype
)
pipe2 = SanaSprintPipeline.from_pretrained(
    "Efficient-Large-Model/Sana_Sprint_1.6B_1024px_diffusers",
    torch_dtype=dtype
)
pipe.to(device)
pipe2.to(device)

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024

def generate_image(prompt, model_size, seed, randomize_seed, width, height, guidance_scale, steps):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    generator = torch.Generator().manual_seed(seed)
    
    selected_pipe = pipe if model_size == "0.6B" else pipe2
    
    result = selected_pipe(
        prompt=prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=steps,
        width=width,
        height=height,
        generator=generator,
        output_type="pil"
    )
    
    image = result.images[0]
    filename = f"output_{seed}.png"
    image.save(filename)
    return image, filename, seed

css = """
#col-container {
    margin: 0 auto;
    max-width: 800px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# πŸš€ Sana Sprint Image Generator")
        
        with gr.Row():
            with gr.Column():
                prompt = gr.Textbox(
                    label="Enter Prompt",
                    placeholder="A surreal landscape with...",
                    lines=3
                )
                
                model_size = gr.Radio(
                    label="Model Size",
                    choices=["0.6B", "1.6B"],
                    value="1.6B"
                )
                
                with gr.Accordion("Advanced Settings", open=False):
                    seed = gr.Slider(
                        label="Seed",
                        minimum=0,
                        maximum=MAX_SEED,
                        value=42,
                        step=1
                    )
                    randomize_seed = gr.Checkbox(
                        label="Randomize Seed",
                        value=True
                    )
                    
                    with gr.Row():
                        width = gr.Slider(
                            label="Width",
                            minimum=256,
                            maximum=MAX_IMAGE_SIZE,
                            value=1024,
                            step=32
                        )
                        height = gr.Slider(
                            label="Height",
                            minimum=256,
                            maximum=MAX_IMAGE_SIZE,
                            value=1024,
                            step=32
                        )
                    
                    guidance_scale = gr.Slider(
                        label="Guidance Scale",
                        minimum=1.0,
                        maximum=15.0,
                        value=4.5,
                        step=0.1
                    )
                    
                    steps = gr.Slider(
                        label="Inference Steps",
                        minimum=1,
                        maximum=50,
                        value=2,
                        step=1
                    )
                
                generate_btn = gr.Button("Generate Image", variant="primary")

            with gr.Column():
                output_image = gr.Image(label="Generated Image")
                file_output = gr.File(label="Download Image")
                seed_info = gr.Textbox(label="Used Seed")
        
        gr.Examples(
            examples=[
                ["a tiny astronaut hatching from an egg on the moon", "1.6B"],
                ["🐢 Wearing πŸ•Ά flying on the 🌈", "1.6B"],
                ["an anime illustration of a wiener schnitzel", "0.6B"]
            ],
            inputs=[prompt, model_size],
            outputs=[output_image, file_output, seed_info],
            fn=generate_image,
            cache_examples=True
        )

    generate_btn.click(
        fn=generate_image,
        inputs=[prompt, model_size, seed, randomize_seed, width, height, guidance_scale, steps],
        outputs=[output_image, file_output, seed_info]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")