ProbioticFarmer's picture
Upload folder using huggingface_hub
40d0b97 verified
Raw History Blame Contribute Delete
8.03 kB
#!/usr/bin/env python3
"""
hf_pro/app.py
=============
Interactive Web UI & Real-Time Orchestrator for Hugging Face Pro High-Budget Benchmark.
Runs on port 7860 (Hugging Face Spaces default).
"""
import os
import sys
import time
import json
import threading
import subprocess
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse
import uvicorn
app = FastAPI(title="Hyperliquid Microstructure 2x3 Benchmark")
# Global execution state
state = {
"status": "INITIALIZING",
"active_model": None,
"completed_models": 0,
"total_models": 6,
"current_trial": 0,
"total_trials": 100,
"results": [],
"logs": []
}
def log_msg(msg: str):
timestamp = time.strftime("%H:%M:%S")
entry = f"[{timestamp}] {msg}"
print(entry)
state["logs"].append(entry)
if len(state["logs"]) > 200:
state["logs"].pop(0)
def run_high_budget_benchmark():
log_msg("🚀 Hugging Face Pro Benchmark worker initialized.")
state["status"] = "RUNNING"
# Import execution logic
try:
import torch
has_cuda = torch.cuda.is_available()
log_msg(f"Hardware check: CUDA Available = {has_cuda}")
if has_cuda:
log_msg(f"Active GPU: {torch.cuda.get_device_name(0)}")
else:
log_msg(f"High-vCPU fallback: {os.cpu_count()} threads available")
import run_hf_pro_high_budget as runner
# 1. Stream data
log_msg("Streaming multi-asset partitions from Hugging Face ProbioticFarmer/hyperliquid-augmented-copytrading...")
train_dates = [f"2026-08-{d:02d}" for d in range(15, 31, 2)] + [f"2026-09-{d:02d}" for d in range(1, 14, 2)]
test_dates = [f"2026-09-{d:02d}" for d in range(15, 30, 2)] + [f"2026-10-{d:02d}" for d in range(1, 14, 2)]
df_tr_raw, df_te_raw = runner.stream_hf_pro_dataset(train_dates, test_dates, max_rows_day=3000)
log_msg(f"Streamed {len(df_tr_raw):,} train fills and {len(df_te_raw):,} test fills.")
# 2. Features
log_msg("Building non-stationary-proof microstructure features (Who, What, Conditions)...")
df_train, feat_cols, cat_cols, num_cols = runner.build_features(df_tr_raw)
df_test, _, _, _ = runner.build_features(df_te_raw)
val_idx = int(len(df_train) * 0.8)
X_tr = df_train.iloc[:val_idx][feat_cols]
y_tr = df_train.iloc[:val_idx]['target']
X_val = df_train.iloc[val_idx:][feat_cols]
y_val = df_train.iloc[val_idx:]['target']
X_te = df_test[feat_cols]
y_te = df_test['target']
# 3. CatBoost By-Hand & Optuna
state["active_model"] = "CatBoost By-Hand"
log_msg("Training CatBoost By-Hand...")
# (Runner execution handles models and appends to state['results'])
runner.main()
if os.path.exists("results_hf_pro_high_budget.json"):
with open("results_hf_pro_high_budget.json") as f:
state["results"] = json.load(f)
state["status"] = "COMPLETED"
log_msg("🏆 All 6 matrix configurations successfully evaluated!")
except Exception as e:
log_msg(f"❌ Execution error: {e}")
state["status"] = "ERROR"
@app.on_event("startup")
def startup_event():
thread = threading.Thread(target=run_high_budget_benchmark, daemon=True)
thread.start()
@app.get("/api/state")
def get_state():
return JSONResponse(state)
@app.get("/", response_class=HTMLResponse)
def index():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperliquid Microstructure Benchmark (2x3 Matrix)</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg: #090d16;
--card: #131b2e;
--border: #1f2d4a;
--accent: #38bdf8;
--text: #f1f5f9;
--text-muted: #94a3b8;
--green: #10b981;
--amber: #f59e0b;
}
body {
margin: 0;
background: var(--bg);
color: var(--text);
font-family: 'Inter', sans-serif;
padding: 30px;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid var(--border);
padding-bottom: 20px;
margin-bottom: 30px;
}
.badge {
background: rgba(56, 189, 248, 0.15);
color: var(--accent);
padding: 6px 14px;
border-radius: 20px;
font-weight: 600;
font-size: 0.85rem;
border: 1px solid rgba(56, 189, 248, 0.3);
}
.grid {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 25px;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 12px;
padding: 24px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}
th, td {
padding: 12px 14px;
text-align: left;
border-bottom: 1px solid var(--border);
font-size: 0.9rem;
}
th {
color: var(--text-muted);
font-weight: 600;
text-transform: uppercase;
font-size: 0.75rem;
letter-spacing: 0.05em;
}
.terminal {
background: #050811;
border: 1px solid var(--border);
border-radius: 8px;
padding: 15px;
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
color: #38bdf8;
height: 380px;
overflow-y: auto;
line-height: 1.5;
}
</style>
</head>
<body>
<div class="header">
<div>
<h1 style="margin: 0 0 6px 0; font-size: 1.6rem;">⚡ Hyperliquid Microstructure Benchmark</h1>
<p style="margin: 0; color: var(--text-muted); font-size: 0.95rem;">High-Budget Tier (Hugging Face Pro Dedicated Compute) • 2x3 Matrix Evaluation</p>
</div>
<div id="status-badge" class="badge">INITIALIZING WORKLOAD</div>
</div>
<div class="grid">
<div class="card">
<h3 style="margin-top: 0;">🏆 2x3 Factorial Experimental Matrix</h3>
<table id="matrix-table">
<thead>
<tr>
<th>Model</th>
<th>Tuning</th>
<th>PR-AUC (Primary)</th>
<th>ROC-AUC</th>
<th>Brier Score</th>
<th>Latency (ms/1k)</th>
</tr>
</thead>
<tbody id="matrix-body">
<tr><td colspan="6" style="text-align:center; color: var(--text-muted);">Waiting for first benchmark iteration to complete...</td></tr>
</tbody>
</table>
</div>
<div class="card">
<h3 style="margin-top: 0;">📟 Live High-Budget Log Stream</h3>
<div id="log-box" class="terminal">Starting runner daemon...</div>
</div>
</div>
<script>
async function pollState() {
try {
const res = await fetch('/api/state');
const data = await res.json();
document.getElementById('status-badge').innerText = data.status + (data.active_model ? ' (' + data.active_model + ')' : '');
const logBox = document.getElementById('log-box');
logBox.innerHTML = data.logs.join('<br>');
logBox.scrollTop = logBox.scrollHeight;
if (data.results && data.results.length > 0) {
const tbody = document.getElementById('matrix-body');
tbody.innerHTML = data.results.map(r => `
<tr>
<td><strong>${r.model}</strong></td>
<td>${r.tuning}</td>
<td style="color: #38bdf8; font-weight: 600;">${r.pr_auc}</td>
<td>${r.roc_auc}</td>
<td>${r.brier_score}</td>
<td>${r.inference_ms_per_1k} ms</td>
</tr>
`).join('');
}
} catch (e) {
console.error(e);
}
}
setInterval(pollState, 2500);
pollState();
</script>
</body>
</html>
"""
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)