ASesYusuf1/SESA_Audio_Separation
14
1import os2import json3from pathlib import Path4 5# Define config directory in Google Drive6CONFIG_DIR = "/content/drive/MyDrive/SESA-Config"7CONFIG_FILE = os.path.join(CONFIG_DIR, "config.json")8 9def load_config():10 """Load configuration from config.json."""11 default_config = {12 "favorites": [],13 "settings": {14 "chunk_size": 352800,15 "overlap": 2,16 "export_format": "wav FLOAT",17 "optimize_mode": "channels_last",18 "enable_amp": True,19 "enable_tf32": True,20 "enable_cudnn_benchmark": True,21 "auto_use_tta": False,22 "use_tta": False,23 "use_demud_phaseremix_inst": False,24 "auto_extract_instrumental": False,25 "extract_instrumental": False,26 "use_apollo": False,27 "auto_use_apollo": False,28 "auto_apollo_chunk_size": 19,29 "auto_apollo_overlap": 2,30 "auto_apollo_method": "normal_method",31 "auto_apollo_normal_model": "Apollo Universal Model",32 "auto_apollo_midside_model": "Apollo Universal Model",33 "apollo_chunk_size": 19,34 "apollo_overlap": 2,35 "apollo_method": "normal_method",36 "apollo_normal_model": "Apollo Universal Model",37 "apollo_midside_model": "Apollo Universal Model",38 "use_matchering": False,39 "auto_use_matchering": False,40 "matchering_passes": 1,41 "auto_matchering_passes": 1,42 "model_category": "Vocal Models",43 "selected_model": None,44 "auto_category": "Vocal Models",45 "selected_models": [],46 "auto_ensemble_type": "avg_wave",47 "manual_ensemble_type": "avg_wave",48 "auto_category_dropdown": "Vocal Models",49 "manual_weights": ""50 },51 "presets": {}52 }53 54 os.makedirs(CONFIG_DIR, exist_ok=True)55 if not os.path.exists(CONFIG_FILE):56 with open(CONFIG_FILE, "w", encoding="utf-8") as f:57 json.dump(default_config, f, indent=2)58 return default_config59 60 try:61 with open(CONFIG_FILE, "r", encoding="utf-8") as f:62 config = json.load(f)63 # Merge with default config to ensure all keys exist64 for key, value in default_config.items():65 if key not in config:66 config[key] = value67 elif isinstance(value, dict):68 for subkey, subvalue in value.items():69 if subkey not in config[key]:70 config[key][subkey] = subvalue71 return config72 except json.JSONDecodeError:73 print("Warning: config.json is corrupted. Creating a new one.")74 with open(CONFIG_FILE, "w", encoding="utf-8") as f:75 json.dump(default_config, f, indent=2)76 return default_config77 78def save_config(favorites, settings, presets):79 """Save configuration to config.json."""80 config = {81 "favorites": favorites,82 "settings": settings,83 "presets": presets84 }85 os.makedirs(CONFIG_DIR, exist_ok=True)86 with open(CONFIG_FILE, "w", encoding="utf-8") as f:87 json.dump(config, f, indent=2)88 89def update_favorites(favorites, model, add=True):90 """Update favorites list."""91 cleaned_model = model92 new_favorites = favorites.copy()93 if add and cleaned_model not in new_favorites:94 new_favorites.append(cleaned_model)95 elif not add and cleaned_model in new_favorites:96 new_favorites.remove(cleaned_model)97 return new_favorites98 99def save_preset(presets, preset_name, models, ensemble_method, **kwargs):100 """Save a preset."""101 new_presets = presets.copy()102 cleaned_models = [clean_model(model) for model in models]103 new_presets[preset_name] = {104 "models": cleaned_models,105 "ensemble_method": ensemble_method,106 "chunk_size": kwargs.get("chunk_size", load_config()["settings"]["chunk_size"]),107 "overlap": kwargs.get("overlap", load_config()["settings"]["overlap"]),108 "auto_use_tta": kwargs.get("auto_use_tta", load_config()["settings"]["auto_use_tta"]),109 "auto_extract_instrumental": kwargs.get("auto_extract_instrumental", load_config()["settings"]["auto_extract_instrumental"]),110 "use_apollo": kwargs.get("use_apollo", load_config()["settings"]["use_apollo"]),111 "auto_apollo_chunk_size": kwargs.get("auto_apollo_chunk_size", load_config()["settings"]["auto_apollo_chunk_size"]),112 "auto_category_dropdown": kwargs.get("auto_category_dropdown", load_config()["settings"]["auto_category_dropdown"]), # Save category113 "auto_apollo_overlap": kwargs.get("auto_apollo_overlap", load_config()["settings"]["auto_apollo_overlap"]),114 "auto_apollo_method": kwargs.get("auto_apollo_method", load_config()["settings"]["auto_apollo_method"]),115 "auto_apollo_normal_model": kwargs.get("auto_apollo_normal_model", load_config()["settings"]["auto_apollo_normal_model"]),116 "auto_apollo_midside_model": kwargs.get("auto_apollo_midside_model", load_config()["settings"]["auto_apollo_midside_model"]),117 "auto_use_matchering": kwargs.get("use_matchering", load_config()["settings"]["use_matchering"]),118 "auto_matchering_passes": kwargs.get("matchering_passes", load_config()["settings"]["matchering_passes"]),119 "auto_category": kwargs.get("auto_category", load_config()["settings"]["auto_category"])120 }121 return new_presets122 123def delete_preset(presets, preset_name):124 """Delete a preset."""125 new_presets = presets.copy()126 if preset_name in new_presets:127 del new_presets[preset_name]128 return new_presets129 130def clean_model(model):131 """Remove ⭐ from model name if present."""132 return model.replace(" ⭐", "") if isinstance(model, str) else model133 