Spaces:
Running on Zero
Running on Zero
no@email.com commited on
Commit ·
8543b90
1
Parent(s): 0c0dd43
add password for generation
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
|
@@ -28,6 +29,7 @@ MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"
|
|
| 28 |
DATASET_REPO_ID = os.environ.get("DATASET_REPO_ID")
|
| 29 |
PRIVATE_MODEL_KEY = os.environ.get("PRIVATE_MODEL_KEY")
|
| 30 |
hf_api = HfApi(token=key) if key and DATASET_REPO_ID else None
|
|
|
|
| 31 |
|
| 32 |
MAX_DIM = 832
|
| 33 |
MIN_DIM = 480
|
|
@@ -247,6 +249,7 @@ def get_duration(
|
|
| 247 |
guidance_scale_2,
|
| 248 |
seed,
|
| 249 |
randomize_seed,
|
|
|
|
| 250 |
request,
|
| 251 |
progress,
|
| 252 |
):
|
|
@@ -325,6 +328,39 @@ def export_to_video_h264(frames, output_path: str, fps: int = FIXED_FPS) -> None
|
|
| 325 |
writer.append_data(frame)
|
| 326 |
|
| 327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
@spaces.GPU(duration=get_duration)
|
| 329 |
def generate_video(
|
| 330 |
input_image,
|
|
@@ -336,6 +372,7 @@ def generate_video(
|
|
| 336 |
guidance_scale_2 = 1,
|
| 337 |
seed = 7777777,
|
| 338 |
randomize_seed = False,
|
|
|
|
| 339 |
request: gr.Request = None,
|
| 340 |
progress=gr.Progress(track_tqdm=True),
|
| 341 |
):
|
|
@@ -379,6 +416,9 @@ def generate_video(
|
|
| 379 |
- The function uses GPU acceleration via the @spaces.GPU decorator
|
| 380 |
- Generation time varies based on steps and duration (see get_duration function)
|
| 381 |
"""
|
|
|
|
|
|
|
|
|
|
| 382 |
if input_image is None:
|
| 383 |
raise gr.Error("Please upload an input image.")
|
| 384 |
|
|
@@ -438,6 +478,14 @@ with gr.Blocks() as demo:
|
|
| 438 |
with gr.Row():
|
| 439 |
with gr.Column():
|
| 440 |
input_image_component = gr.Image(type="filepath", label="Input Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v, lines=3)
|
| 442 |
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 443 |
|
|
@@ -445,20 +493,30 @@ with gr.Blocks() as demo:
|
|
| 445 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 446 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=7777777, interactive=True)
|
| 447 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 448 |
-
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
|
| 449 |
-
guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage")
|
| 450 |
-
guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
|
| 451 |
|
| 452 |
-
generate_button = gr.Button("
|
| 453 |
with gr.Column():
|
| 454 |
video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], interactive=False, elem_id="generated-video")
|
| 455 |
|
| 456 |
ui_inputs = [
|
| 457 |
input_image_component, prompt_input, steps_slider,
|
| 458 |
negative_prompt_input, duration_seconds_input,
|
| 459 |
-
guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox
|
|
|
|
| 460 |
]
|
| 461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
gr.Examples(
|
| 464 |
examples=[
|
|
@@ -498,10 +556,7 @@ with gr.Blocks() as demo:
|
|
| 498 |
6,
|
| 499 |
],
|
| 500 |
],
|
| 501 |
-
inputs=[input_image_component, prompt_input, steps_slider],
|
| 502 |
-
outputs=[video_output, seed_input],
|
| 503 |
-
fn=generate_video,
|
| 504 |
-
cache_examples="lazy"
|
| 505 |
)
|
| 506 |
gr.Markdown("*Content Warning:* This research demonstration is capable of generating content that may not be suitable for all audiences. By using this application, you agree to assume full responsibility for your compliance with the platform's terms of service and any applicable legal and regulatory guidelines.")
|
| 507 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import hmac
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
|
|
|
| 29 |
DATASET_REPO_ID = os.environ.get("DATASET_REPO_ID")
|
| 30 |
PRIVATE_MODEL_KEY = os.environ.get("PRIVATE_MODEL_KEY")
|
| 31 |
hf_api = HfApi(token=key) if key and DATASET_REPO_ID else None
|
| 32 |
+
GENERATION_SECRET=os.environ.get("GENERATION_SECRET")
|
| 33 |
|
| 34 |
MAX_DIM = 832
|
| 35 |
MIN_DIM = 480
|
|
|
|
| 249 |
guidance_scale_2,
|
| 250 |
seed,
|
| 251 |
randomize_seed,
|
| 252 |
+
access_granted,
|
| 253 |
request,
|
| 254 |
progress,
|
| 255 |
):
|
|
|
|
| 328 |
writer.append_data(frame)
|
| 329 |
|
| 330 |
|
| 331 |
+
def reset_verification():
|
| 332 |
+
return (
|
| 333 |
+
gr.update(value="Verify Passcode to Enable Generation", interactive=False),
|
| 334 |
+
"Enter the passcode and click Verify to unlock generation.",
|
| 335 |
+
False,
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
def verify_passcode(passcode: str):
|
| 340 |
+
secret = GENERATION_SECRET or ""
|
| 341 |
+
candidate = (passcode or "").strip()
|
| 342 |
+
|
| 343 |
+
if not secret:
|
| 344 |
+
return (
|
| 345 |
+
gr.update(value="Verify Passcode to Enable Generation", interactive=False),
|
| 346 |
+
"Generation is unavailable because `GENERATION_SECRET` is not configured.",
|
| 347 |
+
False,
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
if hmac.compare_digest(candidate, secret):
|
| 351 |
+
return (
|
| 352 |
+
gr.update(value="Generate Video", interactive=True),
|
| 353 |
+
"Passcode verified. Generation unlocked.",
|
| 354 |
+
True,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
return (
|
| 358 |
+
gr.update(value="Verify Passcode to Enable Generation", interactive=False),
|
| 359 |
+
"Incorrect passcode.",
|
| 360 |
+
False,
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
@spaces.GPU(duration=get_duration)
|
| 365 |
def generate_video(
|
| 366 |
input_image,
|
|
|
|
| 372 |
guidance_scale_2 = 1,
|
| 373 |
seed = 7777777,
|
| 374 |
randomize_seed = False,
|
| 375 |
+
access_granted = False,
|
| 376 |
request: gr.Request = None,
|
| 377 |
progress=gr.Progress(track_tqdm=True),
|
| 378 |
):
|
|
|
|
| 416 |
- The function uses GPU acceleration via the @spaces.GPU decorator
|
| 417 |
- Generation time varies based on steps and duration (see get_duration function)
|
| 418 |
"""
|
| 419 |
+
if not access_granted:
|
| 420 |
+
raise gr.Error("Please verify the passcode before generating.")
|
| 421 |
+
|
| 422 |
if input_image is None:
|
| 423 |
raise gr.Error("Please upload an input image.")
|
| 424 |
|
|
|
|
| 478 |
with gr.Row():
|
| 479 |
with gr.Column():
|
| 480 |
input_image_component = gr.Image(type="filepath", label="Input Image")
|
| 481 |
+
verification_state = gr.State(False)
|
| 482 |
+
passcode_input = gr.Textbox(
|
| 483 |
+
label="Passcode",
|
| 484 |
+
type="password",
|
| 485 |
+
placeholder="Enter passcode to unlock generation",
|
| 486 |
+
)
|
| 487 |
+
verify_button = gr.Button("Verify", variant="secondary")
|
| 488 |
+
verification_status = gr.Markdown("Enter the passcode and click Verify to unlock generation.")
|
| 489 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v, lines=3)
|
| 490 |
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 491 |
|
|
|
|
| 493 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 494 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=7777777, interactive=True)
|
| 495 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 496 |
+
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps", interactive=True)
|
| 497 |
+
guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage", interactive=True)
|
| 498 |
+
guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage", interactive=True)
|
| 499 |
|
| 500 |
+
generate_button = gr.Button("Verify Passcode to Enable Generation", variant="primary", interactive=False)
|
| 501 |
with gr.Column():
|
| 502 |
video_output = gr.Video(label="Generated Video", autoplay=True, sources=["upload"], interactive=False, elem_id="generated-video")
|
| 503 |
|
| 504 |
ui_inputs = [
|
| 505 |
input_image_component, prompt_input, steps_slider,
|
| 506 |
negative_prompt_input, duration_seconds_input,
|
| 507 |
+
guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox,
|
| 508 |
+
verification_state,
|
| 509 |
]
|
| 510 |
+
passcode_input.change(
|
| 511 |
+
fn=reset_verification,
|
| 512 |
+
outputs=[generate_button, verification_status, verification_state],
|
| 513 |
+
)
|
| 514 |
+
verify_button.click(
|
| 515 |
+
fn=verify_passcode,
|
| 516 |
+
inputs=[passcode_input],
|
| 517 |
+
outputs=[generate_button, verification_status, verification_state],
|
| 518 |
+
)
|
| 519 |
+
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 520 |
|
| 521 |
gr.Examples(
|
| 522 |
examples=[
|
|
|
|
| 556 |
6,
|
| 557 |
],
|
| 558 |
],
|
| 559 |
+
inputs=[input_image_component, prompt_input, steps_slider],
|
|
|
|
|
|
|
|
|
|
| 560 |
)
|
| 561 |
gr.Markdown("*Content Warning:* This research demonstration is capable of generating content that may not be suitable for all audiences. By using this application, you agree to assume full responsibility for your compliance with the platform's terms of service and any applicable legal and regulatory guidelines.")
|
| 562 |
|