Rentolly commited on
Commit
e5a76fa
·
1 Parent(s): d44197f

更新 app.py,新增 Scheduler 工具函式,移除一鍵串接功能;新增範例圖片 cat_girl.png 和 gura.png

Browse files
Files changed (3) hide show
  1. app.py +10 -78
  2. examples/cat_girl.png +3 -0
  3. examples/gura.png +3 -0
app.py CHANGED
@@ -43,7 +43,7 @@ SD_PIPE = load_sd_pipe()
43
  BLIP_PROCESSOR, BLIP_MODEL = load_blip()
44
 
45
  # -----------------------------
46
- # Scheduler 工具函式(新增)
47
  # -----------------------------
48
 
49
  # 提供多種取樣方法(sampler/scheduler)
@@ -118,23 +118,6 @@ def caption_image(image: Image.Image, max_len: int = 50):
118
  caption = BLIP_PROCESSOR.decode(out[0], skip_special_tokens=True)
119
  return caption
120
 
121
-
122
- # -----------------------------
123
- # 功能:一鍵串接(Text → Image → Caption)
124
- # -----------------------------
125
-
126
- def generate_and_caption(
127
- prompt: str, neg_prompt: str, steps: int, guidance: float, seed: int | None,
128
- max_len: int,
129
- scheduler_name: str, use_karras: bool, width: int, height: int
130
- ):
131
- image, _ = txt2img(prompt, neg_prompt, steps, guidance, seed, scheduler_name, use_karras, width, height)
132
- if image is None:
133
- return None, ""
134
- cap = caption_image(image, max_len=max_len)
135
- return image, cap
136
-
137
-
138
  # -----------------------------
139
  # Gradio 介面
140
  # -----------------------------
@@ -142,7 +125,8 @@ with gr.Blocks(title="NoobAI-XL + BLIP", theme=gr.themes.Soft()) as demo:
142
  gr.Markdown(
143
  """
144
  # SDXL (Laxhar/noobai-XL-1.0) + BLIP
145
- Vision-Language 展示:**文字生圖** **圖片描述**。
 
146
  """
147
  )
148
 
@@ -151,8 +135,8 @@ with gr.Blocks(title="NoobAI-XL + BLIP", theme=gr.themes.Soft()) as demo:
151
  with gr.TabItem("Text → Image"):
152
  with gr.Row():
153
  with gr.Column(scale=1):
154
- prompt = gr.Textbox(label="Prompt", placeholder="正向提示詞 Prompt")
155
- neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="反向提示詞 Negative Prompt")
156
  steps = gr.Slider(1, 100, value=20, step=1, label="Steps")
157
  guidance = gr.Slider(1.0, 20.0, value=7, step=0.5, label="Guidance Scale")
158
  seed = gr.Number(label="Seed(-1 表示隨機)", value=-1, precision=0)
@@ -211,68 +195,16 @@ with gr.Blocks(title="NoobAI-XL + BLIP", theme=gr.themes.Soft()) as demo:
211
 
212
  cap_btn.click(caption_image, [in_img, max_len], [caption_out])
213
 
