Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
May 9, 2024: Added new audio visualization features, more information can be found [here](https://github.com/get-salt-AI/SaltAI_AudioViz)
  • Loading branch information
WAS-PlaiLabs committed May 9, 2024
1 parent dce2ba0 commit e0cbbfc
Show file tree
Hide file tree
Showing 13 changed files with 5,245 additions and 0 deletions.
154 changes: 154 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
*.pyc
*.zip
28 changes: 28 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

from SaltAI_AudioViz.modules.node_importer import ModuleLoader

ROOT = os.path.abspath(os.path.dirname(__file__))
NAME = "Salt.AI AudioViz"
PACKAGE = "SaltAI_AudioViz"
NODES_DIR = os.path.join(ROOT, 'nodes')
EXTENSION_WEB_DIRS = {}
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}

# Load modules
module_timings = {}
module_loader = ModuleLoader(PACKAGE)
module_loader.load_modules(NODES_DIR)

# Mappings
NODE_CLASS_MAPPINGS = module_loader.NODE_CLASS_MAPPINGS
NODE_DISPLAY_NAME_MAPPINGS = module_loader.NODE_DISPLAY_NAME_MAPPINGS

# Timings and such
print("")
module_loader.report(NAME)
print("")

# Export nodes
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
55 changes: 55 additions & 0 deletions modules/blend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import math
import torch
import torch.nn.functional as F

def slerp(strength, tensor_from, tensor_to, epsilon=1e-6):
low_norm = F.normalize(tensor_from, p=2, dim=-1, eps=epsilon)
high_norm = F.normalize(tensor_to, p=2, dim=-1, eps=epsilon)

dot_product = torch.clamp((low_norm * high_norm).sum(dim=-1), -1.0, 1.0)
omega = torch.acos(dot_product)
so = torch.sin(omega)
zero_so_mask = torch.isclose(so, torch.tensor([0.0], device=so.device), atol=epsilon)
so = torch.where(zero_so_mask, torch.tensor([1.0], device=so.device), so)
sin_omega_minus_strength = torch.sin((1.0 - strength) * omega) / so
sin_strength_omega = torch.sin(strength * omega) / so

res = sin_omega_minus_strength.unsqueeze(-1) * tensor_from + sin_strength_omega.unsqueeze(-1) * tensor_to
res = torch.where(zero_so_mask.unsqueeze(-1),
tensor_from if strength < 0.5 else tensor_to,
res)
return res

# from https://discuss.pytorch.org/t/help-regarding-slerp-function-for-generative-model-sampling/32475
def slerp_latents(val, low, high):
dims = low.shape

#flatten to batches
low = low.reshape(dims[0], -1)
high = high.reshape(dims[0], -1)

low_norm = low/torch.norm(low, dim=1, keepdim=True)
high_norm = high/torch.norm(high, dim=1, keepdim=True)

# in case we divide by zero
low_norm[low_norm != low_norm] = 0.0
high_norm[high_norm != high_norm] = 0.0

omega = torch.acos((low_norm*high_norm).sum(1))
so = torch.sin(omega)
res = (torch.sin((1.0-val)*omega)/so).unsqueeze(1)*low + (torch.sin(val*omega)/so).unsqueeze(1) * high
return res.reshape(dims)

def blend_latents(alpha, latent_1, latent_2):
if not isinstance(alpha, torch.Tensor):
alpha = torch.tensor([alpha], dtype=latent_1.dtype, device=latent_1.device)

blended_latent = (1 - alpha) * latent_1 + alpha * latent_2

return blended_latent

def cosine_interp_latents(val, low, high):
if not isinstance(val, torch.Tensor):
val = torch.tensor([val], dtype=low.dtype, device=low.device)
t = (1 - torch.cos(val * math.pi)) / 2
return (1 - t) * low + t * high
Loading

0 comments on commit e0cbbfc

Please sign in to comment.