Spaces:
Paused
Paused
File size: 6,923 Bytes
1c0138b | 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 | import gradio as gr
import time
import numpy as np
from PIL import Image
import random
# Mock Pipeline Class to simulate the processing stages described
# In a real scenario, this would interface with Diffusers/PyTorch models
class VideoGenerationPipeline:
def __init__(self):
self.device = "cpu" # Mock device
def preprocess(self, image):
"""Simulate Stage 1: Upscaling and Preprocessing"""
time.sleep(1)
return image
def generate_sequence(self, image, steps):
"""Simulate Stage 2: Frame Generation"""
time.sleep(2)
return [image] * steps
def render_video(self, frames, fps):
"""Simulate Stage 3: Video Rendering"""
time.sleep(1)
# Return a dummy video path or placeholder
return "output_video.mp4"
# Initialize the mock pipeline
pipeline = VideoGenerationPipeline()
def process_video(
reference_image,
pace_slider,
motion_slider,
num_frames,
guidance_scale,
progress=gr.Progress()
):
"""
Main inference function simulating a complex multi-stage pipeline.
Uses gr.Progress to provide user feedback during long operations.
"""
if reference_image is None:
raise gr.Error("Please upload a reference image first.")
try:
# Stage 1: Preprocessing
progress(0.1, desc="Stage 1: Preprocessing & Upscaling...")
_ = pipeline.preprocess(reference_image)
# Stage 2: Sequence Generation
progress(0.4, desc="Stage 2: Generating Frame Sequence...")
# Simulate variable processing time based on complexity
time.sleep(1 + (motion_slider * 0.5))
# Stage 3: Choreography/Motion
progress(0.7, desc="Stage 3: Applying Motion & Choreography...")
_ = pipeline.generate_sequence(reference_image, num_frames)
# Stage 4: Final Render
progress(0.9, desc="Stage 4: Rendering Final Video...")
output_path = pipeline.render_video([], fps=25)
progress(1.0, desc="Complete!")
# In a real app, return the actual video file path
# Here we return the input as a placeholder for the demo
return reference_image
except Exception as e:
raise gr.Error(f"Pipeline failed: {str(e)}")
# Gradio 6 Application Structure
# CRITICAL: gr.Blocks() takes NO parameters in Gradio 6
with gr.Blocks() as demo:
# Header with required attribution
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1>Pro-Video Img2Vid Pipeline</h1>
<p>Advanced Image-to-Video Generation Workflow</p>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007bff; text-decoration: none;">Built with anycoder</a>
</div>
""")
# Layout: Sidebar for controls, Main area for IO
with gr.Row():
# Sidebar for Configuration
with gr.Sidebar(width=320):
gr.Markdown("## Configuration")
gr.Markdown("### Generation Settings")
pace_slider = gr.Slider(
minimum=0.6,
maximum=1.4,
value=0.9,
step=0.05,
label="Generation Pace",
info="Lower is faster processing"
)
motion_slider = gr.Slider(
minimum=0.8,
maximum=2.0,
value=1.2,
step=0.1,
label="Motion Intensity",
info="Controls movement magnitude"
)
num_frames = gr.Slider(
minimum=16,
maximum=64,
value=24,
step=4,
label="Frame Count",
info="Total frames in output"
)
guidance_scale = gr.Slider(
minimum=5.0,
maximum=15.0,
value=7.5,
step=0.5,
label="Guidance Scale",
info="Adherence to prompt"
)
gr.Markdown("---")
gr.Markdown("### System Info")
system_status = gr.Textbox(
value="System Ready (CUDA: Available)",
label="Status",
interactive=False
)
# Main Content Area
with gr.Column(scale=1):
gr.Markdown("## Input / Output")
with gr.Row():
with gr.Column():
input_image = gr.Image(
type="pil",
label="Reference Image",
sources=["upload", "clipboard"],
height=400
)
# Action Buttons
with gr.Row():
generate_btn = gr.Button("Generate Video", variant="primary", size="lg")
clear_btn = gr.ClearButton([input_image], variant="stop")
with gr.Column():
output_video = gr.Video(
label="Generated Video Output (25fps)",
autoplay=True,
height=400
)
# Advanced Settings Accordion
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Number(label="Seed (Random for -1)", value=-1, precision=0)
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="blur, distortion, low quality...",
lines=2
)
enable_lora = gr.Checkbox(label="Enable Custom LoRA", value=False)
# Event Listeners
# Gradio 6 uses api_visibility instead of just api_name
generate_btn.click(
fn=process_video,
inputs=[
input_image,
pace_slider,
motion_slider,
num_frames,
guidance_scale
],
outputs=output_video,
api_visibility="public"
)
# Gradio 6 Launch Method
# CRITICAL: All parameters (theme, css, etc.) go here, NOT in gr.Blocks()
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
),
# Custom CSS for additional polish
css="""
.gradio-container {
max-width: 1400px !important;
}
h1 {
font-weight: 700 !important;
color: #1f2937 !important;
}
""",
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |