Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 8 |
+
|
| 9 |
+
warnings.filterwarnings("ignore")
|
| 10 |
+
|
| 11 |
+
model_path = "Disty0/NewBie-image-Exp0.1-Diffusers" # NewBie-AI/NewBie-image-Exp0.1
|
| 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=torch.bfloat16
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
print("正在加载 Pipeline...")
|
| 22 |
+
pipe_newbie = NewbiePipeline.from_pretrained(
|
| 23 |
+
model_path,
|
| 24 |
+
text_encoder_2=text_encoder_2,
|
| 25 |
+
torch_dtype=torch.bfloat16
|
| 26 |
+
)
|
| 27 |
+
pipe_newbie.to("cuda")
|
| 28 |
+
|
| 29 |
+
del text_encoder_2
|
| 30 |
+
torch.cuda.empty_cache()
|
| 31 |
+
print("模型加载完成。")
|
| 32 |
+
|
| 33 |
+
@spaces.GPU()
|
| 34 |
+
def generate_image_newbie(prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):
|
| 35 |
+
if seed < 0:
|
| 36 |
+
seed = random.randint(0, 2**32 - 1)
|
| 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=int(num_inference_steps),
|
| 46 |
+
guidance_scale=guidance_scale,
|
| 47 |
+
generator=generator,
|
| 48 |
+
).images[0]
|
| 49 |
+
|
| 50 |
+
return image, seed
|
| 51 |
+
|
| 52 |
+
# 默认提示词
|
| 53 |
+
newbie_prompt = """<character_1>
|
| 54 |
+
<n>$character_1$</n>
|
| 55 |
+
<gender>1girl, solo</gender>
|
| 56 |
+
<appearance>blonde_hair, long_hair</appearance>
|
| 57 |
+
<clothing>large_hat, white_hat, white_blouse, puffy_sleeves, shoulder_cutout, black_skirt, shirt_tucked_in, socks, shoes</clothing>
|
| 58 |
+
<expression>looking_up</expression>
|
| 59 |
+
<action>sitting, reclining, arm_support, from_side, cowboy_shot, wide_shot</action>
|
| 60 |
+
<position>center</position>
|
| 61 |
+
</character_1>
|
| 62 |
+
|
| 63 |
+
<general_tags>
|
| 64 |
+
<count>1girl</count>
|
| 65 |
+
<artists> (kazutake hazano:1.2), (kazutake hazano:0.5), (onineko:0.8), (r17329 illu:0.2), (ma1ma1helmes b illu:0.2)</artists>
|
| 66 |
+
<style>masterpiece, best_quality, high_resolution, detailed</style>
|
| 67 |
+
<background>detailed_background, scenery, detailed_background</background>
|
| 68 |
+
<atmosphere>cheerful</atmosphere>
|
| 69 |
+
<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>
|
| 70 |
+
<quality>very_aesthetic, masterpiece, no_text</quality>
|
| 71 |
+
<objects>bag</objects>
|
| 72 |
+
<other>2024_year</other>
|
| 73 |
+
</general_tags>"""
|
| 74 |
+
|
| 75 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="NewBie 图像生成器") as demo:
|
| 76 |
+
gr.Markdown("# NewBie 图像生成器 (NewBie Image Generator)")
|
| 77 |
+
gr.Markdown("使用 NewBie 模型生成高质量动漫风格图像。")
|
| 78 |
+
|
| 79 |
+
with gr.Row(variant="panel"):
|
| 80 |
+
with gr.Column(scale=2):
|
| 81 |
+
prompt_newbie = gr.Textbox(
|
| 82 |
+
label="提示词 (Prompt)",
|
| 83 |
+
value=newbie_prompt,
|
| 84 |
+
lines=10,
|
| 85 |
+
placeholder="在此输入生成提示词..."
|
| 86 |
+
)
|
| 87 |
+
negative_prompt_newbie = gr.Textbox(
|
| 88 |
+
label="负面提示词 (Negative Prompt)",
|
| 89 |
+
value="low quality, bad quality, blurry, low resolution, deformed, ugly, bad anatomy",
|
| 90 |
+
lines=3,
|
| 91 |
+
placeholder="在此输入不需要出现的元素..."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
height_newbie = gr.Slider(label="图片高度 (Height)", minimum=512, maximum=2048, step=64, value=1024)
|
| 96 |
+
width_newbie = gr.Slider(label="图片宽度 (Width)", minimum=512, maximum=2048, step=64, value=1024)
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
steps_newbie = gr.Slider(label="推理步数 (Inference Steps)", minimum=1, maximum=100, step=1, value=28)
|
| 100 |
+
guidance_scale_newbie = gr.Slider(label="引导系数 (Guidance Scale)", minimum=1.0, maximum=20.0, step=0.1, value=3.5)
|
| 101 |
+
|
| 102 |
+
seed_newbie = gr.Number(label="随机种子 (Seed, -1 为随机)", value=-1, precision=0)
|
| 103 |
+
|
| 104 |
+
generate_btn_newbie = gr.Button("生成图片 (Generate)", variant="primary")
|
| 105 |
+
|
| 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,
|
| 112 |
+
inputs=[
|
| 113 |
+
prompt_newbie,
|
| 114 |
+
negative_prompt_newbie,
|
| 115 |
+
height_newbie,
|
| 116 |
+
width_newbie,
|
| 117 |
+
steps_newbie,
|
| 118 |
+
guidance_scale_newbie,
|
| 119 |
+
seed_newbie
|
| 120 |
+
],
|
| 121 |
+
outputs=[image_output_newbie, used_seed_newbie]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|