214
- # Tab 3: 一鍵串接
215
- with gr.TabItem("Generate → Caption"):
216
- with gr.Row():
217
- with gr.Column(scale=1):
218
- g_prompt = gr.Textbox(label="Prompt", placeholder="正向提示詞 Prompt")
219
- g_neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="反向提示詞 Negative Prompt")
220
- g_steps = gr.Slider(1, 100, value=20, step=1, label="Steps")
221
- g_guidance = gr.Slider(1.0, 12.0, value=7.0, step=0.5, label="Guidance Scale")
222
- g_seed = gr.Number(label="Seed(-1 為隨機)", value=-1, precision=0)
223
- g_scheduler_name = gr.Dropdown(
224
- choices=list(SCHEDULER_FACTORIES.keys()),
225
- value="DPM++ 2M (DPMSolverMultistep)",
226
- label="取樣方法 (Sampler / Scheduler)"
227
- )
228
- g_use_karras = gr.Checkbox(value=True, label="使用 Karras Sigmas")
229
- g_width = gr.Slider(256, 1536, value=768, step=8, label="寬度 Width")
230
- g_height = gr.Slider(256, 1536, value=768, step=8, label="高度 Height")
231
- g_max_len = gr.Slider(10, 100, value=50, step=5, label="Caption 長度上限")
232
- g_btn = gr.Button("生成 + 描述", variant="primary")
233
-
234
- with gr.Column(scale=1):
235
- g_img = gr.Image(label="Generated Image", format="png")
236
- g_cap = gr.Textbox(label="BLIP Caption")
237
-
238
- def _run_generate_and_caption(prompt, neg_prompt, steps, guidance, seed, max_len, scheduler_name, use_karras, width, height):
239
- prog = None
240
-
241
- try:
242
- prog = gr.Progress(track_tqdm=True)
243
- prog(0, desc="圖片生成中...")
244
- except Exception:
245
- prog = None
246
-
247
- image, caption = generate_and_caption(
248
- prompt, neg_prompt, steps, guidance,
249
- int(seed) if seed is not None else None, int(max_len),
250
- scheduler_name, bool(use_karras), int(width), int(height)
251
- )
252
-
253
- try:
254
- if prog is not None:
255
- prog(1, desc="完成")
256
- except Exception:
257
- pass
258
-
259
- return image, caption
260
-
261
- g_btn.click(
262
- _run_generate_and_caption,
263
- [g_prompt, g_neg_prompt, g_steps, g_guidance, g_seed, g_max_len, g_scheduler_name, g_use_karras, g_width, g_height],
264
- [g_img, g_cap]
265
- )
266
-
267
  gr.Examples(
268
  examples=[
269
- ["masterpiece, best quality, amazing quality, highres, absurdres, 1girl, virtual youtuber, gawr gura, solo, blue hair, grey hair, medium hair, long hair, multicolored hair, streaked hair, bangs, blunt bangs, side ponytail, blue eyes, sharp teeth, teeth, fang, cat ears, hair ornament, blue nails, shark tail, nail polish, tail, fish tail, teeth, shark girl, sleeveless, dress, shirt, :d, blush, smile, paw pose, open mouth, full body, simple_background, looking_at_viewer, upper_body, front view, bubble, flat color, no lineart", "worst quality, old, early, low quality, lowres, signature, username, logo, bad hands, mutated hands, mammal, anthro, furry, ambiguous form, feral, semi-anthro, nsfw", 20, 7.0, 42],
270
- ["1girl, masterpiece, best quality, amazing quality, highres, absurdres, clean lineart, usnr, :d, cat_pose, blush, kawaii, white hair, long hair, hair between eyes, bangs, pink eyes, fang, pink_nails, open mouth, white fake cat ears, simple_background, hugging a cat, light pink dress, hair ornament, long sleeves, puffy long sleeves, pink bow, puffy sleeves, black choker, upper body, looking_at_viewer, front view", "worst quality, old, early, low quality, lowres, signature, username, logo, bad hands, mutated hands, mammal, anthro, furry, ambiguous form, feral, semi-anthro, nsfw", 20, 8.0, 1234],
 
271
  ],
272
- inputs=[prompt, neg_prompt, steps, guidance, seed],
273
  label="Prompt 範例(可直接點選)",
274
  )
275
 
276
  if __name__ == "__main__":
277
  # 在 HF Spaces 使用:不用 demo.launch(share=True)
278
- demo.launch()
 
43
  BLIP_PROCESSOR, BLIP_MODEL = load_blip()
44
 
45
  # -----------------------------
46
+ # Scheduler 工具函式
47
  # -----------------------------
48
 
49
  # 提供多種取樣方法(sampler/scheduler)
 
118
  caption = BLIP_PROCESSOR.decode(out[0], skip_special_tokens=True)
119
  return caption
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  # -----------------------------
122
  # Gradio 介面
123
  # -----------------------------
 
