Spaces:
Running
Running
File size: 12,313 Bytes
3ab16a2 |
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
import gradio as gr
import torch
from diffusers import DiffusionPipeline
import numpy as np
import spaces
import time
from PIL import Image
import io
import base64
# Model configuration
MODEL_ID = "hpcai-tech/Open-Sora-v2"
# Initialize the pipeline
@spaces.GPU(duration=1500)
def load_model():
"""Load the Open-Sora-v2 model"""
try:
pipe = DiffusionPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True
)
pipe.to("cuda")
# Enable memory efficient attention
pipe.enable_attention_slicing()
return pipe
except Exception as e:
print(f"Error loading model: {e}")
return None
# Global model variable
model = None
def initialize_model():
"""Initialize the model on first request"""
global model
if model is None:
model = load_model()
return model is not None
@spaces.GPU(duration=120)
def generate_video(
prompt: str,
duration: int = 4,
height: int = 720,
width: int = 1280,
num_inference_steps: int = 50,
guidance_scale: float = 7.5,
progress=gr.Progress()
) -> str:
"""
Generate a video from text prompt using Open-Sora-v2
Args:
prompt: Text description of the video
duration: Duration in seconds
height: Video height
width: Video width
num_inference_steps: Number of denoising steps
guidance_scale: Guidance scale for generation
Returns:
Path to the generated video file
"""
try:
# Initialize model if not already done
if not initialize_model():
raise Exception("Failed to initialize model")
progress(0.1, desc="Initializing generation...")
# Calculate number of frames based on duration (assuming 30 fps)
num_frames = duration * 30
progress(0.2, desc="Starting video generation...")
# Generate video frames
result = model(
prompt=prompt,
num_frames=num_frames,
height=height,
width=width,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=torch.Generator().manual_seed(42)
)
progress(0.8, desc="Processing frames...")
# Save the generated video
output_path = f"generated_video_{int(time.time())}.mp4"
if hasattr(result, 'videos'):
# Handle video output
video_frames = result.videos[0]
else:
# Handle image sequence output
video_frames = result.frames[0] if hasattr(result, 'frames') else result
# Save as video file
save_video(video_frames, output_path, fps=30)
progress(1.0, desc="Video generation complete!")
return output_path
except Exception as e:
print(f"Error generating video: {e}")
raise gr.Error(f"Video generation failed: {str(e)}")
def save_video(frames, output_path, fps=30):
"""Save video frames to MP4 file"""
try:
import cv2
# Convert frames to numpy if needed
if torch.is_tensor(frames):
frames = frames.cpu().numpy()
# Ensure frames are in the correct format
if len(frames.shape) == 4:
frames = np.transpose(frames, (0, 2, 3, 1)) # TCHW -> THWC
# Normalize frames to 0-255
frames = ((frames + 1.0) * 127.5).astype(np.uint8)
# Get video dimensions
height, width = frames[0].shape[:2]
# Initialize video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
# Write frames
for frame in frames:
if len(frame.shape) == 3:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
out.write(frame)
out.release()
except ImportError:
# Fallback: save as GIF if cv2 is not available
from PIL import Image
if torch.is_tensor(frames):
frames = frames.cpu().numpy()
if len(frames.shape) == 4:
frames = np.transpose(frames, (0, 2, 3, 1))
frames = ((frames + 1.0) * 127.5).astype(np.uint8)
images = [Image.fromarray(frame) for frame in frames]
images[0].save(
output_path.replace('.mp4', '.gif'),
save_all=True,
append_images=images[1:],
duration=33, # ~30 fps
loop=0
)
def create_interface():
"""Create the Gradio interface"""
with gr.Blocks(
title="Text to Video - Open-Sora-v2",
theme=gr.themes.Soft(),
css="""
.header-text {
text-align: center;
font-size: 2em;
margin-bottom: 0.5em;
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.subheader-text {
text-align: center;
color: #666;
margin-bottom: 2em;
}
.generate-btn {
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
border: none;
color: white;
font-weight: bold;
}
.generate-btn:hover {
background: linear-gradient(45deg, #764ba2 0%, #667eea 100%);
}
"""
) as demo:
gr.Markdown("""
<div class="header-text">π¬ Text to Video Generator</div>
<div class="subheader-text">Powered by Open-Sora-v2 - Transform your ideas into stunning videos</div>
<div style="text-align: center; margin-bottom: 1em;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #667eea; text-decoration: none;">
Built with anycoder
</a>
</div>
""")
with gr.Row():
with gr.Column(scale=2):
prompt_input = gr.Textbox(
label="π Describe your video",
placeholder="A beautiful sunset over the ocean with waves gently crashing on the shore, cinematic quality, 4K resolution...",
lines=4,
max_lines=6
)
with gr.Row():
duration_input = gr.Slider(
minimum=2,
maximum=16,
value=4,
step=2,
label="β±οΈ Duration (seconds)"
)
quality_input = gr.Dropdown(
choices=[
("720p HD", 720),
("1080p Full HD", 1080),
("4K Ultra HD", 2160)
],
value=720,
label="π₯ Quality"
)
with gr.Accordion("βοΈ Advanced Settings", open=False):
with gr.Row():
steps_input = gr.Slider(
minimum=20,
maximum=100,
value=50,
step=5,
label="π’ Inference Steps"
)
guidance_input = gr.Slider(
minimum=1.0,
maximum=20.0,
value=7.5,
step=0.5,
label="π― Guidance Scale"
)
generate_btn = gr.Button(
"π Generate Video",
variant="primary",
size="lg",
elem_classes=["generate-btn"]
)
with gr.Column(scale=1):
gr.Markdown("""
### π‘ Example Prompts
- π
"A serene mountain landscape at sunrise with golden light filtering through misty valleys"
- ποΈ "A futuristic cyberpunk city at night with neon signs reflecting on wet streets"
- π "Underwater coral reef with colorful tropical fish swimming in crystal clear water"
- π³ "A magical enchanted forest with glowing mushrooms and fireflies at twilight"
### β‘ Tips for Best Results
- Be descriptive and specific
- Include visual style (cinematic, realistic, anime, etc.)
- Mention lighting and atmosphere
- Specify camera angles if desired
""")
with gr.Row():
video_output = gr.Video(
label="π¬ Generated Video",
visible=False
)
loading_info = gr.Markdown(
"β¨ Your video will appear here after generation",
visible=True
)
# Example prompts
example_prompts = [
[
"A beautiful sunset over the ocean with waves gently crashing on the shore, cinematic quality, warm golden lighting",
4, 720, 50, 7.5
],
[
"A serene mountain landscape at sunrise with mist rolling over the valleys, golden light filtering through the clouds",
4, 720, 50, 7.5
],
[
"A bustling city street at night with neon signs reflecting on wet pavement, cyberpunk aesthetic, blade runner style",
4, 720, 50, 7.5
],
[
"Underwater coral reef with colorful fish swimming, sun rays penetrating through the water, national geographic documentary style",
4, 720, 50, 7.5
]
]
gr.Examples(
examples=example_prompts,
inputs=[prompt_input, duration_input, quality_input, steps_input, guidance_input],
label="π― Try these examples",
cache_examples=False
)
def generate_and_display(prompt, duration, quality, steps, guidance, progress=gr.Progress()):
try:
# Calculate width based on quality (16:9 aspect ratio)
width_map = {720: 1280, 1080: 1920, 2160: 3840}
width = width_map.get(quality, 1280)
# Generate video
video_path = generate_video(
prompt=prompt,
duration=duration,
height=quality,
width=width,
num_inference_steps=steps,
guidance_scale=guidance,
progress=progress
)
return {
video_output: gr.Video(value=video_path, visible=True),
loading_info: gr.Markdown(visible=False)
}
except Exception as e:
return {
video_output: gr.Video(visible=False),
loading_info: gr.Markdown(f"β Error: {str(e)}", visible=True)
}
generate_btn.click(
fn=generate_and_display,
inputs=[prompt_input, duration_input, quality_input, steps_input, guidance_input],
outputs=[video_output, loading_info],
show_progress=True
)
# Initialize model on page load
demo.load(
fn=initialize_model,
inputs=[],
outputs=[],
queue=False
)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch(
share=True,
show_error=True,
show_tips=True,
queue=True
) |