Demusics / main.py
Kremon96's picture
Create main.py
d5615aa verified
# main.py
"""
Main entry point for the music source separation application
"""
import os
import sys
import argparse
from pathlib import Path
from config import BASE_DIR
def setup_environment():
"""Setup the environment and install dependencies"""
print("πŸ”§ Setting up environment...")
# Install requirements
print("πŸ“¦ Installing dependencies...")
exit_code = os.system(f"{sys.executable} -m pip install -r requirements.txt")
if exit_code != 0:
print("❌ Failed to install dependencies")
return False
# Create necessary directories
print("πŸ“ Creating directory structure...")
directories = [
BASE_DIR / "audio_samples",
BASE_DIR / "separated_audio",
BASE_DIR / "models",
BASE_DIR / "logs",
BASE_DIR / "examples"
]
for directory in directories:
directory.mkdir(exist_ok=True)
print(f"βœ“ Created {directory}")
# Download models
print("πŸ€– Downloading pre-trained models...")
try:
from download_models import download_models
if download_models():
print("βœ… Models downloaded successfully")
else:
print("❌ Model download failed")
return False
except Exception as e:
print(f"❌ Error downloading models: {e}")
return False
print("βœ… Environment setup complete!")
return True
def run_application(host="0.0.0.0", port=7860, share=False):
"""Run the Gradio application"""
print("πŸš€ Starting Music Source Separator application...")
print(f"🌐 Access the interface at: http://{host}:{port}")
try:
import app
app.demo.launch(
server_name=host,
server_port=port,
share=share,
show_error=True
)
except Exception as e:
print(f"❌ Failed to start application: {e}")
return False
return True
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Music Source Separator")
parser.add_argument(
"command",
nargs="?",
default="run",
choices=["setup", "run"],
help="Command to execute (setup: install dependencies, run: start app)"
)
parser.add_argument(
"--host",
default="0.0.0.0",
help="Host to run the server on"
)
parser.add_argument(
"--port",
type=int,
default=7860,
help="Port to run the server on"
)
parser.add_argument(
"--share",
action="store_true",
help="Create public share link"
)
args = parser.parse_args()
if args.command == "setup":
success = setup_environment()
sys.exit(0 if success else 1)
else:
success = run_application(args.host, args.port, args.share)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()