Spaces:
Running
on
Zero
Running
on
Zero
首次提交
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +166 -106
- requirements.txt +8 -6
- utils.py +35 -0
__pycache__/utils.cpython-310.pyc
ADDED
|
Binary file (1.13 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,128 +1,186 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if torch.cuda.is_available():
|
| 13 |
-
torch_dtype = torch.float16
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
-
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
-
pipe = pipe.to(device)
|
| 19 |
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
):
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
image
|
| 42 |
-
prompt=prompt,
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
-
generator=generator,
|
| 49 |
-
).images[0]
|
| 50 |
|
| 51 |
-
return
|
| 52 |
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
examples = [
|
| 55 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
"An astronaut riding a green horse",
|
| 57 |
"A delicious ceviche cheesecake slice",
|
| 58 |
]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
with gr.Blocks(css=css) as demo:
|
| 68 |
-
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
prompt = gr.Text(
|
| 73 |
-
label="Prompt",
|
| 74 |
-
show_label=False,
|
| 75 |
-
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
-
|
| 82 |
-
result = gr.Image(label="Result", show_label=False)
|
| 83 |
-
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
label="Seed",
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Row():
|
| 103 |
width = gr.Slider(
|
| 104 |
label="Width",
|
| 105 |
-
minimum=
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=
|
| 108 |
-
value=
|
| 109 |
)
|
| 110 |
-
|
| 111 |
height = gr.Slider(
|
| 112 |
label="Height",
|
| 113 |
-
minimum=
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=
|
| 116 |
-
value=
|
| 117 |
)
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with gr.Row():
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
label="Guidance scale",
|
| 122 |
-
minimum=
|
| 123 |
-
maximum=
|
| 124 |
step=0.1,
|
| 125 |
-
value=
|
| 126 |
)
|
| 127 |
|
| 128 |
num_inference_steps = gr.Slider(
|
|
@@ -130,25 +188,27 @@ with gr.Blocks(css=css) as demo:
|
|
| 130 |
minimum=1,
|
| 131 |
maximum=50,
|
| 132 |
step=1,
|
| 133 |
-
value=
|
| 134 |
)
|
|
|
|
| 135 |
|
| 136 |
-
gr.
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
outputs=[result, seed],
|
| 151 |
-
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import random
|
| 6 |
+
import logging
|
| 7 |
+
import utils
|
| 8 |
+
from diffusers.models import AutoencoderKL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
MAX_SEED = np.iinfo(np.int32).max
|
| 11 |
+
MIN_IMAGE_SIZE = 512
|
| 12 |
+
MAX_IMAGE_SIZE = 2048
|
| 13 |
+
|
| 14 |
+
# Enhanced logging configuration
|
| 15 |
+
logging.basicConfig(
|
| 16 |
+
level=logging.INFO,
|
| 17 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 18 |
+
datefmt='%Y-%m-%d %H:%M:%S'
|
| 19 |
+
)
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
# PyTorch settings for better performance and determinism
|
| 23 |
+
torch.backends.cudnn.deterministic = True
|
| 24 |
+
torch.backends.cudnn.benchmark = False
|
| 25 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 26 |
+
|
| 27 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 28 |
+
logger.info(f"Using device: {device}")
|
| 29 |
+
|
| 30 |
+
# Model initialization
|
| 31 |
+
# if torch.cuda.is_available():
|
| 32 |
+
# try:
|
| 33 |
+
# logger.info("Loading VAE and pipeline...")
|
| 34 |
+
# vae = AutoencoderKL.from_pretrained(
|
| 35 |
+
# "madebyollin/sdxl-vae-fp16-fix",
|
| 36 |
+
# torch_dtype=torch.float16,
|
| 37 |
+
# )
|
| 38 |
+
# pipe = utils.load_pipeline("cagliostrolab/animagine-xl-4.0", device, vae=vae)
|
| 39 |
+
# logger.info("Pipeline loaded successfully on GPU!")
|
| 40 |
+
# except Exception as e:
|
| 41 |
+
# logger.error(f"Error loading VAE, falling back to default: {e}")
|
| 42 |
+
# pipe = utils.load_pipeline("cagliostrolab/animagine-xl-4.0", device)
|
| 43 |
+
# else:
|
| 44 |
+
# logger.warning("CUDA not available, running on CPU")
|
| 45 |
+
# pipe = None
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@spaces.GPU
|
| 50 |
+
def generate(
|
| 51 |
+
prompt: str,
|
| 52 |
+
negative_prompt: str,
|
| 53 |
+
width: int,
|
| 54 |
+
height: int,
|
| 55 |
+
scheduler: str,
|
| 56 |
+
upscaler_strength:float,
|
| 57 |
+
upscale_by:float,
|
| 58 |
+
seed: int,
|
| 59 |
+
randomize_seed: bool,
|
| 60 |
+
guidance_scale: float,
|
| 61 |
+
num_inference_steps: int,
|
| 62 |
+
progress:gr.Progress=gr.Progress(track_tqdm=True),
|
| 63 |
):
|
| 64 |
if randomize_seed:
|
| 65 |
seed = random.randint(0, MAX_SEED)
|
| 66 |
|
| 67 |
+
# generator = torch.Generator().manual_seed(seed)
|
| 68 |
+
|
| 69 |
+
# image = pipe(
|
| 70 |
+
# prompt=prompt,
|
| 71 |
+
# negative_prompt=negative_prompt,
|
| 72 |
+
# guidance_scale=guidance_scale,
|
| 73 |
+
# num_inference_steps=num_inference_steps,
|
| 74 |
+
# width=width,
|
| 75 |
+
# height=height,
|
| 76 |
+
# generator=generator,
|
| 77 |
+
# ).images[0]
|
| 78 |
|
| 79 |
+
# return image, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
return None, seed
|
| 82 |
|
| 83 |
|
| 84 |
+
|
| 85 |
+
scheduler_list = [
|
| 86 |
+
"DPM++ 2M Karras",
|
| 87 |
+
"DPM++ SDE Karras",
|
| 88 |
+
"DPM++ 2M SDE Karras",
|
| 89 |
+
"Euler",
|
| 90 |
+
"Euler a",
|
| 91 |
+
"DDIM"
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
title = "# Animagine XL 4.0 Demo"
|
| 96 |
+
|
| 97 |
examples = [
|
| 98 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 99 |
"An astronaut riding a green horse",
|
| 100 |
"A delicious ceviche cheesecake slice",
|
| 101 |
]
|
| 102 |
|
| 103 |
+
custom_css = """
|
| 104 |
+
#row-container {
|
| 105 |
+
align-items: stretch;
|
| 106 |
+
}
|
| 107 |
+
#output-image{
|
| 108 |
+
flex-grow: 1;
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
with gr.Blocks(css=custom_css).queue() as demo:
|
| 114 |
+
gr.Markdown(title)
|
| 115 |
+
with gr.Row(
|
| 116 |
+
elem_id="row-container"
|
| 117 |
+
):
|
| 118 |
+
with gr.Column():
|
| 119 |
+
gr.Markdown("### Input")
|
| 120 |
+
with gr.Column():
|
| 121 |
+
prompt = gr.Text(
|
| 122 |
+
label="Prompt",
|
| 123 |
+
max_lines=1,
|
| 124 |
+
placeholder="Enter your prompt",
|
| 125 |
+
)
|
| 126 |
+
negative_prompt = gr.Text(
|
| 127 |
+
label="Negative prompt",
|
| 128 |
+
max_lines=1,
|
| 129 |
+
placeholder="Enter a negative prompt",
|
| 130 |
+
)
|
| 131 |
with gr.Row():
|
| 132 |
width = gr.Slider(
|
| 133 |
label="Width",
|
| 134 |
+
minimum=MIN_IMAGE_SIZE,
|
| 135 |
maximum=MAX_IMAGE_SIZE,
|
| 136 |
+
step=8,
|
| 137 |
+
value=832,
|
| 138 |
)
|
|
|
|
| 139 |
height = gr.Slider(
|
| 140 |
label="Height",
|
| 141 |
+
minimum=MIN_IMAGE_SIZE,
|
| 142 |
maximum=MAX_IMAGE_SIZE,
|
| 143 |
+
step=8,
|
| 144 |
+
value=1216,
|
| 145 |
)
|
| 146 |
+
with gr.Row():
|
| 147 |
+
upscaler_strength = gr.Slider(
|
| 148 |
+
label="Upscaler strength",
|
| 149 |
+
minimum=0,
|
| 150 |
+
maximum=1,
|
| 151 |
+
step=0.05,
|
| 152 |
+
value=0.55,
|
| 153 |
+
)
|
| 154 |
+
upscale_by = gr.Slider(
|
| 155 |
+
label="Upscale",
|
| 156 |
+
minimum=1,
|
| 157 |
+
maximum=1.5,
|
| 158 |
+
step=0.1,
|
| 159 |
+
value=1.5,
|
| 160 |
+
)
|
| 161 |
+
with gr.Column():
|
| 162 |
+
scheduler = gr.Dropdown(
|
| 163 |
+
label="scheduler",
|
| 164 |
+
choices=scheduler_list,
|
| 165 |
+
interactive=True,
|
| 166 |
+
value="Euler a",
|
| 167 |
+
)
|
| 168 |
+
with gr.Column():
|
| 169 |
+
seed = gr.Slider(
|
| 170 |
+
label="Seed",
|
| 171 |
+
minimum=0,
|
| 172 |
+
maximum=MAX_SEED,
|
| 173 |
+
step=1,
|
| 174 |
+
value=0,
|
| 175 |
+
)
|
| 176 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 177 |
with gr.Row():
|
| 178 |
guidance_scale = gr.Slider(
|
| 179 |
label="Guidance scale",
|
| 180 |
+
minimum=1.0,
|
| 181 |
+
maximum=12.0,
|
| 182 |
step=0.1,
|
| 183 |
+
value=6.0,
|
| 184 |
)
|
| 185 |
|
| 186 |
num_inference_steps = gr.Slider(
|
|
|
|
| 188 |
minimum=1,
|
| 189 |
maximum=50,
|
| 190 |
step=1,
|
| 191 |
+
value=25,
|
| 192 |
)
|
| 193 |
+
run_button = gr.Button("Run", variant="primary")
|
| 194 |
|
| 195 |
+
with gr.Column():
|
| 196 |
+
gr.Markdown("### Output")
|
| 197 |
+
result = gr.Image(
|
| 198 |
+
label="Generated Image",
|
| 199 |
+
elem_id="output-image"
|
| 200 |
+
)
|
| 201 |
+
run_button.click(
|
| 202 |
+
fn=generate,
|
| 203 |
inputs=[
|
| 204 |
+
prompt, negative_prompt,
|
| 205 |
+
width, height,
|
| 206 |
+
scheduler,
|
| 207 |
+
upscaler_strength,upscale_by,
|
| 208 |
+
seed,randomize_seed,
|
| 209 |
+
guidance_scale,num_inference_steps
|
| 210 |
+
],
|
|
|
|
|
|
|
| 211 |
outputs=[result, seed],
|
| 212 |
+
)
|
| 213 |
|
| 214 |
+
demo.launch()
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
-
accelerate
|
| 2 |
-
diffusers
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate>=1.2.1
|
| 2 |
+
diffusers>=0.32.1
|
| 3 |
+
gradio==4.44.1
|
| 4 |
+
hf-transfer>=0.1.9
|
| 5 |
+
spaces>=0.32.0
|
| 6 |
+
torch>=2.4.0
|
| 7 |
+
transformers>=4.48.0
|
| 8 |
+
tomli>=2.0.1
|
utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import Optional, Any
|
| 3 |
+
from diffusers import (
|
| 4 |
+
DDIMScheduler,
|
| 5 |
+
DPMSolverMultistepScheduler,
|
| 6 |
+
DPMSolverSinglestepScheduler,
|
| 7 |
+
EulerAncestralDiscreteScheduler,
|
| 8 |
+
EulerDiscreteScheduler,
|
| 9 |
+
AutoencoderKL,
|
| 10 |
+
StableDiffusionXLPipeline,
|
| 11 |
+
)
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
def load_pipeline(model_name: str, device: torch.device, hf_token: Optional[str] = None, vae: Optional[AutoencoderKL] = None) -> Any:
|
| 15 |
+
"""Load the Stable Diffusion pipeline."""
|
| 16 |
+
try:
|
| 17 |
+
pipeline = (
|
| 18 |
+
StableDiffusionXLPipeline.from_single_file
|
| 19 |
+
if model_name.endswith(".safetensors")
|
| 20 |
+
else StableDiffusionXLPipeline.from_pretrained
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
pipe = pipeline(
|
| 24 |
+
model_name,
|
| 25 |
+
vae=vae,
|
| 26 |
+
torch_dtype=torch.float16,
|
| 27 |
+
custom_pipeline="lpw_stable_diffusion_xl",
|
| 28 |
+
use_safetensors=True,
|
| 29 |
+
add_watermarker=False
|
| 30 |
+
)
|
| 31 |
+
pipe.to(device)
|
| 32 |
+
return pipe
|
| 33 |
+
except Exception as e:
|
| 34 |
+
logging.error(f"Failed to load pipeline: {str(e)}", exc_info=True)
|
| 35 |
+
raise
|