Spaces:
Runtime error
Runtime error
using runwayml/stable-diffusion-v1-5
Browse files
app.py
CHANGED
|
@@ -1,42 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Use CPU optimized model
|
| 7 |
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
MODEL_ID = "
|
| 11 |
|
| 12 |
-
|
| 13 |
-
# Then uncomment the following lines:
|
| 14 |
-
from huggingface_hub import login
|
| 15 |
-
login(token=os.getenv("HF_TOKEN")) # Add HF_TOKEN as a secret in your Space settings
|
| 16 |
-
|
| 17 |
-
print(f"Loading model {MODEL_ID}...")
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
pipe = FluxPipeline.from_pretrained(
|
| 23 |
MODEL_ID,
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
-
pipe =
|
| 27 |
pipe.set_progress_bar_config(disable=True)
|
| 28 |
-
print("
|
| 29 |
except Exception as e:
|
| 30 |
-
print(f"
|
| 31 |
-
print("
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def generate(prompt):
|
| 35 |
try:
|
| 36 |
result = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
-
num_inference_steps=
|
| 39 |
-
|
|
|
|
| 40 |
)
|
| 41 |
return result.images[0]
|
| 42 |
except Exception as e:
|
|
@@ -47,8 +54,8 @@ interface = gr.Interface(
|
|
| 47 |
fn=generate,
|
| 48 |
inputs=gr.Textbox(lines=3, label="Prompt", placeholder="Enter your image prompt here..."),
|
| 49 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 50 |
-
title="⚡
|
| 51 |
-
description="
|
| 52 |
)
|
| 53 |
|
| 54 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import OnnxStableDiffusionPipeline
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Use CPU optimized model
|
| 7 |
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 8 |
|
| 9 |
+
# Using a proper ONNX model for CPU inference
|
| 10 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 11 |
|
| 12 |
+
print(f"Loading ONNX model {MODEL_ID}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# Use ONNX pipeline for better CPU performance
|
| 16 |
+
pipe = OnnxStableDiffusionPipeline.from_pretrained(
|
|
|
|
| 17 |
MODEL_ID,
|
| 18 |
+
revision="onnx",
|
| 19 |
+
provider="CPUExecutionProvider",
|
| 20 |
+
torch_dtype=torch.float32,
|
| 21 |
+
low_cpu_mem_usage=True
|
| 22 |
)
|
| 23 |
+
pipe.safety_checker = None # Disable safety checker for simplicity
|
| 24 |
pipe.set_progress_bar_config(disable=True)
|
| 25 |
+
print("ONNX Model loaded successfully!")
|
| 26 |
except Exception as e:
|
| 27 |
+
print(f"Failed to load ONNX model: {e}")
|
| 28 |
+
print("Falling back to regular Stable Diffusion pipeline...")
|
| 29 |
+
from diffusers import StableDiffusionPipeline
|
| 30 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 31 |
+
MODEL_ID,
|
| 32 |
+
torch_dtype=torch.float32,
|
| 33 |
+
low_cpu_mem_usage=True
|
| 34 |
+
)
|
| 35 |
+
pipe = pipe.to("cpu")
|
| 36 |
+
pipe.safety_checker = None
|
| 37 |
+
pipe.set_progress_bar_config(disable=True)
|
| 38 |
+
print("Model loaded!")
|
| 39 |
|
| 40 |
def generate(prompt):
|
| 41 |
try:
|
| 42 |
result = pipe(
|
| 43 |
prompt=prompt,
|
| 44 |
+
num_inference_steps=40,
|
| 45 |
+
num_images_per_prompt=1,
|
| 46 |
+
guidance_scale=7.5
|
| 47 |
)
|
| 48 |
return result.images[0]
|
| 49 |
except Exception as e:
|
|
|
|
| 54 |
fn=generate,
|
| 55 |
inputs=gr.Textbox(lines=3, label="Prompt", placeholder="Enter your image prompt here..."),
|
| 56 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 57 |
+
title="⚡ ONNX Stable Diffusion",
|
| 58 |
+
description="Faster CPU-based image generation with ONNX optimization"
|
| 59 |
)
|
| 60 |
|
| 61 |
interface.launch()
|