zelin-bm / server.py
TomatitoToho's picture
Upload server.py with huggingface_hub
db054b4 verified
Raw
History Blame Contribute Delete
5.65 kB
"""Server v3 - runs benchmark_v3.py and serves results."""
import http.server
import socketserver
import os
import json
import subprocess
import threading
import time
import shutil
DATA_DIR = "/app/data"
RESULTS_DIR = f"{DATA_DIR}/results"
MODELS_DIR = f"{DATA_DIR}/models"
LOG_FILE = "/app/tmp/benchmark.log"
FLAG_FILE = "/app/tmp/benchmark_running.flag"
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/" or self.path == "":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
html = """<!DOCTYPE html>
<html><head><title>Zelin Benchmark v3 (Optimized)</title>
<meta charset='utf-8'>
<style>body{font-family:system-ui;max-width:1200px;margin:40px auto;padding:20px;background:#0d0d0d;color:#eee}h1{color:#7c3aed}pre{background:#1a1a1a;padding:15px;border-radius:8px;overflow:auto;max-height:600px;white-space:pre-wrap}.card{background:#1a1a1a;padding:15px;border-radius:8px;margin:10px 0}a{color:#a78bfa}.opt{color:#10b981}</style>
</head><body>
<h1>Zelin Benchmark v3 (Optimized)</h1>
<div class='card'>
<p class='opt'>✨ Optimizations: Luigi OpenBLAS wheel, n_threads=4, n_batch=512, longer warmup</p>
<p><strong>Estado:</strong> <span id='status'>checking...</span></p>
<p><strong>Modelos completados:</strong> <span id='count'>0</span></p>
<p><a href='/results'>Ver resultados JSON</a></p>
</div>
<h3>Logs en vivo:</h3>
<pre id='logs'>cargando...</pre>
<script>
async function update() {
try {
const r = await fetch('/status');
const d = await r.json();
document.getElementById('status').textContent = d.status;
document.getElementById('count').textContent = d.count;
document.getElementById('logs').textContent = d.logs || '(sin logs)';
} catch(e) {
document.getElementById('logs').textContent = 'Error: ' + e;
}
}
update();
setInterval(update, 5000);
</script>
</body></html>"""
self.wfile.write(html.encode())
elif self.path == "/status":
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
status = {
"status": "running" if os.path.exists(FLAG_FILE) else "idle",
"count": 0,
"logs": ""
}
try:
with open(LOG_FILE, "r") as f:
status["logs"] = f.read()[-5000:]
except:
status["logs"] = "(no logs yet)"
try:
if os.path.exists(RESULTS_DIR):
files = [f for f in os.listdir(RESULTS_DIR) if f.startswith("benchmark_partial_")]
status["count"] = len(files)
except:
pass
self.wfile.write(json.dumps(status).encode())
elif self.path == "/results":
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
try:
final_path = f"{RESULTS_DIR}/benchmark_final.json"
if os.path.exists(final_path):
with open(final_path, "r") as f:
self.wfile.write(f.read().encode())
else:
results = []
if os.path.exists(RESULTS_DIR):
for fname in sorted(os.listdir(RESULTS_DIR)):
if fname.startswith("benchmark_") and fname.endswith(".json") and "final" not in fname:
try:
with open(f"{RESULTS_DIR}/{fname}", "r") as f:
results.append(json.load(f))
except:
pass
self.wfile.write(json.dumps({
"status": "partial",
"completed": len(results),
"results": results
}, ensure_ascii=False).encode())
except Exception as e:
self.wfile.write(json.dumps({"error": str(e)}).encode())
else:
self.send_response(404)
self.end_headers()
def log_message(self, *a):
pass
def run_benchmark():
"""Run benchmark_v3.py in background."""
time.sleep(5)
# Clean up old results
try:
if os.path.exists(RESULTS_DIR):
shutil.rmtree(RESULTS_DIR)
os.makedirs(RESULTS_DIR, exist_ok=True)
except:
pass
with open(FLAG_FILE, "w") as f:
f.write("started")
try:
with open(LOG_FILE, "w") as logf:
proc = subprocess.Popen(
["python", "/app/benchmark_v3.py"],
stdout=logf,
stderr=subprocess.STDOUT,
env={**os.environ},
cwd="/app",
)
proc.wait()
except Exception as e:
with open(LOG_FILE, "a") as f:
f.write(f"\nFATAL: {e}\n")
finally:
try:
os.remove(FLAG_FILE)
except:
pass
if __name__ == "__main__":
os.makedirs(RESULTS_DIR, exist_ok=True)
os.makedirs(MODELS_DIR, exist_ok=True)
os.makedirs("/app/tmp", exist_ok=True)
t = threading.Thread(target=run_benchmark, daemon=True)
t.start()
with socketserver.TCPServer(("0.0.0.0", 7860), H) as server:
print("Server on 7860", flush=True)
server.serve_forever()