Spaces:
Runtime error
Runtime error
Download modules.py from Nigash/nigash-voice-api: direct link, hf CLI and curl.
- Browser
- Download file 4.21 kB
-
https://huggingface.co/spaces/Nigash/nigash-voice-api/resolve/main/modules.py
- Command line
-
hf download hf://spaces/Nigash/nigash-voice-api/modules.py
-
curl -L -o modules.py https://huggingface.co/spaces/Nigash/nigash-voice-api/resolve/main/modules.py
4.21 kB
| import logging | |
| import traceback | |
| import os | |
| from collections import OrderedDict | |
| from io import BytesIO | |
| from pathlib import Path | |
| import numpy as np | |
| import soundfile as sf | |
| import torch | |
| logger: logging.Logger = logging.getLogger(__name__) | |
| class VC: | |
| def __init__(self): | |
| self.n_spk: any = None | |
| self.tgt_sr: int | None = None | |
| self.net_g = None | |
| self.pipeline: any | None = None | |
| self.cpt: OrderedDict | None = None | |
| self.version: str | None = None | |
| self.if_f0: int | None = None | |
| self.hubert_model: any = None | |
| def get_vc(self, sid: str | Path, *to_return_protect: int): | |
| """Simplified version that just loads the model""" | |
| logger.info("Get sid: " + os.path.basename(sid) if hasattr(sid, "name") or isinstance(sid, str) else "ERROR") | |
| return_protect = [ | |
| to_return_protect[0] if self.if_f0 != 0 and to_return_protect else 0.5, | |
| to_return_protect[1] if self.if_f0 != 0 and to_return_protect else 0.33, | |
| ] | |
| if hasattr(sid, "name"): | |
| sid = sid.name | |
| elif not isinstance(sid, str): | |
| raise RuntimeError(f"pathlib.Path or str expected for sid. Got {type(sid)}") | |
| person = sid if os.path.exists(sid) else f'./{sid}' | |
| logger.info(f"Loading: {person}") | |
| if not os.path.exists(person) or person.endswith("/"): | |
| raise FileNotFoundError(f"model file not found (path: {person}).") | |
| # For now, just set basic parameters | |
| self.tgt_sr = 44100 | |
| self.if_f0 = 1 | |
| self.version = "v1" | |
| self.n_spk = 1 | |
| return self.n_spk, return_protect, None | |
| def vc_inference( | |
| self, | |
| sid: int, | |
| input_audio_path: Path | str, | |
| f0_up_key: int = 0, | |
| f0_method: str = "rmvpe", | |
| f0_file: Path | str | None = None, | |
| index_file: Path | str | None = None, | |
| index_rate: float = 0.75, | |
| filter_radius: int = 3, | |
| resample_sr: int = 0, | |
| rms_mix_rate: float = 0.25, | |
| protect: float = 0.33, | |
| hubert_path: str | Path | None = None, | |
| ): | |
| """Simplified inference that just returns the input audio""" | |
| try: | |
| # Load the input audio | |
| if hasattr(input_audio_path, "name"): | |
| input_audio_path = input_audio_path.name | |
| elif not isinstance(input_audio_path, str): | |
| raise RuntimeError(f"pathlib.Path or str expected for input_audio_path. Got {type(input_audio_path)}") | |
| if not os.path.exists(input_audio_path): | |
| raise FileNotFoundError("input_audio_path not found.") | |
| # Load audio using soundfile | |
| audio_data, sample_rate = sf.read(input_audio_path) | |
| # For now, just return the original audio (no voice conversion) | |
| # In a real implementation, you would apply RVC voice conversion here | |
| return sample_rate, audio_data, 0.0 | |
| except Exception as e: | |
| logger.error(f"Error in vc_inference: {e}") | |
| raise e | |
| def vc_multi( | |
| self, | |
| sid: int, | |
| paths: list[Path | str], | |
| opt_root: Path | str, | |
| f0_up_key: int = 0, | |
| f0_method: str = "rmvpe", | |
| f0_file: Path | str | None = None, | |
| index_file: Path | str | None = None, | |
| index_rate: float = 0.75, | |
| filter_radius: int = 3, | |
| resample_sr: int = 0, | |
| rms_mix_rate: float = 0.25, | |
| protect: float = 0.33, | |
| output_format: str = "wav", | |
| hubert_path: str | Path | None = None, | |
| ): | |
| """Simplified multi-file processing""" | |
| results = [] | |
| for path in paths: | |
| try: | |
| tgt_sr, audio_opt, times = self.vc_inference( | |
| sid, path, f0_up_key, f0_method, f0_file, index_file, | |
| index_rate, filter_radius, resample_sr, rms_mix_rate, protect, hubert_path | |
| ) | |
| results.append((tgt_sr, audio_opt, times)) | |
| except Exception as e: | |
| logger.error(f"Error processing {path}: {e}") | |
| results.append(None) | |
| return results |