Spaces:
Running
on
Zero
Running
on
Zero
Update app.py from anycoder
Browse files
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 |
-
#
|
| 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 = 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 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=
|
| 108 |
-
guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=
|
| 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 |
-
|
| 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
|
| 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 |
)
|