"""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 = """
✨ Optimizations: Luigi OpenBLAS wheel, n_threads=4, n_batch=512, longer warmup
Estado: checking...
Modelos completados: 0
cargando...""" 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()