File size: 5,664 Bytes
985dde8
 
 
 
 
 
 
 
 
 
204ab4d
 
 
 
985dde8
 
 
 
 
 
204ab4d
985dde8
 
 
3ae4393
985dde8
 
 
204ab4d
985dde8
 
3ae4393
 
204ab4d
 
985dde8
 
 
 
 
 
 
 
 
204ab4d
985dde8
 
 
 
 
204ab4d
985dde8
 
204ab4d
 
985dde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a43f9b
985dde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204ab4d
 
985dde8
 
204ab4d
 
985dde8
 
 
 
 
 
 
 
204ab4d
985dde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204ab4d
 
3ae4393
 
204ab4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gradio as gr
import spaces
import torch
from diffusers import NewbiePipeline
from transformers import AutoModel
import random
import warnings

warnings.filterwarnings("ignore")

# 优化:使用 float16 以获得更好的 GPU 性能和内存效率
torch_dtype = torch.float16

model_path = "Disty0/NewBie-image-Exp0.1-Diffusers"

print("正在加载 Text Encoder...")
text_encoder_2 = AutoModel.from_pretrained(
    model_path, 
    subfolder="text_encoder_2", 
    trust_remote_code=True, 
    torch_dtype=torch_dtype
)

print("正在加载 Pipeline...")
# 优化:使用 EulerDiscreteScheduler 替代 DPMSolver,因为 DPMSolver 与 NewBiePipeline 不兼容
pipe_newbie = NewbiePipeline.from_pretrained(
    model_path, 
    text_encoder_2=text_encoder_2, 
    torch_dtype=torch_dtype
)

# 优化:使用 EulerDiscreteScheduler,它与 NewBiePipeline 兼容
pipe_newbie.scheduler = pipe_newbie.scheduler

pipe_newbie.to("cuda")
print("模型加载完成。")

@spaces.GPU()
def generate_image_newbie(prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
    if seed < 0:
        seed = random.randint(0, 2**32 - 1)

    generator = torch.Generator("cuda").manual_seed(int(seed))

    # 优化:减少推理步数以加快生成速度,同时保持质量
    image = pipe_newbie(
        prompt=prompt,
        negative_prompt=negative_prompt,
        height=int(height),
        width=int(width),
        num_inference_steps=min(num_inference_steps, 25),  # 限制最大步数以加快速度
        guidance_scale=guidance_scale,
        generator=generator,
        # 优化:启用内存高效模式
        output_type="pil"
    ).images[0]

    return image, seed

# 默认提示词
newbie_prompt = """<character_1>
<n>$character_1$</n>
<gender>1girl, solo</gender>
<appearance>blonde_hair, long_hair</appearance>
<clothing>large_hat, white_hat, white_blouse, puffy_sleeves, shoulder_cutout, black_skirt, shirt_tucked_in, socks, shoes</clothing>
<expression>looking_up</expression>
<action>sitting, reclining, arm_support, from_side, cowboy_shot, wide_shot</action>
<position>center</position>
</character_1>

<general_tags>
<count>1girl</count>
<artists> (kazutake hazano:1.2), (kazutake hazano:0.5), (onineko:0.8), (r17329 illu:0.2), (ma1ma1helmes b illu:0.2)</artists>
<style>masterpiece, best_quality, high_resolution, detailed</style>
<background>detailed_background, scenery, detailed_background</background>
<atmosphere>cheerful</atmosphere>
<lighting>dynamic_angle, depth_of_field, high_contrast, colorful, detailed_light, light_leaks, beautiful_detailed_glow, best_shadow, shiny_skin, cinematic_lighting, ray_tracing, from_above, female_focus, close-up, dutch_angle, blue_archive</lighting>
<quality>very_aesthetic, masterpiece, no_text</quality>
<objects>bag</objects>
<other>2024_year</other>
</general_tags>"""

with gr.Blocks(title="NewBie 图像生成器") as demo:
    gr.Markdown("# NewBie 图像生成器 (NewBie Image Generator)")
    gr.Markdown("使用 NewBie 模型生成高质量动漫风格图像。")
    
    with gr.Row(variant="panel"):
        with gr.Column(scale=2):
            prompt_newbie = gr.Textbox(
                label="提示词 (Prompt)",
                value=newbie_prompt,
                lines=10,
                placeholder="在此输入生成提示词..."
            )
            negative_prompt_newbie = gr.Textbox(
                label="负面提示词 (Negative Prompt)",
                value="low quality, bad quality, blurry, low resolution, deformed, ugly, bad anatomy",
                lines=3,
                placeholder="在此输入不需要出现的元素..."
            )
            
            with gr.Row():
                height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=1024, step=64, value=768)  # 优化:降低默认分辨率以加快速度
                width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=1024, step=64, value=768)   # 优化:降低默认分辨率以加快速度
            
            with gr.Row():
                steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=5, maximum=50, step=1, value=20)  # 优化:减少默认步数
                guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=5.0)  # 优化:调整默认值
            
            seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
            
            generate_btn_newbie = gr.Button("生成图片 (Generate)", variant="primary")

        with gr.Column(scale=1):
            image_output_newbie = gr.Image(label="生成结果 (Output)", format="png", interactive=False)
            used_seed_newbie = gr.Number(label="使用的种子 (Used Seed)", interactive=False)
            progress_bar = gr.Textbox(label="生成进度", interactive=False, value="准备就绪...")

    generate_btn_newbie.click(
        fn=generate_image_newbie,
        inputs=[
            prompt_newbie, 
            negative_prompt_newbie, 
            height_newbie, 
            width_newbie, 
            steps_newbie, 
            guidance_scale_newbie, 
            seed_newbie
        ],
        outputs=[image_output_newbie, used_seed_newbie]
    )

if __name__ == "__main__":
    demo.launch(
        theme=gr.themes.Soft(primary_hue="blue"),
        footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}],
        share=True  # 添加 share=True 以创建公共链接
    )