c83908758 commited on
Commit
0cab54f
·
verified ·
1 Parent(s): ba1b138

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -50
app.py CHANGED
@@ -4,49 +4,49 @@ from typing import Optional
4
 
5
  import gradio as gr
6
  import torch
7
- from diffusers import AutoPipelineForText2Image
8
 
9
  MODEL_ID = os.getenv("MODEL_ID", "stablediffusionapi/counterfeit-v30")
10
  DEFAULT_NEGATIVE = os.getenv(
11
  "DEFAULT_NEGATIVE",
12
- "blurry, lowres, worst quality, low quality, bad anatomy, watermark, text, extra fingers",
13
  )
14
 
15
  PRESETS = {
16
- "🌻 向日葵少女": {
17
- "prompt": "1girl, anime style, sunflower field, warm sunlight, soft lighting, detailed face, masterpiece, best quality",
18
  "negative": DEFAULT_NEGATIVE,
19
- "steps": 18,
20
  "cfg": 7.0,
21
- "width": 512,
22
- "height": 768,
23
  "seed": -1,
24
  },
25
- "🏙 赛博少女": {
26
- "prompt": "1girl, anime style, cyberpunk city, neon lights, night street, glowing signs, dynamic pose, masterpiece",
27
  "negative": DEFAULT_NEGATIVE,
28
- "steps": 18,
29
  "cfg": 7.0,
30
- "width": 512,
31
- "height": 768,
32
  "seed": -1,
33
  },
34
  "🌸 樱花和服": {
35
- "prompt": "1girl, anime style, kimono, cherry blossoms, spring, elegant pose, soft light, detailed illustration",
36
  "negative": DEFAULT_NEGATIVE,
37
- "steps": 20,
38
  "cfg": 7.0,
39
- "width": 512,
40
- "height": 768,
41
  "seed": -1,
42
  },
43
- "⚔️ 幻想剑士": {
44
- "prompt": "anime warrior girl, fantasy armor, sword, dramatic lighting, wind, detailed background, masterpiece",
45
  "negative": DEFAULT_NEGATIVE,
46
- "steps": 20,
47
  "cfg": 7.0,
48
- "width": 512,
49
- "height": 768,
50
  "seed": -1,
51
  },
52
  }
@@ -68,8 +68,12 @@ def load_pipe():
68
  pipe.safety_checker = None
69
  if hasattr(pipe, "requires_safety_checker"):
70
  pipe.requires_safety_checker = False
 
 
71
  if hasattr(pipe, "enable_attention_slicing"):
72
  pipe.enable_attention_slicing()
 
 
73
  if hasattr(pipe, "vae") and hasattr(pipe.vae, "enable_tiling"):
74
  pipe.vae.enable_tiling()
75
  pipe = pipe.to(_device)
