Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -236,6 +236,22 @@ LORA_MAPPING_PERSIAN = {
|
|
| 236 |
"انتقال متریال (Material-Transfer)": "Material-Transfer"
|
| 237 |
}
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
def update_dimensions_on_upload(image):
|
| 240 |
if image is None: return 1024, 1024
|
| 241 |
original_width, original_height = image.size
|
|
@@ -251,6 +267,12 @@ def update_dimensions_on_upload(image):
|
|
| 251 |
new_height = (new_height // 16) * 16
|
| 252 |
return new_width, new_height
|
| 253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
# ==========================================
|
| 255 |
# 5. HTML Helpers
|
| 256 |
# ==========================================
|
|
@@ -290,6 +312,9 @@ def infer(
|
|
| 290 |
randomize_seed,
|
| 291 |
guidance_scale,
|
| 292 |
steps,
|
|
|
|
|
|
|
|
|
|
| 293 |
fingerprint,
|
| 294 |
subscription_status,
|
| 295 |
progress=gr.Progress(track_tqdm=True)
|
|
@@ -352,7 +377,16 @@ def infer(
|
|
| 352 |
img1_pil = image_1.convert("RGB")
|
| 353 |
img2_pil = image_2.convert("RGB")
|
| 354 |
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
try:
|
| 358 |
result = pipe(
|
|
@@ -366,11 +400,11 @@ def infer(
|
|
| 366 |
true_cfg_scale=guidance_scale,
|
| 367 |
).images[0]
|
| 368 |
|
| 369 |
-
#
|
| 370 |
if is_image_nsfw(result):
|
| 371 |
return None, seed, get_error_html("تصویر خروجی حاوی محتوای نامناسب بود."), gr.update(visible=True), gr.update(visible=False)
|
| 372 |
|
| 373 |
-
#
|
| 374 |
user_record = get_user_record(fingerprint)
|
| 375 |
remaining = USAGE_LIMIT - user_record["count"] if user_record else 0
|
| 376 |
success_msg = "تصویر با موفقیت ترکیب شد."
|
|
@@ -390,7 +424,8 @@ def infer(
|
|
| 390 |
|
| 391 |
@spaces.GPU(duration=30)
|
| 392 |
def infer_example(image_1, image_2, prompt, lora_adapter):
|
| 393 |
-
|
|
|
|
| 394 |
return res, s
|
| 395 |
|
| 396 |
# ==========================================
|
|
@@ -526,6 +561,12 @@ css_code = """
|
|
| 526 |
:root, body, .gradio-container { font-family: 'Vazirmatn', sans-serif !important; }
|
| 527 |
#fingerprint_storage, #status_storage { display: none !important; }
|
| 528 |
#col-container { margin: 0 auto; max-width: 1100px; direction: rtl; text-align: right; background: white; padding: 30px; border-radius: 20px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
#badge-container { text-align: center; margin-bottom: 20px; height: 30px; }
|
| 530 |
#user-sub-badge { padding: 6px 16px; border-radius: 20px; font-size: 0.9em; color: white; display: none; }
|
| 531 |
.primary-btn { width: 100%; margin-top: 15px; background: linear-gradient(135deg, #FF4500, #FF8533); color: white; border: none; }
|
|
@@ -545,7 +586,10 @@ with gr.Blocks(theme=orange_red_theme) as demo:
|
|
| 545 |
|
| 546 |
with gr.Column(elem_id="col-container"):
|
| 547 |
gr.Markdown("# **ویرایشگر ترکیبی هوشمند (Fusion)**", elem_id="main-title")
|
| 548 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 549 |
gr.HTML('<div id="badge-container"><span id="user-sub-badge"></span></div>')
|
| 550 |
|
| 551 |
with gr.Row(equal_height=True):
|
|
@@ -567,6 +611,23 @@ with gr.Blocks(theme=orange_red_theme) as demo:
|
|
| 567 |
upgrade_button = gr.Button("💎 ارتقا به نسخه نامحدود", variant="primary", elem_classes="upgrade-btn", elem_id="upgrade-btn", visible=False)
|
| 568 |
|
| 569 |
with gr.Accordion("تنظیمات پیشرفته", open=False, visible=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 571 |
randomize_seed = gr.Checkbox(label="تصادفی", value=True)
|
| 572 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
|
@@ -588,8 +649,11 @@ with gr.Blocks(theme=orange_red_theme) as demo:
|
|
| 588 |
["examples/M1.jpg", "examples/M2.jpg", "نورپردازی تصویر اول را حذف کن و بر اساس نور و رنگ تصویر دوم آن را مجدداً نورپردازی کن.", "انتقال نور (Light-Migration)"],
|
| 589 |
["examples/Cloth2.jpg", "examples/Design2.png", "این طرح را روی پیراهن قرار بده.", "طراحی روی لباس (Cloth-Design)"],
|
| 590 |
["examples/Cup1.png", "examples/Wood1.png", "بافت چوب را روی ماگ اعمال کن.", "ویرایش بافت (Texture Edit)"],
|
|
|
|
| 591 |
["examples/F3.jpg", "examples/F4.jpg", "عینک او را با عینک جدید از تصویر ۱ جایگزین کن.", "ترکیب پیشرفته (Super-Fusion)"],
|
| 592 |
["examples/Chair.jpg", "examples/Material.jpg", "متریال تصویر ۱ را دقیقاً شبیه به مرجع تصویر ۲ کن.", "انتقال متریال (Material-Transfer)"],
|
|
|
|
|
|
|
| 593 |
["examples/Cat1.jpg", "examples/Glass1.webp", "یک گربه که عینک تصویر دوم را زده است.", "ترکیب اشیاء (Fuse-Objects)"],
|
| 594 |
],
|
| 595 |
inputs=[image_1, image_2, prompt, lora_adapter],
|
|
@@ -599,12 +663,24 @@ with gr.Blocks(theme=orange_red_theme) as demo:
|
|
| 599 |
label="نمونهها"
|
| 600 |
)
|
| 601 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
# اتصال رویدادها
|
| 603 |
fingerprint_box.change(fn=check_initial_quota, inputs=[fingerprint_box, status_box_input], outputs=[run_button, upgrade_button, status_box])
|
| 604 |
|
| 605 |
run_button.click(
|
| 606 |
fn=infer,
|
| 607 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
outputs=[output_image, seed, status_box, run_button, upgrade_button]
|
| 609 |
)
|
| 610 |
|
|
|
|
| 236 |
"انتقال متریال (Material-Transfer)": "Material-Transfer"
|
| 237 |
}
|
| 238 |
|
| 239 |
+
ASPECT_RATIOS_LIST = [
|
| 240 |
+
"خودکار (پیشفرض)",
|
| 241 |
+
"۱:۱ (مربع - 1024x1024)",
|
| 242 |
+
"۱۶:۹ (افقی - 1344x768)",
|
| 243 |
+
"۹:۱۶ (عمودی - 768x1344)",
|
| 244 |
+
"شخصیسازی (Custom)"
|
| 245 |
+
]
|
| 246 |
+
|
| 247 |
+
ASPECT_RATIOS_MAP = {
|
| 248 |
+
"خودکار (پیشفرض)": "Auto",
|
| 249 |
+
"۱:۱ (مربع - 1024x1024)": (1024, 1024),
|
| 250 |
+
"۱۶:۹ (افقی - 1344x768)": (1344, 768),
|
| 251 |
+
"۹:۱۶ (عمودی - 768x1344)": (768, 1344),
|
| 252 |
+
"شخصیسازی (Custom)": "Custom"
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
def update_dimensions_on_upload(image):
|
| 256 |
if image is None: return 1024, 1024
|
| 257 |
original_width, original_height = image.size
|
|
|
|
| 267 |
new_height = (new_height // 16) * 16
|
| 268 |
return new_width, new_height
|
| 269 |
|
| 270 |
+
def update_sliders_visibility(choice):
|
| 271 |
+
if choice == "شخصیسازی (Custom)":
|
| 272 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 273 |
+
else:
|
| 274 |
+
return gr.update(visible=False), gr.update(visible=False)
|
| 275 |
+
|
| 276 |
# ==========================================
|
| 277 |
# 5. HTML Helpers
|
| 278 |
# ==========================================
|
|
|
|
| 312 |
randomize_seed,
|
| 313 |
guidance_scale,
|
| 314 |
steps,
|
| 315 |
+
aspect_ratio_selection,
|
| 316 |
+
custom_width,
|
| 317 |
+
custom_height,
|
| 318 |
fingerprint,
|
| 319 |
subscription_status,
|
| 320 |
progress=gr.Progress(track_tqdm=True)
|
|
|
|
| 377 |
img1_pil = image_1.convert("RGB")
|
| 378 |
img2_pil = image_2.convert("RGB")
|
| 379 |
|
| 380 |
+
# 7. تنظیم ابعاد تصویر
|
| 381 |
+
selection_value = ASPECT_RATIOS_MAP.get(aspect_ratio_selection)
|
| 382 |
+
|
| 383 |
+
if selection_value == "Custom":
|
| 384 |
+
width = (int(custom_width) // 16) * 16
|
| 385 |
+
height = (int(custom_height) // 16) * 16
|
| 386 |
+
elif selection_value == "Auto" or selection_value is None:
|
| 387 |
+
width, height = update_dimensions_on_upload(img1_pil)
|
| 388 |
+
else:
|
| 389 |
+
width, height = selection_value
|
| 390 |
|
| 391 |
try:
|
| 392 |
result = pipe(
|
|
|
|
| 400 |
true_cfg_scale=guidance_scale,
|
| 401 |
).images[0]
|
| 402 |
|
| 403 |
+
# 8. بررسی ایمنی خروجی
|
| 404 |
if is_image_nsfw(result):
|
| 405 |
return None, seed, get_error_html("تصویر خروجی حاوی محتوای نامناسب بود."), gr.update(visible=True), gr.update(visible=False)
|
| 406 |
|
| 407 |
+
# 9. پیام موفقیت
|
| 408 |
user_record = get_user_record(fingerprint)
|
| 409 |
remaining = USAGE_LIMIT - user_record["count"] if user_record else 0
|
| 410 |
success_msg = "تصویر با موفقیت ترکیب شد."
|
|
|
|
| 424 |
|
| 425 |
@spaces.GPU(duration=30)
|
| 426 |
def infer_example(image_1, image_2, prompt, lora_adapter):
|
| 427 |
+
# مثالها: ابعاد خودکار، اعتبار رایگان
|
| 428 |
+
res, s, status, btn1, btn2 = infer(image_1, image_2, prompt, lora_adapter, 0, True, 1.0, 4, "خودکار (پیشفرض)", 1024, 1024, "example", "paid")
|
| 429 |
return res, s
|
| 430 |
|
| 431 |
# ==========================================
|
|
|
|
| 561 |
:root, body, .gradio-container { font-family: 'Vazirmatn', sans-serif !important; }
|
| 562 |
#fingerprint_storage, #status_storage { display: none !important; }
|
| 563 |
#col-container { margin: 0 auto; max-width: 1100px; direction: rtl; text-align: right; background: white; padding: 30px; border-radius: 20px; }
|
| 564 |
+
|
| 565 |
+
/* Centered Titles */
|
| 566 |
+
#main-title, #main-description {
|
| 567 |
+
text-align: center !important;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
#badge-container { text-align: center; margin-bottom: 20px; height: 30px; }
|
| 571 |
#user-sub-badge { padding: 6px 16px; border-radius: 20px; font-size: 0.9em; color: white; display: none; }
|
| 572 |
.primary-btn { width: 100%; margin-top: 15px; background: linear-gradient(135deg, #FF4500, #FF8533); color: white; border: none; }
|
|
|
|
| 586 |
|
| 587 |
with gr.Column(elem_id="col-container"):
|
| 588 |
gr.Markdown("# **ویرایشگر ترکیبی هوشمند (Fusion)**", elem_id="main-title")
|
| 589 |
+
gr.Markdown(
|
| 590 |
+
"ترکیب، انتقال بافت و ویرایش دو تصویر با هوش مصنوعی آلفا",
|
| 591 |
+
elem_id="main-description"
|
| 592 |
+
)
|
| 593 |
gr.HTML('<div id="badge-container"><span id="user-sub-badge"></span></div>')
|
| 594 |
|
| 595 |
with gr.Row(equal_height=True):
|
|
|
|
| 611 |
upgrade_button = gr.Button("💎 ارتقا به نسخه نامحدود", variant="primary", elem_classes="upgrade-btn", elem_id="upgrade-btn", visible=False)
|
| 612 |
|
| 613 |
with gr.Accordion("تنظیمات پیشرفته", open=False, visible=True):
|
| 614 |
+
aspect_ratio_selection = gr.Dropdown(
|
| 615 |
+
label="ابعاد تصویر خروجی",
|
| 616 |
+
choices=ASPECT_RATIOS_LIST,
|
| 617 |
+
value="خودکار (پیشفرض)",
|
| 618 |
+
interactive=True
|
| 619 |
+
)
|
| 620 |
+
|
| 621 |
+
with gr.Row(visible=False) as custom_dims_row:
|
| 622 |
+
custom_width = gr.Slider(
|
| 623 |
+
label="عرض دلخواه (Width)",
|
| 624 |
+
minimum=256, maximum=2048, step=16, value=1024
|
| 625 |
+
)
|
| 626 |
+
custom_height = gr.Slider(
|
| 627 |
+
label="ارتفاع دلخواه (Height)",
|
| 628 |
+
minimum=256, maximum=2048, step=16, value=1024
|
| 629 |
+
)
|
| 630 |
+
|
| 631 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 632 |
randomize_seed = gr.Checkbox(label="تصادفی", value=True)
|
| 633 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
|
|
|
| 649 |
["examples/M1.jpg", "examples/M2.jpg", "نورپردازی تصویر اول را حذف کن و بر اساس نور و رنگ تصویر دوم آن را مجدداً نورپردازی کن.", "انتقال نور (Light-Migration)"],
|
| 650 |
["examples/Cloth2.jpg", "examples/Design2.png", "این طرح را روی پیراهن قرار بده.", "طراحی روی لباس (Cloth-Design)"],
|
| 651 |
["examples/Cup1.png", "examples/Wood1.png", "بافت چوب را روی ماگ اعمال کن.", "ویرایش بافت (Texture Edit)"],
|
| 652 |
+
["examples/Cloth1.jpg", "examples/Design1.png", "این طرح را روی پیراهن قرار بده.", "طراحی روی لباس (Cloth-Design)"],
|
| 653 |
["examples/F3.jpg", "examples/F4.jpg", "عینک او را با عینک جدید از تصویر ۱ جایگزین کن.", "ترکیب پیشرفته (Super-Fusion)"],
|
| 654 |
["examples/Chair.jpg", "examples/Material.jpg", "متریال تصویر ۱ را دقیقاً شبیه به مرجع تصویر ۲ کن.", "انتقال متریال (Material-Transfer)"],
|
| 655 |
+
["examples/F1.jpg", "examples/F2.jpg", "بطری کوچک را روی میز قرار بده.", "ترکیب پیشرفته (Super-Fusion)"],
|
| 656 |
+
["examples/Mug1.jpg", "examples/Texture1.jpg", "طرح تصویر ۲ را روی ماگ اعمال کن.", "ویرایش بافت (Texture Edit)"],
|
| 657 |
["examples/Cat1.jpg", "examples/Glass1.webp", "یک گربه که عینک تصویر دوم را زده است.", "ترکیب اشیاء (Fuse-Objects)"],
|
| 658 |
],
|
| 659 |
inputs=[image_1, image_2, prompt, lora_adapter],
|
|
|
|
| 663 |
label="نمونهها"
|
| 664 |
)
|
| 665 |
|
| 666 |
+
# تابع نمایش/مخفی کردن اسلایدر ابعاد
|
| 667 |
+
def toggle_row(choice):
|
| 668 |
+
if choice == "شخصیسازی (Custom)":
|
| 669 |
+
return gr.update(visible=True)
|
| 670 |
+
return gr.update(visible=False)
|
| 671 |
+
|
| 672 |
+
aspect_ratio_selection.change(fn=toggle_row, inputs=aspect_ratio_selection, outputs=custom_dims_row)
|
| 673 |
+
|
| 674 |
# اتصال رویدادها
|
| 675 |
fingerprint_box.change(fn=check_initial_quota, inputs=[fingerprint_box, status_box_input], outputs=[run_button, upgrade_button, status_box])
|
| 676 |
|
| 677 |
run_button.click(
|
| 678 |
fn=infer,
|
| 679 |
+
inputs=[
|
| 680 |
+
image_1, image_2, prompt, lora_adapter, seed, randomize_seed,
|
| 681 |
+
guidance_scale, steps, aspect_ratio_selection, custom_width, custom_height,
|
| 682 |
+
fingerprint_box, status_box_input
|
| 683 |
+
],
|
| 684 |
outputs=[output_image, seed, status_box, run_button, upgrade_button]
|
| 685 |
)
|
| 686 |
|