XnOwO commited on
Commit
895a6cf
·
verified ·
1 Parent(s): 3ae4393

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +36 -48
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from diffusers import NewbiePipeline
5
  from transformers import AutoModel
6
  import random
7
  import warnings
@@ -22,15 +22,15 @@ text_encoder_2 = AutoModel.from_pretrained(
22
  )
23
 
24
  print("正在加载 Pipeline...")
25
- # 优化:使用 EulerDiscreteScheduler 替代 DPMSolver,因为 DPMSolver 与 NewBiePipeline 不兼容
26
  pipe_newbie = NewbiePipeline.from_pretrained(
27
  model_path,
28
  text_encoder_2=text_encoder_2,
29
  torch_dtype=torch_dtype
30
  )
31
 
32
- # 优化:使用 EulerDiscreteScheduler,它与 NewBiePipeline 兼容
33
- pipe_newbie.scheduler = pipe_newbie.scheduler
34
 
35
  pipe_newbie.to("cuda")
36
  print("模型加载完成。")
@@ -42,43 +42,27 @@ def generate_image_newbie(prompt, negative_prompt, height, width, num_inference_
42
 
43
  generator = torch.Generator("cuda").manual_seed(int(seed))
44
 
45
- # 优化:减少推理步数以加快生成速度,同时保持质量
46
- image = pipe_newbie(
47
- prompt=prompt,
48
- negative_prompt=negative_prompt,
49
- height=int(height),
50
- width=int(width),
51
- num_inference_steps=min(num_inference_steps, 25), # 限制最大步数以加快速度
52
- guidance_scale=guidance_scale,
53
- generator=generator,
54
- # 优化:启用内存高效模式
55
- output_type="pil"
56
- ).images[0]
57
-
58
- return image, seed
59
-
60
- # 默认提示词
61
- newbie_prompt = """<character_1>
62
- <n>$character_1$</n>
63
- <gender>1girl, solo</gender>
64
- <appearance>blonde_hair, long_hair</appearance>
65
- <clothing>large_hat, white_hat, white_blouse, puffy_sleeves, shoulder_cutout, black_skirt, shirt_tucked_in, socks, shoes</clothing>
66
- <expression>looking_up</expression>
67
- <action>sitting, reclining, arm_support, from_side, cowboy_shot, wide_shot</action>
68
- <position>center</position>
69
- </character_1>
70
-
71
- <general_tags>
72
- <count>1girl</count>
73
- <artists> (kazutake hazano:1.2), (kazutake hazano:0.5), (onineko:0.8), (r17329 illu:0.2), (ma1ma1helmes b illu:0.2)</artists>
74
- <style>masterpiece, best_quality, high_resolution, detailed</style>
75
- <background>detailed_background, scenery, detailed_background</background>
76
- <atmosphere>cheerful</atmosphere>
77
- <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>
78
- <quality>very_aesthetic, masterpiece, no_text</quality>
79
- <objects>bag</objects>
80
- <other>2024_year</other>
81
- </general_tags>"""
82
 
83
  with gr.Blocks(title="NewBie 图像生成器") as demo:
84
  gr.Markdown("# NewBie 图像生成器 (NewBie Image Generator)")
@@ -94,18 +78,18 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
94
  )
95
  negative_prompt_newbie = gr.Textbox(
96
  label="负面提示词 (Negative Prompt)",
97
- value="low quality, bad quality, blurry, low resolution, deformed, ugly, bad anatomy",
98
  lines=3,
99
  placeholder="在此输入不需要出现的元素..."
100
  )
101
 
102
  with gr.Row():
103
- height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=1024, step=64, value=768) # 优化:降低默认分辨率以加快速度
104
- width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=1024, step=64, value=768) # 优化:降低默认分辨率以加快速度
105
 
106
  with gr.Row():
107
- steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=5, maximum=50, step=1, value=20) # 优化:减少默认步数
108
- guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=5.0) # 优化:调整默认值
109
 
110
  seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
111
 
@@ -114,7 +98,7 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
114
  with gr.Column(scale=1):
115
  image_output_newbie = gr.Image(label="生成结果 (Output)", format="png", interactive=False)
116
  used_seed_newbie = gr.Number(label="使用的种子 (Used Seed)", interactive=False)
117
- progress_bar = gr.Textbox(label="生成进度", interactive=False, value="准备就绪...")
118
 
