Download app.py from dirven/numerai_all_48_models: direct link, hf CLI and curl.
- Browser
- Download file 3.78 kB
-
https://huggingface.co/datasets/dirven/numerai_all_48_models/resolve/main/app.py
- Command line
-
hf download hf://datasets/dirven/numerai_all_48_models/app.py
-
curl -L -o app.py https://huggingface.co/datasets/dirven/numerai_all_48_models/resolve/main/app.py
3.78 kB
| from fastapi import FastAPI, BackgroundTasks | |
| from fastapi.responses import PlainTextResponse | |
| import subprocess | |
| import logging | |
| import os | |
| import sys | |
| import time | |
| import threading | |
| from datetime import datetime | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| load_dotenv(Path(__file__).parent / '.env') | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI() | |
| SCRIPTS_DIR = Path("/app/scripts") | |
| LOGS_DIR = Path("/app/logs") | |
| LOGS_DIR.mkdir(exist_ok=True, parents=True) | |
| ORCHESTRATOR_PATH = Path("/app/UNIVERSAL_NUMERAI_ORCHESTRATOR.py") | |
| orchestrator_running = False | |
| orchestrator_result = None | |
| def run_orchestrator(): | |
| global orchestrator_running, orchestrator_result | |
| try: | |
| logger.info(f"[{datetime.utcnow()}] Starting orchestrator...") | |
| result = subprocess.run( | |
| [sys.executable, str(ORCHESTRATOR_PATH)], | |
| capture_output=True, | |
| text=True, | |
| timeout=7200 | |
| ) | |
| orchestrator_result = { | |
| "returncode": result.returncode, | |
| "stdout": result.stdout if result.stdout else "", | |
| "stderr": result.stderr[-5000:] if result.stderr else "" | |
| } | |
| logger.info(f"Orchestrator exit code: {result.returncode}") | |
| if result.stdout: | |
| logger.info(f"Output: {result.stdout[:2000]}") | |
| if result.stderr: | |
| logger.error(f"Errors: {result.stderr[:2000]}") | |
| except subprocess.TimeoutExpired: | |
| orchestrator_result = {"returncode": -1, "stdout": "", "stderr": "Timed out after 2 hours"} | |
| logger.error("Orchestrator timed out after 2 hours") | |
| except Exception as e: | |
| orchestrator_result = {"returncode": -1, "stdout": "", "stderr": str(e)} | |
| logger.error(f"Orchestrator error: {e}") | |
| finally: | |
| orchestrator_running = False | |
| def root(): | |
| return { | |
| "status": "running", | |
| "orchestrator_running": orchestrator_running, | |
| "time": datetime.utcnow().isoformat() | |
| } | |
| def run_all(background_tasks: BackgroundTasks): | |
| global orchestrator_running, orchestrator_thread | |
| if orchestrator_running: | |
| return {"status": "already_running", "message": "Orchestrator is already running"} | |
| orchestrator_running = True | |
| t = threading.Thread(target=run_orchestrator, daemon=True) | |
| t.start() | |
| return {"status": "started", "message": "Orchestrator started"} | |
| def status(): | |
| return { | |
| "orchestrator_running": orchestrator_running, | |
| "time": datetime.utcnow().isoformat() | |
| } | |
| def logs(): | |
| if orchestrator_result: | |
| return orchestrator_result | |
| return {"message": "No logs yet - orchestrator has not run"} | |
| def logfile(): | |
| log_files = sorted(LOGS_DIR.glob("final_results_*.json"), reverse=True) | |
| if log_files: | |
| return PlainTextResponse(log_files[0].read_text(), media_type="application/json") | |
| log_files = sorted(LOGS_DIR.glob("orchestrator_*.log"), reverse=True) | |
| if log_files: | |
| return PlainTextResponse(log_files[0].read_text(), media_type="text/plain") | |
| return {"message": "No log files found"} | |
| async def startup(): | |
| logger.info(f"[{datetime.utcnow()}] Numerai All 48 Space started") | |
| logger.info(f"Scripts dir exists: {SCRIPTS_DIR.exists()}") | |
| logger.info(f"Orchestrator exists: {ORCHESTRATOR_PATH.exists()}") | |
| # List scripts dir | |
| if SCRIPTS_DIR.exists(): | |
| for f in SCRIPTS_DIR.rglob('*.py'): | |
| logger.info(f" Script: {f}") | |
| # Run orchestrator on startup | |
| global orchestrator_running, orchestrator_thread | |
| orchestrator_running = True | |
| t = threading.Thread(target=run_orchestrator, daemon=True) | |
| t.start() |