Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +32 -32
- requirements.txt +14 -13
app.py
CHANGED
|
@@ -1,35 +1,33 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from diffusers import
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
-
MODEL_ID = "
|
| 9 |
-
REFINER_MODEL_ID = "stabilityai/stable-diffusion-xl-refiner-1.0"
|
| 10 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
-
DTYPE = torch.
|
| 12 |
|
| 13 |
class FooocusGenerator:
|
| 14 |
def __init__(self):
|
| 15 |
self.pipe = None
|
| 16 |
-
self.refiner = None
|
| 17 |
self.load_models()
|
| 18 |
|
| 19 |
def load_models(self):
|
| 20 |
# Load base model
|
| 21 |
-
scheduler =
|
| 22 |
-
self.pipe =
|
| 23 |
MODEL_ID,
|
| 24 |
scheduler=scheduler,
|
| 25 |
torch_dtype=DTYPE,
|
| 26 |
-
|
| 27 |
-
variant="fp16" if DEVICE == "cuda" else None
|
| 28 |
)
|
| 29 |
|
| 30 |
if DEVICE == "cuda":
|
| 31 |
-
self.pipe.
|
| 32 |
-
self.pipe.to(DEVICE)
|
| 33 |
|
| 34 |
def generate_image(
|
| 35 |
self,
|
|
@@ -42,23 +40,40 @@ class FooocusGenerator:
|
|
| 42 |
image_seed,
|
| 43 |
sharpness,
|
| 44 |
guidance_scale,
|
| 45 |
-
base_model_name,
|
| 46 |
-
refiner_model_name,
|
| 47 |
progress=gr.Progress()
|
| 48 |
):
|
| 49 |
try:
|
| 50 |
# Process style selections
|
| 51 |
processed_prompt = self.process_style(prompt, style_selections)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
image = self.pipe(
|
| 57 |
prompt=processed_prompt,
|
| 58 |
negative_prompt=negative_prompt,
|
| 59 |
-
num_inference_steps=
|
| 60 |
guidance_scale=guidance_scale,
|
| 61 |
generator=generator,
|
|
|
|
|
|
|
| 62 |
).images[0]
|
| 63 |
|
| 64 |
return image
|
|
@@ -158,19 +173,6 @@ with gr.Blocks(title="Fooocus Web", theme=gr.themes.Soft()) as demo:
|
|
| 158 |
label="Guidance Scale"
|
| 159 |
)
|
| 160 |
|
| 161 |
-
with gr.Row():
|
| 162 |
-
base_model_name = gr.Dropdown(
|
| 163 |
-
choices=["SDXL 1.0"],
|
| 164 |
-
label="Base Model",
|
| 165 |
-
value="SDXL 1.0"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
refiner_model_name = gr.Dropdown(
|
| 169 |
-
choices=["SDXL Refiner"],
|
| 170 |
-
label="Refiner",
|
| 171 |
-
value="SDXL Refiner"
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
generate_btn = gr.Button("🎨 Generate", variant="primary")
|
| 175 |
|
| 176 |
with gr.Column():
|
|
@@ -190,8 +192,6 @@ with gr.Blocks(title="Fooocus Web", theme=gr.themes.Soft()) as demo:
|
|
| 190 |
image_seed,
|
| 191 |
sharpness,
|
| 192 |
guidance_scale,
|
| 193 |
-
base_model_name,
|
| 194 |
-
refiner_model_name
|
| 195 |
],
|
| 196 |
outputs=output_image
|
| 197 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
| 5 |
+
import warnings
|
| 6 |
+
warnings.filterwarnings('ignore')
|
| 7 |
|
| 8 |
# Constants
|
| 9 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5" # Using SD 1.5 for better compatibility
|
|
|
|
| 10 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
DTYPE = torch.float32 # Using float32 for better compatibility
|
| 12 |
|
| 13 |
class FooocusGenerator:
|
| 14 |
def __init__(self):
|
| 15 |
self.pipe = None
|
|
|
|
| 16 |
self.load_models()
|
| 17 |
|
| 18 |
def load_models(self):
|
| 19 |
# Load base model
|
| 20 |
+
scheduler = DDIMScheduler.from_pretrained(MODEL_ID, subfolder="scheduler")
|
| 21 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(
|
| 22 |
MODEL_ID,
|
| 23 |
scheduler=scheduler,
|
| 24 |
torch_dtype=DTYPE,
|
| 25 |
+
safety_checker=None
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
if DEVICE == "cuda":
|
| 29 |
+
self.pipe.enable_attention_slicing()
|
| 30 |
+
self.pipe = self.pipe.to(DEVICE)
|
| 31 |
|
| 32 |
def generate_image(
|
| 33 |
self,
|
|
|
|
| 40 |
image_seed,
|
| 41 |
sharpness,
|
| 42 |
guidance_scale,
|
|
|
|
|
|
|
| 43 |
progress=gr.Progress()
|
| 44 |
):
|
| 45 |
try:
|
| 46 |
# Process style selections
|
| 47 |
processed_prompt = self.process_style(prompt, style_selections)
|
| 48 |
|
| 49 |
+
# Set seed for reproducibility
|
| 50 |
+
if image_seed == -1:
|
| 51 |
+
image_seed = torch.randint(0, 2147483647, (1,)).item()
|
| 52 |
+
|
| 53 |
+
generator = torch.manual_seed(image_seed)
|
| 54 |
+
|
| 55 |
+
# Set steps based on performance selection
|
| 56 |
+
steps = {
|
| 57 |
+
"Speed": 20,
|
| 58 |
+
"Quality": 30,
|
| 59 |
+
"Extreme Speed": 15
|
| 60 |
+
}.get(performance_selection, 30)
|
| 61 |
+
|
| 62 |
+
# Set image dimensions based on aspect ratio
|
| 63 |
+
dimensions = {
|
| 64 |
+
"Square": (512, 512),
|
| 65 |
+
"Portrait": (512, 768),
|
| 66 |
+
"Landscape": (768, 512)
|
| 67 |
+
}.get(aspect_ratios_selection, (512, 512))
|
| 68 |
|
| 69 |
image = self.pipe(
|
| 70 |
prompt=processed_prompt,
|
| 71 |
negative_prompt=negative_prompt,
|
| 72 |
+
num_inference_steps=steps,
|
| 73 |
guidance_scale=guidance_scale,
|
| 74 |
generator=generator,
|
| 75 |
+
width=dimensions[0],
|
| 76 |
+
height=dimensions[1]
|
| 77 |
).images[0]
|
| 78 |
|
| 79 |
return image
|
|
|
|
| 173 |
label="Guidance Scale"
|
| 174 |
)
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
generate_btn = gr.Button("🎨 Generate", variant="primary")
|
| 177 |
|
| 178 |
with gr.Column():
|
|
|
|
| 192 |
image_seed,
|
| 193 |
sharpness,
|
| 194 |
guidance_scale,
|
|
|
|
|
|
|
| 195 |
],
|
| 196 |
outputs=output_image
|
| 197 |
)
|
requirements.txt
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
-
torch==2.
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
gradio==4.19.2
|
| 6 |
-
diffusers==0.
|
| 7 |
-
opencv-python
|
| 8 |
-
einops
|
| 9 |
-
|
| 10 |
-
omegaconf
|
| 11 |
-
|
| 12 |
-
xformers
|
| 13 |
-
triton
|
| 14 |
-
compel
|
|
|
|
| 1 |
+
torch==2.0.1
|
| 2 |
+
torchvision==0.15.2
|
| 3 |
+
transformers==4.30.2
|
| 4 |
+
accelerate==0.21.0
|
| 5 |
+
safetensors==0.3.1
|
| 6 |
gradio==4.19.2
|
| 7 |
+
diffusers==0.19.3
|
| 8 |
+
opencv-python==4.8.0.74
|
| 9 |
+
einops==0.6.1
|
| 10 |
+
pytorch-lightning==2.0.2
|
| 11 |
+
omegaconf==2.3.0
|
| 12 |
+
huggingface-hub==0.16.4
|
| 13 |
+
xformers==0.0.20
|
| 14 |
+
triton==2.0.0
|
| 15 |
+
compel==2.0.1
|