Spaces:
Build error
Build error
V2
Browse files19 unmodified lines20212223242523242526272829303132333435363738394041282942434445464748495051525331545556572 unmodified lines606162406342434445466465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331344849135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792801041051061071081091101111121131141151161171181191201211221231242812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163171261271281291303183193203211321331343223233243253263273283293303313323333343353363373383393403413423433443453463473483493503511361371383523533543551401411423563571441451461471481491501511521531541551561571581593583593603613623633641611621631641651661671681691701711723653661741751763673683693703713721783733741801811821831841851861871881891901913753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644651931941951961971981992002014662032042052062072082092102112122132142154674684694704712172182192202212222232242252262272282294724734744754764774782312322332342352362372384794804814822402412422432442452462472482492502512522532542552562572582592602612622634834844852654864874882682692702712722732742752762772782794894904914924934944954964974984995005015025035045055065075085095105115125132865145155165178 unmodified lines5265275283015295305315325335345353075365373095385395405413135425433155445455465475485495505514 unmodified lines5565575583265595605615622 unmodified lines56556656756856957057157257357457557657757857958058158258316 unmodified lines60060160235760360460560636160760860961061161261361410 unmodified lines6256266276286296306316326336346356366376386396406416426436446456466476489 unmodified lines6586596606616626636642 unmodified lines6676686694054064074084094104116706716726736742 unmodified lines6776786794206806816826836846856866872 unmodified lines6906916924296936946956961 unmodified line6986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357364387377387397405 unmodified lines74674774845074975075175275375445675575675775819 unmodified linesfrom pydub import AudioSegmentimport noisereduce as nrimport edge_ttsfrom huggingface_hub import hf_hub_download, HfApi # For robust HF link handling
# Suppress loggingfrom huggingface_hub import hf_hub_download, HfApifrom typing import List, Tuple, Optional, Dict, Anyimport jsonfrom pathlib import Path
# Configure logginglogging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("rvc_app.log"), logging.StreamHandler() ])logger = logging.getLogger("RVC_APP")
# Suppress third-party logginglogging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
# Initialize converterconverter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)# ConstantsPITCH_ALGO_OPT = ["pm", "harvest", "crepe", "rmvpe", "rmvpe+"]MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MBDOWNLOAD_DIR = "downloads"OUTPUT_DIR = "output"CONFIG_FILE = "rvc_config.json"SUPPORTED_AUDIO_FORMATS = [".wav", ".mp3", ".ogg", ".flac", ".m4a"]
# Create necessary directoriesos.makedirs(DOWNLOAD_DIR, exist_ok=True)os.makedirs(OUTPUT_DIR, exist_ok=True)
# Theme & Title# Theme and UI Configurationtitle = "<center><strong><font size='7'>🔊 RVC+</font></strong></center>"description = """<div style="text-align: center; font-size: 1.1em; color: #aaa; margin: 10px 0;">2 unmodified linesAuthors are not liable for inappropriate usage.</div>"""theme = "Thatguy099/Sonix" # Maintained as requested
# Global constantsPITCH_ALGO_OPT = ["pm", "harvest", "crepe", "rmvpe", "rmvpe+"]MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MBDOWNLOAD_DIR = "downloads"OUTPUT_DIR = "output"# Theme definition (keeping your existing theme code)from gradio.themes import Softfrom gradio.themes.utils import colors, fonts, sizes
# Define the new OrangeRed color palettecolors.orange_red = colors.Color( name="orange_red", c50="#FFF0E5", c100="#FFE0CC", c200="#FFC299", c300="#FFA366", c400="#FF8533", c500="#FF4500", # OrangeRed base color c600="#E63E00", c700="#CC3700", c800="#B33000", c900="#992900", c950="#802200",)
class OrangeRedTheme(Soft): def __init__( self, *, primary_hue: colors.Color | str = colors.gray, secondary_hue: colors.Color | str = colors.orange_red, neutral_hue: colors.Color | str = colors.slate, text_size: sizes.Size | str = sizes.text_lg, font: fonts.Font | str | Iterable[fonts.Font | str] = ( fonts.GoogleFont("Outfit"), "Arial", "sans-serif", ), font_mono: fonts.Font | str | Iterable[fonts.Font | str] = ( fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace", ), ): super().__init__( primary_hue=primary_hue, secondary_hue=secondary_hue, neutral_hue=neutral_hue, text_size=text_size, font=font, font_mono=font_mono, ) super().set( background_fill_primary="*primary_50", background_fill_primary_dark="*primary_900", body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)", body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)", button_primary_text_color="white", button_primary_text_color_hover="white", button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)", button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)", button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)", button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)", button_secondary_text_color="black", button_secondary_text_color_hover="white", button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)", button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)", button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)", button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)", slider_color="*secondary_500", slider_color_dark="*secondary_600", block_title_text_weight="600", block_border_width="3px", block_shadow="*shadow_drop_lg", button_primary_shadow="*shadow_drop_lg", button_large_padding="11px", color_accent_soft="*primary_100", block_label_background_fill="*primary_200", )
os.makedirs(DOWNLOAD_DIR, exist_ok=True)os.makedirs(OUTPUT_DIR, exist_ok=True)# Instantiate the themeorange_red_theme = OrangeRedTheme()
# CSS (keeping your existing CSS)css = """#main-title h1 { font-size: 2.3em !important;}#output-title h2 { font-size: 2.1em !important;}:root { --color-grey-50: #f9fafb; --banner-background: var(--secondary-400); --banner-text-color: var(--primary-100); --banner-background-dark: var(--secondary-800); --banner-text-color-dark: var(--primary-100); --banner-chrome-height: calc(16px + 43px); --chat-chrome-height-wide-no-banner: 320px; --chat-chrome-height-narrow-no-banner: 450px; --chat-chrome-height-wide: calc(var(--chat-chrome-height-wide-no-banner) + var(--banner-chrome-height)); --chat-chrome-height-narrow: calc(var(--chat-chrome-height-narrow-no-banner) + var(--banner-chrome-height));}.banner-message { background-color: var(--banner-background); padding: 5px; margin: 0; border-radius: 5px; border: none; }.banner-message-text { font-size: 13px; font-weight: bolder; color: var(--banner-text-color) !important; }body.dark .banner-message { background-color: var(--banner-background-dark) !important; }body.dark .gradio-container .contain .banner-message .banner-message-text { color: var(--banner-text-color-dark) !important; }.toast-body { background-color: var(--color-grey-50); }.html-container:has(.css-styles) { padding: 0; margin: 0; }.css-styles { height: 0; }.model-message { text-align: end; }.model-dropdown-container { display: flex; align-items: center; gap: 10px; padding: 0; }.user-input-container .multimodal-textbox{ border: none !important; }.control-button { height: 51px; }button.cancel { border: var(--button-border-width) solid var(--button-cancel-border-color); background: var(--button-cancel-background-fill); color: var(--button-cancel-text-color); box-shadow: var(--button-cancel-shadow); }button.cancel:hover, .cancel[disabled] { background: var(--button-cancel-background-fill-hover); color: var(--button-cancel-text-color-hover); }.opt-out-message { top: 8px; }.opt-out-message .html-container, .opt-out-checkbox label { font-size: 14px !important; padding: 0 !important; margin: 0 !important; color: var(--neutral-400) !important; }div.block.chatbot { height: calc(100svh - var(--chat-chrome-height-wide)) !important; max-height: 900px !important; }div.no-padding { padding: 0 !important; }@media (max-width: 1280px) { div.block.chatbot { height: calc(100svh - var(--chat-chrome-height-wide)) !important; } }@media (max-width: 1024px) { .responsive-row { flex-direction: column; } .model-message { text-align: start; font-size: 10px !important; } .model-dropdown-container { flex-direction: column; align-items: flex-start; } div.block.chatbot { height: calc(100svh - var(--chat-chrome-height-narrow)) !important; }}@media (max-width: 400px) { .responsive-row { flex-direction: column; } .model-message { text-align: start; font-size: 10px !important; } .model-dropdown-container { flex-direction: column; align-items: flex-start; } div.block.chatbot { max-height: 360px !important; }}@media (max-height: 932px) { .chatbot { max-height: 500px !important; } }@media (max-height: 1280px) { div.block.chatbot { max-height: 800px !important; } }"""
# --- Utility Functions ---def find_files(directory, exts=(".pth", ".index", ".zip")): return [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(exts)]
def unzip_in_folder(zip_path, extract_to): with zipfile.ZipFile(zip_path, 'r') as zip_ref: for member in zip_ref.infolist(): if not member.is_dir(): # Preserve filename, avoid path traversal member.filename = os.path.basename(member.filename) zip_ref.extract(member, extract_to)
def get_file_size(url): """Check file size for Hugging Face URLs, supporting resolve/main links.""" if "huggingface" not in url.lower(): raise ValueError("❌ Only Hugging Face links are allowed.") try: # The Hugging Face Hub API can handle various link types api = HfApi() # Extract repo_id and filename from the URL if "/resolve/main/" in url: parts = url.split("/resolve/main/") elif "/resolve/" in url: # Handle specific branches parts = url.split("/resolve/") parts[1] = parts[1].split("/", 1)[1] # Remove branch name else: # Assume it's a blob link or direct file link parts = url.rstrip("/").rsplit("/", 2) if len(parts) == 3: repo_parts = "/".join(parts[0].split("/")[-2:]) filename = parts[2] repo_id = f"{parts[0].split('/')[-2]}/{parts[0].split('/')[-1]}" file_info = api.repo_info(repo_id=repo_id, repo_type="model") file_entry = next((f for f in file_info.siblings if f.rfilename == filename), None) if not file_entry: raise ValueError(f"❌ File '{filename}' not found in repository '{repo_id}'.") file_size = file_entry.size if file_size > MAX_FILE_SIZE: raise ValueError(f"⚠️ File too large: {file_size / 1e6:.1f} MB (>500MB)") return file_size else: raise ValueError("❌ Unable to parse Hugging Face URL.") repo_parts = parts[0].split("/")[-2:] repo_id = f"{repo_parts[0]}/{repo_parts[1]}" filename = parts[1] file_info = api.repo_info(repo_id=repo_id, repo_type="model") file_entry = next((f for f in file_info.siblings if f.rfilename == filename), None) if not file_entry: raise ValueError(f"❌ File '{filename}' not found in repository '{repo_id}'.")# Model Management Classclass ModelManager: """Manages model loading, downloading, and caching.""" def __init__(self): self.converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None) self.loaded_models = {} # Cache for loaded models self.config = self._load_config() def _load_config(self) -> Dict[str, Any]: """Load configuration from file if exists.""" if os.path.exists(CONFIG_FILE): try: with open(CONFIG_FILE, 'r') as f: return json.load(f) except Exception as e: logger.error(f"Failed to load config: {e}") return {"recent_models": [], "default_settings": {}} def save_config(self): """Save current configuration to file.""" try: with open(CONFIG_FILE, 'w') as f: json.dump(self.config, f) except Exception as e: logger.error(f"Failed to save config: {e}") def add_recent_model(self, model_path: str): """Add a model to recent models list.""" if model_path not in self.config["recent_models"]: self.config["recent_models"].append(model_path) # Keep only the 5 most recent models self.config["recent_models"] = self.config["recent_models"][-5:] self.save_config() def find_files(self, directory: str, exts: Tuple[str] = (".pth", ".index", ".zip")) -> List[str]: """Find files with specific extensions in a directory.""" return [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(exts)] def unzip_in_folder(self, zip_path: str, extract_to: str): """Unzip a file to a specific folder.""" with zipfile.ZipFile(zip_path, 'r') as zip_ref: for member in zip_ref.infolist(): if not member.is_dir(): # Preserve filename, avoid path traversal member.filename = os.path.basename(member.filename) zip_ref.extract(member, extract_to) def get_file_size(self, url: str) -> int: """Check file size for Hugging Face URLs.""" if "huggingface" not in url.lower(): raise ValueError("❌ Only Hugging Face links are allowed.") try: api = HfApi() # Extract repo_id and filename from the URL if "/resolve/main/" in url: parts = url.split("/resolve/main/") elif "/resolve/" in url: # Handle specific branches parts = url.split("/resolve/") parts[1] = parts[1].split("/", 1)[1] # Remove branch name else: # Assume it's a blob link or direct file link parts = url.rstrip("/").rsplit("/", 2) if len(parts) == 3: repo_parts = "/".join(parts[0].split("/")[-2:]) filename = parts[2] repo_id = f"{parts[0].split('/')[-2]}/{parts[0].split('/')[-1]}" file_info = api.repo_info(repo_id=repo_id, repo_type="model") file_entry = next((f for f in file_info.siblings if f.rfilename == filename), None) if not file_entry: raise ValueError(f"❌ File '{filename}' not found in repository '{repo_id}'.") file_size = file_entry.size if file_size > MAX_FILE_SIZE: raise ValueError(f"⚠️ File too large: {file_size / 1e6:.1f} MB (>500MB)") return file_size else: raise ValueError("❌ Unable to parse Hugging Face URL.") repo_parts = parts[0].split("/")[-2:] repo_id = f"{repo_parts[0]}/{repo_parts[1]}" filename = parts[1] file_info = api.repo_info(repo_id=repo_id, repo_type="model") file_entry = next((f for f in file_info.siblings if f.rfilename == filename), None) if not file_entry: raise ValueError(f"❌ File '{filename}' not found in repository '{repo_id}'.") file_size = file_entry.size if file_size > MAX_FILE_SIZE: raise ValueError(f"⚠️ File too large: {file_size / 1e6:.1f} MB (>500MB)") return file_size except Exception as e: raise RuntimeError(f"❌ Failed to fetch file info: {str(e)}")
def clear_directory_later(directory, delay=30): """Clear temp directory after delay in a background thread.""" def _clear(): time.sleep(delay) if os.path.exists(directory): shutil.rmtree(directory, ignore_errors=True) print(f"🧹 Cleaned up: {directory}") threading.Thread(target=_clear, daemon=True).start()
def find_model_and_index(directory): files = find_files(directory) model = next((f for f in files if f.endswith(".pth")), None) index = next((f for f in files if f.endswith(".index")), None) return model, index file_size = file_entry.size if file_size > MAX_FILE_SIZE: raise ValueError(f"⚠️ File too large: {file_size / 1e6:.1f} MB (>500MB)") return file_size except Exception as e: raise RuntimeError(f"❌ Failed to fetch file info: {str(e)}") def clear_directory_later(self, directory: str, delay: int = 30): """Clear temp directory after delay in a background thread.""" def _clear(): time.sleep(delay) if os.path.exists(directory): shutil.rmtree(directory, ignore_errors=True) logger.info(f"🧹 Cleaned up: {directory}") threading.Thread(target=_clear, daemon=True).start() def find_model_and_index(self, directory: str) -> Tuple[Optional[str], Optional[str]]: """Find model and index files in a directory.""" files = self.find_files(directory) model = next((f for f in files if f.endswith(".pth")), None) index = next((f for f in files if f.endswith(".index")), None) return model, index @spaces.GPU(duration=60) def download_model(self, url_data: str) -> Tuple[str, Optional[str]]: """Download model from Hugging Face URL.""" if not url_data.strip(): raise ValueError("❌ No URL provided.")
urls = [u.strip() for u in url_data.split(",") if u.strip()] if len(urls) > 2: raise ValueError("❌ Provide up to two URLs (model.pth, index.index).")
# Validate size first for url in urls: self.get_file_size(url)
# --- Model Download Handler ---@spaces.GPU(duration=60)def download_model(url_data): if not url_data.strip(): raise ValueError("❌ No URL provided.") folder_name = f"model_{random.randint(1000, 9999)}" directory = os.path.join(DOWNLOAD_DIR, folder_name) os.makedirs(directory, exist_ok=True)
urls = [u.strip() for u in url_data.split(",") if u.strip()] if len(urls) > 2: raise ValueError("❌ Provide up to two URLs (model.pth, index.index).") try: downloaded_files = [] for url in urls: # Use the robust Hugging Face Hub library for download parsed_url = urllib.parse.urlparse(url) path_parts = parsed_url.path.strip("/").split("/") if len(path_parts) < 4: raise ValueError("❌ Invalid Hugging Face URL structure.") repo_id = f"{path_parts[0]}/{path_parts[1]}" revision = "main" if "resolve" in path_parts: resolve_idx = path_parts.index("resolve") if resolve_idx + 1 < len(path_parts): revision = path_parts[resolve_idx + 1] filename = "/".join(path_parts[resolve_idx + 2:]) else: # Assume it's a blob link pointing to a file filename = path_parts[-1] # Download the file local_path = hf_hub_download( repo_id=repo_id, filename=filename, revision=revision, cache_dir=directory, local_dir=directory, local_dir_use_symlinks=False ) downloaded_files.append(local_path)
# Validate size first for url in urls: get_file_size(url) # Unzip if needed for f in self.find_files(directory, (".zip",)): self.unzip_in_folder(f, directory)
folder_name = f"model_{random.randint(1000, 9999)}" directory = os.path.join(DOWNLOAD_DIR, folder_name) os.makedirs(directory, exist_ok=True) model, index = self.find_model_and_index(directory)
try: downloaded_files = [] for url in urls: # Use the robust Hugging Face Hub library for download # This handles resolve/main, blob, and other link types seamlessly parsed_url = urllib.parse.urlparse(url) path_parts = parsed_url.path.strip("/").split("/") if len(path_parts) < 4: raise ValueError("❌ Invalid Hugging Face URL structure.") repo_id = f"{path_parts[0]}/{path_parts[1]}" revision = "main" if "resolve" in path_parts: resolve_idx = path_parts.index("resolve") if resolve_idx + 1 < len(path_parts): revision = path_parts[resolve_idx + 1] filename = "/".join(path_parts[resolve_idx + 2:]) if not model: raise ValueError("❌ .pth model file not found in downloaded content.") gr.Info(f"✅ Model loaded: {os.path.basename(model)}") if index: gr.Info(f"📌 Index loaded: {os.path.basename(index)}") else: # Assume it's a blob link pointing to a file filename = path_parts[-1] # Download the file local_path = hf_hub_download( repo_id=repo_id, filename=filename, revision=revision, cache_dir=directory, local_dir=directory, local_dir_use_symlinks=False ) downloaded_files.append(local_path) gr.Warning("⚠️ Index file not found – conversion may be less accurate.")
# Unzip if needed for f in find_files(directory, (".zip",)): unzip_in_folder(f, directory) # Schedule cleanup self.clear_directory_later(directory, delay=30) # Add to recent models self.add_recent_model(os.path.abspath(model))
model, index = find_model_and_index(directory) return os.path.abspath(model), os.path.abspath(index) if index else None
if not model: raise ValueError("❌ .pth model file not found in downloaded content.") gr.Info(f"✅ Model loaded: {os.path.basename(model)}") if index: gr.Info(f"📌 Index loaded: {os.path.basename(index)}") else: gr.Warning("⚠️ Index file not found – conversion may be less accurate.")
# Schedule cleanup clear_directory_later(directory, delay=30)
return os.path.abspath(model), os.path.abspath(index) if index else None except Exception as e: shutil.rmtree(directory, ignore_errors=True) logger.error(f"Download failed: {e}") raise gr.Error(f"❌ Download failed: {str(e)}")
# Audio Processing Classclass AudioProcessor: """Handles audio processing tasks like noise reduction and effects.""" @staticmethod def apply_noisereduce(audio_paths: List[str]) -> List[str]: """Apply noise reduction to audio files.""" results = [] for path in audio_paths: out_path = f"{os.path.splitext(path)[0]}_denoised.wav" try: audio = AudioSegment.from_file(path) samples = np.array(audio.get_array_of_samples()) sr = audio.frame_rate reduced = nr.reduce_noise(y=samples.astype(np.float32), sr=sr, prop_decrease=0.6) reduced_audio = AudioSegment( reduced.tobytes(), frame_rate=sr, sample_width=audio.sample_width, channels=audio.channels ) reduced_audio.export(out_path, format="wav") results.append(out_path) gr.Info("🔊 Noise reduction applied.") except Exception as e: logger.error(f"Noise reduction failed: {e}") results.append(path) return results @staticmethod def apply_audio_effects(audio_paths: List[str]) -> List[str]: """Apply audio effects to audio files.""" results = [] board = Pedalboard([ HighpassFilter(cutoff_frequency_hz=80), Compressor(ratio=4, threshold_db=-15), Reverb(room_size=0.15, damping=0.7, wet_level=0.15, dry_level=0.85) ]) for path in audio_paths: out_path = f"{os.path.splitext(path)[0]}_reverb.wav" try: with AudioFile(path) as f: with AudioFile(out_path, 'w', f.samplerate, f.num_channels) as o: while f.tell() < f.frames: chunk = f.read(int(f.samplerate)) effected = board(chunk, f.samplerate) o.write(effected) results.append(out_path) gr.Info("🎛️ Audio effects applied.") except Exception as e: logger.error(f"Effects failed: {e}") results.append(path) return results @staticmethod def validate_audio_files(file_paths: List[str]) -> List[str]: """Validate that files are supported audio formats.""" valid_files = [] for path in file_paths: if os.path.splitext(path)[1].lower() in SUPPORTED_AUDIO_FORMATS: valid_files.append(path) else: gr.Warning(f"⚠️ Skipping unsupported file: {os.path.basename(path)}") return valid_files
# TTS Handler Classclass TTSHandler: """Handles text-to-speech functionality.""" @staticmethod async def generate_tts(text: str, voice: str, output_path: str): """Generate TTS audio from text.""" communicate = edge_tts.Communicate(text, voice.split("-")[0]) await communicate.save(output_path) @staticmethod def infer_tts(tts_voice: str, tts_text: str, play_tts: bool) -> Tuple[List[str], Optional[str]]: """Generate TTS audio with the specified voice.""" if not tts_text.strip(): raise ValueError("❌ Text is empty.") folder = f"tts_{random.randint(10000, 99999)}" out_dir = os.path.join(OUTPUT_DIR, folder) os.makedirs(out_dir, exist_ok=True) out_path = os.path.join(out_dir, "tts_output.mp3")
except Exception as e: shutil.rmtree(directory, ignore_errors=True) raise gr.Error(f"❌ Download failed: {str(e)}")
# --- Audio Processing ---def apply_noisereduce(audio_paths): results = [] for path in audio_paths: out_path = f"{os.path.splitext(path)[0]}_denoised.wav" try: audio = AudioSegment.from_file(path) samples = np.array(audio.get_array_of_samples()) sr = audio.frame_rate reduced = nr.reduce_noise(y=samples.astype(np.float32), sr=sr, prop_decrease=0.6) reduced_audio = AudioSegment( reduced.tobytes(), frame_rate=sr, sample_width=audio.sample_width, channels=audio.channels ) reduced_audio.export(out_path, format="wav") results.append(out_path) gr.Info("🔊 Noise reduction applied.") asyncio.run(TTSHandler.generate_tts(tts_text, tts_voice, out_path)) if play_tts: return [out_path], out_path return [out_path], None except Exception as e: print(f"Noise reduction failed: {e}") results.append(path) return results
def apply_audio_effects(audio_paths): results = [] board = Pedalboard([ HighpassFilter(cutoff_frequency_hz=80), Compressor(ratio=4, threshold_db=-15), Reverb(room_size=0.15, damping=0.7, wet_level=0.15, dry_level=0.85) ]) for path in audio_paths: out_path = f"{os.path.splitext(path)[0]}_reverb.wav" logger.error(f"TTS generation failed: {e}") raise gr.Error(f"TTS generation failed: {str(e)}") @staticmethod def get_voice_list() -> List[str]: """Get list of available TTS voices.""" try: with AudioFile(path) as f: with AudioFile(out_path, 'w', f.samplerate, f.num_channels) as o: while f.tell() < f.frames: chunk = f.read(int(f.samplerate)) effected = board(chunk, f.samplerate) o.write(effected) results.append(out_path) gr.Info("🎛️ Audio effects applied.") return sorted( [f"{v['ShortName']}-{v['Gender']}" for v in asyncio.run(edge_tts.list_voices())] ) except Exception as e: print(f"Effects failed: {e}") results.append(path) return results
# --- TTS Handler ---async def generate_tts(text, voice, output_path): communicate = edge_tts.Communicate(text, voice.split("-")[0]) await communicate.save(output_path)
def infer_tts(tts_voice, tts_text, play_tts): if not tts_text.strip(): raise ValueError("❌ Text is empty.") folder = f"tts_{random.randint(10000, 99999)}" out_dir = os.path.join(OUTPUT_DIR, folder) os.makedirs(out_dir, exist_ok=True) out_path = os.path.join(out_dir, "tts_output.mp3")
try: asyncio.run(generate_tts(tts_text, tts_voice, out_path)) if play_tts: return [out_path], out_path return [out_path], None except Exception as e: raise gr.Error(f"TTS generation failed: {str(e)}") logger.error(f"Failed to get voice list: {e}") return ["en-US-JennyNeural-Female"] # Fallback
# --- Main Conversion Function ---# Main Conversion Function@spaces.GPU()def run_conversion( audio_files, model_path, pitch_algo, pitch_level, index_path, index_rate, filter_radius, rms_mix_rate, protect, denoise, effects,): audio_files: List[str], model_path: str, pitch_algo: str, pitch_level: int, index_path: Optional[str], index_rate: float, filter_radius: int, rms_mix_rate: float, protect: float, denoise: bool, effects: bool, model_manager: ModelManager) -> List[str]: """Run voice conversion on the provided audio files.""" if not audio_files: raise ValueError("❌ Please upload at least one audio file.") # Validate audio files audio_files = AudioProcessor.validate_audio_files(audio_files) if not audio_files: raise ValueError("❌ No valid audio files provided.")
random_tag = f"USER_{random.randint(10000000, 99999999)}"
# Configure converter converter.apply_conf( model_manager.converter.apply_conf( tag=random_tag, file_model=model_path, pitch_algo=pitch_algo,8 unmodified lines
# Run conversion try: results = converter(audio_files, random_tag, overwrite=False, parallel_workers=8) results = model_manager.converter(audio_files, random_tag, overwrite=False, parallel_workers=8) except Exception as e: logger.error(f"Conversion failed: {e}") raise gr.Error(f"❌ Conversion failed: {str(e)}")
# Post-processing if denoise: results = apply_noisereduce(results) results = AudioProcessor.apply_noisereduce(results) if effects: results = apply_audio_effects(results) results = AudioProcessor.apply_audio_effects(results)
return results
# --- Gradio UI Builder ---# Gradio UI Builderdef create_ui(): with gr.Blocks(theme=theme, title="RVC+", fill_width=True, delete_cache=(3200, 3200)) as app: """Create and configure the Gradio UI.""" # Initialize model manager model_manager = ModelManager() with gr.Blocks(theme=orange_red_theme, title="RVC+", fill_width=True, delete_cache=(3200, 3200), css=css) as app: gr.HTML(title) gr.HTML(description)
4 unmodified lines with gr.Column(scale=1): gr.Markdown("### 🔊 Upload Audio") audio_input = gr.File( label="Audio Files (WAV, MP3, OGG)", label="Audio Files (WAV, MP3, OGG, FLAC, M4A)", file_count="multiple", type="filepath" )2 unmodified lines model_file = gr.File(label="Upload .pth Model", type="filepath") index_file = gr.File(label="Upload .index File (Optional)", type="filepath") # Recent models dropdown recent_models = gr.Dropdown( label="Recent Models", choices=model_manager.config["recent_models"], value=None, interactive=True ) recent_models.change( lambda x: x if x else None, inputs=[recent_models], outputs=[model_file] ) use_url = gr.Checkbox(label="🌐 Download from Hugging Face URL", value=False) with gr.Group(visible=False) as url_group:16 unmodified lines ) download_btn.click( download_model, model_manager.download_model, inputs=[model_url], outputs=[model_file, index_file] ).then( lambda: gr.update(visible=False), # Hide URL group after download lambda: gr.update(visible=False), # Hide URL group after download outputs=[url_group] ).then( lambda: gr.update(choices=model_manager.config["recent_models"]), outputs=[recent_models] )
with gr.Column(scale=1):10 unmodified lines denoise = gr.Checkbox(False, label="🔇 Denoise Output") reverb = gr.Checkbox(False, label="🎛️ Add Reverb") # Save settings button save_settings_btn = gr.Button("💾 Save as Default", size="sm") save_settings_btn.click( lambda *args: model_manager.config.update({"default_settings": { "pitch_algo": args[0], "pitch_level": args[1], "index_rate": args[2], "filter_radius": args[3], "rms_mix_rate": args[4], "protect": args[5], "denoise": args[6], "reverb": args[7] }}) or model_manager.save_config(), inputs=[pitch_algo, pitch_level, index_rate, filter_radius, rms_mix_rate, protect, denoise, reverb] )
convert_btn = gr.Button("🚀 Convert Voice", variant="primary", size="lg") output_files = gr.File(label="✅ Converted Audio", file_count="multiple") # Progress indicator progress = gr.Progress()
convert_btn.click( run_conversion,9 unmodified lines protect, denoise, reverb, gr.State(model_manager) # Pass model manager as state ], outputs=output_files, )2 unmodified lines with gr.Tab("🗣️ Text-to-Speech", id=1): gr.Markdown("### Convert text to speech using Edge TTS.") # Pre-fetch voices outside the main event loop if possible, or handle async properly try: tts_voice_list = sorted( [f"{v['ShortName']}-{v['Gender']}" for v in asyncio.run(edge_tts.list_voices())] ) except: tts_voice_list = ["en-US-JennyNeural-Female"] # Fallback # Get voice list tts_voice_list = TTSHandler.get_voice_list()
with gr.Row(): with gr.Column(scale=1):2 unmodified lines label="Text Input", lines=5 ) tts_voice = gr.Dropdown(tts_voice_list, value=tts_voice_list[0] if tts_voice_list else None, label="Voice") tts_voice = gr.Dropdown( tts_voice_list, value=tts_voice_list[0] if tts_voice_list else None, label="Voice" ) tts_play = gr.Checkbox(False, label="🎧 Auto-play audio") tts_btn = gr.Button("🔊 Generate Speech", variant="secondary") 2 unmodified lines tts_preview = gr.Audio(label="Preview", visible=False, autoplay=True)
tts_btn.click( infer_tts, TTSHandler.infer_tts, inputs=[tts_voice, tts_text, tts_play], outputs=[tts_output_audio, tts_preview], ).then(1 unmodified line inputs=[tts_preview], outputs=[tts_preview] ) # ============= TAB 3: Settings ============= with gr.Tab("⚙️ Settings", id=2): gr.Markdown("### Application Settings") with gr.Row(): with gr.Column(): gr.Markdown("#### Model Management") clear_cache_btn = gr.Button("🗑️ Clear Model Cache", variant="secondary") clear_cache_btn.click( lambda: shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True) or gr.Info("Cache cleared"), outputs=[] ) gr.Markdown("#### Recent Models") recent_models_list = gr.DataFrame( value=[[model] for model in model_manager.config["recent_models"]], headers=["Model Path"], datatype=["str"], interactive=False ) with gr.Row(): with gr.Column(): gr.Markdown("#### System Information") system_info = gr.HTML( f""" <div> <p><strong>Python Version:</strong> {os.sys.version}</p> <p><strong>Platform:</strong> {os.sys.platform}</p> <p><strong>Download Directory:</strong> {os.path.abspath(DOWNLOAD_DIR)}</p> <p><strong>Output Directory:</strong> {os.path.abspath(OUTPUT_DIR)}</p> </div> """ )
# Examples (Moved to be more prominent) # Examples gr.Markdown("### 📚 Examples") gr.Examples( examples=[5 unmodified lines index_rate, filter_radius, rms_mix_rate, protect, denoise, reverb ], outputs=output_files, fn=run_conversion, fn=lambda *args: run_conversion(*args, model_manager), cache_examples=False, )
return app
# --- Launch App ---# Launch Appif __name__ == "__main__": app = create_ui() app.queue(default_concurrency_limit=10)app.py+545-246