Download video_executor.py from PRB1/Velocity: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/spaces/PRB1/Velocity/resolve/main/video_executor.py
- Command line
-
hf download hf://spaces/PRB1/Velocity/video_executor.py
-
curl -L -o video_executor.py https://huggingface.co/spaces/PRB1/Velocity/resolve/main/video_executor.py
3.09 kB
| import os | |
| import sys | |
| import tempfile | |
| from pathlib import Path | |
| import traceback | |
| class VideoExecutor: | |
| def __init__(self): | |
| """Initialize video code executor""" | |
| self.temp_dir = tempfile.gettempdir() | |
| def execute_code(self, code, images, audio_path, segment_number): | |
| """Execute generated code safely and return video path""" | |
| print(f"⚙️ Executing segment {segment_number} code...") | |
| # Create execution environment | |
| exec_globals = { | |
| '__builtins__': __builtins__, | |
| 'images': images, | |
| 'audio_path': audio_path, | |
| } | |
| # Import necessary modules into execution environment | |
| exec(""" | |
| from moviepy.editor import * | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| import random | |
| """, exec_globals) | |
| try: | |
| # Execute the generated code | |
| exec(code, exec_globals) | |
| # Find the segment creation function | |
| func_name = f'create_segment_{segment_number}' | |
| if func_name not in exec_globals: | |
| raise ValueError(f"❌ Function '{func_name}' not found in generated code") | |
| # Call the function | |
| segment_func = exec_globals[func_name] | |
| clip = segment_func(images, audio_path) | |
| if clip is None: | |
| raise ValueError("❌ Function returned None instead of a clip") | |
| # Render to temp file | |
| output_path = os.path.join(self.temp_dir, f"segment_{segment_number}_{os.urandom(4).hex()}.mp4") | |
| print(f"🎬 Rendering segment to: {output_path}") | |
| clip.write_videofile( | |
| output_path, | |
| fps=30, | |
| codec="libx264", | |
| audio_codec="aac", | |
| preset='medium', | |
| threads=4, | |
| logger=None | |
| ) | |
| # Cleanup | |
| clip.close() | |
| print(f"✅ Segment {segment_number} rendered successfully") | |
| return output_path | |
| except Exception as e: | |
| print(f"❌ Error executing code: {str(e)}") | |
| print("Traceback:") | |
| traceback.print_exc() | |
| raise e | |
| def validate_code(self, code): | |
| """Basic validation of generated code""" | |
| # Check for dangerous operations | |
| dangerous_patterns = [ | |
| 'os.system', | |
| 'subprocess', | |
| 'eval(', | |
| '__import__', | |
| 'open(', | |
| 'file(', | |
| ] | |
| for pattern in dangerous_patterns: | |
| if pattern in code: | |
| raise ValueError(f"⚠️ Potentially dangerous operation detected: {pattern}") | |
| # Check for required imports | |
| required_patterns = [ | |
| 'from moviepy', | |
| 'def create_segment_', | |
| 'return ' | |
| ] | |
| for pattern in required_patterns: | |
| if pattern not in code: | |
| raise ValueError(f"⚠️ Required pattern missing: {pattern}") |