Spaces:
Runtime error
Runtime error
Create Shelly/multimodal/video_generation.py
Browse files
Shelly/multimodal/video_generation.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Shelly AI - Video Generation
|
| 3 |
+
|
| 4 |
+
Provides an interface for external video-generation providers.
|
| 5 |
+
|
| 6 |
+
The configured provider performs the actual rendering.
|
| 7 |
+
This module handles validation, invocation, and history.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import threading
|
| 14 |
+
import time
|
| 15 |
+
import uuid
|
| 16 |
+
|
| 17 |
+
from typing import Any, Callable, Dict, Optional
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class VideoGeneration:
|
| 21 |
+
def __init__(
|
| 22 |
+
self,
|
| 23 |
+
provider: Optional[Callable[..., Any]] = None,
|
| 24 |
+
output_directory: str = "generated_media",
|
| 25 |
+
maximum_duration: int = 300,
|
| 26 |
+
):
|
| 27 |
+
self._lock = threading.RLock()
|
| 28 |
+
|
| 29 |
+
self.provider = provider
|
| 30 |
+
|
| 31 |
+
self.output_directory = os.path.abspath(
|
| 32 |
+
output_directory
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
self.maximum_duration = max(
|
| 36 |
+
1,
|
| 37 |
+
int(maximum_duration),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
self.history = []
|
| 41 |
+
|
| 42 |
+
def set_provider(
|
| 43 |
+
self,
|
| 44 |
+
provider: Callable[..., Any],
|
| 45 |
+
) -> None:
|
| 46 |
+
if not callable(provider):
|
| 47 |
+
raise TypeError("Provider must be callable.")
|
| 48 |
+
|
| 49 |
+
with self._lock:
|
| 50 |
+
self.provider = provider
|
| 51 |
+
|
| 52 |
+
def generate(
|
| 53 |
+
self,
|
| 54 |
+
prompt: str,
|
| 55 |
+
duration: int = 5,
|
| 56 |
+
width: int = 1280,
|
| 57 |
+
height: int = 720,
|
| 58 |
+
fps: int = 24,
|
| 59 |
+
options: Optional[Dict[str, Any]] = None,
|
| 60 |
+
) -> Dict[str, Any]:
|
| 61 |
+
prompt = str(prompt).strip()
|
| 62 |
+
|
| 63 |
+
if not prompt:
|
| 64 |
+
raise ValueError("Video prompt cannot be empty.")
|
| 65 |
+
|
| 66 |
+
duration = int(duration)
|
| 67 |
+
width = int(width)
|
| 68 |
+
height = int(height)
|
| 69 |
+
fps = int(fps)
|
| 70 |
+
|
| 71 |
+
if not 1 <= duration <= self.maximum_duration:
|
| 72 |
+
raise ValueError(
|
| 73 |
+
"Video duration exceeds configured limits."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
if not 64 <= width <= 4096:
|
| 77 |
+
raise ValueError("Width must be between 64 and 4096.")
|
| 78 |
+
|
| 79 |
+
if not 64 <= height <= 4096:
|
| 80 |
+
raise ValueError("Height must be between 64 and 4096.")
|
| 81 |
+
|
| 82 |
+
if not 1 <= fps <= 120:
|
| 83 |
+
raise ValueError("FPS must be between 1 and 120.")
|
| 84 |
+
|
| 85 |
+
with self._lock:
|
| 86 |
+
provider = self.provider
|
| 87 |
+
|
| 88 |
+
if provider is None:
|
| 89 |
+
raise RuntimeError(
|
| 90 |
+
"No video generation provider is configured."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
os.makedirs(
|
| 94 |
+
self.output_directory,
|
| 95 |
+
exist_ok=True,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
started = time.time()
|
| 99 |
+
|
| 100 |
+
output = provider(
|
| 101 |
+
prompt=prompt,
|
| 102 |
+
duration=duration,
|
| 103 |
+
width=width,
|
| 104 |
+
height=height,
|
| 105 |
+
fps=fps,
|
| 106 |
+
options=dict(options or {}),
|
| 107 |
+
output_directory=self.output_directory,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
result = {
|
| 111 |
+
"id": str(uuid.uuid4()),
|
| 112 |
+
"prompt": prompt,
|
| 113 |
+
"duration_seconds": duration,
|
| 114 |
+
"width": width,
|
| 115 |
+
"height": height,
|
| 116 |
+
"fps": fps,
|
| 117 |
+
"output": output,
|
| 118 |
+
"processing_time": time.time() - started,
|
| 119 |
+
"timestamp": time.time(),
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
with self._lock:
|
| 123 |
+
self.history.append(result)
|
| 124 |
+
|
| 125 |
+
if len(self.history) > 500:
|
| 126 |
+
self.history = self.history[-500:]
|
| 127 |
+
|
| 128 |
+
return dict(result)
|
| 129 |
+
|
| 130 |
+
def get_history(self, limit: int = 50) -> list:
|
| 131 |
+
with self._lock:
|
| 132 |
+
return [
|
| 133 |
+
dict(item)
|
| 134 |
+
for item in self.history[-max(1, int(limit)):]
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
def get_status(self) -> Dict[str, Any]:
|
| 138 |
+
with self._lock:
|
| 139 |
+
return {
|
| 140 |
+
"provider_configured": self.provider is not None,
|
| 141 |
+
"output_directory": self.output_directory,
|
| 142 |
+
"maximum_duration": self.maximum_duration,
|
| 143 |
+
"history_count": len(self.history),
|
| 144 |
+
}
|
| 145 |
+
|