1
0
mirror of https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git synced 2026-06-07 10:10:23 +08:00

fix(web): unload model error

IndexError: tuple index out of range
This commit is contained in:
源文雨
2024-06-03 16:07:03 +09:00
parent 17e703a9ad
commit ccecac6133
5 changed files with 41 additions and 31 deletions

View File

@@ -190,27 +190,30 @@ def _extend_difference(n, a, b):
def hash_similarity(h1: str, h2: str) -> float:
h1b, h2b = decode_from_string(h1), decode_from_string(h2)
if len(h1b) != half_hash_len * 2 or len(h2b) != half_hash_len * 2:
raise Exception("invalid hash length")
h1n, h2n = np.frombuffer(h1b, dtype=">i2"), np.frombuffer(h2b, dtype=">i2")
d = 0
for i in range(half_hash_len // 4):
a = i * 2
b = a + 1
ax = complex(h1n[a], h1n[b])
bx = complex(h2n[a], h2n[b])
if abs(ax) == 0 or abs(bx) == 0:
continue
d += np.abs(ax - bx)
frac = np.linalg.norm(h1n) * np.linalg.norm(h2n)
cosine = (
np.dot(h1n.astype(np.float32), h2n.astype(np.float32)) / frac
if frac != 0
else 1.0
)
distance = _extend_difference(np.exp(-d / expand_factor), 0.5, 1.0)
return round((abs(cosine) + distance) / 2, 6)
try:
h1b, h2b = decode_from_string(h1), decode_from_string(h2)
if len(h1b) != half_hash_len * 2 or len(h2b) != half_hash_len * 2:
raise Exception("invalid hash length")
h1n, h2n = np.frombuffer(h1b, dtype=">i2"), np.frombuffer(h2b, dtype=">i2")
d = 0
for i in range(half_hash_len // 4):
a = i * 2
b = a + 1
ax = complex(h1n[a], h1n[b])
bx = complex(h2n[a], h2n[b])
if abs(ax) == 0 or abs(bx) == 0:
continue
d += np.abs(ax - bx)
frac = np.linalg.norm(h1n) * np.linalg.norm(h2n)
cosine = (
np.dot(h1n.astype(np.float32), h2n.astype(np.float32)) / frac
if frac != 0
else 1.0
)
distance = _extend_difference(np.exp(-d / expand_factor), 0.5, 1.0)
return round((abs(cosine) + distance) / 2, 6)
except Exception as e:
return str(e)
def hash_id(h: str) -> str:

View File

@@ -89,11 +89,15 @@ class VC:
elif torch.backends.mps.is_available():
torch.mps.empty_cache()
return (
{"visible": False, "__type__": "update"},
to_return_protect0,
to_return_protect1,
"",
"",
(
{"visible": False, "__type__": "update"},
to_return_protect0,
to_return_protect1,
{"value": to_return_protect[2], "__type__": "update"},
{"value": to_return_protect[3], "__type__": "update"},
{"value": "", "__type__": "update"},
) if to_return_protect
else {"visible": True, "maximum": 0, "__type__": "update"}
)
person = f'{os.getenv("weight_root")}/{sid}'
logger.info(f"Loading: {person}")
@@ -221,10 +225,10 @@ class VC:
% (index_info, *times),
(tgt_sr, audio_opt),
)
except:
except Exception as e:
info = traceback.format_exc()
logger.warning(info)
return info, (None, None)
return str(e), None
def vc_multi(
self,

View File

@@ -253,7 +253,10 @@ class Pipeline(object):
# _, I = index.search(npy, 1)
# npy = big_npy[I.squeeze()]
score, ix = index.search(npy, k=8)
try:
score, ix = index.search(npy, k=8)
except:
raise Exception("index mistatch")
weight = np.square(1 / score)
weight /= weight.sum(axis=1, keepdims=True)
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)