Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
import uuid
|
|
@@ -7,7 +15,7 @@ import numpy as np
|
|
| 7 |
from PIL import Image
|
| 8 |
import spaces
|
| 9 |
import torch
|
| 10 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 11 |
|
| 12 |
css = '''
|
| 13 |
.gradio-container{max-width: 570px !important}
|
|
@@ -18,9 +26,7 @@ footer {
|
|
| 18 |
'''
|
| 19 |
|
| 20 |
DESCRIPTIONXX = """
|
| 21 |
-
|
| 22 |
## TEXT 2 IMAGE🥠
|
| 23 |
-
|
| 24 |
"""
|
| 25 |
examples = [
|
| 26 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
@@ -33,7 +39,7 @@ examples = [
|
|
| 33 |
MODEL_OPTIONS = {
|
| 34 |
"Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
| 35 |
"Turbovision": "SG161222/RealVisXL_V3.0_Turbo",
|
| 36 |
-
|
| 37 |
}
|
| 38 |
|
| 39 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
|
@@ -44,13 +50,20 @@ BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
|
| 44 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 45 |
|
| 46 |
def load_and_prepare_model(model_id):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if USE_TORCH_COMPILE:
|
| 56 |
pipe.compile()
|
|
@@ -60,7 +73,7 @@ def load_and_prepare_model(model_id):
|
|
| 60 |
|
| 61 |
return pipe
|
| 62 |
|
| 63 |
-
# Preload and compile
|
| 64 |
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
| 65 |
|
| 66 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 3 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 4 |
+
# in the Software without restriction, including without limitation the rights
|
| 5 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 6 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 7 |
+
# furnished to do so, subject to the following conditions:
|
| 8 |
+
#
|
| 9 |
+
# ...
|
| 10 |
import os
|
| 11 |
import random
|
| 12 |
import uuid
|
|
|
|
| 15 |
from PIL import Image
|
| 16 |
import spaces
|
| 17 |
import torch
|
| 18 |
+
from diffusers import DiffusionPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 19 |
|
| 20 |
css = '''
|
| 21 |
.gradio-container{max-width: 570px !important}
|
|
|
|
| 26 |
'''
|
| 27 |
|
| 28 |
DESCRIPTIONXX = """
|
|
|
|
| 29 |
## TEXT 2 IMAGE🥠
|
|
|
|
| 30 |
"""
|
| 31 |
examples = [
|
| 32 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
|
|
| 39 |
MODEL_OPTIONS = {
|
| 40 |
"Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
| 41 |
"Turbovision": "SG161222/RealVisXL_V3.0_Turbo",
|
| 42 |
+
"FLUX.1-schnell": "black-forest-labs/FLUX.1-schnell",
|
| 43 |
}
|
| 44 |
|
| 45 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
|
|
|
| 50 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 51 |
|
| 52 |
def load_and_prepare_model(model_id):
|
| 53 |
+
if model_id == "black-forest-labs/FLUX.1-schnell":
|
| 54 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 55 |
+
model_id,
|
| 56 |
+
torch_dtype=torch.bfloat16,
|
| 57 |
+
revision="refs/pr/1"
|
| 58 |
+
).to(device)
|
| 59 |
+
else:
|
| 60 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 61 |
+
model_id,
|
| 62 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 63 |
+
use_safetensors=True,
|
| 64 |
+
add_watermarker=False,
|
| 65 |
+
).to(device)
|
| 66 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 67 |
|
| 68 |
if USE_TORCH_COMPILE:
|
| 69 |
pipe.compile()
|
|
|
|
| 73 |
|
| 74 |
return pipe
|
| 75 |
|
| 76 |
+
# Preload and compile all models
|
| 77 |
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}
|
| 78 |
|
| 79 |
MAX_SEED = np.iinfo(np.int32).max
|