ASesYusuf1/SESA_Audio_Separation
14
1import os2import threading3import urllib.request4import time5import sys6import random7import argparse8import librosa9from tqdm.auto import tqdm10import torch11import soundfile as sf12import torch.nn as nn13from datetime import datetime14import numpy as np15import shutil16from gui import create_interface17 18# pyngrok import (optional - only needed for ngrok sharing)19try:20 from pyngrok import ngrok21 NGROK_AVAILABLE = True22except ImportError:23 NGROK_AVAILABLE = False24 ngrok = None25 26from assets.i18n.i18n import I18nAuto # I18nAuto'yu içe aktar27 28import warnings29warnings.filterwarnings("ignore")30 31def generate_random_port():32 """Generates a random port between 1000 and 9000."""33 return random.randint(1000, 9000)34 35def start_gradio(port, share=False):36 """Starts the Gradio interface with optional sharing."""37 demo = create_interface()38 demo.launch(39 server_port=port,40 server_name='0.0.0.0',41 share=share,42 allowed_paths=[os.path.join(os.path.expanduser("~"), "Music-Source-Separation", "input"), "/tmp", "/content"],43 inline=False44 )45 46def start_localtunnel(port, i18n):47 """Starts the Gradio interface with localtunnel sharing."""48 print(i18n("starting_localtunnel").format(port=port))49 os.system('npm install -g localtunnel &>/dev/null')50 51 with open('url.txt', 'w') as file:52 file.write('')53 os.system(f'lt --port {port} >> url.txt 2>&1 &')54 time.sleep(2)55 56 endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip("\n")57 with open('url.txt', 'r') as file:58 tunnel_url = file.read().replace("your url is: ", "").strip()59 60 print(i18n("share_link").format(url=tunnel_url))61 print(i18n("password_ip").format(ip=endpoint_ip))62 63 start_gradio(port, share=False)64 65def start_ngrok(port, ngrok_token, i18n):66 """Starts the Gradio interface with ngrok sharing."""67 if not NGROK_AVAILABLE:68 print("pyngrok modülü yüklü değil. 'pip install pyngrok' ile yükleyin.")69 sys.exit(1)70 print(i18n("starting_ngrok").format(port=port))71 try:72 ngrok.set_auth_token(ngrok_token)73 ngrok.kill()74 tunnel = ngrok.connect(port)75 print(i18n("ngrok_url").format(url=tunnel.public_url))76 77 start_gradio(port, share=False)78 except Exception as e:79 print(i18n("ngrok_error").format(error=str(e)))80 sys.exit(1)81 82def main(method="gradio", port=None, ngrok_token=""):83 """Main entry point for the application."""84 # I18nAuto'yu başlat85 i18n = I18nAuto()86 87 # Portu otomatik belirle veya kullanıcıdan geleni kullan88 port = port or generate_random_port()89 print(i18n("selected_port").format(port=port))90 91 # Paylaşım yöntemine göre işlem yap92 if method == "gradio":93 print(i18n("starting_gradio_with_sharing"))94 start_gradio(port, share=True)95 elif method == "localtunnel":96 start_localtunnel(port, i18n)97 elif method == "ngrok":98 if not ngrok_token:99 print(i18n("ngrok_token_required"))100 sys.exit(1)101 start_ngrok(port, ngrok_token, i18n)102 else:103 print(i18n("invalid_method"))104 sys.exit(1)105 106 # Sürekli çalışır durumda tut (gerekirse)107 try:108 while True:109 time.sleep(5)110 except KeyboardInterrupt:111 print(i18n("process_stopped"))112 sys.exit(0)113 114if __name__ == "__main__":115 parser = argparse.ArgumentParser(description="Music Source Separation Web UI")116 parser.add_argument("--method", type=str, default="gradio", choices=["gradio", "localtunnel", "ngrok"], help="Sharing method (default: gradio)")117 parser.add_argument("--port", type=int, default=None, help="Server port (default: random between 1000-9000)")118 parser.add_argument("--ngrok-token", type=str, default="", help="Ngrok authentication token (required for ngrok)")119 args = parser.parse_args()120 121 main(method=args.method, port=args.port, ngrok_token=args.ngrok_token)122 