Ani14/Video-agent
0
1"""2Utility functions for video processing and file handling3"""4import os5import tempfile6import uuid7from typing import Optional, Tuple8import torch9import numpy as np10from PIL import Image11 12def create_temp_video_path(extension: str = "mp4") -> str:13 """Create a temporary file path for video output"""14 temp_dir = tempfile.gettempdir()15 unique_id = str(uuid.uuid4())16 return os.path.join(temp_dir, f"video_{unique_id}.{extension}")17 18def validate_generation_params(19 width: int, 20 height: int, 21 num_frames: int, 22 num_inference_steps: int,23 guidance_scale: float24) -> Tuple[bool, Optional[str]]:25 """Validate video generation parameters"""26 27 # Check width and height28 if width < 64 or width > 1920:29 return False, "Width must be between 64 and 1920 pixels"30 if height < 64 or height > 1080:31 return False, "Height must be between 64 and 1080 pixels"32 33 # Check if dimensions are divisible by 8 (common requirement for video models)34 if width % 8 != 0:35 return False, "Width must be divisible by 8"36 if height % 8 != 0:37 return False, "Height must be divisible by 8"38 39 # Check frames40 if num_frames < 1 or num_frames > 200:41 return False, "Number of frames must be between 1 and 200"42 43 # Check inference steps44 if num_inference_steps < 1 or num_inference_steps > 100:45 return False, "Number of inference steps must be between 1 and 100"46 47 # Check guidance scale48 if guidance_scale < 0 or guidance_scale > 20:49 return False, "Guidance scale must be between 0 and 20"50 51 return True, None52 53def validate_prompt(prompt: str) -> Tuple[bool, Optional[str]]:54 """Validate the input prompt"""55 if not prompt or len(prompt.strip()) == 0:56 return False, "Prompt cannot be empty"57 58 if len(prompt) > 1000:59 return False, "Prompt must be less than 1000 characters"60 61 return True, None62 63def get_memory_usage() -> str:64 """Get current GPU memory usage if available"""65 if torch.cuda.is_available():66 allocated = torch.cuda.memory_allocated() / 1024**3 # Convert to GB67 cached = torch.cuda.memory_reserved() / 1024**368 return f"GPU Memory - Allocated: {allocated:.2f}GB, Cached: {cached:.2f}GB"69 else:70 return "GPU not available"71 72def cleanup_temp_files(file_path: str) -> None:73 """Clean up temporary files"""74 try:75 if os.path.exists(file_path):76 os.remove(file_path)77 except Exception as e:78 print(f"Warning: Could not remove temporary file {file_path}: {e}")79 80def format_generation_info(81 prompt: str,82 negative_prompt: str,83 width: int,84 height: int,85 num_frames: int,86 num_inference_steps: int,87 guidance_scale: float,88 generation_time: float89) -> str:90 """Format generation information for display"""91 info = f"""92**Generation Details:**93- **Prompt:** {prompt}94- **Negative Prompt:** {negative_prompt if negative_prompt else "None"}95- **Dimensions:** {width}x{height}96- **Frames:** {num_frames}97- **Inference Steps:** {num_inference_steps}98- **Guidance Scale:** {guidance_scale}99- **Generation Time:** {generation_time:.2f} seconds100- **Memory Usage:** {get_memory_usage()}101"""102 return info103 