Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
-
from diffusers import StableDiffusionPipeline
|
| 6 |
|
| 7 |
# 如果需要使用 Hugging Face 访问令牌,取消下面一行的注释并设置环境变量 HUGGINGFACE_TOKEN
|
| 8 |
# from huggingface_hub import login
|
|
@@ -15,35 +15,28 @@ llm = pipeline(
|
|
| 15 |
device=0 if torch.cuda.is_available() else -1
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# Step 2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# SD v1.5
|
| 20 |
sd_v15 = StableDiffusionPipeline.from_pretrained(
|
| 21 |
"runwayml/stable-diffusion-v1-5",
|
| 22 |
-
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
sd_v15.enable_attention_slicing()
|
| 26 |
-
# 如果安装了 xformers,启用更高效的注意力实现
|
| 27 |
-
try:
|
| 28 |
-
sd_v15.enable_xformers_memory_efficient_attention()
|
| 29 |
-
except Exception:
|
| 30 |
-
pass
|
| 31 |
-
# 启用CPU内存卸载,减轻GPU显存压力
|
| 32 |
-
sd_v15.enable_model_cpu_offload()
|
| 33 |
-
sd_v15 = sd_v15.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 34 |
|
| 35 |
# SD XL
|
| 36 |
sd_xl = StableDiffusionPipeline.from_pretrained(
|
| 37 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 38 |
-
|
| 39 |
)
|
| 40 |
-
sd_xl.
|
| 41 |
-
try:
|
| 42 |
-
sd_xl.enable_xformers_memory_efficient_attention()
|
| 43 |
-
except Exception:
|
| 44 |
-
pass
|
| 45 |
-
sd_xl.enable_model_cpu_offload()
|
| 46 |
-
sd_xl = sd_xl.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 47 |
|
| 48 |
# 可选:语音输入模块,使用 Whisper
|
| 49 |
asr = pipeline(
|
|
@@ -57,7 +50,7 @@ def transcribe(audio_path):
|
|
| 57 |
return text
|
| 58 |
|
| 59 |
|
| 60 |
-
def generate(description, model_choice, guidance_scale, negative_prompt, style):
|
| 61 |
# 构造给 LLM 的指令
|
| 62 |
instruction = (
|
| 63 |
f"请将以下简短描述扩展为 Stable Diffusion 友好的提示词,包含细节和风格:\n"
|
|
@@ -71,7 +64,8 @@ def generate(description, model_choice, guidance_scale, negative_prompt, style):
|
|
| 71 |
image = pipeline_model(
|
| 72 |
prompt,
|
| 73 |
guidance_scale=guidance_scale,
|
| 74 |
-
negative_prompt=negative_prompt
|
|
|
|
| 75 |
).images[0]
|
| 76 |
return prompt, image
|
| 77 |
|
|
@@ -94,6 +88,10 @@ with gr.Blocks(title="Prompt-to-Image Generator") as demo:
|
|
| 94 |
minimum=0, maximum=20, step=0.5, value=7.5,
|
| 95 |
label="Guidance Scale"
|
| 96 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
neg_text = gr.Textbox(
|
| 98 |
label="反向提示词",
|
| 99 |
placeholder="排除内容(如:低分辨率、水印)"
|
|
@@ -117,7 +115,7 @@ with gr.Blocks(title="Prompt-to-Image Generator") as demo:
|
|
| 117 |
# 点击按钮生成提示词并绘图
|
| 118 |
generate_btn.click(
|
| 119 |
fn=generate,
|
| 120 |
-
inputs=[desc_input, model_radio, guidance_slider, neg_text, style_dropdown],
|
| 121 |
outputs=[prompt_output, image_output]
|
| 122 |
)
|
| 123 |
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 6 |
|
| 7 |
# 如果需要使用 Hugging Face 访问令牌,取消下面一行的注释并设置环境变量 HUGGINGFACE_TOKEN
|
| 8 |
# from huggingface_hub import login
|
|
|
|
| 15 |
device=0 if torch.cuda.is_available() else -1
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Step 2: 加载并量化 Stable Diffusion 模型以加速推理
|
| 19 |
+
# 使用 8-bit 量化和自动设备映射
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
load_kwargs = {
|
| 22 |
+
"torch_dtype": torch.float16 if device == "cuda" else torch.float32,
|
| 23 |
+
"device_map": "auto",
|
| 24 |
+
"load_in_8bit": True # 需要安装 bitsandbytes
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
# SD v1.5
|
| 28 |
sd_v15 = StableDiffusionPipeline.from_pretrained(
|
| 29 |
"runwayml/stable-diffusion-v1-5",
|
| 30 |
+
**load_kwargs
|
| 31 |
)
|
| 32 |
+
sd_v15.scheduler = DPMSolverMultistepScheduler.from_config(sd_v15.scheduler.config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# SD XL
|
| 35 |
sd_xl = StableDiffusionPipeline.from_pretrained(
|
| 36 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 37 |
+
**load_kwargs
|
| 38 |
)
|
| 39 |
+
sd_xl.scheduler = DPMSolverMultistepScheduler.from_config(sd_xl.scheduler.config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# 可选:语音输入模块,使用 Whisper
|
| 42 |
asr = pipeline(
|
|
|
|
| 50 |
return text
|
| 51 |
|
| 52 |
|
| 53 |
+
def generate(description, model_choice, guidance_scale, negative_prompt, style, steps):
|
| 54 |
# 构造给 LLM 的指令
|
| 55 |
instruction = (
|
| 56 |
f"请将以下简短描述扩展为 Stable Diffusion 友好的提示词,包含细节和风格:\n"
|
|
|
|
| 64 |
image = pipeline_model(
|
| 65 |
prompt,
|
| 66 |
guidance_scale=guidance_scale,
|
| 67 |
+
negative_prompt=negative_prompt,
|
| 68 |
+
num_inference_steps=steps
|
| 69 |
).images[0]
|
| 70 |
return prompt, image
|
| 71 |
|
|
|
|
| 88 |
minimum=0, maximum=20, step=0.5, value=7.5,
|
| 89 |
label="Guidance Scale"
|
| 90 |
)
|
| 91 |
+
steps_slider = gr.Slider(
|
| 92 |
+
minimum=1, maximum=50, step=1, value=20,
|
| 93 |
+
label="推理步数 (步数减少可加速)"
|
| 94 |
+
)
|
| 95 |
neg_text = gr.Textbox(
|
| 96 |
label="反向提示词",
|
| 97 |
placeholder="排除内容(如:低分辨率、水印)"
|
|
|
|
| 115 |
# 点击按钮生成提示词并绘图
|
| 116 |
generate_btn.click(
|
| 117 |
fn=generate,
|
| 118 |
+
inputs=[desc_input, model_radio, guidance_slider, neg_text, style_dropdown, steps_slider],
|
| 119 |
outputs=[prompt_output, image_output]
|
| 120 |
)
|
| 121 |
|