Spaces:
Running
Running
File size: 9,798 Bytes
61723d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | import gradio as gr
import torch
import os
import gc
from typing import Optional, Tuple
from model import WanVideoGenerator, get_available_loras, LORA_CACHE_DIR
# Initialize the generator
generator = WanVideoGenerator()
def generate_video(
prompt: str,
negative_prompt: str,
image_input: Optional[dict],
lora_name: str,
lora_scale: float,
height: int,
width: int,
num_frames: int,
guidance_scale: float,
num_inference_steps: int,
fps: int,
seed: int,
progress: gr.Progress = gr.Progress()
) -> Tuple[str, str]:
"""Generate video from text or image prompt with optional LoRA."""
if not prompt.strip():
raise gr.Error("Please provide a prompt!")
progress(0, desc="Initializing...")
# Handle image input for TI2V (Text-Image-to-Video)
image = None
if image_input is not None:
image = image_input
try:
# Load LoRA if specified
if lora_name != "None":
progress(0.1, desc=f"Loading LoRA: {lora_name}...")
generator.load_lora(lora_name, lora_scale)
else:
generator.unload_lora()
progress(0.2, desc="Generating video...")
# Generate video
video_path = generator.generate(
prompt=prompt,
negative_prompt=negative_prompt,
image=image,
height=height,
width=width,
num_frames=num_frames,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
fps=fps,
seed=seed if seed >= 0 else None,
progress_callback=lambda p: progress(0.2 + 0.7 * p, desc="Generating frames...")
)
progress(1.0, desc="Complete!")
# Get generation info
info = f"""Generation Info:
- Prompt: {prompt}
- LoRA: {lora_name} (scale: {lora_scale})
- Resolution: {width}x{height}
- Frames: {num_frames}
- Steps: {num_inference_steps}
- Guidance: {guidance_scale}
- FPS: {fps}
- Seed: {seed if seed >= 0 else 'Random'}"""
return video_path, info
except Exception as e:
raise gr.Error(f"Generation failed: {str(e)}")
finally:
# Cleanup
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
def refresh_loras():
"""Refresh the list of available LoRAs."""
loras = get_available_loras()
return gr.Dropdown(choices=["None"] + loras, value="None")
# Custom CSS for professional look
custom_css = """
#header-text {
text-align: center;
margin-bottom: 20px;
}
#header-text h1 {
font-size: 2.5em;
font-weight: bold;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
#header-text p {
color: #666;
font-size: 1.1em;
}
.lora-section {
background: linear-gradient(135deg, #667eea22 0%, #764ba222 100%);
border-radius: 12px;
padding: 15px;
margin-bottom: 15px;
}
.generation-info {
font-family: monospace;
font-size: 0.9em;
background: #1a1a2e;
color: #eee;
padding: 15px;
border-radius: 8px;
}
"""
# Build the Gradio interface
with gr.Blocks() as demo:
# Header
with gr.Row():
with gr.Column():
gr.HTML("""
<div id="header-text">
<h1>π¬ Wan2.2-TI2V-5B Video Generator</h1>
<p>Generate stunning videos from text or image prompts with LoRA acceleration</p>
<p style="font-size: 0.9em;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea; text-decoration: none;">
Built with anycoder
</a>
</p>
</div>
""")
with gr.Row():
# Left column - Inputs
with gr.Column(scale=1):
# Prompt section
prompt = gr.Textbox(
label="Prompt",
placeholder="Describe the video you want to generate...",
lines=3,
info="Be descriptive for better results"
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="What to avoid in the video...",
lines=2,
value="blur, distortion, low quality, jittery, morphing"
)
# Image input for TI2V
with gr.Accordion("Image Input (TI2V)", open=False):
gr.Markdown("Upload an image for Text-Image-to-Video generation")
image_input = gr.Image(
label="Input Image",
type="pil",
height=300
)
# LoRA section
with gr.Group(elem_classes="lora-section"):
gr.Markdown("### π LoRA Acceleration")
with gr.Row():
lora_dropdown = gr.Dropdown(
choices=["None"] + get_available_loras(),
value="None",
label="Select LoRA",
info="Choose a LoRA for faster generation"
)
refresh_btn = gr.Button("π", size="sm", variant="secondary")
lora_scale = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.8,
step=0.05,
label="LoRA Scale",
info="Higher values = stronger LoRA effect"
)
gr.Markdown("""
**Available LoRAs:**
- `wan-fast-lora`: Optimized for speed (2-3x faster)
- `wan-quality-lora`: Enhanced quality
- `wan-motion-lora`: Better motion dynamics
""")
# Generation parameters
with gr.Accordion("βοΈ Advanced Settings", open=True):
with gr.Row():
height = gr.Slider(256, 1024, value=480, step=64, label="Height")
width = gr.Slider(256, 1024, value=848, step=64, label="Width")
with gr.Row():
num_frames = gr.Slider(8, 81, value=25, step=1, label="Number of Frames")
fps = gr.Slider(6, 30, value=16, step=1, label="FPS")
with gr.Row():
guidance_scale = gr.Slider(1.0, 20.0, value=5.0, step=0.5, label="Guidance Scale")
num_inference_steps = gr.Slider(4, 50, value=20, step=1, label="Inference Steps")
seed = gr.Slider(-1, 999999, value=-1, step=1, label="Seed (-1 for random)")
# Generate button
generate_btn = gr.Button("π¬ Generate Video", variant="primary", size="lg")
# Right column - Output
with gr.Column(scale=1):
video_output = gr.Video(
label="Generated Video",
height=450,
autoplay=True
)
info_output = gr.Textbox(
label="Generation Info",
lines=10,
elem_classes="generation-info",
show_copy_button=True
)
# Examples
with gr.Accordion("π Example Prompts", open=False):
gr.Examples(
examples=[
["A majestic dragon flying through clouds at sunset, cinematic lighting, 4K quality", None, "wan-fast-lora"],
["A serene ocean wave crashing on a beach, slow motion, golden hour", None, "wan-fast-lora"],
["A futuristic city with flying cars and neon lights, cyberpunk style", None, "wan-quality-lora"],
["A cat playing with a ball of yarn, cute, fluffy, soft lighting", None, "wan-fast-lora"],
["Time-lapse of flowers blooming in a garden, vibrant colors", None, "wan-motion-lora"],
],
inputs=[prompt, image_input, lora_dropdown],
label="Click to use example prompts"
)
# Footer
gr.HTML("""
<div style="text-align: center; margin-top: 20px; padding: 10px; border-top: 1px solid #ddd;">
<p style="color: #666; font-size: 0.9em;">
Model: <a href="https://huggingface.co/Wan-AI/Wan2.1-T2V-14B" target="_blank">Wan2.2-TI2V-5B</a> |
Powered by <a href="https://huggingface.co" target="_blank">π€ Hugging Face</a>
</p>
</div>
""")
# Event handlers
refresh_btn.click(refresh_loras, outputs=lora_dropdown)
generate_btn.click(
fn=generate_video,
inputs=[
prompt,
negative_prompt,
image_input,
lora_dropdown,
lora_scale,
height,
width,
num_frames,
guidance_scale,
num_inference_steps,
fps,
seed
],
outputs=[video_output, info_output],
api_visibility="public"
)
# Launch the app
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="purple",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="md",
spacing_size="md",
radius_size="lg"
),
css=custom_css,
footer_links=[
{"label": "API", "url": "/api"},
{"label": "Gradio", "url": "https://gradio.app"},
{"label": "anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
],
allowed_paths=[LORA_CACHE_DIR] if os.path.exists(LORA_CACHE_DIR) else []
) |