Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,7 @@ import numpy as np
|
|
| 4 |
import random
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
-
from diffusers import
|
| 8 |
-
#from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
| 9 |
-
#from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
| 10 |
|
| 11 |
dtype = torch.bfloat16
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -15,17 +13,25 @@ pipe = SanaSprintPipeline.from_pretrained(
|
|
| 15 |
"Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers",
|
| 16 |
torch_dtype=torch.bfloat16
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
pipe.to(device)
|
|
|
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
MAX_IMAGE_SIZE = 1024
|
| 21 |
|
| 22 |
@spaces.GPU(duration=5)
|
| 23 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=2, progress=gr.Progress(track_tqdm=True)):
|
| 24 |
if randomize_seed:
|
| 25 |
seed = random.randint(0, MAX_SEED)
|
| 26 |
generator = torch.Generator().manual_seed(seed)
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
prompt=prompt,
|
| 30 |
guidance_scale=guidance_scale,
|
| 31 |
num_inference_steps=num_inference_steps,
|
|
@@ -37,12 +43,21 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
|
|
| 37 |
print(img)
|
| 38 |
return img.images[0], seed
|
| 39 |
|
| 40 |
-
examples
|
| 41 |
-
|
| 42 |
-
"a
|
| 43 |
-
"
|
|
|
|
| 44 |
]
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
css="""
|
| 47 |
#col-container {
|
| 48 |
margin: 0 auto;
|
|
@@ -53,7 +68,15 @@ css="""
|
|
| 53 |
with gr.Blocks(css=css) as demo:
|
| 54 |
|
| 55 |
with gr.Column(elem_id="col-container"):
|
| 56 |
-
gr.Markdown(f"""# Sana Sprint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
|
|
@@ -117,18 +140,29 @@ with gr.Blocks(css=css) as demo:
|
|
| 117 |
value=2,
|
| 118 |
)
|
| 119 |
|
| 120 |
-
gr.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
gr.on(
|
| 129 |
triggers=[run_button.click, prompt.submit],
|
| 130 |
fn = infer,
|
| 131 |
-
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 132 |
outputs = [result, seed]
|
| 133 |
)
|
| 134 |
|
|
|
|
| 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"
|
|
|
|
| 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=3.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,
|
|
|
|
| 43 |
print(img)
|
| 44 |
return img.images[0], seed
|
| 45 |
|
| 46 |
+
# Different examples for each model size
|
| 47 |
+
examples_06B = [
|
| 48 |
+
"a majestic castle on a floating island",
|
| 49 |
+
"a robotic chef cooking in a futuristic kitchen",
|
| 50 |
+
"a magical forest with glowing mushrooms"
|
| 51 |
]
|
| 52 |
|
| 53 |
+
examples_16B = [
|
| 54 |
+
"a steampunk city with airships in the sky",
|
| 55 |
+
"a photorealistic fox in a snowy landscape",
|
| 56 |
+
"an underwater temple with ancient ruins"
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
# We'll use the appropriate set based on the model selection
|
| 60 |
+
|
| 61 |
css="""
|
| 62 |
#col-container {
|
| 63 |
margin: 0 auto;
|
|
|
|
| 68 |
with gr.Blocks(css=css) as demo:
|
| 69 |
|
| 70 |
with gr.Column(elem_id="col-container"):
|
| 71 |
+
gr.Markdown(f"""# Sana Sprint""")
|
| 72 |
+
|
| 73 |
+
# Add radio button for model selection
|
| 74 |
+
model_size = gr.Radio(
|
| 75 |
+
label="Model Size",
|
| 76 |
+
choices=["0.6B", "1.6B"],
|
| 77 |
+
value="0.6B",
|
| 78 |
+
interactive=True
|
| 79 |
+
)
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
|
|
|
|
| 140 |
value=2,
|
| 141 |
)
|
| 142 |
|
| 143 |
+
with gr.Row():
|
| 144 |
+
examples_container = gr.Examples(
|
| 145 |
+
examples = examples_06B, # Start with 0.6B examples
|
| 146 |
+
fn = infer,
|
| 147 |
+
inputs = [prompt, model_size],
|
| 148 |
+
outputs = [result, seed],
|
| 149 |
+
cache_examples="lazy",
|
| 150 |
+
label="Example Prompts"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Update examples when model size changes
|
| 154 |
+
def update_examples(model_choice):
|
| 155 |
+
if model_choice == "0.6B":
|
| 156 |
+
return gr.Examples.update(examples=examples_06B)
|
| 157 |
+
else:
|
| 158 |
+
return gr.Examples.update(examples=examples_16B)
|
| 159 |
+
|
| 160 |
+
model_size.change(fn=update_examples, inputs=[model_size], outputs=[examples_container])
|
| 161 |
|
| 162 |
gr.on(
|
| 163 |
triggers=[run_button.click, prompt.submit],
|
| 164 |
fn = infer,
|
| 165 |
+
inputs = [prompt, model_size, seed, randomize_seed, width, height, guidance_scale, num_inference_steps], # Add model_size to inputs
|
| 166 |
outputs = [result, seed]
|
| 167 |
)
|
| 168 |
|