import os import shutil import time import numpy as np from typing import Literal, Dict, Any, Tuple from config import LOCAL_STRINGS, DEFAULT_SR def get_localized_strings(lang: Literal["en", "ar"]) -> Dict[str, Any]: """Retrieves the localized strings dictionary.""" return LOCAL_STRINGS.get(lang, LOCAL_STRINGS["en"]) def log_status(lang: Literal["en", "ar"], message_key: str, **kwargs) -> str: """Logs a localized status message.""" strings = get_localized_strings(lang) message = strings.get(message_key, message_key).format(**kwargs) return f"[{time.strftime('%H:%M:%S')}] {message}" def generate_mock_pth(model_name: str, temp_dir: str) -> str: """Simulates RVC model creation and returns the path to the dummy .pth file.""" # Ensure temporary directory exists os.makedirs(temp_dir, exist_ok=True) # Create a unique, descriptive path for the model file filename = f"{model_name}_{int(time.time())}.pth" model_path = os.path.join(temp_dir, filename) # Simulate writing a small placeholder model file (real models are MBs/GBs) try: with open(model_path, 'w') as f: f.write(f"RVC Model Data: {model_name}, Training Simulated at {time.ctime()}") return model_path except IOError: # Handle potential permissions issues during file writing return None def clean_file_paths(paths: List[str]): """Cleans up the temporary files created during the session.""" for path in paths: if path and os.path.exists(path): try: os.remove(path) except Exception as e: print(f"Error cleaning up file {path}: {e}") def get_rvc_model_path(model_file_data: dict, model_name: str) -> str: """ Retrieves the actual file path from the Gradio FileData object or uses a default. Gradio components return paths or FileData dicts upon upload. """ if model_file_data and isinstance(model_file_data, dict) and 'path' in model_file_data: return model_file_data['path'] # Fallback to a placeholder if no file is explicitly uploaded (for demo purposes) return DEFAULT_RVC_MODEL_PATH def load_audio_from_path(file_path: str) -> Tuple[int, np.ndarray]: """Loads audio file using librosa, resampling to DEFAULT_SR.""" import librosa try: audio, sr = librosa.load(file_path, sr=DEFAULT_SR, mono=True) # Convert to 16-bit PCM integer format for standard Gradio audio tuple audio_int16 = (audio * MAX_WAV_VALUE).astype(np.int16) return DEFAULT_SR, audio_int16 except Exception as e: print(f"Error loading audio file {file_path}: {e}") raise gr.Error(f"Failed to load audio: {str(e)}")