Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
import uuid
|
| 6 |
+
from huggingface_hub import snapshot_download
|
| 7 |
+
|
| 8 |
+
# --- 1. Environment Setup: Download the required base model ---
|
| 9 |
+
# This runs only once when the Space starts.
|
| 10 |
+
# The Ditto models are already in the repo, but the base model is separate.
|
| 11 |
+
print("Downloading base model 'Wan-AI/Wan2.1-VACE-14B'...")
|
| 12 |
+
try:
|
| 13 |
+
snapshot_download(
|
| 14 |
+
repo_id="Wan-AI/Wan2.1-VACE-14B",
|
| 15 |
+
local_dir="models/Wan-AI/Wan2.1-VACE-14B",
|
| 16 |
+
local_dir_use_symlinks=False # Use full downloads on Spaces
|
| 17 |
+
)
|
| 18 |
+
print("Base model downloaded successfully.")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"ERROR: Failed to download base model. The app may not work. Error: {e}")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# --- 2. The Core Inference Function ---
|
| 24 |
+
# This function wraps the command-line script provided in the model card.
|
| 25 |
+
def run_video_edit(input_video_path, prompt_text):
|
| 26 |
+
if not input_video_path:
|
| 27 |
+
raise gr.Error("You must upload an input video.")
|
| 28 |
+
if not prompt_text or not prompt_text.strip():
|
| 29 |
+
raise gr.Error("You must provide an editing instruction.")
|
| 30 |
+
|
| 31 |
+
print(f"Starting video edit process for: {input_video_path}")
|
| 32 |
+
print(f"Instruction: {prompt_text}")
|
| 33 |
+
|
| 34 |
+
# Define paths for the script and the specific LoRA model to use
|
| 35 |
+
inference_script_path = "inference/infer_ditto.py"
|
| 36 |
+
lora_model_path = "models/lora/Editto-XL.safetensors" # Using the main XL model
|
| 37 |
+
|
| 38 |
+
# Create a unique path for the output video in a temporary directory
|
| 39 |
+
output_filename = f"{uuid.uuid4()}.mp4"
|
| 40 |
+
output_video_path = os.path.join("/tmp", output_filename)
|
| 41 |
+
|
| 42 |
+
# Construct the command as specified in the model card
|
| 43 |
+
command = [
|
| 44 |
+
sys.executable, # Use the current python interpreter
|
| 45 |
+
inference_script_path,
|
| 46 |
+
"--input_video", input_video_path,
|
| 47 |
+
"--output_video", output_video_path,
|
| 48 |
+
"--prompt", prompt_text,
|
| 49 |
+
"--lora_path", lora_model_path,
|
| 50 |
+
"--num_frames", "73", # Default from the model card's example
|
| 51 |
+
"--device_id", "0"
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
print(f"Executing command: {' '.join(command)}")
|
| 55 |
+
|
| 56 |
+
# Run the subprocess and capture output for debugging
|
| 57 |
+
try:
|
| 58 |
+
process = subprocess.run(
|
| 59 |
+
command,
|
| 60 |
+
check=True,
|
| 61 |
+
capture_output=True,
|
| 62 |
+
text=True
|
| 63 |
+
)
|
| 64 |
+
print("Inference script stdout:")
|
| 65 |
+
print(process.stdout)
|
| 66 |
+
print("Inference script stderr:")
|
| 67 |
+
print(process.stderr)
|
| 68 |
+
except subprocess.CalledProcessError as e:
|
| 69 |
+
print("ERROR: The inference script failed.")
|
| 70 |
+
print("Return code:", e.returncode)
|
| 71 |
+
print("Stdout:", e.stdout)
|
| 72 |
+
print("Stderr:", e.stderr)
|
| 73 |
+
# Display the error to the user in the Gradio UI
|
| 74 |
+
raise gr.Error(f"The model script failed. Check the logs for details. Stderr: {e.stderr}")
|
| 75 |
+
|
| 76 |
+
if not os.path.exists(output_video_path):
|
| 77 |
+
raise gr.Error("Inference completed, but the output video file was not created. Check the logs.")
|
| 78 |
+
|
| 79 |
+
print(f"Process finished successfully. Output video at: {output_video_path}")
|
| 80 |
+
return output_video_path
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# --- 3. Build the Gradio User Interface ---
|
| 84 |
+
with gr.Blocks(css="#col-container {max-width: 780px; margin: auto;}") as demo:
|
| 85 |
+
with gr.Column(elem_id="col-container"):
|
| 86 |
+
gr.Markdown(
|
| 87 |
+
"""
|
| 88 |
+
# Ditto / Editto: Instruction-Based Video Editing
|
| 89 |
+
This demo uses the official inference script from the [QingyanBai/Ditto_models](https://huggingface.co/QingyanBai/Ditto_models) repository to edit videos.
|
| 90 |
+
Upload a video, provide a text instruction, and click "Edit Video".
|
| 91 |
+
**Note:** Running on a ZeroGPU, the first startup and each video process will take some time. Please be patient.
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
input_video = gr.Video(label="Input Video")
|
| 98 |
+
instruction = gr.Textbox(label="Editing Instruction", placeholder="e.g., make it snowing")
|
| 99 |
+
submit_btn = gr.Button("Edit Video", variant="primary")
|
| 100 |
+
with gr.Column():
|
| 101 |
+
output_video = gr.Video(label="Edited Video", interactive=False)
|
| 102 |
+
|
| 103 |
+
gr.Markdown("## Example Instructions")
|
| 104 |
+
gr.Examples(
|
| 105 |
+
examples=[
|
| 106 |
+
["change the background to a beach"],
|
| 107 |
+
["make it a cartoon"],
|
| 108 |
+
["add fireworks to the sky"],
|
| 109 |
+
["make it night"],
|
| 110 |
+
["turn it into a watercolor painting"]
|
| 111 |
+
],
|
| 112 |
+
inputs=[instruction],
|
| 113 |
+
label="Click an example to use it (you still need to upload a video)"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
submit_btn.click(
|
| 117 |
+
fn=run_video_edit,
|
| 118 |
+
inputs=[input_video, instruction],
|
| 119 |
+
outputs=[output_video]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
demo.launch()
|