2023-01-08 09:33:45 +00:00
|
|
|
# Copyright 2022 Lunar Ring. All rights reserved.
|
2023-01-11 11:58:59 +00:00
|
|
|
# Written by Johannes Stelzer, email stelzer@lunar-ring.ai twitter @j_stelzer
|
2023-01-08 09:33:45 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2023-02-22 09:15:03 +00:00
|
|
|
import os
|
2023-01-08 09:33:45 +00:00
|
|
|
import torch
|
|
|
|
torch.backends.cudnn.benchmark = False
|
2023-02-22 09:15:03 +00:00
|
|
|
torch.set_grad_enabled(False)
|
2023-01-08 09:33:45 +00:00
|
|
|
import numpy as np
|
|
|
|
import warnings
|
|
|
|
warnings.filterwarnings('ignore')
|
|
|
|
import warnings
|
|
|
|
from tqdm.auto import tqdm
|
|
|
|
from PIL import Image
|
2023-01-15 15:52:42 +00:00
|
|
|
from movie_util import MovieSaver, concatenate_movies
|
2023-02-22 09:15:03 +00:00
|
|
|
from latent_blending import LatentBlending
|
2023-01-08 09:33:45 +00:00
|
|
|
from stable_diffusion_holder import StableDiffusionHolder
|
|
|
|
import gradio as gr
|
2023-02-16 10:48:45 +00:00
|
|
|
from dotenv import find_dotenv, load_dotenv
|
2023-02-18 06:56:30 +00:00
|
|
|
import shutil
|
2023-07-07 07:07:15 +00:00
|
|
|
import uuid
|
2023-02-22 09:15:03 +00:00
|
|
|
from utils import get_time, add_frames_linear_interp
|
|
|
|
from huggingface_hub import hf_hub_download
|
2023-01-15 11:02:11 +00:00
|
|
|
|
2023-01-08 09:33:45 +00:00
|
|
|
|
|
|
|
class BlendingFrontend():
|
2023-02-22 09:15:03 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
sdh,
|
|
|
|
share=False):
|
|
|
|
r"""
|
|
|
|
Gradio Helper Class to collect UI data and start latent blending.
|
|
|
|
Args:
|
|
|
|
sdh:
|
|
|
|
StableDiffusionHolder
|
|
|
|
share: bool
|
|
|
|
Set true to get a shareable gradio link (e.g. for running a remote server)
|
|
|
|
"""
|
|
|
|
self.share = share
|
|
|
|
|
|
|
|
# UI Defaults
|
2023-02-18 06:56:30 +00:00
|
|
|
self.num_inference_steps = 30
|
2023-01-08 09:33:45 +00:00
|
|
|
self.depth_strength = 0.25
|
2023-02-18 06:56:30 +00:00
|
|
|
self.seed1 = 420
|
2023-01-08 09:33:45 +00:00
|
|
|
self.seed2 = 420
|
2023-01-08 10:48:44 +00:00
|
|
|
self.prompt1 = ""
|
|
|
|
self.prompt2 = ""
|
|
|
|
self.negative_prompt = ""
|
2023-01-09 08:58:26 +00:00
|
|
|
self.fps = 30
|
2023-02-22 09:15:03 +00:00
|
|
|
self.duration_video = 8
|
2023-02-16 10:48:45 +00:00
|
|
|
self.t_compute_max_allowed = 10
|
2023-02-22 09:15:03 +00:00
|
|
|
|
|
|
|
self.lb = LatentBlending(sdh)
|
|
|
|
self.lb.sdh.num_inference_steps = self.num_inference_steps
|
|
|
|
self.init_parameters_from_lb()
|
|
|
|
self.init_save_dir()
|
|
|
|
|
|
|
|
# Vars
|
2023-01-15 15:52:42 +00:00
|
|
|
self.list_fp_imgs_current = []
|
2023-02-17 09:50:57 +00:00
|
|
|
self.recycle_img1 = False
|
|
|
|
self.recycle_img2 = False
|
2023-02-20 07:29:21 +00:00
|
|
|
self.list_all_segments = []
|
|
|
|
self.dp_session = ""
|
2023-02-21 18:46:16 +00:00
|
|
|
self.user_id = None
|
2023-02-22 09:15:03 +00:00
|
|
|
|
|
|
|
def init_parameters_from_lb(self):
|
|
|
|
r"""
|
|
|
|
Automatically init parameters from latentblending instance
|
|
|
|
"""
|
|
|
|
self.height = self.lb.sdh.height
|
|
|
|
self.width = self.lb.sdh.width
|
|
|
|
self.guidance_scale = self.lb.guidance_scale
|
|
|
|
self.guidance_scale_mid_damper = self.lb.guidance_scale_mid_damper
|
|
|
|
self.mid_compression_scaler = self.lb.mid_compression_scaler
|
|
|
|
self.branch1_crossfeed_power = self.lb.branch1_crossfeed_power
|
|
|
|
self.branch1_crossfeed_range = self.lb.branch1_crossfeed_range
|
|
|
|
self.branch1_crossfeed_decay = self.lb.branch1_crossfeed_decay
|
|
|
|
self.parental_crossfeed_power = self.lb.parental_crossfeed_power
|
|
|
|
self.parental_crossfeed_range = self.lb.parental_crossfeed_range
|
|
|
|
self.parental_crossfeed_power_decay = self.lb.parental_crossfeed_power_decay
|
|
|
|
|
2023-02-16 10:48:45 +00:00
|
|
|
def init_save_dir(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Initializes the directory where stuff is being saved.
|
|
|
|
You can specify this directory in a ".env" file in your latentblending root, setting
|
|
|
|
DIR_OUT='/path/to/saving'
|
|
|
|
"""
|
|
|
|
load_dotenv(find_dotenv(), verbose=False)
|
2023-02-20 07:29:21 +00:00
|
|
|
self.dp_out = os.getenv("DIR_OUT")
|
2023-02-19 14:46:02 +00:00
|
|
|
if self.dp_out is None:
|
2023-02-16 10:48:45 +00:00
|
|
|
self.dp_out = ""
|
2023-02-18 06:56:30 +00:00
|
|
|
self.dp_imgs = os.path.join(self.dp_out, "imgs")
|
|
|
|
os.makedirs(self.dp_imgs, exist_ok=True)
|
|
|
|
self.dp_movies = os.path.join(self.dp_out, "movies")
|
|
|
|
os.makedirs(self.dp_movies, exist_ok=True)
|
2023-02-22 09:15:03 +00:00
|
|
|
self.save_empty_image()
|
|
|
|
|
2023-02-15 17:21:00 +00:00
|
|
|
def save_empty_image(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Saves an empty/black dummy image.
|
|
|
|
"""
|
2023-02-18 06:56:30 +00:00
|
|
|
self.fp_img_empty = os.path.join(self.dp_imgs, 'empty.jpg')
|
2023-01-15 15:52:42 +00:00
|
|
|
Image.fromarray(np.zeros((self.height, self.width, 3), dtype=np.uint8)).save(self.fp_img_empty, quality=5)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-01-08 09:33:45 +00:00
|
|
|
def randomize_seed1(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Randomizes the first seed
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
seed = np.random.randint(0, 10000000)
|
2023-02-15 17:21:00 +00:00
|
|
|
self.seed1 = int(seed)
|
2023-01-08 09:33:45 +00:00
|
|
|
print(f"randomize_seed1: new seed = {self.seed1}")
|
|
|
|
return seed
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-01-08 09:33:45 +00:00
|
|
|
def randomize_seed2(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Randomizes the second seed
|
|
|
|
"""
|
2023-01-08 09:33:45 +00:00
|
|
|
seed = np.random.randint(0, 10000000)
|
2023-02-15 17:21:00 +00:00
|
|
|
self.seed2 = int(seed)
|
2023-01-08 09:33:45 +00:00
|
|
|
print(f"randomize_seed2: new seed = {self.seed2}")
|
|
|
|
return seed
|
2023-02-22 09:15:03 +00:00
|
|
|
|
|
|
|
def setup_lb(self, list_ui_vals):
|
|
|
|
r"""
|
|
|
|
Sets all parameters from the UI. Since gradio does not support to pass dictionaries,
|
|
|
|
we have to instead pass keys (list_ui_keys, global) and values (list_ui_vals)
|
|
|
|
"""
|
2023-02-15 17:21:00 +00:00
|
|
|
# Collect latent blending variables
|
2023-02-22 09:15:03 +00:00
|
|
|
self.lb.set_width(list_ui_vals[list_ui_keys.index('width')])
|
|
|
|
self.lb.set_height(list_ui_vals[list_ui_keys.index('height')])
|
|
|
|
self.lb.set_prompt1(list_ui_vals[list_ui_keys.index('prompt1')])
|
|
|
|
self.lb.set_prompt2(list_ui_vals[list_ui_keys.index('prompt2')])
|
|
|
|
self.lb.set_negative_prompt(list_ui_vals[list_ui_keys.index('negative_prompt')])
|
|
|
|
self.lb.guidance_scale = list_ui_vals[list_ui_keys.index('guidance_scale')]
|
|
|
|
self.lb.guidance_scale_mid_damper = list_ui_vals[list_ui_keys.index('guidance_scale_mid_damper')]
|
|
|
|
self.t_compute_max_allowed = list_ui_vals[list_ui_keys.index('duration_compute')]
|
|
|
|
self.lb.num_inference_steps = list_ui_vals[list_ui_keys.index('num_inference_steps')]
|
|
|
|
self.lb.sdh.num_inference_steps = list_ui_vals[list_ui_keys.index('num_inference_steps')]
|
|
|
|
self.duration_video = list_ui_vals[list_ui_keys.index('duration_video')]
|
|
|
|
self.lb.seed1 = list_ui_vals[list_ui_keys.index('seed1')]
|
|
|
|
self.lb.seed2 = list_ui_vals[list_ui_keys.index('seed2')]
|
|
|
|
self.lb.branch1_crossfeed_power = list_ui_vals[list_ui_keys.index('branch1_crossfeed_power')]
|
|
|
|
self.lb.branch1_crossfeed_range = list_ui_vals[list_ui_keys.index('branch1_crossfeed_range')]
|
|
|
|
self.lb.branch1_crossfeed_decay = list_ui_vals[list_ui_keys.index('branch1_crossfeed_decay')]
|
|
|
|
self.lb.parental_crossfeed_power = list_ui_vals[list_ui_keys.index('parental_crossfeed_power')]
|
|
|
|
self.lb.parental_crossfeed_range = list_ui_vals[list_ui_keys.index('parental_crossfeed_range')]
|
|
|
|
self.lb.parental_crossfeed_power_decay = list_ui_vals[list_ui_keys.index('parental_crossfeed_power_decay')]
|
|
|
|
self.num_inference_steps = list_ui_vals[list_ui_keys.index('num_inference_steps')]
|
|
|
|
self.depth_strength = list_ui_vals[list_ui_keys.index('depth_strength')]
|
|
|
|
|
|
|
|
if len(list_ui_vals[list_ui_keys.index('user_id')]) > 1:
|
|
|
|
self.user_id = list_ui_vals[list_ui_keys.index('user_id')]
|
2023-02-21 18:46:16 +00:00
|
|
|
else:
|
|
|
|
# generate new user id
|
2023-07-07 07:07:15 +00:00
|
|
|
self.user_id = uuid.uuid4().hex
|
2023-02-22 09:15:03 +00:00
|
|
|
print(f"made new user_id: {self.user_id} at {get_time('second')}")
|
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
def save_latents(self, fp_latents, list_latents):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Saves a latent trajectory on disk, in npy format.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
list_latents_cpu = [l.cpu().numpy() for l in list_latents]
|
|
|
|
np.save(fp_latents, list_latents_cpu)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
def load_latents(self, fp_latents):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Loads a latent trajectory from disk, converts to torch tensor.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
list_latents_cpu = np.load(fp_latents)
|
|
|
|
list_latents = [torch.from_numpy(l).to(self.lb.device) for l in list_latents_cpu]
|
|
|
|
return list_latents
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-15 17:21:00 +00:00
|
|
|
def compute_img1(self, *args):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Computes the first transition image and returns it for display.
|
|
|
|
Sets all other transition images and last image to empty (as they are obsolete with this operation)
|
|
|
|
"""
|
|
|
|
list_ui_vals = args
|
|
|
|
self.setup_lb(list_ui_vals)
|
2023-02-21 18:46:16 +00:00
|
|
|
fp_img1 = os.path.join(self.dp_imgs, f"img1_{self.user_id}")
|
2023-02-15 17:21:00 +00:00
|
|
|
img1 = Image.fromarray(self.lb.compute_latents1(return_image=True))
|
2023-02-22 09:15:03 +00:00
|
|
|
img1.save(fp_img1 + ".jpg")
|
|
|
|
self.save_latents(fp_img1 + ".npy", self.lb.tree_latents[0])
|
2023-02-17 09:50:57 +00:00
|
|
|
self.recycle_img1 = True
|
|
|
|
self.recycle_img2 = False
|
2023-02-22 09:15:03 +00:00
|
|
|
return [fp_img1 + ".jpg", self.fp_img_empty, self.fp_img_empty, self.fp_img_empty, self.fp_img_empty, self.user_id]
|
|
|
|
|
2023-02-15 17:21:00 +00:00
|
|
|
def compute_img2(self, *args):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Computes the last transition image and returns it for display.
|
|
|
|
Sets all other transition images to empty (as they are obsolete with this operation)
|
|
|
|
"""
|
|
|
|
if not os.path.isfile(os.path.join(self.dp_imgs, f"img1_{self.user_id}.jpg")): # don't do anything
|
2023-02-21 18:46:16 +00:00
|
|
|
return [self.fp_img_empty, self.fp_img_empty, self.fp_img_empty, self.fp_img_empty, self.user_id]
|
2023-02-22 09:15:03 +00:00
|
|
|
list_ui_vals = args
|
|
|
|
self.setup_lb(list_ui_vals)
|
2023-02-21 18:46:16 +00:00
|
|
|
|
|
|
|
self.lb.tree_latents[0] = self.load_latents(os.path.join(self.dp_imgs, f"img1_{self.user_id}.npy"))
|
|
|
|
fp_img2 = os.path.join(self.dp_imgs, f"img2_{self.user_id}")
|
2023-02-15 17:21:00 +00:00
|
|
|
img2 = Image.fromarray(self.lb.compute_latents2(return_image=True))
|
2023-02-22 09:15:03 +00:00
|
|
|
img2.save(fp_img2 + '.jpg')
|
|
|
|
self.save_latents(fp_img2 + ".npy", self.lb.tree_latents[-1])
|
2023-02-17 09:50:57 +00:00
|
|
|
self.recycle_img2 = True
|
2023-02-21 18:46:16 +00:00
|
|
|
# fixme save seeds. change filenames?
|
2023-02-22 09:15:03 +00:00
|
|
|
return [self.fp_img_empty, self.fp_img_empty, self.fp_img_empty, fp_img2 + ".jpg", self.user_id]
|
2023-02-21 18:46:16 +00:00
|
|
|
|
2023-02-15 17:21:00 +00:00
|
|
|
def compute_transition(self, *args):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Computes transition images and movie.
|
|
|
|
"""
|
|
|
|
list_ui_vals = args
|
|
|
|
self.setup_lb(list_ui_vals)
|
2023-02-20 07:29:21 +00:00
|
|
|
print("STARTING TRANSITION...")
|
2023-01-08 09:33:45 +00:00
|
|
|
fixed_seeds = [self.seed1, self.seed2]
|
2023-02-21 18:46:16 +00:00
|
|
|
# Inject loaded latents (other user interference)
|
|
|
|
self.lb.tree_latents[0] = self.load_latents(os.path.join(self.dp_imgs, f"img1_{self.user_id}.npy"))
|
|
|
|
self.lb.tree_latents[-1] = self.load_latents(os.path.join(self.dp_imgs, f"img2_{self.user_id}.npy"))
|
2023-02-15 17:21:00 +00:00
|
|
|
imgs_transition = self.lb.run_transition(
|
2023-02-22 09:15:03 +00:00
|
|
|
recycle_img1=self.recycle_img1,
|
|
|
|
recycle_img2=self.recycle_img2,
|
|
|
|
num_inference_steps=self.num_inference_steps,
|
|
|
|
depth_strength=self.depth_strength,
|
2023-02-17 09:50:57 +00:00
|
|
|
t_compute_max_allowed=self.t_compute_max_allowed,
|
2023-02-22 09:15:03 +00:00
|
|
|
fixed_seeds=fixed_seeds)
|
|
|
|
print(f"Latent Blending pass finished ({get_time('second')}). Resulted in {len(imgs_transition)} images")
|
|
|
|
|
2023-02-15 17:21:00 +00:00
|
|
|
# Subselect three preview images
|
2023-02-22 09:15:03 +00:00
|
|
|
idx_img_prev = np.round(np.linspace(0, len(imgs_transition) - 1, 5)[1:-1]).astype(np.int32)
|
|
|
|
|
2023-01-15 11:02:11 +00:00
|
|
|
list_imgs_preview = []
|
2023-02-15 17:21:00 +00:00
|
|
|
for j in idx_img_prev:
|
2023-01-15 11:02:11 +00:00
|
|
|
list_imgs_preview.append(Image.fromarray(imgs_transition[j]))
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-01-15 15:52:42 +00:00
|
|
|
# Save the preview imgs as jpgs on disk so we are not sending umcompressed data around
|
2023-02-22 09:15:03 +00:00
|
|
|
current_timestamp = get_time('second')
|
2023-01-15 15:52:42 +00:00
|
|
|
self.list_fp_imgs_current = []
|
2023-01-15 11:02:11 +00:00
|
|
|
for i in range(len(list_imgs_preview)):
|
2023-02-22 09:15:03 +00:00
|
|
|
fp_img = os.path.join(self.dp_imgs, f"img_preview_{i}_{current_timestamp}.jpg")
|
2023-01-15 11:02:11 +00:00
|
|
|
list_imgs_preview[i].save(fp_img)
|
2023-01-15 15:52:42 +00:00
|
|
|
self.list_fp_imgs_current.append(fp_img)
|
|
|
|
# Insert cheap frames for the movie
|
2023-02-15 17:21:00 +00:00
|
|
|
imgs_transition_ext = add_frames_linear_interp(imgs_transition, self.duration_video, self.fps)
|
2023-01-08 09:33:45 +00:00
|
|
|
|
2023-01-15 11:02:11 +00:00
|
|
|
# Save as movie
|
2023-02-21 18:46:16 +00:00
|
|
|
self.fp_movie = self.get_fp_video_last()
|
2023-02-18 06:56:30 +00:00
|
|
|
if os.path.isfile(self.fp_movie):
|
|
|
|
os.remove(self.fp_movie)
|
|
|
|
ms = MovieSaver(self.fp_movie, fps=self.fps)
|
2023-01-08 09:33:45 +00:00
|
|
|
for img in tqdm(imgs_transition_ext):
|
|
|
|
ms.write_frame(img)
|
|
|
|
ms.finalize()
|
2023-01-15 11:02:11 +00:00
|
|
|
print("DONE SAVING MOVIE! SENDING BACK...")
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-01-15 15:52:42 +00:00
|
|
|
# Assemble Output, updating the preview images and le movie
|
2023-02-18 06:56:30 +00:00
|
|
|
list_return = self.list_fp_imgs_current + [self.fp_movie]
|
2023-01-15 11:02:11 +00:00
|
|
|
return list_return
|
2023-01-15 15:52:42 +00:00
|
|
|
|
|
|
|
def stack_forward(self, prompt2, seed2):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Allows to generate multi-segment movies. Sets last image -> first image with all
|
|
|
|
relevant parameters.
|
|
|
|
"""
|
2023-01-15 15:52:42 +00:00
|
|
|
# Save preview images, prompts and seeds into dictionary for stacking
|
2023-02-20 07:29:21 +00:00
|
|
|
if len(self.list_all_segments) == 0:
|
|
|
|
timestamp_session = get_time('second')
|
|
|
|
self.dp_session = os.path.join(self.dp_out, f"session_{timestamp_session}")
|
|
|
|
os.makedirs(self.dp_session)
|
|
|
|
|
2023-02-22 09:15:03 +00:00
|
|
|
idx_segment = len(self.list_all_segments)
|
2023-02-20 07:29:21 +00:00
|
|
|
dp_segment = os.path.join(self.dp_session, f"segment_{str(idx_segment).zfill(3)}")
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-20 07:29:21 +00:00
|
|
|
self.list_all_segments.append(dp_segment)
|
|
|
|
self.lb.write_imgs_transition(dp_segment)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
fp_movie_last = self.get_fp_video_last()
|
|
|
|
fp_movie_next = self.get_fp_video_next()
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
shutil.copyfile(fp_movie_last, fp_movie_next)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:53:26 +00:00
|
|
|
self.lb.tree_latents[0] = self.load_latents(os.path.join(self.dp_imgs, f"img1_{self.user_id}.npy"))
|
|
|
|
self.lb.tree_latents[-1] = self.load_latents(os.path.join(self.dp_imgs, f"img2_{self.user_id}.npy"))
|
2023-01-15 15:52:42 +00:00
|
|
|
self.lb.swap_forward()
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:53:26 +00:00
|
|
|
shutil.copyfile(os.path.join(self.dp_imgs, f"img2_{self.user_id}.npy"), os.path.join(self.dp_imgs, f"img1_{self.user_id}.npy"))
|
2023-02-19 14:46:02 +00:00
|
|
|
fp_multi = self.multi_concat()
|
|
|
|
list_out = [fp_multi]
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
list_out.extend([os.path.join(self.dp_imgs, f"img2_{self.user_id}.jpg")])
|
2023-02-22 09:15:03 +00:00
|
|
|
list_out.extend([self.fp_img_empty] * 4)
|
2023-02-19 14:46:02 +00:00
|
|
|
list_out.append(gr.update(interactive=False, value=prompt2))
|
|
|
|
list_out.append(gr.update(interactive=False, value=seed2))
|
2023-01-15 15:52:42 +00:00
|
|
|
list_out.append("")
|
|
|
|
list_out.append(np.random.randint(0, 10000000))
|
2023-02-19 14:46:02 +00:00
|
|
|
print(f"stack_forward: fp_multi {fp_multi}")
|
|
|
|
return list_out
|
|
|
|
|
2023-02-18 06:56:30 +00:00
|
|
|
def multi_concat(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Concatentates all stacked segments into one long movie.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
list_fp_movies = self.get_fp_video_all()
|
2023-02-18 06:56:30 +00:00
|
|
|
# Concatenate movies and save
|
2023-02-21 18:46:16 +00:00
|
|
|
fp_final = os.path.join(self.dp_session, f"concat_{self.user_id}.mp4")
|
2023-02-18 06:56:30 +00:00
|
|
|
concatenate_movies(fp_final, list_fp_movies)
|
|
|
|
return fp_final
|
2023-02-19 14:46:02 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
def get_fp_video_all(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Collects all stacked movie segments.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
list_all = os.listdir(self.dp_movies)
|
|
|
|
str_beg = f"movie_{self.user_id}_"
|
|
|
|
list_user = [l for l in list_all if str_beg in l]
|
|
|
|
list_user.sort()
|
|
|
|
list_user = [os.path.join(self.dp_movies, l) for l in list_user]
|
|
|
|
return list_user
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
def get_fp_video_next(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Gets the filepath of the next movie segment.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
list_videos = self.get_fp_video_all()
|
|
|
|
if len(list_videos) == 0:
|
|
|
|
idx_next = 0
|
|
|
|
else:
|
|
|
|
idx_next = len(list_videos)
|
|
|
|
fp_video_next = os.path.join(self.dp_movies, f"movie_{self.user_id}_{str(idx_next).zfill(3)}.mp4")
|
|
|
|
return fp_video_next
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
def get_fp_video_last(self):
|
2023-02-22 09:15:03 +00:00
|
|
|
r"""
|
|
|
|
Gets the current video that was saved.
|
|
|
|
"""
|
2023-02-21 18:46:16 +00:00
|
|
|
fp_video_last = os.path.join(self.dp_movies, f"last_{self.user_id}.mp4")
|
|
|
|
return fp_video_last
|
|
|
|
|
|
|
|
|
2023-02-22 09:15:03 +00:00
|
|
|
if __name__ == "__main__":
|
2023-07-07 07:07:15 +00:00
|
|
|
# fp_ckpt = hf_hub_download(repo_id="stabilityai/stable-diffusion-2-1-base", filename="v2-1_512-ema-pruned.ckpt")
|
|
|
|
fp_ckpt = hf_hub_download(repo_id="stabilityai/stable-diffusion-2-1", filename="v2-1_768-ema-pruned.ckpt")
|
2023-02-22 09:15:03 +00:00
|
|
|
bf = BlendingFrontend(StableDiffusionHolder(fp_ckpt))
|
|
|
|
# self = BlendingFrontend(None)
|
|
|
|
|
2023-01-12 03:11:56 +00:00
|
|
|
with gr.Blocks() as demo:
|
2023-02-23 09:32:53 +00:00
|
|
|
gr.HTML("""<h1>Latent Blending</h1>
|
|
|
|
<p>Create butter-smooth transitions between prompts, powered by stable diffusion</p>
|
2023-02-22 17:36:14 +00:00
|
|
|
<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.
|
|
|
|
<br/>
|
2023-02-23 09:32:53 +00:00
|
|
|
<a href="https://huggingface.co/spaces/lunarring/latentblending?duplicate=true">
|
2023-02-22 17:36:14 +00:00
|
|
|
<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
2023-02-23 09:32:53 +00:00
|
|
|
</p>""")
|
2023-02-22 17:36:14 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
|
|
|
prompt1 = gr.Textbox(label="prompt 1")
|
|
|
|
prompt2 = gr.Textbox(label="prompt 2")
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
2023-02-22 17:36:14 +00:00
|
|
|
duration_compute = gr.Slider(10, 25, bf.t_compute_max_allowed, step=1, label='waiting time', interactive=True)
|
2023-02-22 09:15:03 +00:00
|
|
|
duration_video = gr.Slider(1, 100, bf.duration_video, step=0.1, label='video duration', interactive=True)
|
2023-07-07 07:07:15 +00:00
|
|
|
height = gr.Slider(256, 1024, bf.height, step=128, label='height', interactive=True)
|
|
|
|
width = gr.Slider(256, 1024, bf.width, step=128, label='width', interactive=True)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Accordion("Advanced Settings (click to expand)", open=False):
|
|
|
|
|
|
|
|
with gr.Accordion("Diffusion settings", open=True):
|
|
|
|
with gr.Row():
|
|
|
|
num_inference_steps = gr.Slider(5, 100, bf.num_inference_steps, step=1, label='num_inference_steps', interactive=True)
|
2023-02-22 09:15:03 +00:00
|
|
|
guidance_scale = gr.Slider(1, 25, bf.guidance_scale, step=0.1, label='guidance_scale', interactive=True)
|
|
|
|
negative_prompt = gr.Textbox(label="negative prompt")
|
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Accordion("Seed control: adjust seeds for first and last images", open=True):
|
|
|
|
with gr.Row():
|
|
|
|
b_newseed1 = gr.Button("randomize seed 1", variant='secondary')
|
|
|
|
seed1 = gr.Number(bf.seed1, label="seed 1", interactive=True)
|
|
|
|
seed2 = gr.Number(bf.seed2, label="seed 2", interactive=True)
|
|
|
|
b_newseed2 = gr.Button("randomize seed 2", variant='secondary')
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Accordion("Last image crossfeeding.", open=True):
|
|
|
|
with gr.Row():
|
2023-02-22 09:15:03 +00:00
|
|
|
branch1_crossfeed_power = gr.Slider(0.0, 1.0, bf.branch1_crossfeed_power, step=0.01, label='branch1 crossfeed power', interactive=True)
|
|
|
|
branch1_crossfeed_range = gr.Slider(0.0, 1.0, bf.branch1_crossfeed_range, step=0.01, label='branch1 crossfeed range', interactive=True)
|
|
|
|
branch1_crossfeed_decay = gr.Slider(0.0, 1.0, bf.branch1_crossfeed_decay, step=0.01, label='branch1 crossfeed decay', interactive=True)
|
2023-02-21 11:15:55 +00:00
|
|
|
|
|
|
|
with gr.Accordion("Transition settings", open=True):
|
|
|
|
with gr.Row():
|
2023-02-22 09:15:03 +00:00
|
|
|
parental_crossfeed_power = gr.Slider(0.0, 1.0, bf.parental_crossfeed_power, step=0.01, label='parental crossfeed power', interactive=True)
|
|
|
|
parental_crossfeed_range = gr.Slider(0.0, 1.0, bf.parental_crossfeed_range, step=0.01, label='parental crossfeed range', interactive=True)
|
|
|
|
parental_crossfeed_power_decay = gr.Slider(0.0, 1.0, bf.parental_crossfeed_power_decay, step=0.01, label='parental crossfeed decay', interactive=True)
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
2023-02-22 09:15:03 +00:00
|
|
|
depth_strength = gr.Slider(0.01, 0.99, bf.depth_strength, step=0.01, label='depth_strength', interactive=True)
|
|
|
|
guidance_scale_mid_damper = gr.Slider(0.01, 2.0, bf.guidance_scale_mid_damper, step=0.01, label='guidance_scale_mid_damper', interactive=True)
|
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
2023-02-22 17:36:14 +00:00
|
|
|
b_compute1 = gr.Button('step1: compute first image', variant='primary')
|
|
|
|
b_compute2 = gr.Button('step2: compute last image', variant='primary')
|
|
|
|
b_compute_transition = gr.Button('step3: compute transition', variant='primary')
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
|
|
|
img1 = gr.Image(label="1/5")
|
|
|
|
img2 = gr.Image(label="2/5", show_progress=False)
|
|
|
|
img3 = gr.Image(label="3/5", show_progress=False)
|
|
|
|
img4 = gr.Image(label="4/5", show_progress=False)
|
|
|
|
img5 = gr.Image(label="5/5")
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
2023-02-22 09:15:03 +00:00
|
|
|
vid_single = gr.Video(label="current single trans")
|
|
|
|
vid_multi = gr.Video(label="concatented multi trans")
|
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
with gr.Row():
|
|
|
|
b_stackforward = gr.Button('append last movie segment (left) to multi movie (right)', variant='primary')
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:52:57 +00:00
|
|
|
with gr.Row():
|
|
|
|
gr.Markdown(
|
|
|
|
"""
|
|
|
|
# Parameters
|
|
|
|
## Main
|
2023-07-07 07:07:15 +00:00
|
|
|
- waiting time: set your waiting time for the transition. high values = better quality
|
2023-02-21 11:52:57 +00:00
|
|
|
- video duration: seconds per segment
|
|
|
|
- height/width: in pixels
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:52:57 +00:00
|
|
|
## Diffusion settings
|
|
|
|
- num_inference_steps: number of diffusion steps
|
|
|
|
- guidance_scale: latent blending seems to prefer lower values here
|
|
|
|
- negative prompt: enter negative prompt here, applied for all images
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:54:34 +00:00
|
|
|
## Last image crossfeeding
|
2023-02-21 11:52:57 +00:00
|
|
|
- branch1_crossfeed_power: Controls the level of cross-feeding between the first and last image branch. For preserving structures.
|
|
|
|
- branch1_crossfeed_range: Sets the duration of active crossfeed during development. High values enforce strong structural similarity.
|
|
|
|
- branch1_crossfeed_decay: Sets decay for branch1_crossfeed_power. Lower values make the decay stronger across the range.
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:52:57 +00:00
|
|
|
## Transition settings
|
|
|
|
- parental_crossfeed_power: Similar to branch1_crossfeed_power, however applied for the images withinin the transition.
|
|
|
|
- parental_crossfeed_range: Similar to branch1_crossfeed_range, however applied for the images withinin the transition.
|
|
|
|
- parental_crossfeed_power_decay: Similar to branch1_crossfeed_decay, however applied for the images withinin the transition.
|
|
|
|
- depth_strength: Determines when the blending process will begin in terms of diffusion steps. Low values more inventive but can cause motion.
|
|
|
|
- guidance_scale_mid_damper: Decreases the guidance scale in the middle of a transition.
|
2023-02-22 09:15:03 +00:00
|
|
|
""")
|
|
|
|
|
2023-02-21 18:46:16 +00:00
|
|
|
with gr.Row():
|
|
|
|
user_id = gr.Textbox(label="user id", interactive=False)
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
# Collect all UI elemts in list to easily pass as inputs in gradio
|
|
|
|
dict_ui_elem = {}
|
|
|
|
dict_ui_elem["prompt1"] = prompt1
|
|
|
|
dict_ui_elem["negative_prompt"] = negative_prompt
|
|
|
|
dict_ui_elem["prompt2"] = prompt2
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
dict_ui_elem["duration_compute"] = duration_compute
|
|
|
|
dict_ui_elem["duration_video"] = duration_video
|
|
|
|
dict_ui_elem["height"] = height
|
|
|
|
dict_ui_elem["width"] = width
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
dict_ui_elem["depth_strength"] = depth_strength
|
|
|
|
dict_ui_elem["branch1_crossfeed_power"] = branch1_crossfeed_power
|
|
|
|
dict_ui_elem["branch1_crossfeed_range"] = branch1_crossfeed_range
|
|
|
|
dict_ui_elem["branch1_crossfeed_decay"] = branch1_crossfeed_decay
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
dict_ui_elem["num_inference_steps"] = num_inference_steps
|
|
|
|
dict_ui_elem["guidance_scale"] = guidance_scale
|
|
|
|
dict_ui_elem["guidance_scale_mid_damper"] = guidance_scale_mid_damper
|
|
|
|
dict_ui_elem["seed1"] = seed1
|
|
|
|
dict_ui_elem["seed2"] = seed2
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
dict_ui_elem["parental_crossfeed_range"] = parental_crossfeed_range
|
|
|
|
dict_ui_elem["parental_crossfeed_power"] = parental_crossfeed_power
|
|
|
|
dict_ui_elem["parental_crossfeed_power_decay"] = parental_crossfeed_power_decay
|
2023-02-21 18:46:16 +00:00
|
|
|
dict_ui_elem["user_id"] = user_id
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
# Convert to list, as gradio doesn't seem to accept dicts
|
2023-02-22 09:15:03 +00:00
|
|
|
list_ui_vals = []
|
2023-02-21 11:15:55 +00:00
|
|
|
list_ui_keys = []
|
|
|
|
for k in dict_ui_elem.keys():
|
2023-02-22 09:15:03 +00:00
|
|
|
list_ui_vals.append(dict_ui_elem[k])
|
2023-02-21 11:15:55 +00:00
|
|
|
list_ui_keys.append(k)
|
|
|
|
bf.list_ui_keys = list_ui_keys
|
2023-02-22 09:15:03 +00:00
|
|
|
|
2023-02-21 11:15:55 +00:00
|
|
|
b_newseed1.click(bf.randomize_seed1, outputs=seed1)
|
|
|
|
b_newseed2.click(bf.randomize_seed2, outputs=seed2)
|
2023-02-22 09:15:03 +00:00
|
|
|
b_compute1.click(bf.compute_img1, inputs=list_ui_vals, outputs=[img1, img2, img3, img4, img5, user_id])
|
|
|
|
b_compute2.click(bf.compute_img2, inputs=list_ui_vals, outputs=[img2, img3, img4, img5, user_id])
|
|
|
|
b_compute_transition.click(bf.compute_transition,
|
|
|
|
inputs=list_ui_vals,
|
|
|
|
outputs=[img2, img3, img4, vid_single])
|
|
|
|
|
|
|
|
b_stackforward.click(bf.stack_forward,
|
|
|
|
inputs=[prompt2, seed2],
|
|
|
|
outputs=[vid_multi, img1, img2, img3, img4, img5, prompt1, seed1, prompt2])
|
|
|
|
|
2023-02-20 07:29:21 +00:00
|
|
|
demo.launch(share=bf.share, inbrowser=True, inline=False)
|