leaderboard-asr / app.py
sawadogosalif's picture
Create app.py
7a66010 verified
#!/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()