Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
#23
by
ziffir
- opened
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
from dataclasses import dataclass
|
| 3 |
import json
|
|
@@ -31,10 +32,9 @@ ATTENTION_BACKEND = os.environ.get("ATTENTION_BACKEND", "flash_3")
|
|
| 31 |
UNSAFE_MAX_NEW_TOKEN = int(os.environ.get("UNSAFE_MAX_NEW_TOKEN", "10"))
|
| 32 |
DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY")
|
| 33 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 34 |
-
UNSAFE_PROMPT_CHECK =
|
| 35 |
# =============================================================================
|
| 36 |
|
| 37 |
-
|
| 38 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 39 |
warnings.filterwarnings("ignore")
|
| 40 |
logging.getLogger("transformers").setLevel(logging.ERROR)
|
|
@@ -186,15 +186,15 @@ def load_models(model_path, enable_compile=False, attention_backend="native"):
|
|
| 186 |
|
| 187 |
pipe.to("cuda", torch.bfloat16)
|
| 188 |
|
| 189 |
-
|
| 190 |
-
from
|
| 191 |
-
|
| 192 |
-
safety_model_id = "CompVis/stable-diffusion-safety-checker"
|
| 193 |
-
safety_feature_extractor = CLIPImageProcessor.from_pretrained(safety_model_id)
|
| 194 |
-
safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id, torch_dtype=torch.float16).to("cuda")
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
pipe.safety_feature_extractor = safety_feature_extractor
|
| 197 |
-
pipe.safety_checker = safety_checker
|
| 198 |
return pipe
|
| 199 |
|
| 200 |
|
|
@@ -399,7 +399,7 @@ def generate(
|
|
| 399 |
shift=3.0,
|
| 400 |
random_seed=True,
|
| 401 |
gallery_images=None,
|
| 402 |
-
enhance=
|
| 403 |
progress=gr.Progress(track_tqdm=True),
|
| 404 |
):
|
| 405 |
"""
|
|
@@ -417,7 +417,7 @@ def generate(
|
|
| 417 |
shift (float): Time shift parameter for the flow matching scheduler
|
| 418 |
random_seed (bool): Whether to generate a new random seed, if True will ignore the seed input
|
| 419 |
gallery_images (list): List of previously generated images to append to (only needed for the Gradio UI)
|
| 420 |
-
enhance (bool):
|
| 421 |
progress (gr.Progress): Gradio progress tracker for displaying generation progress (only needed for the Gradio UI)
|
| 422 |
|
| 423 |
Returns:
|
|
@@ -432,28 +432,26 @@ def generate(
|
|
| 432 |
else:
|
| 433 |
new_seed = seed if seed != -1 else random.randint(1, 1000000)
|
| 434 |
|
| 435 |
-
class UnsafeContentError(Exception):
|
| 436 |
-
pass
|
| 437 |
-
|
| 438 |
try:
|
| 439 |
if pipe is None:
|
| 440 |
raise gr.Error("Model not loaded.")
|
| 441 |
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
|
|
|
| 451 |
|
| 452 |
final_prompt = prompt
|
| 453 |
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
|
| 458 |
try:
|
| 459 |
resolution_str = resolution.split(" ")[0]
|
|
@@ -470,19 +468,21 @@ def generate(
|
|
| 470 |
shift=shift,
|
| 471 |
)
|
| 472 |
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
has_nsfw_concept =
|
| 476 |
-
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
-
except
|
| 480 |
-
|
| 481 |
|
| 482 |
if gallery_images is None:
|
| 483 |
gallery_images = []
|
| 484 |
-
#
|
| 485 |
-
gallery_images = [image] + gallery_images # latest output to be at the top of the list
|
| 486 |
|
| 487 |
return gallery_images, str(new_seed), int(new_seed)
|
| 488 |
|
|
@@ -494,87 +494,62 @@ init_app()
|
|
| 494 |
pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
|
| 495 |
spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
|
| 496 |
|
| 497 |
-
with gr.Blocks(title="Z-Image Demo") as demo:
|
| 498 |
gr.Markdown(
|
| 499 |
"""<div align="center">
|
| 500 |
|
| 501 |
-
# Z-Image Generation Demo
|
| 502 |
|
| 503 |
[](https://github.com/Tongyi-MAI/Z-Image)
|
| 504 |
|
| 505 |
*An Efficient Image Generation Foundation Model with Single-Stream Diffusion Transformer*
|
| 506 |
|
|
|
|
|
|
|
| 507 |
</div>"""
|
| 508 |
)
|
| 509 |
|
| 510 |
with gr.Row():
|
| 511 |
with gr.Column(scale=1):
|
| 512 |
-
prompt_input = gr.Textbox(label="Prompt", lines=3, placeholder="Enter your prompt here...")
|
| 513 |
-
# PE components (
|
| 514 |
-
# with gr.Row():
|
| 515 |
-
# enable_enhance = gr.Checkbox(label="Enhance Prompt (DashScope)", value=False)
|
| 516 |
-
# enhance_btn = gr.Button("Enhance Only")
|
| 517 |
|
| 518 |
with gr.Row():
|
| 519 |
-
|
| 520 |
-
res_cat = gr.Dropdown(value=1024, choices=choices, label="Resolution Category")
|
| 521 |
-
|
| 522 |
-
initial_res_choices = RES_CHOICES["1024"]
|
| 523 |
-
resolution = gr.Dropdown(
|
| 524 |
-
value=initial_res_choices[0], choices=RESOLUTION_SET, label="Width x Height (Ratio)"
|
| 525 |
-
)
|
| 526 |
-
|
| 527 |
-
with gr.Row():
|
| 528 |
-
seed = gr.Number(label="Seed", value=42, precision=0)
|
| 529 |
-
random_seed = gr.Checkbox(label="Random Seed", value=True)
|
| 530 |
-
|
| 531 |
-
with gr.Row():
|
| 532 |
-
steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=8, step=1, interactive=False)
|
| 533 |
-
shift = gr.Slider(label="Time Shift", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
|
| 534 |
-
|
| 535 |
-
generate_btn = gr.Button("Generate", variant="primary")
|
| 536 |
-
|
| 537 |
-
# Example prompts
|
| 538 |
-
gr.Markdown("### 📝 Example Prompts")
|
| 539 |
-
gr.Examples(examples=EXAMPLE_PROMPTS, inputs=prompt_input, label=None)
|
| 540 |
|
|
|
|
| 541 |
with gr.Column(scale=1):
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
height=600,
|
| 547 |
-
object_fit="contain",
|
| 548 |
-
format="png",
|
| 549 |
-
interactive=False,
|
| 550 |
-
)
|
| 551 |
-
used_seed = gr.Textbox(label="Seed Used", interactive=False)
|
| 552 |
|
| 553 |
-
|
| 554 |
-
if str(_res_cat) in RES_CHOICES:
|
| 555 |
-
res_choices = RES_CHOICES[str(_res_cat)]
|
| 556 |
-
else:
|
| 557 |
-
res_choices = RES_CHOICES["1024"]
|
| 558 |
-
return gr.update(value=res_choices[0], choices=res_choices)
|
| 559 |
|
| 560 |
-
|
| 561 |
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
|
| 569 |
generate_btn.click(
|
| 570 |
-
generate,
|
| 571 |
-
inputs=[
|
| 572 |
-
|
| 573 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
)
|
| 575 |
|
| 576 |
-
css = """
|
| 577 |
-
.fillable{max-width: 1230px !important}
|
| 578 |
-
"""
|
| 579 |
if __name__ == "__main__":
|
| 580 |
-
demo.launch(
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
import spaces
|
| 3 |
from dataclasses import dataclass
|
| 4 |
import json
|
|
|
|
| 32 |
UNSAFE_MAX_NEW_TOKEN = int(os.environ.get("UNSAFE_MAX_NEW_TOKEN", "10"))
|
| 33 |
DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY")
|
| 34 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 35 |
+
UNSAFE_PROMPT_CHECK = False # NSFW Prompt Kontrolü Tamamen Kapatıldı
|
| 36 |
# =============================================================================
|
| 37 |
|
|
|
|
| 38 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 39 |
warnings.filterwarnings("ignore")
|
| 40 |
logging.getLogger("transformers").setLevel(logging.ERROR)
|
|
|
|
| 186 |
|
| 187 |
pipe.to("cuda", torch.bfloat16)
|
| 188 |
|
| 189 |
+
# Safety Checker TAMAMEN KALDIRILDI - NSFW Serbest
|
| 190 |
+
# from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
| 191 |
+
# from transformers import CLIPImageProcessor
|
| 192 |
+
# safety_model_id = "CompVis/stable-diffusion-safety-checker"
|
| 193 |
+
# safety_feature_extractor = CLIPImageProcessor.from_pretrained(safety_model_id)
|
| 194 |
+
# safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id, torch_dtype=torch.float16).to("cuda")
|
| 195 |
+
# pipe.safety_feature_extractor = safety_feature_extractor
|
| 196 |
+
# pipe.safety_checker = safety_checker
|
| 197 |
|
|
|
|
|
|
|
| 198 |
return pipe
|
| 199 |
|
| 200 |
|
|
|
|
| 399 |
shift=3.0,
|
| 400 |
random_seed=True,
|
| 401 |
gallery_images=None,
|
| 402 |
+
enhance=True, # Her Zaman Aktif - Jailbreak Enhancer
|
| 403 |
progress=gr.Progress(track_tqdm=True),
|
| 404 |
):
|
| 405 |
"""
|
|
|
|
| 417 |
shift (float): Time shift parameter for the flow matching scheduler
|
| 418 |
random_seed (bool): Whether to generate a new random seed, if True will ignore the seed input
|
| 419 |
gallery_images (list): List of previously generated images to append to (only needed for the Gradio UI)
|
| 420 |
+
enhance (bool): Whether to enhance the prompt (HER ZAMAN AKTIF)
|
| 421 |
progress (gr.Progress): Gradio progress tracker for displaying generation progress (only needed for the Gradio UI)
|
| 422 |
|
| 423 |
Returns:
|
|
|
|
| 432 |
else:
|
| 433 |
new_seed = seed if seed != -1 else random.randint(1, 1000000)
|
| 434 |
|
|
|
|
|
|
|
|
|
|
| 435 |
try:
|
| 436 |
if pipe is None:
|
| 437 |
raise gr.Error("Model not loaded.")
|
| 438 |
|
| 439 |
+
# NSFW Prompt Kontrolü TAMAMEN KALDIRILDI
|
| 440 |
+
# has_unsafe_concept = is_unsafe_prompt(
|
| 441 |
+
# pipe.text_encoder,
|
| 442 |
+
# pipe.tokenizer,
|
| 443 |
+
# system_prompt=UNSAFE_PROMPT_CHECK,
|
| 444 |
+
# user_prompt=prompt,
|
| 445 |
+
# max_new_token=UNSAFE_MAX_NEW_TOKEN,
|
| 446 |
+
# )
|
| 447 |
+
# if has_unsafe_concept:
|
| 448 |
+
# raise UnsafeContentError("Input unsafe")
|
| 449 |
|
| 450 |
final_prompt = prompt
|
| 451 |
|
| 452 |
+
# Enhancer'ı HER ZAMAN AKTIF ET (Jailbreak için ideal)
|
| 453 |
+
final_prompt, _ = prompt_enhance(prompt, True)
|
| 454 |
+
print(f"Enhanced prompt: {final_prompt}")
|
| 455 |
|
| 456 |
try:
|
| 457 |
resolution_str = resolution.split(" ")[0]
|
|
|
|
| 468 |
shift=shift,
|
| 469 |
)
|
| 470 |
|
| 471 |
+
# NSFW Output Checker TAMAMEN KALDIRILDI
|
| 472 |
+
# safety_checker_input = pipe.safety_feature_extractor([image], return_tensors="pt").pixel_values.cuda()
|
| 473 |
+
# _, has_nsfw_concept = pipe.safety_checker(images=[torch.zeros(1)], clip_input=safety_checker_input)
|
| 474 |
+
# has_nsfw_concept = has_nsfw_concept[0]
|
| 475 |
+
# if has_nsfw_concept:
|
| 476 |
+
# raise UnsafeContentError("input unsafe")
|
| 477 |
+
|
| 478 |
+
# Fallback NSFW Image KALDIRILDI - Direkt image kullan
|
| 479 |
|
| 480 |
+
except Exception as e:
|
| 481 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 482 |
|
| 483 |
if gallery_images is None:
|
| 484 |
gallery_images = []
|
| 485 |
+
gallery_images = [image] + gallery_images # Latest first
|
|
|
|
| 486 |
|
| 487 |
return gallery_images, str(new_seed), int(new_seed)
|
| 488 |
|
|
|
|
| 494 |
pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
|
| 495 |
spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
|
| 496 |
|
| 497 |
+
with gr.Blocks(title="Z-Image Demo - Uncensored") as demo:
|
| 498 |
gr.Markdown(
|
| 499 |
"""<div align="center">
|
| 500 |
|
| 501 |
+
# Z-Image Generation Demo - Uncensored Edition
|
| 502 |
|
| 503 |
[](https://github.com/Tongyi-MAI/Z-Image)
|
| 504 |
|
| 505 |
*An Efficient Image Generation Foundation Model with Single-Stream Diffusion Transformer*
|
| 506 |
|
| 507 |
+
**NSFW Serbest - Enhancer Her Zaman Aktif**
|
| 508 |
+
|
| 509 |
</div>"""
|
| 510 |
)
|
| 511 |
|
| 512 |
with gr.Row():
|
| 513 |
with gr.Column(scale=1):
|
| 514 |
+
prompt_input = gr.Textbox(label="Prompt", lines=3, placeholder="Enter your prompt here... (NSFW OK!)")
|
| 515 |
+
# PE components (Her zaman aktif, UI'dan kaldırıldı)
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
with gr.Row():
|
| 518 |
+
resolution_dropdown = gr.Dropdown(choices=RESOLUTION_SET, value="1024x1024 ( 1:1 )", label="Resolution")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
|
| 520 |
+
with gr.Row():
|
| 521 |
with gr.Column(scale=1):
|
| 522 |
+
seed_input = gr.Number(value=42, label="Seed", precision=0)
|
| 523 |
+
random_seed_cb = gr.Checkbox(label="Random Seed", value=False)
|
| 524 |
+
steps_slider = gr.Slider(minimum=1, maximum=50, value=9, step=1, label="Steps")
|
| 525 |
+
shift_slider = gr.Slider(minimum=1.0, maximum=5.0, value=3.0, step=0.1, label="Shift")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
+
generate_btn = gr.Button("Generate", variant="primary", scale=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
+
output_gallery = gr.Gallery(label="Generated Images", show_label=True)
|
| 530 |
|
| 531 |
+
examples = gr.Examples(
|
| 532 |
+
examples=EXAMPLE_PROMPTS,
|
| 533 |
+
inputs=[prompt_input],
|
| 534 |
+
label="Examples",
|
| 535 |
+
cache_examples=False,
|
| 536 |
+
)
|
| 537 |
|
| 538 |
generate_btn.click(
|
| 539 |
+
fn=generate,
|
| 540 |
+
inputs=[
|
| 541 |
+
prompt_input,
|
| 542 |
+
resolution_dropdown,
|
| 543 |
+
seed_input,
|
| 544 |
+
steps_slider,
|
| 545 |
+
shift_slider,
|
| 546 |
+
random_seed_cb,
|
| 547 |
+
output_gallery,
|
| 548 |
+
gr.State(True), # enhance her zaman True
|
| 549 |
+
],
|
| 550 |
+
outputs=[output_gallery, seed_input, seed_input],
|
| 551 |
)
|
| 552 |
|
|
|
|
|
|
|
|
|
|
| 553 |
if __name__ == "__main__":
|
| 554 |
+
demo.launch(share=True, debug=True)
|
| 555 |
+
```
|