From edee9dcd8546b9f0022646593413f1ae2a6fdfca Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:43:20 +0300 Subject: [PATCH] Add error handing to emotion RVC mode --- modules/voice_conversion/rvc_module.py | 103 +++++++++++++------------ 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/modules/voice_conversion/rvc_module.py b/modules/voice_conversion/rvc_module.py index 02daad4..db65150 100644 --- a/modules/voice_conversion/rvc_module.py +++ b/modules/voice_conversion/rvc_module.py @@ -62,7 +62,7 @@ def rvc_get_models_list(): if not os.path.isdir(folder_path): print("> WARNING:",folder_name,"is not a folder, ignored") continue - + print("> Found model folder",folder_name) # Check pth @@ -73,7 +73,7 @@ def rvc_get_models_list(): valid_folder = True if file_name.endswith(".index"): print(" > index:",file_name) - + if valid_folder: print(" > Valid folder added to list") model_list.append(folder_name) @@ -103,10 +103,10 @@ def rvc_upload_models(): request_file = request_files.get(request_file_name) request_file.save(zip_file_path) - + model_folder_name, _ = os.path.splitext(request_file_name) model_folder_path = os.path.join(RVC_MODELS_PATH,model_folder_name) - + shutil.unpack_archive(zip_file_path, model_folder_path) print("> Cleaning up model folder",model_folder_path) @@ -129,7 +129,7 @@ def rvc_upload_models(): os.rmdir(folder_path) print("> Success") - + response = json.dumps({"status":"ok"}) return response @@ -156,7 +156,7 @@ def rvc_process_audio(): try: file = request.files.get('AudioFile') print(DEBUG_PREFIX, "received:", file) - + # Create new instances of io.BytesIO() for each request input_audio_path = io.BytesIO() output_audio_path = io.BytesIO() @@ -164,14 +164,14 @@ def rvc_process_audio(): if save_file: input_audio_path = RVC_INPUT_PATH output_audio_path = RVC_OUTPUT_PATH - + file.save(input_audio_path) - + if not save_file: input_audio_path.seek(0) - + parameters = json.loads(request.form["json"]) - + print(DEBUG_PREFIX, "Received audio conversion request with model", parameters) folder_path = RVC_MODELS_PATH+parameters["modelName"]+"/" @@ -180,35 +180,40 @@ def rvc_process_audio(): # HACK: emotion mode EXPERIMENTAL if classification_mode: - print(DEBUG_PREFIX,"EXPERIMENT MODE: emotions") + try: + print(DEBUG_PREFIX,"EXPERIMENT MODE: emotions") - print("> Searching overide code ($emotion$)") - emotion = None - for code in ["anger","fear", "joy","love","sadness","surprise"]: - if "$"+code+"$" in parameters["text"]: - print(" > Overide detected:",code) - emotion = code - parameters["text"] = parameters["text"].replace("$"+code+"$","") - print(parameters["text"]) - break - - if emotion is None: - print("> calling text classification pipeline") - emotions_score = classify_module.classify_text_emotion(parameters["text"]) + print("> Searching overide code ($emotion$)") + emotion = None + for code in ["anger","fear", "joy","love","sadness","surprise"]: + if "$"+code+"$" in parameters["text"]: + print(" > Overide detected:",code) + emotion = code + parameters["text"] = parameters["text"].replace("$"+code+"$","") + print(parameters["text"]) + break - print(" > ",emotions_score) - emotion = emotions_score[0]["label"] - print(" > Selected:", emotion) + if emotion is None: + print("> calling text classification pipeline") + emotions_score = classify_module.classify_text_emotion(parameters["text"]) - model_path = folder_path+emotion+".pth" - index_path = folder_path+emotion+".index" + print(" > ",emotions_score) + emotion = emotions_score[0]["label"] + print(" > Selected:", emotion) - if not os.path.exists(model_path): - print(" > WARNING emotion model pth not found:",model_path," will try loading default") + model_path = folder_path+emotion+".pth" + 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 - - if not os.path.exists(index_path): - print(" > WARNING emotion model index not found:",index_path) index_path = None if model_path is None: @@ -221,7 +226,7 @@ def rvc_process_audio(): if model_path is None: abort(500, DEBUG_PREFIX + " No pth file found.") - + print(DEBUG_PREFIX, "Check for index file", folder_path) for file_name in os.listdir(folder_path): if file_name.endswith(".index"): @@ -232,11 +237,11 @@ def rvc_process_audio(): if index_path is None: index_path = "" print(DEBUG_PREFIX, "no index file found, proceeding without index") - - + + print(DEBUG_PREFIX, "loading", model_path) rvc.load_rvc(model_path) - + info, (tgt_sr, wav_opt) = rvc.vc_single( sid=0, input_audio_path=input_audio_path, @@ -251,7 +256,7 @@ def rvc_process_audio(): rms_mix_rate=float(parameters["rmsMixRate"]), protect=float(parameters["protect"]), crepe_hop_length=128) - + #print(info) # DBG #out_path = os.path.join("data/", "rvc_output.wav") @@ -259,9 +264,9 @@ def rvc_process_audio(): if not save_file: output_audio_path.seek(0) # Reset cursor position - + print(DEBUG_PREFIX, "Audio converted using RVC model:", rvc.rvc_model_name) - + # Return the output_audio_path object as a response response = send_file(output_audio_path, mimetype="audio/x-wav") return response @@ -272,7 +277,7 @@ def rvc_process_audio(): 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) @@ -284,7 +289,7 @@ def fix_model_install(): if file_name in IGNORED_FILES: continue - + # Must be a folder if not os.path.isdir(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) 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) - print("> Searching for index files") + print("> Searching for index files") for file_name in file_names: file_path = os.path.join(RVC_MODELS_PATH,file_name) if file_name in IGNORED_FILES: continue - + # Must be a folder if not os.path.isdir(file_path): new_folder_path, file_extension = os.path.splitext(file_path) if file_extension != ".index": continue - + print(" > WARNING: index file found!",file_path) print(" > Searching for possible model folder") @@ -349,10 +354,10 @@ def fix_model_install(): else: os.rename(file_path, new_file_path) print(" > File moved, new path:",new_file_path) - + found = True break if not found: print(" > WARNING: no corresponding folder found, move or delete the file manually to stop warnings.") - print(DEBUG_PREFIX,"RVC model folder checked.") \ No newline at end of file + print(DEBUG_PREFIX,"RVC model folder checked.")