Spaces:
Sleeping
Sleeping
Download server/app.py from largebunny/Automatron: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/spaces/largebunny/Automatron/resolve/main/server/app.py
- Command line
-
hf download hf://spaces/largebunny/Automatron/server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/largebunny/Automatron/resolve/main/server/app.py
1.93 kB
| """ | |
| 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} | |
| def reset(): | |
| """Reset endpoint required by HF Space validator.""" | |
| return jsonify({"status": "ok", "message": "Environment reset"}), 200 | |
| def health(): | |
| """Health check endpoint.""" | |
| return jsonify({"status": "healthy"}), 200 | |
| 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 | |
| def get_result(): | |
| """Get last inference result.""" | |
| return jsonify({ | |
| "running": state["running"], | |
| "result": state["last_result"] | |
| }), 200 | |
| def index(): | |
| return jsonify({"status": "online"}), 200 | |
| def main(): | |
| """Entry point for the Flask server.""" | |
| app.run(host="0.0.0.0", port=7860, debug=False) | |
| if __name__ == "__main__": | |
| main() | |