Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
import spaces
|
6 |
+
import torch
|
7 |
+
from diffusers import SanaSprintPipeline
|
8 |
+
|
9 |
+
dtype = torch.bfloat16
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
pipe = SanaSprintPipeline.from_pretrained(
|
13 |
+
"Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers",
|
14 |
+
torch_dtype=torch.bfloat16
|
15 |
+
)
|
16 |
+
pipe2 = SanaSprintPipeline.from_pretrained(
|
17 |
+
"Efficient-Large-Model/Sana_Sprint_1.6B_1024px_diffusers",
|
18 |
+
torch_dtype=torch.bfloat16
|
19 |
+
)
|
20 |
+
pipe.to(device)
|
21 |
+
pipe2.to(device)
|
22 |
+
MAX_SEED = np.iinfo(np.int32).max
|
23 |
+
MAX_IMAGE_SIZE = 1024
|
24 |
+
|
25 |
+
@spaces.GPU(duration=5)
|
26 |
+
def infer(prompt, model_size, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=4.5, num_inference_steps=2, progress=gr.Progress(track_tqdm=True)):
|
27 |
+
if randomize_seed:
|
28 |
+
seed = random.randint(0, MAX_SEED)
|
29 |
+
generator = torch.Generator().manual_seed(seed)
|
30 |
+
|
31 |
+
# Choose the appropriate model based on selected model size
|
32 |
+
selected_pipe = pipe if model_size == "0.6B" else pipe2
|
33 |
+
|
34 |
+
img = selected_pipe(
|
35 |
+
prompt=prompt,
|
36 |
+
guidance_scale=guidance_scale,
|
37 |
+
num_inference_steps=num_inference_steps,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
generator=generator,
|
41 |
+
output_type="pil"
|
42 |
+
)
|
43 |
+
print(img)
|
44 |
+
return img.images[0], seed
|
45 |
+
|
46 |
+
examples = [
|
47 |
+
["a tiny astronaut hatching from an egg on the moon", "1.6B"],
|
48 |
+
["🐶 Wearing 🕶 flying on the 🌈", "1.6B"],
|
49 |
+
["an anime illustration of a wiener schnitzel", "0.6B"],
|
50 |
+
["a photorealistic landscape of mountains at sunset", "0.6B"],
|
51 |
+
]
|
52 |
+
|
53 |
+
css="""
|
54 |
+
#col-container {
|
55 |
+
margin: 0 auto;
|
56 |
+
max-width: 520px;
|
57 |
+
}
|
58 |
+
"""
|
59 |
+
|
60 |
+
with gr.Blocks(css=css) as demo:
|
61 |
+
|
62 |
+
with gr.Column(elem_id="col-container"):
|
63 |
+
gr.Markdown(f"""# Sana Sprint""")
|
64 |
+
gr.Markdown("Demo for the real-time [Sana Sprint](https://huggingface.co/collections/Efficient-Large-Model/sana-sprint-67d6810d65235085b3b17c76) model")
|
65 |
+
with gr.Row():
|
66 |
+
|
67 |
+
prompt = gr.Text(
|
68 |
+
label="Prompt",
|
69 |
+
show_label=False,
|
70 |
+
max_lines=1,
|
71 |
+
placeholder="Enter your prompt",
|
72 |
+
container=False,
|
73 |
+
)
|
74 |
+
|
75 |
+
run_button = gr.Button("Run", scale=0)
|
76 |
+
|
77 |
+
result = gr.Image(label="Result", show_label=False)
|
78 |
+
|
79 |
+
model_size = gr.Radio(
|
80 |
+
label="Model Size",
|
81 |
+
choices=["0.6B", "1.6B"],
|
82 |
+
value="1.6B",
|
83 |
+
interactive=True
|
84 |
+
)
|
85 |
+
|
86 |
+
with gr.Accordion("Advanced Settings", open=False):
|
87 |
+
|
88 |
+
seed = gr.Slider(
|
89 |
+
label="Seed",
|
90 |
+
minimum=0,
|
91 |
+
maximum=MAX_SEED,
|
92 |
+
step=1,
|
93 |
+
value=0,
|
94 |
+
)
|
95 |
+
|
96 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
|
100 |
+
width = gr.Slider(
|
101 |
+
label="Width",
|
102 |
+
minimum=256,
|
103 |
+
maximum=MAX_IMAGE_SIZE,
|
104 |
+
step=32,
|
105 |
+
value=1024,
|
106 |
+
)
|
107 |
+
|
108 |
+
height = gr.Slider(
|
109 |
+
label="Height",
|
110 |
+
minimum=256,
|
111 |
+
maximum=MAX_IMAGE_SIZE,
|
112 |
+
step=32,
|
113 |
+
value=1024,
|
114 |
+
)
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
|
118 |
+
guidance_scale = gr.Slider(
|
119 |
+
label="Guidance Scale",
|
120 |
+
minimum=1,
|
121 |
+
maximum=15,
|
122 |
+
step=0.1,
|
123 |
+
value=4.5,
|
124 |
+
)
|
125 |
+
|
126 |
+
num_inference_steps = gr.Slider(
|
127 |
+
label="Number of inference steps",
|
128 |
+
minimum=1,
|
129 |
+
maximum=50,
|
130 |
+
step=1,
|
131 |
+
value=2,
|
132 |
+
)
|
133 |
+
|
134 |
+
gr.Examples(
|
135 |
+
examples = examples,
|
136 |
+
fn = infer,
|
137 |
+
inputs = [prompt, model_size], # Add model_size to inputs
|
138 |
+
outputs = [result, seed],
|
139 |
+
cache_examples="lazy"
|
140 |
+
)
|
141 |
+
|
142 |
+
gr.on(
|
143 |
+
triggers=[run_button.click, prompt.submit],
|
144 |
+
fn = infer,
|
145 |
+
inputs = [prompt, model_size, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], # Add model_size to inputs
|
146 |
+
outputs = [result, seed]
|
147 |
+
)
|
148 |
+
|
149 |
+
demo.launch()
|