Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,68 @@ import torch
|
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import warnings
|
| 7 |
|
| 8 |
# 忽略一些警告
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# ==================== 模型加载优化 ====================
|
| 12 |
print("🚀 Loading Z-Image-Turbo pipeline...")
|
| 13 |
start_time = time.time()
|
|
@@ -46,8 +102,10 @@ print(f"✅ Pipeline loaded in {load_time:.2f} seconds!")
|
|
| 46 |
@spaces.GPU
|
| 47 |
def generate_image(
|
| 48 |
prompt,
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
num_inference_steps=8,
|
| 52 |
seed=42,
|
| 53 |
randomize_seed=True,
|
|
@@ -63,16 +121,25 @@ def generate_image(
|
|
| 63 |
prompt = prompt.strip()
|
| 64 |
negative_prompt = negative_prompt_text.strip() if negative_prompt_text else None
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# 限制最大尺寸防止OOM(T4 GPU限制)
|
| 71 |
-
MAX_SIZE =
|
| 72 |
if height > MAX_SIZE or width > MAX_SIZE:
|
| 73 |
height = min(height, MAX_SIZE)
|
| 74 |
width = min(width, MAX_SIZE)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# 生成随机种子
|
| 77 |
if randomize_seed:
|
| 78 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
|
@@ -113,34 +180,11 @@ def generate_image(
|
|
| 113 |
return image, seed, info
|
| 114 |
|
| 115 |
except torch.cuda.OutOfMemoryError:
|
| 116 |
-
return None, seed, "💥 Out of memory! Try smaller image size
|
| 117 |
except Exception as e:
|
| 118 |
error_msg = str(e)[:100]
|
| 119 |
return None, seed, f"❌ Error: {error_msg}"
|
| 120 |
|
| 121 |
-
# ==================== 快速生成函数(用于示例) ====================
|
| 122 |
-
def quick_generate(prompt):
|
| 123 |
-
"""为示例使用的快速生成函数"""
|
| 124 |
-
return generate_image(
|
| 125 |
-
prompt=prompt,
|
| 126 |
-
height=768,
|
| 127 |
-
width=768,
|
| 128 |
-
num_inference_steps=8,
|
| 129 |
-
seed=42,
|
| 130 |
-
randomize_seed=False
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
# ==================== 示例提示词 ====================
|
| 134 |
-
examples = [
|
| 135 |
-
["A beautiful Chinese woman in traditional red Hanfu, intricate embroidery, cinematic lighting, photorealistic"],
|
| 136 |
-
["Cyberpunk city at night, neon lights, rainy streets, futuristic architecture, Blade Runner style"],
|
| 137 |
-
["Majestic dragon flying over ancient Chinese palace, sunset, epic fantasy art"],
|
| 138 |
-
["Cute anime girl with pink hair, cyberpunk street background, vibrant colors"],
|
| 139 |
-
["Fantasy landscape with floating islands, waterfalls, magical creatures, digital painting"],
|
| 140 |
-
["Portrait of a wise old samurai, detailed armor, cherry blossoms, studio lighting"],
|
| 141 |
-
["Steampunk airship flying over Victorian London, gears and cogs, detailed"],
|
| 142 |
-
]
|
| 143 |
-
|
| 144 |
# ==================== 主题配置 ====================
|
| 145 |
custom_theme = gr.themes.Soft(
|
| 146 |
primary_hue="yellow",
|
|
@@ -161,8 +205,7 @@ custom_theme = gr.themes.Soft(
|
|
| 161 |
slider_color="*primary_500",
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# ====================
|
| 165 |
-
|
| 166 |
global_styles = """
|
| 167 |
/* 全局样式 */
|
| 168 |
body {
|
|
@@ -231,27 +274,39 @@ button.secondary:hover {
|
|
| 231 |
|
| 232 |
/* 信息显示 */
|
| 233 |
#info-display {
|
| 234 |
-
background:
|
| 235 |
border: 1px solid #e2e8f0 !important;
|
| 236 |
border-radius: 8px !important;
|
| 237 |
font-size: 0.9rem !important;
|
| 238 |
padding: 0.75rem !important;
|
| 239 |
}
|
| 240 |
|
| 241 |
-
/* 提示框 */
|
| 242 |
-
#tips-box {
|
| 243 |
-
background: #fefce8 !important;
|
| 244 |
-
border-left: 4px solid #fbbf24 !important;
|
| 245 |
-
padding: 12px !important;
|
| 246 |
-
border-radius: 8px !important;
|
| 247 |
-
margin-top: 1rem !important;
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
/* 进度条 */
|
| 251 |
.progress-bar {
|
| 252 |
background: linear-gradient(90deg, #fbbf24 0%, #f59e0b 100%) !important;
|
| 253 |
}
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
/* 移动端适配 */
|
| 256 |
@media (max-width: 768px) {
|
| 257 |
.gradio-container {
|
|
@@ -322,6 +377,18 @@ button.secondary:hover {
|
|
| 322 |
}
|
| 323 |
"""
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
with gr.Blocks(
|
| 326 |
theme=custom_theme,
|
| 327 |
title="Z-Image-Turbo • Ultra-Fast AI Image Generator",
|
|
@@ -336,7 +403,7 @@ with gr.Blocks(
|
|
| 336 |
<h1 style="font-size: 2.5rem; font-weight: 800; margin-bottom: 0.5rem; background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 50%, #d97706 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">
|
| 337 |
🎨 Z-Image-Turbo</h1>
|
| 338 |
<p style="font-size: 1.1rem; color: #64748b; margin-bottom: 1.5rem;">
|
| 339 |
-
Generate stunning images in <strong>8 steps</strong> • Optimized for speed</p>
|
| 340 |
</div>
|
| 341 |
""",
|
| 342 |
elem_id="header"
|
|
@@ -376,26 +443,52 @@ with gr.Blocks(
|
|
| 376 |
scale=1
|
| 377 |
)
|
| 378 |
|
| 379 |
-
#
|
| 380 |
-
with gr.Accordion("
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
minimum=512,
|
| 385 |
-
maximum=
|
| 386 |
-
value=
|
| 387 |
step=64,
|
| 388 |
-
info="512-
|
| 389 |
)
|
| 390 |
-
|
| 391 |
-
label="
|
| 392 |
minimum=512,
|
| 393 |
-
maximum=
|
| 394 |
-
value=
|
| 395 |
step=64,
|
| 396 |
-
info="512-
|
| 397 |
)
|
| 398 |
-
|
|
|
|
|
|
|
| 399 |
num_inference_steps = gr.Slider(
|
| 400 |
label="Inference Steps",
|
| 401 |
minimum=4,
|
|
@@ -418,16 +511,6 @@ with gr.Blocks(
|
|
| 418 |
visible=False,
|
| 419 |
scale=2
|
| 420 |
)
|
| 421 |
-
|
| 422 |
-
# 显示/隐藏种子输入
|
| 423 |
-
def toggle_seed_visibility(randomize):
|
| 424 |
-
return gr.Number(visible=not randomize)
|
| 425 |
-
|
| 426 |
-
randomize_seed.change(
|
| 427 |
-
toggle_seed_visibility,
|
| 428 |
-
inputs=[randomize_seed],
|
| 429 |
-
outputs=[seed]
|
| 430 |
-
)
|
| 431 |
|
| 432 |
# 示例提示词
|
| 433 |
gr.Examples(
|
|
@@ -474,12 +557,13 @@ with gr.Blocks(
|
|
| 474 |
# 建议提示
|
| 475 |
gr.Markdown(
|
| 476 |
"""
|
| 477 |
-
<div style="background:
|
| 478 |
<strong>💡 Tips for best results:</strong>
|
| 479 |
<ul style="margin: 0.5rem 0 0 1rem; padding-left: 0.5rem;">
|
| 480 |
<li>Use descriptive prompts</li>
|
| 481 |
<li>Start with 8 steps for speed</li>
|
| 482 |
-
<li>
|
|
|
|
| 483 |
<li>Use negative prompts to exclude unwanted elements</li>
|
| 484 |
</ul>
|
| 485 |
</div>
|
|
@@ -502,6 +586,36 @@ with gr.Blocks(
|
|
| 502 |
|
| 503 |
# ==================== 事件处理 ====================
|
| 504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
# 复制种子按钮
|
| 506 |
def copy_seed_to_clipboard(s):
|
| 507 |
if s != 0:
|
|
@@ -517,7 +631,8 @@ with gr.Blocks(
|
|
| 517 |
# 生成按钮点击
|
| 518 |
generate_btn.click(
|
| 519 |
fn=generate_image,
|
| 520 |
-
inputs=[prompt,
|
|
|
|
| 521 |
outputs=[output_image, used_seed, info_display],
|
| 522 |
api_name="generate"
|
| 523 |
)
|
|
@@ -525,7 +640,8 @@ with gr.Blocks(
|
|
| 525 |
# Enter键提交
|
| 526 |
prompt.submit(
|
| 527 |
fn=generate_image,
|
| 528 |
-
inputs=[prompt,
|
|
|
|
| 529 |
outputs=[output_image, used_seed, info_display]
|
| 530 |
)
|
| 531 |
|
|
|
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
import time
|
| 5 |
+
import re
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import warnings
|
| 8 |
|
| 9 |
# 忽略一些警告
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
+
# ==================== 分辨率配置 ====================
|
| 13 |
+
RES_CHOICES = {
|
| 14 |
+
"1024": [
|
| 15 |
+
"1024x1024 (1:1)",
|
| 16 |
+
"1152x896 (9:7)",
|
| 17 |
+
"896x1152 (7:9)",
|
| 18 |
+
"1152x864 (4:3)",
|
| 19 |
+
"864x1152 (3:4)",
|
| 20 |
+
"1248x832 (3:2)",
|
| 21 |
+
"832x1248 (2:3)",
|
| 22 |
+
"1280x720 (16:9)",
|
| 23 |
+
"720x1280 (9:16)",
|
| 24 |
+
"1344x576 (21:9)",
|
| 25 |
+
"576x1344 (9:21)",
|
| 26 |
+
],
|
| 27 |
+
"1280": [
|
| 28 |
+
"1280x1280 (1:1)",
|
| 29 |
+
"1440x1120 (9:7)",
|
| 30 |
+
"1120x1440 (7:9)",
|
| 31 |
+
"1472x1104 (4:3)",
|
| 32 |
+
"1104x1472 (3:4)",
|
| 33 |
+
"1536x1024 (3:2)",
|
| 34 |
+
"1024x1536 (2:3)",
|
| 35 |
+
"1536x864 (16:9)",
|
| 36 |
+
"864x1536 (9:16)",
|
| 37 |
+
"1680x720 (21:9)",
|
| 38 |
+
"720x1680 (9:21)",
|
| 39 |
+
],
|
| 40 |
+
"1536": [
|
| 41 |
+
"1536x1536 (1:1)",
|
| 42 |
+
"1728x1344 (9:7)",
|
| 43 |
+
"1344x1728 (7:9)",
|
| 44 |
+
"1728x1296 (4:3)",
|
| 45 |
+
"1296x1728 (3:4)",
|
| 46 |
+
"1872x1248 (3:2)",
|
| 47 |
+
"1248x1872 (2:3)",
|
| 48 |
+
"2048x1152 (16:9)",
|
| 49 |
+
"1152x2048 (9:16)",
|
| 50 |
+
"2016x864 (21:9)",
|
| 51 |
+
"864x2016 (9:21)",
|
| 52 |
+
],
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def get_resolution(resolution_str):
|
| 56 |
+
"""从分辨率字符串提取宽高"""
|
| 57 |
+
match = re.search(r"(\d+)\s*[×x]\s*(\d+)", resolution_str)
|
| 58 |
+
if match:
|
| 59 |
+
width = int(match.group(1))
|
| 60 |
+
height = int(match.group(2))
|
| 61 |
+
# 确保是8的倍数
|
| 62 |
+
width = width - width % 8
|
| 63 |
+
height = height - height % 8
|
| 64 |
+
return width, height
|
| 65 |
+
return 1024, 1024
|
| 66 |
+
|
| 67 |
# ==================== 模型加载优化 ====================
|
| 68 |
print("🚀 Loading Z-Image-Turbo pipeline...")
|
| 69 |
start_time = time.time()
|
|
|
|
| 102 |
@spaces.GPU
|
| 103 |
def generate_image(
|
| 104 |
prompt,
|
| 105 |
+
resolution_choice,
|
| 106 |
+
custom_height=None,
|
| 107 |
+
custom_width=None,
|
| 108 |
+
use_custom_res=False,
|
| 109 |
num_inference_steps=8,
|
| 110 |
seed=42,
|
| 111 |
randomize_seed=True,
|
|
|
|
| 121 |
prompt = prompt.strip()
|
| 122 |
negative_prompt = negative_prompt_text.strip() if negative_prompt_text else None
|
| 123 |
|
| 124 |
+
# 确定分辨率
|
| 125 |
+
if use_custom_res and custom_height and custom_width:
|
| 126 |
+
# 使用自定义分辨率
|
| 127 |
+
height = max(512, int(custom_height) - int(custom_height) % 8)
|
| 128 |
+
width = max(512, int(custom_width) - int(custom_width) % 8)
|
| 129 |
+
else:
|
| 130 |
+
# 使用预设分辨率
|
| 131 |
+
width, height = get_resolution(resolution_choice)
|
| 132 |
|
| 133 |
# 限制最大尺寸防止OOM(T4 GPU限制)
|
| 134 |
+
MAX_SIZE = 1536
|
| 135 |
if height > MAX_SIZE or width > MAX_SIZE:
|
| 136 |
height = min(height, MAX_SIZE)
|
| 137 |
width = min(width, MAX_SIZE)
|
| 138 |
|
| 139 |
+
# 检查宽高积是否过大
|
| 140 |
+
if height * width > 2048 * 2048:
|
| 141 |
+
return None, 0, "❌ Resolution too large! Maximum total pixels: 2048x2048"
|
| 142 |
+
|
| 143 |
# 生成随机种子
|
| 144 |
if randomize_seed:
|
| 145 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
|
|
|
| 180 |
return image, seed, info
|
| 181 |
|
| 182 |
except torch.cuda.OutOfMemoryError:
|
| 183 |
+
return None, seed, "💥 Out of memory! Try smaller image size"
|
| 184 |
except Exception as e:
|
| 185 |
error_msg = str(e)[:100]
|
| 186 |
return None, seed, f"❌ Error: {error_msg}"
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# ==================== 主题配置 ====================
|
| 189 |
custom_theme = gr.themes.Soft(
|
| 190 |
primary_hue="yellow",
|
|
|
|
| 205 |
slider_color="*primary_500",
|
| 206 |
)
|
| 207 |
|
| 208 |
+
# ==================== 全局样式 ====================
|
|
|
|
| 209 |
global_styles = """
|
| 210 |
/* 全局样式 */
|
| 211 |
body {
|
|
|
|
| 274 |
|
| 275 |
/* 信息显示 */
|
| 276 |
#info-display {
|
| 277 |
+
background: rgba(71,85,105) !important;
|
| 278 |
border: 1px solid #e2e8f0 !important;
|
| 279 |
border-radius: 8px !important;
|
| 280 |
font-size: 0.9rem !important;
|
| 281 |
padding: 0.75rem !important;
|
| 282 |
}
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
/* 进度条 */
|
| 285 |
.progress-bar {
|
| 286 |
background: linear-gradient(90deg, #fbbf24 0%, #f59e0b 100%) !important;
|
| 287 |
}
|
| 288 |
|
| 289 |
+
/* 分辨率选择器 */
|
| 290 |
+
.resolution-option {
|
| 291 |
+
border: 1px solid #e2e8f0 !important;
|
| 292 |
+
border-radius: 8px !important;
|
| 293 |
+
padding: 0.5rem !important;
|
| 294 |
+
margin: 0.25rem !important;
|
| 295 |
+
cursor: pointer !important;
|
| 296 |
+
transition: all 0.2s ease !important;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
.resolution-option:hover {
|
| 300 |
+
background: #fefce8 !important;
|
| 301 |
+
border-color: #fbbf24 !important;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
.resolution-option.selected {
|
| 305 |
+
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%) !important;
|
| 306 |
+
border-color: #f59e0b !important;
|
| 307 |
+
font-weight: 600 !important;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
/* 移动端适配 */
|
| 311 |
@media (max-width: 768px) {
|
| 312 |
.gradio-container {
|
|
|
|
| 377 |
}
|
| 378 |
"""
|
| 379 |
|
| 380 |
+
# ==================== 示例提示词 ====================
|
| 381 |
+
examples = [
|
| 382 |
+
["A beautiful Chinese woman in traditional red Hanfu, intricate embroidery, cinematic lighting, photorealistic"],
|
| 383 |
+
["Cyberpunk city at night, neon lights, rainy streets, futuristic architecture, Blade Runner style"],
|
| 384 |
+
["Majestic dragon flying over ancient Chinese palace, sunset, epic fantasy art"],
|
| 385 |
+
["Cute anime girl with pink hair, cyberpunk street background, vibrant colors"],
|
| 386 |
+
["Fantasy landscape with floating islands, waterfalls, magical creatures, digital painting"],
|
| 387 |
+
["Portrait of a wise old samurai, detailed armor, cherry blossoms, studio lighting"],
|
| 388 |
+
["Steampunk airship flying over Victorian London, gears and cogs, detailed"],
|
| 389 |
+
]
|
| 390 |
+
|
| 391 |
+
# ==================== Gradio界面 ====================
|
| 392 |
with gr.Blocks(
|
| 393 |
theme=custom_theme,
|
| 394 |
title="Z-Image-Turbo • Ultra-Fast AI Image Generator",
|
|
|
|
| 403 |
<h1 style="font-size: 2.5rem; font-weight: 800; margin-bottom: 0.5rem; background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 50%, #d97706 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;">
|
| 404 |
🎨 Z-Image-Turbo</h1>
|
| 405 |
<p style="font-size: 1.1rem; color: #64748b; margin-bottom: 1.5rem;">
|
| 406 |
+
Generate stunning images in <strong>8 steps</strong> • Optimized for speed • Multiple resolutions</p>
|
| 407 |
</div>
|
| 408 |
""",
|
| 409 |
elem_id="header"
|
|
|
|
| 443 |
scale=1
|
| 444 |
)
|
| 445 |
|
| 446 |
+
# 分辨率设置
|
| 447 |
+
with gr.Accordion("🖼️ Resolution Settings", open=True):
|
| 448 |
+
# 分辨率预设选择
|
| 449 |
+
resolution_preset = gr.Radio(
|
| 450 |
+
label="Preset Resolutions",
|
| 451 |
+
choices=["1024", "1280", "1536"],
|
| 452 |
+
value="1024",
|
| 453 |
+
type="value",
|
| 454 |
+
elem_classes="resolution-preset"
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
# 分辨率选项动态更新
|
| 458 |
+
resolution_choices = gr.Dropdown(
|
| 459 |
+
label="Select Resolution",
|
| 460 |
+
choices=RES_CHOICES["1024"],
|
| 461 |
+
value="1024x1024 (1:1)",
|
| 462 |
+
elem_classes="resolution-choices"
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
# 自定义分辨率选项
|
| 466 |
+
use_custom_res = gr.Checkbox(
|
| 467 |
+
label="Use Custom Resolution",
|
| 468 |
+
value=False,
|
| 469 |
+
info="Manually set width and height"
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
with gr.Row(visible=False) as custom_res_row:
|
| 473 |
+
custom_width = gr.Slider(
|
| 474 |
+
label="Width",
|
| 475 |
minimum=512,
|
| 476 |
+
maximum=1536,
|
| 477 |
+
value=1024,
|
| 478 |
step=64,
|
| 479 |
+
info="512-1536 pixels"
|
| 480 |
)
|
| 481 |
+
custom_height = gr.Slider(
|
| 482 |
+
label="Height",
|
| 483 |
minimum=512,
|
| 484 |
+
maximum=1536,
|
| 485 |
+
value=1024,
|
| 486 |
step=64,
|
| 487 |
+
info="512-1536 pixels"
|
| 488 |
)
|
| 489 |
+
|
| 490 |
+
# 高级设置
|
| 491 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 492 |
num_inference_steps = gr.Slider(
|
| 493 |
label="Inference Steps",
|
| 494 |
minimum=4,
|
|
|
|
| 511 |
visible=False,
|
| 512 |
scale=2
|
| 513 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
| 515 |
# 示例提示词
|
| 516 |
gr.Examples(
|
|
|
|
| 557 |
# 建议提示
|
| 558 |
gr.Markdown(
|
| 559 |
"""
|
| 560 |
+
<div style="background: rgba(71,85,105); border-left: 4px solid #fbbf24; padding: 0.75rem; border-radius: 0.5rem; margin-top: 1rem;">
|
| 561 |
<strong>💡 Tips for best results:</strong>
|
| 562 |
<ul style="margin: 0.5rem 0 0 1rem; padding-left: 0.5rem;">
|
| 563 |
<li>Use descriptive prompts</li>
|
| 564 |
<li>Start with 8 steps for speed</li>
|
| 565 |
+
<li>1024x1024 is optimal for most cases</li>
|
| 566 |
+
<li>Higher resolutions require more VRAM</li>
|
| 567 |
<li>Use negative prompts to exclude unwanted elements</li>
|
| 568 |
</ul>
|
| 569 |
</div>
|
|
|
|
| 586 |
|
| 587 |
# ==================== 事件处理 ====================
|
| 588 |
|
| 589 |
+
# 更新分辨率选项
|
| 590 |
+
def update_resolution_choices(preset):
|
| 591 |
+
return gr.Dropdown(choices=RES_CHOICES[preset], value=RES_CHOICES[preset][0])
|
| 592 |
+
|
| 593 |
+
resolution_preset.change(
|
| 594 |
+
update_resolution_choices,
|
| 595 |
+
inputs=[resolution_preset],
|
| 596 |
+
outputs=[resolution_choices]
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
# 显示/隐藏自定义分辨率
|
| 600 |
+
def toggle_custom_resolution(use_custom):
|
| 601 |
+
return gr.Row(visible=use_custom), gr.Dropdown(visible=not use_custom)
|
| 602 |
+
|
| 603 |
+
use_custom_res.change(
|
| 604 |
+
toggle_custom_resolution,
|
| 605 |
+
inputs=[use_custom_res],
|
| 606 |
+
outputs=[custom_res_row, resolution_choices]
|
| 607 |
+
)
|
| 608 |
+
|
| 609 |
+
# 显示/隐藏种子输入
|
| 610 |
+
def toggle_seed_visibility(randomize):
|
| 611 |
+
return gr.Number(visible=not randomize)
|
| 612 |
+
|
| 613 |
+
randomize_seed.change(
|
| 614 |
+
toggle_seed_visibility,
|
| 615 |
+
inputs=[randomize_seed],
|
| 616 |
+
outputs=[seed]
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
# 复制种子按钮
|
| 620 |
def copy_seed_to_clipboard(s):
|
| 621 |
if s != 0:
|
|
|
|
| 631 |
# 生成按钮点击
|
| 632 |
generate_btn.click(
|
| 633 |
fn=generate_image,
|
| 634 |
+
inputs=[prompt, resolution_choices, custom_height, custom_width, use_custom_res,
|
| 635 |
+
num_inference_steps, seed, randomize_seed, negative_prompt],
|
| 636 |
outputs=[output_image, used_seed, info_display],
|
| 637 |
api_name="generate"
|
| 638 |
)
|
|
|
|
| 640 |
# Enter键提交
|
| 641 |
prompt.submit(
|
| 642 |
fn=generate_image,
|
| 643 |
+
inputs=[prompt, resolution_choices, custom_height, custom_width, use_custom_res,
|
| 644 |
+
num_inference_steps, seed, randomize_seed, negative_prompt],
|
| 645 |
outputs=[output_image, used_seed, info_display]
|
| 646 |
)
|
| 647 |
|