Spaces:
Build error
Build error
Refactor: Update app.py to use diffusers_xl_canny_full and lllyasviel/sd-controlnet-canny models for multi-step generation
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
# import gradio as gr
|
| 3 |
# import torch
|
| 4 |
# from PIL import Image
|
|
@@ -91,28 +90,34 @@ import torch
|
|
| 91 |
from diffusers import StableDiffusionPipeline
|
| 92 |
import logging
|
| 93 |
from PIL import Image
|
| 94 |
-
import
|
| 95 |
-
import os
|
| 96 |
|
| 97 |
-
#
|
| 98 |
logging.basicConfig(level=logging.DEBUG)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
# Load model
|
| 104 |
-
model_id = "CompVis/stable-diffusion-v1-4" # Smaller model
|
| 105 |
device = "cpu"
|
| 106 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id,
|
| 107 |
pipe = pipe.to(device)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
def generate_image(prompt):
|
| 110 |
try:
|
| 111 |
logging.debug("Starting image generation process...")
|
| 112 |
|
| 113 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with torch.no_grad():
|
| 115 |
-
generated_images = pipe(prompt=prompt, height=
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if not generated_images or not isinstance(generated_images[0], Image.Image):
|
| 118 |
error_message = "Error: Generated image is not valid."
|
|
@@ -121,24 +126,23 @@ def generate_image(prompt):
|
|
| 121 |
|
| 122 |
final_image = generated_images[0]
|
| 123 |
logging.debug("Image generation process completed successfully.")
|
| 124 |
-
|
| 125 |
-
# Clean up memory
|
| 126 |
-
torch.cuda.empty_cache()
|
| 127 |
-
gc.collect()
|
| 128 |
-
|
| 129 |
return final_image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
error_message = f"Error: {str(e)}"
|
| 132 |
logging.error(error_message)
|
| 133 |
return error_message
|
| 134 |
|
| 135 |
-
#
|
| 136 |
interface = gr.Interface(
|
| 137 |
fn=generate_image,
|
| 138 |
inputs=gr.components.Textbox(label="Prompt"),
|
| 139 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
| 140 |
-
title="
|
| 141 |
-
description="Generate
|
| 142 |
)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
# import gradio as gr
|
| 2 |
# import torch
|
| 3 |
# from PIL import Image
|
|
|
|
| 90 |
from diffusers import StableDiffusionPipeline
|
| 91 |
import logging
|
| 92 |
from PIL import Image
|
| 93 |
+
import signal
|
|
|
|
| 94 |
|
| 95 |
+
# 配置日志记录
|
| 96 |
logging.basicConfig(level=logging.DEBUG)
|
| 97 |
|
| 98 |
+
# 加载模型
|
| 99 |
+
model_id = "runwayml/stable-diffusion-v1-5" # 使用较小的模型
|
|
|
|
|
|
|
|
|
|
| 100 |
device = "cpu"
|
| 101 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
|
| 102 |
pipe = pipe.to(device)
|
| 103 |
|
| 104 |
+
# 设置超时
|
| 105 |
+
def timeout_handler(signum, frame):
|
| 106 |
+
raise TimeoutError("Image generation timed out")
|
| 107 |
+
|
| 108 |
def generate_image(prompt):
|
| 109 |
try:
|
| 110 |
logging.debug("Starting image generation process...")
|
| 111 |
|
| 112 |
+
# 设置60秒超时
|
| 113 |
+
signal.signal(signal.SIGALRM, timeout_handler)
|
| 114 |
+
signal.alarm(900)
|
| 115 |
+
|
| 116 |
+
# 使用Stable Diffusion模型生成图像,减少图像尺寸
|
| 117 |
with torch.no_grad():
|
| 118 |
+
generated_images = pipe(prompt=prompt, height=256, width=256, num_inference_steps=20).images
|
| 119 |
+
|
| 120 |
+
signal.alarm(0) # 取消超时
|
| 121 |
|
| 122 |
if not generated_images or not isinstance(generated_images[0], Image.Image):
|
| 123 |
error_message = "Error: Generated image is not valid."
|
|
|
|
| 126 |
|
| 127 |
final_image = generated_images[0]
|
| 128 |
logging.debug("Image generation process completed successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return final_image
|
| 130 |
+
except TimeoutError as te:
|
| 131 |
+
error_message = f"Error: Image generation timed out. {str(te)}"
|
| 132 |
+
logging.error(error_message)
|
| 133 |
+
return error_message
|
| 134 |
except Exception as e:
|
| 135 |
error_message = f"Error: {str(e)}"
|
| 136 |
logging.error(error_message)
|
| 137 |
return error_message
|
| 138 |
|
| 139 |
+
# 定义Gradio接口
|
| 140 |
interface = gr.Interface(
|
| 141 |
fn=generate_image,
|
| 142 |
inputs=gr.components.Textbox(label="Prompt"),
|
| 143 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
| 144 |
+
title="Simple Stable Diffusion (CPU Version)",
|
| 145 |
+
description="Generate images using a simple prompt with Stable Diffusion on CPU."
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|