Spaces:
Sleeping
Sleeping
CantonMonkey commited on
Commit ·
8523f93
1
Parent(s): 66c30d7
diffusion
Browse files
app.py
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipeline
|
| 3 |
from PIL import Image
|
|
@@ -24,11 +74,6 @@ pipe.enable_attention_slicing()
|
|
| 24 |
|
| 25 |
# 文生图生成函数
|
| 26 |
def generate_image(prompt, num_steps=20, height=256, width=256):
|
| 27 |
-
"""
|
| 28 |
-
prompt: str, 文本描述
|
| 29 |
-
num_steps: int, 推理步数(CPU 可少一些)
|
| 30 |
-
height, width: int, 输出图像分辨率
|
| 31 |
-
"""
|
| 32 |
image = pipe(prompt, num_inference_steps=num_steps, height=height, width=width).images[0]
|
| 33 |
return image
|
| 34 |
|
|
@@ -43,7 +88,7 @@ interface = gr.Interface(
|
|
| 43 |
],
|
| 44 |
outputs=gr.Image(type="pil"),
|
| 45 |
title="Text2Image Demo (v1-4 + ControlNet)",
|
| 46 |
-
description="Generate images from text using Stable Diffusion v1-4 + ControlNet
|
| 47 |
)
|
| 48 |
|
| 49 |
interface.launch()
|
|
|
|
| 1 |
+
# import torch
|
| 2 |
+
# from diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipeline
|
| 3 |
+
# from PIL import Image
|
| 4 |
+
# import gradio as gr
|
| 5 |
+
|
| 6 |
+
# # 自动选择设备
|
| 7 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
# print(f"Using device: {device}")
|
| 9 |
+
|
| 10 |
+
# # 加载 ControlNet 模型
|
| 11 |
+
# controlnet = ControlNetModel.from_pretrained(
|
| 12 |
+
# "lllyasviel/sd-controlnet-canny", torch_dtype=torch.float32
|
| 13 |
+
# )
|
| 14 |
+
|
| 15 |
+
# # 加载 Stable Diffusion + ControlNet
|
| 16 |
+
# pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 17 |
+
# "CompVis/stable-diffusion-v1-4",
|
| 18 |
+
# controlnet=controlnet,
|
| 19 |
+
# torch_dtype=torch.float32
|
| 20 |
+
# ).to(device)
|
| 21 |
+
|
| 22 |
+
# # CPU 下节省显存
|
| 23 |
+
# pipe.enable_attention_slicing()
|
| 24 |
+
|
| 25 |
+
# # 文生图生成函数
|
| 26 |
+
# def generate_image(prompt, num_steps=20, height=256, width=256):
|
| 27 |
+
# """
|
| 28 |
+
# prompt: str, 文本描述
|
| 29 |
+
# num_steps: int, 推理步数(CPU 可少一些)
|
| 30 |
+
# height, width: int, 输出图像分辨率
|
| 31 |
+
# """
|
| 32 |
+
# image = pipe(prompt, num_inference_steps=num_steps, height=height, width=width).images[0]
|
| 33 |
+
# return image
|
| 34 |
+
|
| 35 |
+
# # Gradio 界面
|
| 36 |
+
# interface = gr.Interface(
|
| 37 |
+
# fn=generate_image,
|
| 38 |
+
# inputs=[
|
| 39 |
+
# gr.Textbox(label="Prompt", placeholder="Enter text prompt here..."),
|
| 40 |
+
# gr.Slider(5, 50, value=20, step=1, label="Inference Steps"),
|
| 41 |
+
# gr.Slider(128, 512, value=256, step=64, label="Height"),
|
| 42 |
+
# gr.Slider(128, 512, value=256, step=64, label="Width"),
|
| 43 |
+
# ],
|
| 44 |
+
# outputs=gr.Image(type="pil"),
|
| 45 |
+
# title="Text2Image Demo (v1-4 + ControlNet)",
|
| 46 |
+
# description="Generate images from text using Stable Diffusion v1-4 + ControlNet (CPU/GPU compatible)"
|
| 47 |
+
# )
|
| 48 |
+
|
| 49 |
+
# interface.launch()
|
| 50 |
+
|
| 51 |
import torch
|
| 52 |
from diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipeline
|
| 53 |
from PIL import Image
|
|
|
|
| 74 |
|
| 75 |
# 文生图生成函数
|
| 76 |
def generate_image(prompt, num_steps=20, height=256, width=256):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
image = pipe(prompt, num_inference_steps=num_steps, height=height, width=width).images[0]
|
| 78 |
return image
|
| 79 |
|
|
|
|
| 88 |
],
|
| 89 |
outputs=gr.Image(type="pil"),
|
| 90 |
title="Text2Image Demo (v1-4 + ControlNet)",
|
| 91 |
+
description="Generate images from text using Stable Diffusion v1-4 + ControlNet"
|
| 92 |
)
|
| 93 |
|
| 94 |
interface.launch()
|