125
  gr.Markdown(
126
  """
127
  # SDXL (Laxhar/noobai-XL-1.0) + BLIP
128
+ 動漫風格的 Stable Diffusion XL 模型,搭配 BLIP 影像描述模型。
129
+ 每個 Step 大約需要 1 分鐘的 CPU 運算時間,請耐心等候。
130
  """
131
  )
132
 
 
135
  with gr.TabItem("Text → Image"):
136
  with gr.Row():
137
  with gr.Column(scale=1):
138
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="正向提示詞 Prompt")
139
+ neg_prompt = gr.Textbox(label="Negative Prompt", lines=3, placeholder="反向提示詞 Negative Prompt")
140
  steps = gr.Slider(1, 100, value=20, step=1, label="Steps")
141
  guidance = gr.Slider(1.0, 20.0, value=7, step=0.5, label="Guidance Scale")
142
  seed = gr.Number(label="Seed(-1 表示隨機)", value=-1, precision=0)
 
195
 
196
  cap_btn.click(caption_image, [in_img, max_len], [caption_out])
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  gr.Examples(
199
  examples=[
200
+ ["examples/gura.jpg", "masterpiece, best quality, amazing quality, highres, absurdres, 1girl, virtual youtuber, gawr gura, solo, blue hair, grey hair, medium hair, long hair, multicolored hair, streaked hair, bangs, blunt bangs, side ponytail, blue eyes, sharp teeth, teeth, fang, cat ears, hair ornament, blue nails, shark tail, nail polish, tail, fish tail, teeth, shark girl, sleeveless, dress, shirt, :d, blush, smile, paw pose, open mouth, full body, simple_background, looking_at_viewer, upper_body, front view, bubble, flat color, no lineart", "worst quality, old, early, low quality, lowres, signature, username, logo, bad hands, mutated hands, mammal, anthro, furry, ambiguous form, feral, semi-anthro, nsfw", 20, 7.0, 42, "DPM++ 2M (DPMSolverMultistep)", True, 768, 768],
201
+ ["examples/cat_girl.jpg", "masterpiece, best quality, amazing quality, highres, absurdres, 1girl, clean lineart, usnr, :d, cat_pose, blush, kawaii, white hair, long hair, hair between eyes, bangs, pink eyes, fang, pink_nails, open mouth, white fake cat ears, simple_background, hugging a cat, light pink dress, hair ornament, long sleeves, puffy long sleeves, pink bow, puffy sleeves, black choker, upper body, looking_at_viewer, front view", "worst quality, old, early, low quality, lowres, signature, username, logo, bad hands, mutated hands, mammal, anthro, furry, ambiguous form, feral, semi-anthro, nsfw", 20, 8.0, 1234, "DPM++ 2M (DPMSolverMultistep)", True, 768, 768],
202
+ # ["examples/miku.jpg", "masterpiece, best quality, amazing quality, highres, absurdres, 1girl, hatsune miku, white pupils, power elements, microphone, vibrant blue color palette, abstract,abstract background, dreamlike atmosphere, delicate linework, wind-swept hair, energy", "worst quality, old, early, low quality, lowres, signature, username, logo, bad hands, mutated hands, mammal, anthro, furry, ambiguous form, feral, semi-anthro, nsfw", 28, 7.0, 24, "DPM++ 2M (DPMSolverMultistep)", True, 768, 768],
203
  ],
204
+ inputs=[in_img, prompt, neg_prompt, steps, guidance, seed, scheduler_name, use_karras, width, height],
205
  label="Prompt 範例(可直接點選)",
206
  )
207
 
208
  if __name__ == "__main__":
209
  # 在 HF Spaces 使用:不用 demo.launch(share=True)
210
+ demo.launch()
examples/cat_girl.png ADDED

Git LFS Details

  • SHA256: e5ec2125156260d9ea483d18880775194aa3c91d660ca753629ffc140c77cbcd
  • Pointer size: 131 Bytes
  • Size of remote file: 570 kB
examples/gura.png ADDED

Git LFS Details

  • SHA256: e54662f12ee7ecf1bf617c8ea8f0221c1f7784224b97286ee9078168a360c57c
  • Pointer size: 131 Bytes
  • Size of remote file: 816 kB