Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
import numpy as np
|
| 4 |
import random
|
|
|
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import torch
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
|
| 11 |
|
| 12 |
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 13 |
-
|
| 14 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 15 |
pipe = pipe.to(device)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
trigger_word = "Turbo Portrait"
|
| 19 |
-
pipe.fuse_lora(lora_scale=1.0)
|
| 20 |
-
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
MAX_IMAGE_SIZE = 1024
|
| 23 |
|
|
@@ -44,65 +42,66 @@ style_list = [
|
|
| 44 |
"negative_prompt": "",
|
| 45 |
},
|
| 46 |
]
|
| 47 |
-
|
| 48 |
STYLE_NAMES = [s["name"] for s in style_list]
|
| 49 |
DEFAULT_STYLE_NAME = STYLE_NAMES[0]
|
| 50 |
|
|
|
|
| 51 |
grid_sizes = {
|
| 52 |
"2x1": (2, 1),
|
| 53 |
"1x2": (1, 2),
|
| 54 |
"2x2": (2, 2),
|
| 55 |
"2x3": (2, 3),
|
| 56 |
"3x2": (3, 2),
|
| 57 |
-
"1x1": (1, 1)
|
| 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 |
examples = [
|
| 108 |
"A tiny astronaut hatching from an egg on the moon, 4k, planet theme",
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
+
|
| 5 |
+
import spaces
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
# Device and model setup
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
|
| 13 |
|
| 14 |
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
|
|
|
| 15 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 16 |
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
+
# Constants
|
|
|
|
|
|
|
|
|
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
MAX_IMAGE_SIZE = 1024
|
| 21 |
|
|
|
|
| 42 |
"negative_prompt": "",
|
| 43 |
},
|
| 44 |
]
|
|
|
|
| 45 |
STYLE_NAMES = [s["name"] for s in style_list]
|
| 46 |
DEFAULT_STYLE_NAME = STYLE_NAMES[0]
|
| 47 |
|
| 48 |
+
# Define grid layouts
|
| 49 |
grid_sizes = {
|
| 50 |
"2x1": (2, 1),
|
| 51 |
"1x2": (1, 2),
|
| 52 |
"2x2": (2, 2),
|
| 53 |
"2x3": (2, 3),
|
| 54 |
"3x2": (3, 2),
|
| 55 |
+
"1x1": (1, 1),
|
| 56 |
}
|
| 57 |
+
\ n@spaces.GPU
|
| 58 |
+
def infer(
|
| 59 |
+
prompt,
|
| 60 |
+
negative_prompt="",
|
| 61 |
+
seed=42,
|
| 62 |
+
randomize_seed=False,
|
| 63 |
+
width=1024,
|
| 64 |
+
height=1024,
|
| 65 |
+
guidance_scale=7.5,
|
| 66 |
+
num_inference_steps=10,
|
| 67 |
+
style="Style Zero",
|
| 68 |
+
grid_size="1x1",
|
| 69 |
+
progress=gr.Progress(track_tqdm=True),
|
| 70 |
+
):
|
| 71 |
+
# Apply seed
|
| 72 |
+
if randomize_seed:
|
| 73 |
+
seed = random.randint(0, MAX_SEED)
|
| 74 |
+
generator = torch.Generator().manual_seed(seed)
|
| 75 |
+
|
| 76 |
+
# Style formatting
|
| 77 |
+
selected_style = next(s for s in style_list if s["name"] == style)
|
| 78 |
+
styled_prompt = selected_style["prompt"].format(prompt=prompt)
|
| 79 |
+
styled_negative = selected_style["negative_prompt"] or negative_prompt
|
| 80 |
+
|
| 81 |
+
# Grid calculation
|
| 82 |
+
grid_x, grid_y = grid_sizes.get(grid_size, (1, 1))
|
| 83 |
+
num_images = grid_x * grid_y
|
| 84 |
+
|
| 85 |
+
# Inference
|
| 86 |
+
output = pipe(
|
| 87 |
+
prompt=styled_prompt,
|
| 88 |
+
negative_prompt=styled_negative,
|
| 89 |
+
width=width,
|
| 90 |
+
height=height,
|
| 91 |
+
guidance_scale=guidance_scale,
|
| 92 |
+
num_inference_steps=num_inference_steps,
|
| 93 |
+
generator=generator,
|
| 94 |
+
num_images_per_prompt=num_images,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Combine into grid
|
| 98 |
+
grid_img = Image.new('RGB', (width * grid_x, height * grid_y))
|
| 99 |
+
for i, img in enumerate(output.images[:num_images]):
|
| 100 |
+
x = (i % grid_x) * width
|
| 101 |
+
y = (i // grid_x) * height
|
| 102 |
+
grid_img.paste(img, (x, y))
|
| 103 |
+
|
| 104 |
+
return grid_img, seed
|
| 105 |
|
| 106 |
examples = [
|
| 107 |
"A tiny astronaut hatching from an egg on the moon, 4k, planet theme",
|