Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,63 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
import io
|
| 3 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
@app.route("/")
|
| 8 |
def index():
|
| 9 |
-
# Render the terminal-like interface
|
| 10 |
return render_template("index.html")
|
| 11 |
|
| 12 |
@app.route("/execute", methods=["POST"])
|
| 13 |
def execute_code():
|
| 14 |
-
|
| 15 |
-
code = request.json.get("code", "")
|
| 16 |
-
|
| 17 |
-
# Redirect standard output to capture execution output
|
| 18 |
output = io.StringIO()
|
| 19 |
sys.stdout = output
|
| 20 |
sys.stderr = output
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
result = f"Error: {e}"
|
| 28 |
finally:
|
| 29 |
-
# Reset
|
| 30 |
sys.stdout = sys.__stdout__
|
| 31 |
sys.stderr = sys.__stderr__
|
| 32 |
|
| 33 |
return jsonify({"result": result})
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
# Run the app on host 0.0.0.0 and port 7860 for compatibility with Hugging Face Spaces
|
| 37 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template
|
| 2 |
import io
|
| 3 |
import sys
|
| 4 |
+
import subprocess
|
| 5 |
+
import tempfile
|
| 6 |
+
import shutil
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# Create a temporary directory for user-installed packages
|
| 12 |
+
temp_dir = tempfile.mkdtemp()
|
| 13 |
+
|
| 14 |
@app.route("/")
|
| 15 |
def index():
|
|
|
|
| 16 |
return render_template("index.html")
|
| 17 |
|
| 18 |
@app.route("/execute", methods=["POST"])
|
| 19 |
def execute_code():
|
| 20 |
+
code = request.json.get("code", "").strip()
|
|
|
|
|
|
|
|
|
|
| 21 |
output = io.StringIO()
|
| 22 |
sys.stdout = output
|
| 23 |
sys.stderr = output
|
| 24 |
+
|
| 25 |
try:
|
| 26 |
+
if code.startswith("!pip install"):
|
| 27 |
+
# Handle pip install commands with temporary directory
|
| 28 |
+
package = code.split("!pip install", 1)[1].strip()
|
| 29 |
+
if package:
|
| 30 |
+
result = subprocess.run(
|
| 31 |
+
[sys.executable, "-m", "pip", "install", package, "--target", temp_dir],
|
| 32 |
+
stdout=subprocess.PIPE,
|
| 33 |
+
stderr=subprocess.PIPE,
|
| 34 |
+
text=True,
|
| 35 |
+
)
|
| 36 |
+
return jsonify({"result": result.stdout + result.stderr})
|
| 37 |
+
else:
|
| 38 |
+
return jsonify({"result": "Error: No package specified for installation."})
|
| 39 |
+
else:
|
| 40 |
+
# Adjust sys.path to include temporary package directory
|
| 41 |
+
sys.path.insert(0, temp_dir)
|
| 42 |
+
exec(code)
|
| 43 |
+
result = output.getvalue()
|
| 44 |
except Exception as e:
|
| 45 |
result = f"Error: {e}"
|
| 46 |
finally:
|
| 47 |
+
# Reset stdout and stderr
|
| 48 |
sys.stdout = sys.__stdout__
|
| 49 |
sys.stderr = sys.__stderr__
|
| 50 |
|
| 51 |
return jsonify({"result": result})
|
| 52 |
|
| 53 |
+
@app.route("/cleanup", methods=["POST"])
|
| 54 |
+
def cleanup():
|
| 55 |
+
# Delete the temporary directory
|
| 56 |
+
global temp_dir
|
| 57 |
+
if os.path.exists(temp_dir):
|
| 58 |
+
shutil.rmtree(temp_dir)
|
| 59 |
+
temp_dir = tempfile.mkdtemp() # Recreate for the next session
|
| 60 |
+
return jsonify({"result": "Temporary files cleaned up."})
|
| 61 |
+
|
| 62 |
if __name__ == "__main__":
|
|
|
|
| 63 |
app.run(host="0.0.0.0", port=7860)
|