Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,32 @@ import os
|
|
2 |
import random
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
-
from torchvision import transforms
|
6 |
-
from PIL import Image
|
7 |
-
import numpy as np
|
8 |
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
9 |
-
from io import BytesIO
|
10 |
|
11 |
-
#
|
|
|
12 |
MODEL_CACHE = "model_cache"
|
13 |
os.makedirs(MODEL_CACHE, exist_ok=True)
|
14 |
|
15 |
-
# Initialize
|
16 |
def get_pipeline():
|
17 |
-
model_id = "OFA-Sys/small-stable-diffusion-v0"
|
18 |
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(
|
19 |
-
|
20 |
subfolder="scheduler",
|
21 |
-
cache_dir=MODEL_CACHE
|
|
|
22 |
)
|
23 |
|
24 |
return StableDiffusionPipeline.from_pretrained(
|
25 |
-
|
26 |
scheduler=scheduler,
|
27 |
torch_dtype=torch.float32,
|
28 |
cache_dir=MODEL_CACHE,
|
29 |
-
safety_checker=None
|
|
|
30 |
).to("cpu")
|
31 |
|
32 |
-
# Load model
|
33 |
pipeline = get_pipeline()
|
34 |
|
35 |
def generate_image(
|
@@ -47,14 +45,15 @@ def generate_image(
|
|
47 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
48 |
|
49 |
# Generate image
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
return image, seed
|
60 |
|
@@ -70,7 +69,7 @@ STYLE_PRESETS = {
|
|
70 |
def apply_style(prompt, style_name):
|
71 |
return f"{STYLE_PRESETS[style_name]}, {prompt}"
|
72 |
|
73 |
-
#
|
74 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
75 |
gr.Markdown("""
|
76 |
# ⚡ FLUX Turbo Generator
|
@@ -132,10 +131,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
132 |
label="Example Prompts"
|
133 |
)
|
134 |
|
135 |
-
# Launch
|
136 |
if __name__ == "__main__":
|
|
|
137 |
demo.launch(
|
138 |
server_name="0.0.0.0",
|
139 |
-
server_port=int(os.getenv("PORT", 7860))
|
140 |
-
enable_queue=True
|
141 |
-
)
|
|
|
2 |
import random
|
3 |
import gradio as gr
|
4 |
import torch
|
|
|
|
|
|
|
5 |
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
|
|
6 |
|
7 |
+
# Configuration
|
8 |
+
MODEL_ID = "OFA-Sys/small-stable-diffusion-v0"
|
9 |
MODEL_CACHE = "model_cache"
|
10 |
os.makedirs(MODEL_CACHE, exist_ok=True)
|
11 |
|
12 |
+
# Initialize pipeline with updated scheduler config
|
13 |
def get_pipeline():
|
|
|
14 |
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(
|
15 |
+
MODEL_ID,
|
16 |
subfolder="scheduler",
|
17 |
+
cache_dir=MODEL_CACHE,
|
18 |
+
steps_offset=1 # Fix for the deprecation warning
|
19 |
)
|
20 |
|
21 |
return StableDiffusionPipeline.from_pretrained(
|
22 |
+
MODEL_ID,
|
23 |
scheduler=scheduler,
|
24 |
torch_dtype=torch.float32,
|
25 |
cache_dir=MODEL_CACHE,
|
26 |
+
safety_checker=None,
|
27 |
+
local_files_only=False
|
28 |
).to("cpu")
|
29 |
|
30 |
+
# Load model
|
31 |
pipeline = get_pipeline()
|
32 |
|
33 |
def generate_image(
|
|
|
45 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
46 |
|
47 |
# Generate image
|
48 |
+
with torch.no_grad():
|
49 |
+
image = pipeline(
|
50 |
+
prompt,
|
51 |
+
width=width,
|
52 |
+
height=height,
|
53 |
+
guidance_scale=guidance_scale,
|
54 |
+
num_inference_steps=num_inference_steps,
|
55 |
+
generator=generator
|
56 |
+
).images[0]
|
57 |
|
58 |
return image, seed
|
59 |
|
|
|
69 |
def apply_style(prompt, style_name):
|
70 |
return f"{STYLE_PRESETS[style_name]}, {prompt}"
|
71 |
|
72 |
+
# Create Gradio interface
|
73 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
74 |
gr.Markdown("""
|
75 |
# ⚡ FLUX Turbo Generator
|
|
|
131 |
label="Example Prompts"
|
132 |
)
|
133 |
|
134 |
+
# Launch with corrected parameters
|
135 |
if __name__ == "__main__":
|
136 |
+
demo.queue() # Enable queuing
|
137 |
demo.launch(
|
138 |
server_name="0.0.0.0",
|
139 |
+
server_port=int(os.getenv("PORT", 7860))
|
|
|
|