Create multimodal_pipeline.py
Browse files- multimodal_pipeline.py +51 -0
multimodal_pipeline.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import DiffusionPipeline, StableDiffusionImageVariationPipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
class BootyShakerPipeline:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.txt2video_pipe = DiffusionPipeline.from_pretrained("ChromiumPlutoniumAI/BootyShakerAI")
|
| 10 |
+
self.img2video_pipe = StableDiffusionImageVariationPipeline.from_pretrained("ChromiumPlutoniumAI/BootyShakerAI")
|
| 11 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
def generate_from_text(self, prompt, num_frames=16, fps=8):
|
| 14 |
+
video = self.txt2video_pipe(
|
| 15 |
+
prompt,
|
| 16 |
+
num_inference_steps=50,
|
| 17 |
+
num_frames=num_frames
|
| 18 |
+
).frames
|
| 19 |
+
return self.frames_to_video(video, fps)
|
| 20 |
+
|
| 21 |
+
def generate_from_image(self, image, num_frames=16, fps=8):
|
| 22 |
+
if isinstance(image, str):
|
| 23 |
+
image = Image.open(image)
|
| 24 |
+
video = self.img2video_pipe(
|
| 25 |
+
image,
|
| 26 |
+
num_inference_steps=50,
|
| 27 |
+
num_frames=num_frames
|
| 28 |
+
).frames
|
| 29 |
+
return self.frames_to_video(video, fps)
|
| 30 |
+
|
| 31 |
+
def apply_alterations(self, video, style="bounce"):
|
| 32 |
+
styles = {
|
| 33 |
+
"bounce": self.bounce_effect,
|
| 34 |
+
"wave": self.wave_effect,
|
| 35 |
+
"shake": self.shake_effect
|
| 36 |
+
}
|
| 37 |
+
return styles[style](video)
|
| 38 |
+
|
| 39 |
+
@staticmethod
|
| 40 |
+
def frames_to_video(frames, fps):
|
| 41 |
+
output_file = "output.mp4"
|
| 42 |
+
writer = cv2.VideoWriter(
|
| 43 |
+
output_file,
|
| 44 |
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
| 45 |
+
fps,
|
| 46 |
+
(frames[0].shape[1], frames[0].shape[0])
|
| 47 |
+
)
|
| 48 |
+
for frame in frames:
|
| 49 |
+
writer.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
| 50 |
+
writer.release()
|
| 51 |
+
return output_file
|