Spaces:
Runtime error
Runtime error
Download backend.py from MLDeveloper/python_compiler: direct link, hf CLI and curl.
- Browser
- Download file 952 Bytes
-
https://huggingface.co/spaces/MLDeveloper/python_compiler/resolve/5a1aaa630814730f236f2d52b463f84b74df8430/backend.py
- Command line
-
hf download hf://spaces/MLDeveloper/python_compiler@5a1aaa630814730f236f2d52b463f84b74df8430/backend.py
-
curl -L -o backend.py https://huggingface.co/spaces/MLDeveloper/python_compiler/resolve/5a1aaa630814730f236f2d52b463f84b74df8430/backend.py
952 Bytes
| from flask import Flask, request, jsonify | |
| import subprocess | |
| app = Flask(__name__) | |
| def run_code(): | |
| try: | |
| # Ensure request contains JSON data | |
| if not request.is_json: | |
| return jsonify({"output": "Invalid request. Expecting JSON format."}), 400 | |
| data = request.get_json() | |
| code = data.get("code", "") | |
| if not code.strip(): | |
| return jsonify({"output": "No code provided!"}), 400 | |
| # Run Python code in a subprocess | |
| process = subprocess.run( | |
| ["python", "-c", code], | |
| capture_output=True, | |
| text=True | |
| ) | |
| output = process.stdout.strip() if process.stdout else process.stderr.strip() | |
| return jsonify({"output": output}) | |
| except Exception as e: | |
| return jsonify({"output": f"Error: {str(e)}"}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=5000, debug=True) | |