Athspi / app.py
Artificial-superintelligence's picture
Update app.py
5bc4351 verified
raw
history blame
1.73 kB
from flask import Flask, request, jsonify, render_template
import os
import subprocess
import tempfile
import shutil
app = Flask(__name__)
# Create a temporary directory for operations
temp_dir = tempfile.mkdtemp()
@app.route("/")
def index():
return render_template("index.html")
@app.route("/execute", methods=["POST"])
def execute_code():
command = request.json.get("code", "").strip()
response = ""
try:
# Ensure all operations happen within the temporary directory
os.chdir(temp_dir)
if command.startswith("!"):
# Handle shell commands (e.g., git clone, cd, pip install)
shell_command = command[1:]
process = subprocess.run(
shell_command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
response = process.stdout + process.stderr
else:
# Handle Python code execution
process = subprocess.run(
["python3", "-c", command],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
response = process.stdout + process.stderr
except Exception as e:
response = f"Error: {e}"
return jsonify({"result": response})
@app.route("/cleanup", methods=["POST"])
def cleanup():
# Clean up the temporary directory and recreate it
global temp_dir
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
temp_dir = tempfile.mkdtemp()
return jsonify({"result": "Temporary files cleaned up."})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)