@@ -116,36 +120,37 @@ def generate(
116
  if seed >= 0:
117
  generator = torch.Generator(device=_device).manual_seed(int(seed))
118
 
119
- image = pipe(
120
- prompt=prompt,
121
- negative_prompt=negative_prompt,
122
- num_inference_steps=int(steps),
123
- guidance_scale=float(guidance_scale),
124
- width=int(width),
125
- height=int(height),
126
- generator=generator,
127
- ).images[0]
 
128
  return image
129
 
130
 
131
  with gr.Blocks(title="wode") as demo:
132
  gr.Markdown(
133
- f"# wode\n\n免费档二次元生图(已切到 Counterfeit 路线)\n\n当前模型:`{MODEL_ID}`\n\n建议参数:`512x768``16-24 steps``CFG 6-8`。\n\n⚠️ 免费 CPU 慢,首张图等 `1~3 分钟` 都正常。"
134
  )
135
  with gr.Row():
136
  with gr.Column():
137
  prompt = gr.Textbox(
138
  label="正向提示词",
139
  lines=4,
140
- placeholder="例如:1girl, anime style, detailed face, masterpiece, best quality",
141
  )
142
- negative_prompt = gr.Textbox(label="反向提示词", lines=2, value=DEFAULT_NEGATIVE)
143
  with gr.Row():
144
- steps = gr.Slider(8, 32, value=20, step=1, label="步数 Steps")
145
  guidance_scale = gr.Slider(1, 12, value=7.0, step=0.5, label="引导强度 CFG")
146
  with gr.Row():
147
- width = gr.Slider(256, 768, value=512, step=64, label="宽度")
148
- height = gr.Slider(256, 1024, value=768, step=64, label="高度")
149
  seed = gr.Number(label="随机种子(-1 为随机)", value=-1, precision=0)
150
  with gr.Row():
151
  run = gr.Button("开始生成", variant="primary")
@@ -155,16 +160,16 @@ with gr.Blocks(title="wode") as demo:
155
 
156
  gr.Markdown("## 二次元预设按钮")
157
  with gr.Row():
158
- preset1 = gr.Button("🌻 向日葵少女")
159
- preset2 = gr.Button("🏙 赛博少女")
160
  preset3 = gr.Button("🌸 樱花和服")
161
- preset4 = gr.Button("⚔️ 幻想剑士")
162
 
163
  for btn, name in [
164
- (preset1, "🌻 向日葵少女"),
165
- (preset2, "🏙 赛博少女"),
166
  (preset3, "🌸 樱花和服"),
167
- (preset4, "⚔️ 幻想剑士"),
168
  ]:
169
  btn.click(
170
  fn=lambda n=name: apply_preset(n),
@@ -176,21 +181,21 @@ with gr.Blocks(title="wode") as demo:
176
  label="也可以直接点下面示例",
177
  examples=[
178
  [
179
- PRESETS["🌻 向日葵少女"]["prompt"],
180
  DEFAULT_NEGATIVE,
181
- 18,
182
  7.0,
183
- 512,
184
- 768,
185
  -1,
186
  ],
187
  [
188
- PRESETS["🏙 赛博少女"]["prompt"],
189
  DEFAULT_NEGATIVE,
190
- 18,
191
  7.0,
192
- 512,
193
- 768,
194
  -1,
195
  ],
196
  ],
 
4
 
5
  import gradio as gr
6
  import torch
7
+ from diffusers import AutoPipelineForText2Image, EulerAncestralDiscreteScheduler
8
 
9
  MODEL_ID = os.getenv("MODEL_ID", "stablediffusionapi/counterfeit-v30")
10
  DEFAULT_NEGATIVE = os.getenv(
11
  "DEFAULT_NEGATIVE",
12
+ "blurry, lowres, worst quality, low quality, bad anatomy, watermark, text, extra fingers, realistic, photo, 3d, child, loli, young-looking",
13
  )
14
 
15
  PRESETS = {
16
+ "🎀 动漫头像": {
17
+ "prompt": "masterpiece, best quality, 1girl, solo, anime style, upper body portrait, detailed eyes, clean lineart, soft cel shading",
18
  "negative": DEFAULT_NEGATIVE,
19
+ "steps": 12,
20
  "cfg": 7.0,
21
+ "width": 384,
22
+ "height": 576,
23
  "seed": -1,
24
  },
25
+ "💼 成熟御姐": {
26
+ "prompt": "masterpiece, best quality, 1woman, mature female, solo, anime style, office lady, detailed eyes, upper body portrait, clean lineart",
27
  "negative": DEFAULT_NEGATIVE,
28
+ "steps": 12,
29
  "cfg": 7.0,
30
+ "width": 384,
31
+ "height": 576,
32
  "seed": -1,
33
  },
34
  "🌸 樱花和服": {
35
+ "prompt": "masterpiece, best quality, 1girl, anime style, kimono, cherry blossoms, elegant pose, detailed face, clean lineart",
36
  "negative": DEFAULT_NEGATIVE,
37
+ "steps": 14,
38
  "cfg": 7.0,
39
+ "width": 448,
40
+ "height": 640,
41
  "seed": -1,
42
  },
43
+ "🏙 赛博少女": {
44
+ "prompt": "masterpiece, best quality, 1girl, anime style, cyberpunk city, neon lights, upper body, detailed eyes, clean lineart",
45
  "negative": DEFAULT_NEGATIVE,
46
+ "steps": 14,
47
  "cfg": 7.0,
48
+ "width": 448,
49
+ "height": 640,
50
  "seed": -1,
51
  },
52
  }
 
68
  pipe.safety_checker = None
69
  if hasattr(pipe, "requires_safety_checker"):
70
  pipe.requires_safety_checker = False
71
+ if hasattr(pipe, "scheduler") and pipe.scheduler is not None:
72
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
73
  if hasattr(pipe, "enable_attention_slicing"):
74
  pipe.enable_attention_slicing()
75
+ if hasattr(pipe, "enable_vae_slicing"):
76
+ pipe.enable_vae_slicing()
77
  if hasattr(pipe, "vae") and hasattr(pipe.vae, "enable_tiling"):
78
  pipe.vae.enable_tiling()
79
  pipe = pipe.to(_device)
 
120
  if seed >= 0:
121
  generator = torch.Generator(device=_device).manual_seed(int(seed))
122
 
123
+ with torch.inference_mode():
124
+ image = pipe(
125
+ prompt=prompt,
126
+ negative_prompt=negative_prompt,
127
+ num_inference_steps=int(steps),
128
+ guidance_scale=float(guidance_scale),
129
+ width=int(width),
130
+ height=int(height),
131
+ generator=generator,
132
+ ).images[0]
133
  return image
134
 
135
 
136
  with gr.Blocks(title="wode") as demo:
137
  gr.Markdown(
138
+ f"# wode\n\n免费档二次元生图(Counterfeit 路线)\n\n当前模型:`{MODEL_ID}`\n\n推荐先用:`384x576 / 12 steps / CFG 7`。\n\n想更像动漫角色:正向词尽量写 `1girl/1woman + anime style + detailed eyes + clean lineart`。\n想偏成年风格:加 `mature female` 或 `adult woman`。默认反向词已压制 `realistic/photo` 和 `child-like`。\n\n⚠️ 免费 CPU 仍然会慢,通常要等 `1~5 分钟`。"
139
  )
140
  with gr.Row():
141
  with gr.Column():
142
  prompt = gr.Textbox(
143
  label="正向提示词",
144
  lines=4,
145
+ placeholder="例如:masterpiece, best quality, 1girl, anime style, detailed eyes, clean lineart",
146
  )
147
+ negative_prompt = gr.Textbox(label="反向提示词", lines=3, value=DEFAULT_NEGATIVE)
148
  with gr.Row():
149
+ steps = gr.Slider(6, 24, value=12, step=1, label="步数 Steps")
150
  guidance_scale = gr.Slider(1, 12, value=7.0, step=0.5, label="引导强度 CFG")
151
  with gr.Row():
152
+ width = gr.Slider(256, 640, value=384, step=64, label="宽度")
153
+ height = gr.Slider(256, 896, value=576, step=64, label="高度")
154
  seed = gr.Number(label="随机种子(-1 为随机)", value=-1, precision=0)
155
  with gr.Row():
156
  run = gr.Button("开始生成", variant="primary")
 
160
 
161
  gr.Markdown("## 二次元预设按钮")
162
  with gr.Row():
163
+ preset1 = gr.Button("🎀 动漫头像")
164
+ preset2 = gr.Button("💼 成熟御姐")
165
  preset3 = gr.Button("🌸 樱花和服")
166
+ preset4 = gr.Button("🏙 赛博少女")
167
 
168
  for btn, name in [
169
+ (preset1, "🎀 动漫头像"),
170
+ (preset2, "💼 成熟御姐"),
171
  (preset3, "🌸 樱花和服"),
172
+ (preset4, "🏙 赛博少女"),
173
  ]:
174
  btn.click(
175
  fn=lambda n=name: apply_preset(n),
 
181
  label="也可以直接点下面示例",
182
  examples=[
183
  [
184
+ PRESETS["🎀 动漫头像"]["prompt"],
185
  DEFAULT_NEGATIVE,
186
+ 12,
187
  7.0,
188
+ 384,
189
+ 576,
190
  -1,
191
  ],
192
  [
193
+ PRESETS["💼 成熟御姐"]["prompt"],
194
  DEFAULT_NEGATIVE,
195
+ 12,
196
  7.0,
197
+ 384,
198
+ 576,
199
  -1,
200
  ],
201
  ],