File size: 2,063 Bytes
d160964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Load pinned upstream Vocos decoder class bodies without feature-extraction dependencies."""
import ast
import hashlib
from pathlib import Path, PurePosixPath

SOURCES={
    'vendor/vocos/models.py':'7031a1e223678cfeb522046f7e179e3ff9efd70aa32f969a4234880b59be1b71',
    'vendor/vocos/heads.py':'1ae5389805e63a77eaaf36502396591e34e0d92ccc4ae94e69e3f495f551a38d',
    'vendor/vocos/modules.py':'1132b0081333cc4abe1c36b2bd709185ceaa243519ea8903bec7ab08e67d5d64',
    'vendor/vocos/spectral_ops.py':'5577771daae83a9ffc6df3f1a3ce7f3f9c2543562c8ca3f5aff740ee2b515682',
}


def file(root,name):
    if not isinstance(name,str) or '\\' in name or ':' in name:raise ValueError('Unsafe asset path')
    rel=PurePosixPath(name)
    if rel.is_absolute() or not rel.parts or any(x in ('','.','..') for x in rel.parts):raise ValueError('Unsafe asset path')
    p=root.joinpath(*rel.parts)
    if any(root.joinpath(*rel.parts[:i]).is_symlink() for i in range(1,len(rel.parts)+1)) or not p.is_file() or not p.resolve().is_relative_to(root):raise ValueError('Invalid asset')
    return p


def sha(path):
    with path.open('rb') as f:return hashlib.file_digest(f,'sha256').hexdigest()


def exact_classes(root,namespace):
    selections=[('vendor/vocos/modules.py',('AdaLayerNorm','ConvNeXtBlock')),
                ('vendor/vocos/spectral_ops.py',('ISTFT',)),
                ('vendor/vocos/models.py',('Backbone','VocosBackbone')),
                ('vendor/vocos/heads.py',('FourierHead','ISTFTHead'))]
    for name,classes in selections:
        path=file(root,name)
        if sha(path)!=SOURCES[name]:raise ValueError('Upstream Vocos source differs')
        text=path.read_text(encoding='utf-8');tree=ast.parse(text)
        for classname in classes:
            nodes=[x for x in tree.body if isinstance(x,ast.ClassDef) and x.name==classname]
            if len(nodes)!=1:raise ValueError('Unexpected Vocos source structure')
            node=ast.fix_missing_locations(ast.Module(body=nodes,type_ignores=[]))
            exec(compile(node,str(path),'exec'),namespace)