Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers import SanaPipeline
|
| 5 |
+
|
| 6 |
+
# Check if GPU is available, fallback safely to CPU
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
current_model_id = ""
|
| 9 |
+
pipe = None
|
| 10 |
+
|
| 11 |
+
def load_sana_model(model_name):
|
| 12 |
+
global pipe, current_model_id
|
| 13 |
+
|
| 14 |
+
# Map selection names to exact Hugging Face repositories
|
| 15 |
+
model_map = {
|
| 16 |
+
"Sana-1.6B (High Quality)": "Efficient-Large-Model/SANA1.5_1.6B_1024px_diffusers",
|
| 17 |
+
"Sana-0.6B (Blazing Fast)": "Efficient-Large-Model/Sana_600M_1024px_diffusers"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
selected_id = model_map[model_name]
|
| 21 |
+
|
| 22 |
+
# Avoid reloading if the exact model is already cached in memory
|
| 23 |
+
if current_model_id == selected_id and pipe is not None:
|
| 24 |
+
return f"Using already loaded {model_name}"
|
| 25 |
+
|
| 26 |
+
print(f"Loading {selected_id} onto {device}...")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Load the pipeline wrapper in bfloat16 to optimize VRAM footprint
|
| 30 |
+
pipe = SanaPipeline.from_pretrained(
|
| 31 |
+
selected_id,
|
| 32 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
|
| 33 |
+
)
|
| 34 |
+
pipe.to(device)
|
| 35 |
+
|
| 36 |
+
# Cast internal sub-networks safely to manage performance bounds
|
| 37 |
+
if device == "cuda":
|
| 38 |
+
pipe.vae.to(torch.bfloat16)
|
| 39 |
+
pipe.text_encoder.to(torch.bfloat16)
|
| 40 |
+
|
| 41 |
+
current_model_id = selected_id
|
| 42 |
+
return f"Successfully loaded {model_name}!"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Error loading model: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Default model initialization
|
| 47 |
+
load_sana_model("Sana-1.6B (High Quality)")
|
| 48 |
+
|
| 49 |
+
def generate_image(prompt, model_name, steps, guidance_scale, seed):
|
| 50 |
+
global pipe
|
| 51 |
+
if pipe is None:
|
| 52 |
+
load_sana_model(model_name)
|
| 53 |
+
|
| 54 |
+
# Check model compatibility on the fly if the user toggled weights
|
| 55 |
+
load_sana_model(model_name)
|
| 56 |
+
|
| 57 |
+
# Configure exact deterministic randomness seed
|
| 58 |
+
if seed == -1:
|
| 59 |
+
generator = None
|
| 60 |
+
else:
|
| 61 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
# Sana's specialized autoencoder targets sharp 1024x1024 resolutions natively
|
| 65 |
+
output = pipe(
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
height=1024,
|
| 68 |
+
width=1024,
|
| 69 |
+
guidance_scale=float(guidance_scale),
|
| 70 |
+
num_inference_steps=int(steps),
|
| 71 |
+
generator=generator
|
| 72 |
+
)
|
| 73 |
+
return output.images[0], "Generation completed successfully!"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return None, f"An error occurred: {str(e)}"
|
| 76 |
+
|
| 77 |
+
# Build custom minimalist theme interface
|
| 78 |
+
css = """
|
| 79 |
+
footer {visibility: hidden !important;}
|
| 80 |
+
#generate_btn {background: linear-gradient(90deg, #10b981, #059669) !important; color: white !important;}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(theme=gr.themes.Base(), css=css) as demo:
|
| 84 |
+
gr.Markdown("# 🚀 Sana Linear-Attention Image Engine")
|
| 85 |
+
gr.Markdown("Deeply optimized text-to-image generation running instantly via pure diffusers implementation.")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column(scale=4):
|
| 89 |
+
prompt_input = gr.Textbox(
|
| 90 |
+
label="Your Prompt String",
|
| 91 |
+
placeholder="A cyberpunk character portrait, glowing neon line accents, minimalist black aesthetic...",
|
| 92 |
+
lines=3
|
| 93 |
+
)
|
| 94 |
+
model_selector = gr.Radio(
|
| 95 |
+
choices=["Sana-1.6B (High Quality)", "Sana-0.6B (Blazing Fast)"],
|
| 96 |
+
value="Sana-1.6B (High Quality)",
|
| 97 |
+
label="Active Core Architecture"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Accordion("Advanced Hyperparameters", open=False):
|
| 101 |
+
inference_steps = gr.Slider(minimum=4, maximum=50, value=20, step=1, label="Inference Loop Steps")
|
| 102 |
+
cfg_scale = gr.Slider(minimum=1.0, maximum=10.0, value=4.5, step=0.1, label="Guidance Scale (CFG)")
|
| 103 |
+
random_seed = gr.Number(value=-1, label="Manual Seed Value (-1 for Random)")
|
| 104 |
+
|
| 105 |
+
generate_button = gr.Button("Synthesize Frame Assets", variant="primary", elem_id="generate_btn")
|
| 106 |
+
status_output = gr.Textbox(label="Backend System Logging", interactive=False)
|
| 107 |
+
|
| 108 |
+
with gr.Column(scale=5):
|
| 109 |
+
image_output = gr.Image(label="Render Output Pipeline", type="pil", interactive=False)
|
| 110 |
+
|
| 111 |
+
# Bind active logic flows
|
| 112 |
+
generate_button.click(
|
| 113 |
+
fn=generate_image,
|
| 114 |
+
inputs=[prompt_input, model_selector, inference_steps, cfg_scale, random_seed],
|
| 115 |
+
outputs=[image_output, status_output]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
demo.queue().launch()
|
| 120 |
+
|