Spaces:
Sleeping
Sleeping
# Function to check if config.json already exists in a subfolder and if an image file exists | |
check_exists() { | |
local folder="$1" | |
local config_file="$folder/config.json" | |
local icon_file=$(find "$folder" -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.gif" -o -iname "*.png" \) -print -quit) | |
if [ -f "$config_file" ]; then | |
if [ -n "$icon_file" ]; then | |
icon=$(basename "$icon_file") | |
fi | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Function to update config.json if there are missing fields | |
update_config() { | |
local folder="$1" | |
local config_file="$folder/config.json" | |
local model_file=$(find "$folder" -maxdepth 1 -type f -name "*.pth" -exec basename {} \; -quit) | |
local feat_index_file=$(find "$folder" -maxdepth 1 -type f -name "*.index" -exec basename {} \; -quit) | |
local feat_npy_file=$(find "$folder" -maxdepth 1 -type f -name "*.npy" -exec basename {} \; -quit) | |
# Remove the .pth extension from the model file name | |
local name=$(basename "$model_file" .pth) | |
# Replace underscores with spaces, remove "RVC" or "(RVC)", "Epoch", square brackets, parentheses, and curly brackets from the folder name, | |
# and replace any occurrences of consecutive spaces with a single space | |
local note=$(basename "$folder" | sed 's/_/ /g; s/\(RVC\)//g; s/RVC//g; s/[(){}]//g; s/\[\]//g; s/Epoch//g; s/ */ /g; s/^ //; s/ $//') | |
# Set other configuration values | |
local speaker_id=0 | |
local author="Rubin" | |
local source="Rubin" | |
local icon="" | |
if check_exists "$folder"; then | |
if [ -n "$icon" ]; then | |
icon=$(basename "$icon_file") | |
fi | |
# Update the fields for the existing config.json | |
# Creating a new json string with the updated fields | |
local new_config=$(cat <<EOF | |
{ | |
"model": "$model_file", | |
"feat_index": "$feat_index_file", | |
"feat_npy": "$feat_npy_file", | |
"speaker_id": $speaker_id, | |
"name": "$name", | |
"author": "$author", | |
"source": "$source", | |
"note": "$note", | |
"icon": "$icon" | |
} | |
EOF | |
) | |
# Write the new config to the file | |
echo "$new_config" > "$config_file" | |
echo "Updated config.json file in $folder" | |
else | |
# Create the config.json file | |
cat > "$folder/config.json" <<EOF | |
{ | |
"model": "$model_file", | |
"feat_index": "$feat_index_file", | |
"feat_npy": "$feat_npy_file", | |
"speaker_id": $speaker_id, | |
"name": "$name", | |
"author": "$author", | |
"source": "$source", | |
"note": "$note", | |
"icon": "$icon" | |
} | |
EOF | |
if [ -n "$icon" ]; then | |
echo "config.json file created successfully in $folder with icon: $icon" | |
else | |
echo "config.json file created successfully in $folder (no icon)" | |
fi | |
fi | |
# Process subfolders recursively | |
while IFS= read -r -d '' subfolder; do | |
update_config "$subfolder" | |
done < <(find "$folder" -mindepth 1 -type d -print0) | |
} | |
# Set the folder path to the current directory | |
folder_path=$(pwd) | |
# Update config.json files recursively in subfolders | |
update_config "$folder_path" | |