2022-12-27 01:25:19 -07:00
|
|
|
import dataclasses
|
|
|
|
import io
|
|
|
|
import typing as T
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pydub
|
|
|
|
import streamlit as st
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from riffusion.datatypes import InferenceInput, PromptInput
|
|
|
|
from riffusion.spectrogram_params import SpectrogramParams
|
|
|
|
from riffusion.streamlit import util as streamlit_util
|
|
|
|
|
|
|
|
|
2023-01-04 23:47:06 -07:00
|
|
|
def render_interpolation() -> None:
|
2022-12-27 01:25:19 -07:00
|
|
|
st.set_page_config(layout="wide", page_icon="🎸")
|
|
|
|
|
|
|
|
st.subheader(":performing_arts: Interpolation")
|
|
|
|
st.write(
|
|
|
|
"""
|
|
|
|
Interpolate between prompts in the latent space.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2023-01-05 18:55:01 -07:00
|
|
|
with st.expander("Help", False):
|
|
|
|
st.write(
|
|
|
|
"""
|
|
|
|
This tool allows specifying two endpoints and generating a long-form interpolation
|
|
|
|
between them that traverses the latent space. The interpolation is generated by
|
|
|
|
the method described at https://www.riffusion.com/about. A seed image is used to
|
|
|
|
set the beat and tempo of the generated audio, and can be set in the sidebar.
|
|
|
|
Usually the seed is changed or the prompt, but not both at once. You can browse
|
|
|
|
infinite variations of the same prompt by changing the seed.
|
|
|
|
|
|
|
|
For example, try going from "church bells" to "jazz" with 10 steps and 0.75 denoising.
|
|
|
|
This will generate a 50 second clip at 5 seconds per step. Then play with the seeds
|
|
|
|
or denoising to get different variations.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
# Sidebar params
|
|
|
|
|
|
|
|
device = streamlit_util.select_device(st.sidebar)
|
2023-01-14 14:59:36 -07:00
|
|
|
extension = streamlit_util.select_audio_extension(st.sidebar)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
|
|
|
num_interpolation_steps = T.cast(
|
|
|
|
int,
|
|
|
|
st.sidebar.number_input(
|
|
|
|
"Interpolation steps",
|
2023-01-16 19:16:02 -07:00
|
|
|
value=12,
|
2022-12-27 01:25:19 -07:00
|
|
|
min_value=1,
|
|
|
|
max_value=20,
|
|
|
|
help="Number of model generations between the two prompts. Controls the duration.",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
num_inference_steps = T.cast(
|
|
|
|
int,
|
|
|
|
st.sidebar.number_input(
|
|
|
|
"Steps per sample", value=50, help="Number of denoising steps per model run"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-01-14 12:31:33 -07:00
|
|
|
guidance = st.sidebar.number_input(
|
|
|
|
"Guidance",
|
|
|
|
value=7.0,
|
|
|
|
help="How much the model listens to the text prompt",
|
|
|
|
)
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
init_image_name = st.sidebar.selectbox(
|
|
|
|
"Seed image",
|
|
|
|
# TODO(hayk): Read from directory
|
2022-12-27 08:22:02 -07:00
|
|
|
options=["og_beat", "agile", "marim", "motorway", "vibes", "custom"],
|
2022-12-27 01:25:19 -07:00
|
|
|
index=0,
|
2023-01-05 18:55:01 -07:00
|
|
|
help="Which seed image to use for img2img. Custom allows uploading your own.",
|
2022-12-27 01:25:19 -07:00
|
|
|
)
|
|
|
|
assert init_image_name is not None
|
2022-12-27 08:22:02 -07:00
|
|
|
if init_image_name == "custom":
|
|
|
|
init_image_file = st.sidebar.file_uploader(
|
|
|
|
"Upload a custom seed image",
|
2023-01-14 14:59:36 -07:00
|
|
|
type=streamlit_util.IMAGE_EXTENSIONS,
|
2022-12-27 08:22:02 -07:00
|
|
|
label_visibility="collapsed",
|
|
|
|
)
|
|
|
|
if init_image_file:
|
|
|
|
st.sidebar.image(init_image_file)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
2023-01-16 19:16:02 -07:00
|
|
|
alpha_power = st.sidebar.number_input("Alpha Power", value=1.0)
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
show_individual_outputs = st.sidebar.checkbox(
|
|
|
|
"Show individual outputs",
|
|
|
|
value=False,
|
|
|
|
help="Show each model output",
|
|
|
|
)
|
|
|
|
show_images = st.sidebar.checkbox(
|
|
|
|
"Show individual images",
|
|
|
|
value=False,
|
|
|
|
help="Show each generated image",
|
|
|
|
)
|
|
|
|
|
2023-01-16 19:16:02 -07:00
|
|
|
alphas = np.linspace(0, 1, num_interpolation_steps)
|
|
|
|
|
|
|
|
# Apply power scaling to alphas to customize the interpolation curve
|
|
|
|
alphas_shifted = alphas * 2 - 1
|
|
|
|
alphas_shifted = (np.abs(alphas_shifted) ** alpha_power * np.sign(alphas_shifted) + 1) / 2
|
|
|
|
alphas = alphas_shifted
|
|
|
|
|
|
|
|
alphas_str = ", ".join([f"{alpha:.2f}" for alpha in alphas])
|
|
|
|
st.write(f"**Alphas** : [{alphas_str}]")
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
# Prompt inputs A and B in two columns
|
|
|
|
|
2022-12-27 08:22:02 -07:00
|
|
|
with st.form(key="interpolation_form"):
|
|
|
|
left, right = st.columns(2)
|
|
|
|
|
|
|
|
with left:
|
|
|
|
st.write("##### Prompt A")
|
2023-01-16 19:52:07 -07:00
|
|
|
prompt_input_a = PromptInput(
|
|
|
|
guidance=guidance, **get_prompt_inputs(key="a", denoising_default=0.75)
|
|
|
|
)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
2022-12-27 08:22:02 -07:00
|
|
|
with right:
|
|
|
|
st.write("##### Prompt B")
|
2023-01-16 19:52:07 -07:00
|
|
|
prompt_input_b = PromptInput(
|
|
|
|
guidance=guidance, **get_prompt_inputs(key="b", denoising_default=0.75)
|
|
|
|
)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
2022-12-27 08:22:02 -07:00
|
|
|
st.form_submit_button("Generate", type="primary")
|
2022-12-27 01:25:19 -07:00
|
|
|
|
|
|
|
if not prompt_input_a.prompt or not prompt_input_b.prompt:
|
|
|
|
st.info("Enter both prompts to interpolate between them")
|
|
|
|
return
|
|
|
|
|
2022-12-27 08:22:02 -07:00
|
|
|
if init_image_name == "custom":
|
|
|
|
if not init_image_file:
|
|
|
|
st.info("Upload a custom seed image")
|
|
|
|
return
|
|
|
|
init_image = Image.open(init_image_file).convert("RGB")
|
|
|
|
else:
|
|
|
|
init_image_path = (
|
|
|
|
Path(__file__).parent.parent.parent.parent / "seed_images" / f"{init_image_name}.png"
|
|
|
|
)
|
|
|
|
init_image = Image.open(str(init_image_path)).convert("RGB")
|
2022-12-27 01:25:19 -07:00
|
|
|
|
|
|
|
# TODO(hayk): Move this code into a shared place and add to riffusion.cli
|
|
|
|
image_list: T.List[Image.Image] = []
|
|
|
|
audio_bytes_list: T.List[io.BytesIO] = []
|
|
|
|
for i, alpha in enumerate(alphas):
|
|
|
|
inputs = InferenceInput(
|
|
|
|
alpha=float(alpha),
|
|
|
|
num_inference_steps=num_inference_steps,
|
|
|
|
seed_image_id="og_beat",
|
|
|
|
start=prompt_input_a,
|
|
|
|
end=prompt_input_b,
|
|
|
|
)
|
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
with st.expander("Example input JSON", expanded=False):
|
|
|
|
st.json(dataclasses.asdict(inputs))
|
|
|
|
|
|
|
|
image, audio_bytes = run_interpolation(
|
|
|
|
inputs=inputs,
|
|
|
|
init_image=init_image,
|
|
|
|
device=device,
|
2023-01-14 14:59:36 -07:00
|
|
|
extension=extension,
|
2022-12-27 01:25:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
if show_individual_outputs:
|
|
|
|
st.write(f"#### ({i + 1} / {len(alphas)}) Alpha={alpha:.2f}")
|
|
|
|
if show_images:
|
|
|
|
st.image(image)
|
|
|
|
st.audio(audio_bytes)
|
|
|
|
|
|
|
|
image_list.append(image)
|
|
|
|
audio_bytes_list.append(audio_bytes)
|
|
|
|
|
|
|
|
st.write("#### Final Output")
|
|
|
|
|
2023-01-14 14:59:36 -07:00
|
|
|
# TODO(hayk): Concatenate with overlap and better blending like in audio to audio
|
2022-12-27 01:25:19 -07:00
|
|
|
audio_segments = [pydub.AudioSegment.from_file(audio_bytes) for audio_bytes in audio_bytes_list]
|
|
|
|
concat_segment = audio_segments[0]
|
|
|
|
for segment in audio_segments[1:]:
|
|
|
|
concat_segment = concat_segment.append(segment, crossfade=0)
|
|
|
|
|
|
|
|
audio_bytes = io.BytesIO()
|
2023-01-14 14:59:36 -07:00
|
|
|
concat_segment.export(audio_bytes, format=extension)
|
2022-12-27 01:25:19 -07:00
|
|
|
audio_bytes.seek(0)
|
|
|
|
|
|
|
|
st.write(f"Duration: {concat_segment.duration_seconds:.3f} seconds")
|
|
|
|
st.audio(audio_bytes)
|
|
|
|
|
2023-01-14 14:59:36 -07:00
|
|
|
output_name = (
|
|
|
|
f"{prompt_input_a.prompt.replace(' ', '_')}_"
|
|
|
|
f"{prompt_input_b.prompt.replace(' ', '_')}.{extension}"
|
|
|
|
)
|
|
|
|
st.download_button(
|
|
|
|
output_name,
|
|
|
|
data=audio_bytes,
|
|
|
|
file_name=output_name,
|
|
|
|
mime=f"audio/{extension}",
|
|
|
|
)
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
|
2023-01-14 12:31:33 -07:00
|
|
|
def get_prompt_inputs(
|
|
|
|
key: str,
|
|
|
|
include_negative_prompt: bool = False,
|
|
|
|
cols: bool = False,
|
2023-01-16 19:52:07 -07:00
|
|
|
denoising_default: float = 0.5,
|
2023-01-14 12:31:33 -07:00
|
|
|
) -> T.Dict[str, T.Any]:
|
2022-12-27 01:25:19 -07:00
|
|
|
"""
|
|
|
|
Compute prompt inputs from widgets.
|
|
|
|
"""
|
2023-01-14 12:31:33 -07:00
|
|
|
p: T.Dict[str, T.Any] = {}
|
|
|
|
|
|
|
|
# Optionally use columns
|
|
|
|
left, right = T.cast(T.Any, st.columns(2) if cols else (st, st))
|
|
|
|
|
|
|
|
visibility = "visible" if cols else "collapsed"
|
|
|
|
p["prompt"] = left.text_input("Prompt", label_visibility=visibility, key=f"prompt_{key}")
|
|
|
|
|
|
|
|
if include_negative_prompt:
|
|
|
|
p["negative_prompt"] = right.text_input("Negative Prompt", key=f"negative_prompt_{key}")
|
|
|
|
|
|
|
|
p["seed"] = T.cast(
|
2023-01-05 18:55:01 -07:00
|
|
|
int,
|
2023-01-14 12:31:33 -07:00
|
|
|
left.number_input(
|
2023-01-05 18:55:01 -07:00
|
|
|
"Seed",
|
|
|
|
value=42,
|
|
|
|
key=f"seed_{key}",
|
|
|
|
help="Integer used to generate a random result. Vary this to explore alternatives.",
|
|
|
|
),
|
|
|
|
)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
2023-01-14 12:31:33 -07:00
|
|
|
p["denoising"] = right.number_input(
|
|
|
|
"Denoising",
|
2023-01-16 19:52:07 -07:00
|
|
|
value=denoising_default,
|
2023-01-14 12:31:33 -07:00
|
|
|
key=f"denoising_{key}",
|
|
|
|
help="How much to modify the seed image",
|
2022-12-27 01:25:19 -07:00
|
|
|
)
|
|
|
|
|
2023-01-14 12:31:33 -07:00
|
|
|
return p
|
|
|
|
|
2022-12-27 01:25:19 -07:00
|
|
|
|
|
|
|
@st.experimental_memo
|
|
|
|
def run_interpolation(
|
2023-01-14 14:59:36 -07:00
|
|
|
inputs: InferenceInput, init_image: Image.Image, device: str = "cuda", extension: str = "mp3"
|
2022-12-27 01:25:19 -07:00
|
|
|
) -> T.Tuple[Image.Image, io.BytesIO]:
|
|
|
|
"""
|
|
|
|
Cached function for riffusion interpolation.
|
|
|
|
"""
|
2022-12-27 08:22:02 -07:00
|
|
|
pipeline = streamlit_util.load_riffusion_checkpoint(
|
|
|
|
device=device,
|
|
|
|
# No trace so we can have variable width
|
|
|
|
no_traced_unet=True,
|
|
|
|
)
|
2022-12-27 01:25:19 -07:00
|
|
|
|
|
|
|
image = pipeline.riffuse(
|
|
|
|
inputs,
|
|
|
|
init_image=init_image,
|
|
|
|
mask_image=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO(hayk): Change the frequency range to [20, 20k] once the model is retrained
|
|
|
|
params = SpectrogramParams(
|
|
|
|
min_frequency=0,
|
|
|
|
max_frequency=10000,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Reconstruct from image to audio
|
|
|
|
audio_bytes = streamlit_util.audio_bytes_from_spectrogram_image(
|
|
|
|
image=image,
|
|
|
|
params=params,
|
|
|
|
device=device,
|
2023-01-14 14:59:36 -07:00
|
|
|
output_format=extension,
|
2022-12-27 01:25:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
return image, audio_bytes
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-01-04 23:47:06 -07:00
|
|
|
render_interpolation()
|