119
  generate_btn_newbie.click(
120
  fn=generate_image_newbie,
@@ -128,11 +112,15 @@ with gr.Blocks(title="NewBie 图像生成器") as demo:
128
  seed_newbie
129
  ],
130
  outputs=[image_output_newbie, used_seed_newbie]
 
 
 
 
131
  )
132
 
133
  if __name__ == "__main__":
134
  demo.launch(
135
  theme=gr.themes.Soft(primary_hue="blue"),
136
  footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}],
137
- share=True # 添加 share=True 以创建公共链接
138
  )
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
+ from diffusers import NewbiePipeline, EulerDiscreteScheduler
5
  from transformers import AutoModel
6
  import random
7
  import warnings
 
22
  )
23
 
24
  print("正在加载 Pipeline...")
25
+ # 优化:明确设置 EulerDiscreteScheduler 以确保兼容性
26
  pipe_newbie = NewbiePipeline.from_pretrained(
27
  model_path,
28
  text_encoder_2=text_encoder_2,
29
  torch_dtype=torch_dtype
30
  )
31
 
32
+ # 确保使用兼容的调度器
33
+ pipe_newbie.scheduler = EulerDiscreteScheduler.from_config(pipe_newbie.scheduler.config)
34
 
35
  pipe_newbie.to("cuda")
36
  print("模型加载完成。")
 
42
 
43
  generator = torch.Generator("cuda").manual_seed(int(seed))
44
 
45
+ try:
46
+ # 优化:调整参数以避免黑色图像
47
+ image = pipe_newbie(
48
+ prompt=prompt,
49
+ negative_prompt=negative_prompt,
50
+ height=int(height),
51
+ width=int(width),
52
+ num_inference_steps=num_inference_steps, # 使用用户指定的步数
53
+ guidance_scale=guidance_scale,
54
+ generator=generator,
55
+ output_type="pil"
56
+ ).images[0]
57
+
58
+ return image, seed
59
+ except Exception as e:
60
+ print(f"生成图像时出错: {e}")
61
+ # 返回一个错误图像或占位符
62
+ return None, seed
63
+
64
+ # 默认提示词 - 优化提示词以生成更好的图像
65
+ newbie_prompt = """masterpiece, best quality, high resolution, detailed, 1girl, solo, blonde hair, long hair, large hat, white hat, white blouse, puffy sleeves, shoulder cutout, black skirt, shirt tucked in, socks, shoes, sitting, reclining, arm support, from side, cowboy shot, wide shot, center, cheerful, 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, detailed background, scenery, very aesthetic, masterpiece, no text, bag, 2024 year"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  with gr.Blocks(title="NewBie 图像生成器") as demo:
68
  gr.Markdown("# NewBie 图像生成器 (NewBie Image Generator)")
 
78
  )
79
  negative_prompt_newbie = gr.Textbox(
80
  label="负面提示词 (Negative Prompt)",
81
+ value="low quality, bad quality, blurry, low resolution, deformed, ugly, bad anatomy, black image, dark, black background",
82
  lines=3,
83
  placeholder="在此输入不需要出现的元素..."
84
  )
85
 
86
  with gr.Row():
87
+ height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=1024, step=64, value=768)
88
+ width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=1024, step=64, value=768)
89
 
90
  with gr.Row():
91
+ steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=10, maximum=50, step=1, value=25) # 增加默认步数
92
+ guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=20.0, step=0.5, value=7.5) # 增加默认值
93
 
94
  seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
95
 
 
98
  with gr.Column(scale=1):
99
  image_output_newbie = gr.Image(label="生成结果 (Output)", format="png", interactive=False)
100
  used_seed_newbie = gr.Number(label="使用的种子 (Used Seed)", interactive=False)
101
+ error_message = gr.Textbox(label="错误信息", interactive=False, visible=False)
102
 
103
  generate_btn_newbie.click(
104
  fn=generate_image_newbie,
 
112
  seed_newbie
113
  ],
114
  outputs=[image_output_newbie, used_seed_newbie]
115
+ ).then(
116
+ fn=lambda img, seed: (img, seed, gr.update(visible=True) if img is None else gr.update(visible=False)),
117
+ inputs=[image_output_newbie, used_seed_newbie],
118
+ outputs=[image_output_newbie, used_seed_newbie, error_message]
119
  )
120
 
121
  if __name__ == "__main__":
122
  demo.launch(
123
  theme=gr.themes.Soft(primary_hue="blue"),
124
  footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}],
125
+ share=True
126
  )