File size: 4,125 Bytes
7a66010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#!/usr/bin/env python3
"""
Local setup script to replicate Docker container functionality
Run this instead of the Dockerfile for local development
"""
import os
import subprocess
import sys
import shutil
import re
from pathlib import Path

def mask_sensitive_info(text):
    """Mask GitHub tokens in text for safe printing"""
    if text is None:
        return text
    # Replace GitHub tokens (ghp_) with asterisks
    masked = re.sub(r'ghp_[a-zA-Z0-9_]+', '************', text)
    return masked

def run_command(cmd, check=True, shell=False):
    """Run a command and handle errors"""
    print(f"Running: {mask_sensitive_info(cmd)}")
    try:
        if shell:
            result = subprocess.run(cmd, shell=True, check=check, capture_output=True, text=True)
        else:
            result = subprocess.run(cmd.split(), check=check, capture_output=True, text=True)
        
        if result.stdout:
            print(mask_sensitive_info(result.stdout))
        if result.stderr:
            print(mask_sensitive_info(result.stderr), file=sys.stderr)
        return result
    except subprocess.CalledProcessError as e:
        print(f"Error running command: {e}")
        print(f"stdout: {mask_sensitive_info(e.stdout)}")
        print(f"stderr: {mask_sensitive_info(e.stderr)}")
        raise

def setup_environment():
    """Set up the local environment similar to the Docker container"""
    # Create app directory structure
    app_dir = Path("app")
    app_dir.mkdir(exist_ok=True)
    os.chdir(app_dir)
    
    print("Setting up local environment...")
    
    # Check if git is installed
    try:
        run_command("git --version")
    except subprocess.CalledProcessError:
        print("Git is not installed. Please install git first.")
        sys.exit(1)
    
    # Clone the repository (you'll need to handle authentication)
    github_token = os.getenv('GITHUB_TOKEN')
    if not github_token:
        print("Please set GITHUB_TOKEN environment variable")
        print("Export it like: export GITHUB_TOKEN=your_token_here")
        sys.exit(1)
    
    repo_url = f"https://sawadogosalif:{github_token}@github.com/BurkimbIA/spaces.git"
    
    # Clone repository if it doesn't exist
    if not Path("spaces").exists():
        print("Cloning repository...")
        run_command(f"git clone {repo_url}")
    else:
        print("Repository already exists, pulling latest changes...")
        os.chdir("spaces")
        run_command("git pull")
        os.chdir("..")

    # Copy files from leaderboards/asr/ to current directory
    source_path = Path("spaces/leaderboards/asr")
    if source_path.exists():
        print("Copying files from spaces/leaderboards/asr...")
        for item in source_path.iterdir():
            if item.is_file():
                shutil.copy2(item, ".")
            elif item.is_dir():
                shutil.copytree(item, item.name, dirs_exist_ok=True)
    else:
        print("Warning: spaces/leaderboards/asr directory not found in cloned repo")
    
    # Install Python requirements
    if Path("requirements.txt").exists():
        print("Installing Python requirements...")
        run_command(f"{sys.executable} -m pip install -r requirements.txt")
    else:
        print("Warning: requirements.txt not found")
    
    print("Setup complete!")

def main():
    """Main function to run the app"""
    try:
        # Set up environment first
        setup_environment()
        
        # Check if app.py exists and run it
        if Path("app.py").exists():
            print("Starting the application...")
            # Run the actual app.py that was copied from the repo
            subprocess.run([sys.executable, "app.py"])
        else:
            print("app.py not found after setup. Please check the repository structure.")
            print("Available files:")
            for file in Path(".").iterdir():
                print(f"  {file}")
                
    except KeyboardInterrupt:
        print("\nApplication stopped by user")
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()