Spaces:
Sleeping
Sleeping
File size: 1,856 Bytes
e2e2212 8f6270d e2e2212 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | """
Minimal HF Space server with /reset endpoint for submission validator.
"""
from flask import Flask, jsonify, request
import subprocess
import os
import threading
app = Flask(__name__)
# Store inference state
state = {"running": False, "last_result": None}
@app.post("/reset")
def reset():
"""Reset endpoint required by HF Space validator."""
return jsonify({"status": "ok", "message": "Environment reset"}), 200
@app.get("/health")
def health():
"""Health check endpoint."""
return jsonify({"status": "healthy"}), 200
@app.post("/run_inference")
def run_inference():
"""Run the autonomous inference in background."""
if state["running"]:
return jsonify({"status": "already_running"}), 429
def run_in_background():
state["running"] = True
try:
result = subprocess.run(
["python", "inference.py"],
capture_output=True,
text=True,
timeout=1200 # 20 minutes
)
state["last_result"] = {
"exit_code": result.returncode,
"stdout": result.stdout,
"stderr": result.stderr
}
except subprocess.TimeoutExpired:
state["last_result"] = {"error": "Inference timeout (>20 min)"}
finally:
state["running"] = False
thread = threading.Thread(target=run_in_background, daemon=True)
thread.start()
return jsonify({"status": "inference_started"}), 202
@app.get("/result")
def get_result():
"""Get last inference result."""
return jsonify({
"running": state["running"],
"result": state["last_result"]
}), 200
@app.get("/")
def index():
return jsonify({"status": "online"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)
|