Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,78 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import os
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import shutil
|
| 5 |
-
import subprocess
|
| 6 |
-
from gtts import gTTS
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Temporary directory for running commands and storing files
|
| 11 |
-
temp_dir = tempfile.mkdtemp()
|
| 12 |
-
UPLOAD_FOLDER = os.path.join(temp_dir, "uploads")
|
| 13 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
|
| 15 |
@app.route('/')
|
| 16 |
def index():
|
|
|
|
| 17 |
return render_template('index.html')
|
| 18 |
|
|
|
|
| 19 |
@app.route('/run', methods=['POST'])
|
| 20 |
def run_command():
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
python_file = os.path.join(temp_dir, "temp_script.py")
|
| 29 |
-
with open(python_file, "w") as f:
|
| 30 |
-
f.write(script_code)
|
| 31 |
-
process = subprocess.run(
|
| 32 |
-
["python3", python_file],
|
| 33 |
-
capture_output=True,
|
| 34 |
-
text=True,
|
| 35 |
-
cwd=temp_dir
|
| 36 |
-
)
|
| 37 |
-
result["output"] = process.stdout
|
| 38 |
-
if process.stderr:
|
| 39 |
-
result["error"] = process.stderr
|
| 40 |
-
|
| 41 |
-
# Handle shell commands like `git clone`
|
| 42 |
-
else:
|
| 43 |
-
process = subprocess.run(
|
| 44 |
-
command,
|
| 45 |
-
shell=True,
|
| 46 |
-
capture_output=True,
|
| 47 |
-
text=True,
|
| 48 |
-
cwd=temp_dir
|
| 49 |
-
)
|
| 50 |
-
result["output"] = process.stdout
|
| 51 |
-
if process.stderr:
|
| 52 |
-
result["error"] = process.stderr
|
| 53 |
-
|
| 54 |
-
# Check for generated audio file
|
| 55 |
-
for file in os.listdir(temp_dir):
|
| 56 |
-
if file.endswith(".mp3"):
|
| 57 |
-
result["audio_file"] = file
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
return jsonify(result)
|
| 63 |
|
| 64 |
-
@app.
|
| 65 |
-
def
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
@app.route('/cleanup', methods=['POST'])
|
| 69 |
-
def cleanup():
|
| 70 |
-
shutil.rmtree(temp_dir)
|
| 71 |
-
os.makedirs(temp_dir, exist_ok=True)
|
| 72 |
-
return jsonify({"status": "Temporary files deleted."})
|
| 73 |
|
| 74 |
-
if __name__ ==
|
| 75 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
import os
|
| 3 |
+
import subprocess
|
| 4 |
import tempfile
|
| 5 |
import shutil
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
BASE_DIR = tempfile.mkdtemp() # Temporary directory for executing user scripts
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def cleanup_temp_dir():
|
| 12 |
+
"""Cleans up the temporary directory."""
|
| 13 |
+
try:
|
| 14 |
+
shutil.rmtree(BASE_DIR)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error cleaning up temp dir: {e}")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@app.route('/')
|
| 20 |
def index():
|
| 21 |
+
"""Render the main terminal interface."""
|
| 22 |
return render_template('index.html')
|
| 23 |
|
| 24 |
+
|
| 25 |
@app.route('/run', methods=['POST'])
|
| 26 |
def run_command():
|
| 27 |
+
"""Executes Python scripts or commands."""
|
| 28 |
+
user_input = request.json.get('code', '')
|
| 29 |
+
response = {"output": "", "error": ""}
|
| 30 |
|
| 31 |
+
# Save the user's Python script to a temporary file
|
| 32 |
+
temp_file_path = os.path.join(BASE_DIR, "user_script.py")
|
| 33 |
+
with open(temp_file_path, 'w') as script_file:
|
| 34 |
+
script_file.write(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Execute the script and capture output
|
| 37 |
+
try:
|
| 38 |
+
process = subprocess.Popen(
|
| 39 |
+
["python3", temp_file_path],
|
| 40 |
+
stdout=subprocess.PIPE,
|
| 41 |
+
stderr=subprocess.PIPE,
|
| 42 |
+
text=True
|
| 43 |
+
)
|
| 44 |
+
stdout, stderr = process.communicate()
|
| 45 |
+
response["output"] = stdout
|
| 46 |
+
response["error"] = stderr
|
| 47 |
except Exception as e:
|
| 48 |
+
response["error"] = str(e)
|
| 49 |
+
|
| 50 |
+
# Cleanup temporary file
|
| 51 |
+
if os.path.exists(temp_file_path):
|
| 52 |
+
os.remove(temp_file_path)
|
| 53 |
+
|
| 54 |
+
return jsonify(response)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@app.route('/clean', methods=['POST'])
|
| 58 |
+
def clean():
|
| 59 |
+
"""Cleans the temporary directory when the user leaves."""
|
| 60 |
+
cleanup_temp_dir()
|
| 61 |
+
return jsonify({"status": "Temporary files cleaned."})
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@app.before_first_request
|
| 65 |
+
def setup():
|
| 66 |
+
"""Set up the temporary directory before the first request."""
|
| 67 |
+
if not os.path.exists(BASE_DIR):
|
| 68 |
+
os.makedirs(BASE_DIR)
|
| 69 |
|
|
|
|
| 70 |
|
| 71 |
+
@app.teardown_appcontext
|
| 72 |
+
def teardown(exception):
|
| 73 |
+
"""Clean up after the app shuts down."""
|
| 74 |
+
cleanup_temp_dir()
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
app.run(host="0.0.0.0", port=7860)
|