mirror of
https://github.com/SillyTavern/SillyTavern-Extras.git
synced 2026-05-01 03:41:24 +00:00
Add error handing to emotion RVC mode
This commit is contained in:
@@ -62,7 +62,7 @@ def rvc_get_models_list():
|
|||||||
if not os.path.isdir(folder_path):
|
if not os.path.isdir(folder_path):
|
||||||
print("> WARNING:",folder_name,"is not a folder, ignored")
|
print("> WARNING:",folder_name,"is not a folder, ignored")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("> Found model folder",folder_name)
|
print("> Found model folder",folder_name)
|
||||||
|
|
||||||
# Check pth
|
# Check pth
|
||||||
@@ -73,7 +73,7 @@ def rvc_get_models_list():
|
|||||||
valid_folder = True
|
valid_folder = True
|
||||||
if file_name.endswith(".index"):
|
if file_name.endswith(".index"):
|
||||||
print(" > index:",file_name)
|
print(" > index:",file_name)
|
||||||
|
|
||||||
if valid_folder:
|
if valid_folder:
|
||||||
print(" > Valid folder added to list")
|
print(" > Valid folder added to list")
|
||||||
model_list.append(folder_name)
|
model_list.append(folder_name)
|
||||||
@@ -103,10 +103,10 @@ def rvc_upload_models():
|
|||||||
|
|
||||||
request_file = request_files.get(request_file_name)
|
request_file = request_files.get(request_file_name)
|
||||||
request_file.save(zip_file_path)
|
request_file.save(zip_file_path)
|
||||||
|
|
||||||
model_folder_name, _ = os.path.splitext(request_file_name)
|
model_folder_name, _ = os.path.splitext(request_file_name)
|
||||||
model_folder_path = os.path.join(RVC_MODELS_PATH,model_folder_name)
|
model_folder_path = os.path.join(RVC_MODELS_PATH,model_folder_name)
|
||||||
|
|
||||||
shutil.unpack_archive(zip_file_path, model_folder_path)
|
shutil.unpack_archive(zip_file_path, model_folder_path)
|
||||||
|
|
||||||
print("> Cleaning up model folder",model_folder_path)
|
print("> Cleaning up model folder",model_folder_path)
|
||||||
@@ -129,7 +129,7 @@ def rvc_upload_models():
|
|||||||
os.rmdir(folder_path)
|
os.rmdir(folder_path)
|
||||||
|
|
||||||
print("> Success")
|
print("> Success")
|
||||||
|
|
||||||
response = json.dumps({"status":"ok"})
|
response = json.dumps({"status":"ok"})
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ def rvc_process_audio():
|
|||||||
try:
|
try:
|
||||||
file = request.files.get('AudioFile')
|
file = request.files.get('AudioFile')
|
||||||
print(DEBUG_PREFIX, "received:", file)
|
print(DEBUG_PREFIX, "received:", file)
|
||||||
|
|
||||||
# Create new instances of io.BytesIO() for each request
|
# Create new instances of io.BytesIO() for each request
|
||||||
input_audio_path = io.BytesIO()
|
input_audio_path = io.BytesIO()
|
||||||
output_audio_path = io.BytesIO()
|
output_audio_path = io.BytesIO()
|
||||||
@@ -164,14 +164,14 @@ def rvc_process_audio():
|
|||||||
if save_file:
|
if save_file:
|
||||||
input_audio_path = RVC_INPUT_PATH
|
input_audio_path = RVC_INPUT_PATH
|
||||||
output_audio_path = RVC_OUTPUT_PATH
|
output_audio_path = RVC_OUTPUT_PATH
|
||||||
|
|
||||||
file.save(input_audio_path)
|
file.save(input_audio_path)
|
||||||
|
|
||||||
if not save_file:
|
if not save_file:
|
||||||
input_audio_path.seek(0)
|
input_audio_path.seek(0)
|
||||||
|
|
||||||
parameters = json.loads(request.form["json"])
|
parameters = json.loads(request.form["json"])
|
||||||
|
|
||||||
print(DEBUG_PREFIX, "Received audio conversion request with model", parameters)
|
print(DEBUG_PREFIX, "Received audio conversion request with model", parameters)
|
||||||
|
|
||||||
folder_path = RVC_MODELS_PATH+parameters["modelName"]+"/"
|
folder_path = RVC_MODELS_PATH+parameters["modelName"]+"/"
|
||||||
@@ -180,35 +180,40 @@ def rvc_process_audio():
|
|||||||
|
|
||||||
# HACK: emotion mode EXPERIMENTAL
|
# HACK: emotion mode EXPERIMENTAL
|
||||||
if classification_mode:
|
if classification_mode:
|
||||||
print(DEBUG_PREFIX,"EXPERIMENT MODE: emotions")
|
try:
|
||||||
|
print(DEBUG_PREFIX,"EXPERIMENT MODE: emotions")
|
||||||
|
|
||||||
print("> Searching overide code ($emotion$)")
|
print("> Searching overide code ($emotion$)")
|
||||||
emotion = None
|
emotion = None
|
||||||
for code in ["anger","fear", "joy","love","sadness","surprise"]:
|
for code in ["anger","fear", "joy","love","sadness","surprise"]:
|
||||||
if "$"+code+"$" in parameters["text"]:
|
if "$"+code+"$" in parameters["text"]:
|
||||||
print(" > Overide detected:",code)
|
print(" > Overide detected:",code)
|
||||||
emotion = code
|
emotion = code
|
||||||
parameters["text"] = parameters["text"].replace("$"+code+"$","")
|
parameters["text"] = parameters["text"].replace("$"+code+"$","")
|
||||||
print(parameters["text"])
|
print(parameters["text"])
|
||||||
break
|
break
|
||||||
|
|
||||||
if emotion is None:
|
|
||||||
print("> calling text classification pipeline")
|
|
||||||
emotions_score = classify_module.classify_text_emotion(parameters["text"])
|
|
||||||
|
|
||||||
print(" > ",emotions_score)
|
if emotion is None:
|
||||||
emotion = emotions_score[0]["label"]
|
print("> calling text classification pipeline")
|
||||||
print(" > Selected:", emotion)
|
emotions_score = classify_module.classify_text_emotion(parameters["text"])
|
||||||
|
|
||||||
model_path = folder_path+emotion+".pth"
|
print(" > ",emotions_score)
|
||||||
index_path = folder_path+emotion+".index"
|
emotion = emotions_score[0]["label"]
|
||||||
|
print(" > Selected:", emotion)
|
||||||
|
|
||||||
if not os.path.exists(model_path):
|
model_path = folder_path+emotion+".pth"
|
||||||
print(" > WARNING emotion model pth not found:",model_path," will try loading default")
|
index_path = folder_path+emotion+".index"
|
||||||
|
|
||||||
|
if not os.path.exists(model_path):
|
||||||
|
print(" > WARNING emotion model pth not found:",model_path," will try loading default")
|
||||||
|
model_path = None
|
||||||
|
|
||||||
|
if not os.path.exists(index_path):
|
||||||
|
print(" > WARNING emotion model index not found:",index_path)
|
||||||
|
index_path = None
|
||||||
|
except Exception as e:
|
||||||
|
print(f" > ERROR: emotion mode failed {str(e)}")
|
||||||
model_path = None
|
model_path = None
|
||||||
|
|
||||||
if not os.path.exists(index_path):
|
|
||||||
print(" > WARNING emotion model index not found:",index_path)
|
|
||||||
index_path = None
|
index_path = None
|
||||||
|
|
||||||
if model_path is None:
|
if model_path is None:
|
||||||
@@ -221,7 +226,7 @@ def rvc_process_audio():
|
|||||||
|
|
||||||
if model_path is None:
|
if model_path is None:
|
||||||
abort(500, DEBUG_PREFIX + " No pth file found.")
|
abort(500, DEBUG_PREFIX + " No pth file found.")
|
||||||
|
|
||||||
print(DEBUG_PREFIX, "Check for index file", folder_path)
|
print(DEBUG_PREFIX, "Check for index file", folder_path)
|
||||||
for file_name in os.listdir(folder_path):
|
for file_name in os.listdir(folder_path):
|
||||||
if file_name.endswith(".index"):
|
if file_name.endswith(".index"):
|
||||||
@@ -232,11 +237,11 @@ def rvc_process_audio():
|
|||||||
if index_path is None:
|
if index_path is None:
|
||||||
index_path = ""
|
index_path = ""
|
||||||
print(DEBUG_PREFIX, "no index file found, proceeding without index")
|
print(DEBUG_PREFIX, "no index file found, proceeding without index")
|
||||||
|
|
||||||
|
|
||||||
print(DEBUG_PREFIX, "loading", model_path)
|
print(DEBUG_PREFIX, "loading", model_path)
|
||||||
rvc.load_rvc(model_path)
|
rvc.load_rvc(model_path)
|
||||||
|
|
||||||
info, (tgt_sr, wav_opt) = rvc.vc_single(
|
info, (tgt_sr, wav_opt) = rvc.vc_single(
|
||||||
sid=0,
|
sid=0,
|
||||||
input_audio_path=input_audio_path,
|
input_audio_path=input_audio_path,
|
||||||
@@ -251,7 +256,7 @@ def rvc_process_audio():
|
|||||||
rms_mix_rate=float(parameters["rmsMixRate"]),
|
rms_mix_rate=float(parameters["rmsMixRate"]),
|
||||||
protect=float(parameters["protect"]),
|
protect=float(parameters["protect"]),
|
||||||
crepe_hop_length=128)
|
crepe_hop_length=128)
|
||||||
|
|
||||||
#print(info) # DBG
|
#print(info) # DBG
|
||||||
|
|
||||||
#out_path = os.path.join("data/", "rvc_output.wav")
|
#out_path = os.path.join("data/", "rvc_output.wav")
|
||||||
@@ -259,9 +264,9 @@ def rvc_process_audio():
|
|||||||
|
|
||||||
if not save_file:
|
if not save_file:
|
||||||
output_audio_path.seek(0) # Reset cursor position
|
output_audio_path.seek(0) # Reset cursor position
|
||||||
|
|
||||||
print(DEBUG_PREFIX, "Audio converted using RVC model:", rvc.rvc_model_name)
|
print(DEBUG_PREFIX, "Audio converted using RVC model:", rvc.rvc_model_name)
|
||||||
|
|
||||||
# Return the output_audio_path object as a response
|
# Return the output_audio_path object as a response
|
||||||
response = send_file(output_audio_path, mimetype="audio/x-wav")
|
response = send_file(output_audio_path, mimetype="audio/x-wav")
|
||||||
return response
|
return response
|
||||||
@@ -272,7 +277,7 @@ def rvc_process_audio():
|
|||||||
|
|
||||||
def fix_model_install():
|
def fix_model_install():
|
||||||
"""
|
"""
|
||||||
Fix RVC model organisation, move found pth/index file into
|
Fix RVC model organisation, move found pth/index file into
|
||||||
"""
|
"""
|
||||||
print(DEBUG_PREFIX,"Checking RVC models folder:",RVC_MODELS_PATH)
|
print(DEBUG_PREFIX,"Checking RVC models folder:",RVC_MODELS_PATH)
|
||||||
|
|
||||||
@@ -284,7 +289,7 @@ def fix_model_install():
|
|||||||
|
|
||||||
if file_name in IGNORED_FILES:
|
if file_name in IGNORED_FILES:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Must be a folder
|
# Must be a folder
|
||||||
if not os.path.isdir(file_path):
|
if not os.path.isdir(file_path):
|
||||||
new_folder_path, file_extension = os.path.splitext(file_path)
|
new_folder_path, file_extension = os.path.splitext(file_path)
|
||||||
@@ -313,21 +318,21 @@ def fix_model_install():
|
|||||||
os.rename(file_path, new_file_path)
|
os.rename(file_path, new_file_path)
|
||||||
print(" > File moved, new path:",new_file_path)
|
print(" > File moved, new path:",new_file_path)
|
||||||
|
|
||||||
# 2) search for index file and put in corresponding folder
|
# 2) search for index file and put in corresponding folder
|
||||||
file_names = os.listdir(RVC_MODELS_PATH)
|
file_names = os.listdir(RVC_MODELS_PATH)
|
||||||
print("> Searching for index files")
|
print("> Searching for index files")
|
||||||
for file_name in file_names:
|
for file_name in file_names:
|
||||||
file_path = os.path.join(RVC_MODELS_PATH,file_name)
|
file_path = os.path.join(RVC_MODELS_PATH,file_name)
|
||||||
|
|
||||||
if file_name in IGNORED_FILES:
|
if file_name in IGNORED_FILES:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Must be a folder
|
# Must be a folder
|
||||||
if not os.path.isdir(file_path):
|
if not os.path.isdir(file_path):
|
||||||
new_folder_path, file_extension = os.path.splitext(file_path)
|
new_folder_path, file_extension = os.path.splitext(file_path)
|
||||||
if file_extension != ".index":
|
if file_extension != ".index":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(" > WARNING: index file found!",file_path)
|
print(" > WARNING: index file found!",file_path)
|
||||||
print(" > Searching for possible model folder")
|
print(" > Searching for possible model folder")
|
||||||
|
|
||||||
@@ -349,10 +354,10 @@ def fix_model_install():
|
|||||||
else:
|
else:
|
||||||
os.rename(file_path, new_file_path)
|
os.rename(file_path, new_file_path)
|
||||||
print(" > File moved, new path:",new_file_path)
|
print(" > File moved, new path:",new_file_path)
|
||||||
|
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if not found:
|
if not found:
|
||||||
print(" > WARNING: no corresponding folder found, move or delete the file manually to stop warnings.")
|
print(" > WARNING: no corresponding folder found, move or delete the file manually to stop warnings.")
|
||||||
|
|
||||||
print(DEBUG_PREFIX,"RVC model folder checked.")
|
print(DEBUG_PREFIX,"RVC model folder checked.")
|
||||||
|
|||||||
Reference in New Issue
Block a user