Spaces:
Sleeping
Sleeping
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -5,29 +5,35 @@ from diffusers import NewbiePipeline
|
|
| 5 |
from transformers import AutoModel
|
| 6 |
import random
|
| 7 |
import warnings
|
|
|
|
| 8 |
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
print("正在加载 Text Encoder...")
|
| 14 |
text_encoder_2 = AutoModel.from_pretrained(
|
| 15 |
model_path,
|
| 16 |
subfolder="text_encoder_2",
|
| 17 |
trust_remote_code=True,
|
| 18 |
-
torch_dtype=
|
| 19 |
)
|
| 20 |
|
| 21 |
print("正在加载 Pipeline...")
|
|
|
|
| 22 |
pipe_newbie = NewbiePipeline.from_pretrained(
|
| 23 |
model_path,
|
| 24 |
text_encoder_2=text_encoder_2,
|
| 25 |
-
torch_dtype=
|
| 26 |
)
|
| 27 |
-
pipe_newbie.to("cuda")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
print("模型加载完成。")
|
| 32 |
|
| 33 |
@spaces.GPU()
|
|
@@ -37,14 +43,18 @@ def generate_image_newbie(prompt, negative_prompt, height, width, num_inference_
|
|
| 37 |
|
| 38 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 39 |
|
|
|
|
|
|
|
| 40 |
image = pipe_newbie(
|
| 41 |
prompt=prompt,
|
| 42 |
negative_prompt=negative_prompt,
|
| 43 |
height=int(height),
|
| 44 |
width=int(width),
|
| 45 |
-
num_inference_steps=
|
| 46 |
guidance_scale=guidance_scale,
|
| 47 |
generator=generator,
|
|
|
|
|
|
|
| 48 |
).images[0]
|
| 49 |
|
| 50 |
return image, seed
|
|
@@ -92,12 +102,12 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
|
|
| 92 |
)
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
-
height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=
|
| 96 |
-
width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
-
steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=
|
| 100 |
-
guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=
|
| 101 |
|
| 102 |
seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
|
| 103 |
|
|
@@ -106,6 +116,7 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
|
|
| 106 |
with gr.Column(scale=1):
|
| 107 |
image_output_newbie = gr.Image(label="生成结果 (Output)", format="png", interactive=False)
|
| 108 |
used_seed_newbie = gr.Number(label="使用的种子 (Used Seed)", interactive=False)
|
|
|
|
| 109 |
|
| 110 |
generate_btn_newbie.click(
|
| 111 |
fn=generate_image_newbie,
|
|
@@ -122,4 +133,7 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
|
|
| 122 |
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import AutoModel
|
| 6 |
import random
|
| 7 |
import warnings
|
| 8 |
+
from diffusers import DPMSolverMultistepScheduler
|
| 9 |
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
+
# 优化:使用 float16 以获得更好的 GPU 性能和内存效率
|
| 13 |
+
torch_dtype = torch.float16
|
| 14 |
+
|
| 15 |
+
model_path = "Disty0/NewBie-image-Exp0.1-Diffusers"
|
| 16 |
|
| 17 |
print("正在加载 Text Encoder...")
|
| 18 |
text_encoder_2 = AutoModel.from_pretrained(
|
| 19 |
model_path,
|
| 20 |
subfolder="text_encoder_2",
|
| 21 |
trust_remote_code=True,
|
| 22 |
+
torch_dtype=torch_dtype
|
| 23 |
)
|
| 24 |
|
| 25 |
print("正在加载 Pipeline...")
|
| 26 |
+
# 优化:使用更快的采样器 DPMSolverMultistepScheduler
|
| 27 |
pipe_newbie = NewbiePipeline.from_pretrained(
|
| 28 |
model_path,
|
| 29 |
text_encoder_2=text_encoder_2,
|
| 30 |
+
torch_dtype=torch_dtype
|
| 31 |
)
|
|
|
|
| 32 |
|
| 33 |
+
# 优化:使用更快的采样器
|
| 34 |
+
pipe_newbie.scheduler = DPMSolverMultistepScheduler.from_config(pipe_newbie.scheduler.config)
|
| 35 |
+
|
| 36 |
+
pipe_newbie.to("cuda")
|
| 37 |
print("模型加载完成。")
|
| 38 |
|
| 39 |
@spaces.GPU()
|
|
|
|
| 43 |
|
| 44 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 45 |
|
| 46 |
+
# 优化:减少推理步数以加快生成速度,同时保持质量
|
| 47 |
+
# 使用 DPMSolver 可以在较少的步数下获得更好的结果
|
| 48 |
image = pipe_newbie(
|
| 49 |
prompt=prompt,
|
| 50 |
negative_prompt=negative_prompt,
|
| 51 |
height=int(height),
|
| 52 |
width=int(width),
|
| 53 |
+
num_inference_steps=min(num_inference_steps, 25), # 限制最大步数以加快速度
|
| 54 |
guidance_scale=guidance_scale,
|
| 55 |
generator=generator,
|
| 56 |
+
# 优化:启用内存高效模式
|
| 57 |
+
output_type="pil"
|
| 58 |
).images[0]
|
| 59 |
|
| 60 |
return image, seed
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
+
height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=1024, step=64, value=768) # 优化:降低默认分辨率以加快速度
|
| 106 |
+
width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=1024, step=64, value=768) # 优化:降低默认分辨率以加快速度
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
+
steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=5, maximum=50, step=1, value=20) # 优化:减少默认步数
|
| 110 |
+
guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=5.0) # 优化:调整默认值
|
| 111 |
|
| 112 |
seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
|
| 113 |
|
|
|
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
image_output_newbie = gr.Image(label="生成结果 (Output)", format="png", interactive=False)
|
| 118 |
used_seed_newbie = gr.Number(label="使用的种子 (Used Seed)", interactive=False)
|
| 119 |
+
progress_bar = gr.Textbox(label="生成进度", interactive=False, value="准备就绪...")
|
| 120 |
|
| 121 |
generate_btn_newbie.click(
|
| 122 |
fn=generate_image_newbie,
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
+
demo.launch(
|
| 137 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 138 |
+
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
|
| 139 |
+
)
|