Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import os
|
| 3 |
import torch
|
| 4 |
import random
|
|
@@ -13,23 +12,29 @@ import gradio as gr
|
|
| 13 |
# Download the model files
|
| 14 |
ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
pipe = StableDiffusionXLPipeline(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
pipe = pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@spaces.GPU(duration=200)
|
| 35 |
def generate_image(prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, num_images_per_prompt, use_random_seed, seed, progress=gr.Progress(track_tqdm=True)):
|
|
@@ -38,16 +43,19 @@ def generate_image(prompt, negative_prompt, height, width, num_inference_steps,
|
|
| 38 |
else:
|
| 39 |
seed = int(seed) # Ensure seed is an integer
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
return image, seed
|
| 52 |
|
| 53 |
description = """
|
|
@@ -86,4 +94,4 @@ iface = gr.Interface(
|
|
| 86 |
theme='bethecloud/storj_theme',
|
| 87 |
)
|
| 88 |
|
| 89 |
-
iface.launch(debug=
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import random
|
|
|
|
| 12 |
# Download the model files
|
| 13 |
ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
|
| 14 |
|
| 15 |
+
# Function to load models
|
| 16 |
+
def load_models():
|
| 17 |
+
# Load models on demand to reduce initial memory footprint
|
| 18 |
+
text_encoder = ChatGLMModel.from_pretrained(
|
| 19 |
+
os.path.join(ckpt_dir, 'text_encoder'),
|
| 20 |
+
torch_dtype=torch.float16).half()
|
| 21 |
+
tokenizer = ChatGLMTokenizer.from_pretrained(os.path.join(ckpt_dir, 'text_encoder'))
|
| 22 |
+
vae = AutoencoderKL.from_pretrained(os.path.join(ckpt_dir, "vae"), revision=None).half()
|
| 23 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(os.path.join(ckpt_dir, "scheduler"))
|
| 24 |
+
unet = UNet2DConditionModel.from_pretrained(os.path.join(ckpt_dir, "unet"), revision=None).half()
|
| 25 |
|
| 26 |
+
pipe = StableDiffusionXLPipeline(
|
| 27 |
+
vae=vae,
|
| 28 |
+
text_encoder=text_encoder,
|
| 29 |
+
tokenizer=tokenizer,
|
| 30 |
+
unet=unet,
|
| 31 |
+
scheduler=scheduler,
|
| 32 |
+
force_zeros_for_empty_prompt=False)
|
| 33 |
+
pipe = pipe.to("cuda")
|
| 34 |
+
|
| 35 |
+
return pipe
|
| 36 |
+
|
| 37 |
+
pipe = load_models()
|
| 38 |
|
| 39 |
@spaces.GPU(duration=200)
|
| 40 |
def generate_image(prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, num_images_per_prompt, use_random_seed, seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 43 |
else:
|
| 44 |
seed = int(seed) # Ensure seed is an integer
|
| 45 |
|
| 46 |
+
# Move the model to the GPU for inference
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
image = pipe(
|
| 49 |
+
prompt=prompt,
|
| 50 |
+
negative_prompt=negative_prompt,
|
| 51 |
+
height=height,
|
| 52 |
+
width=width,
|
| 53 |
+
num_inference_steps=num_inference_steps,
|
| 54 |
+
guidance_scale=guidance_scale,
|
| 55 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 56 |
+
generator=torch.Generator(pipe.device).manual_seed(seed)
|
| 57 |
+
).images
|
| 58 |
+
|
| 59 |
return image, seed
|
| 60 |
|
| 61 |
description = """
|
|
|
|
| 94 |
theme='bethecloud/storj_theme',
|
| 95 |
)
|
| 96 |
|
| 97 |
+
iface.launch() # Set debug=False for production
|