File size: 2,296 Bytes
1917e67 5bc4351 382764a 1917e67 21fe3fa 1917e67 382764a 1917e67 382764a 1917e67 4d30d4b 1917e67 4d30d4b a794cee 1917e67 a794cee 1917e67 a794cee 1917e67 a794cee 1917e67 a794cee 1917e67 a794cee 1917e67 a794cee 1917e67 382764a 1917e67 5bc4351 1917e67 21fe3fa 1917e67 382764a 1917e67 382764a 4d30d4b 1917e67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
from flask import Flask, render_template, request, jsonify, send_from_directory
import os
import tempfile
import shutil
import subprocess
from gtts import gTTS
app = Flask(__name__)
# Temporary directory for running commands and storing files
temp_dir = tempfile.mkdtemp()
UPLOAD_FOLDER = os.path.join(temp_dir, "uploads")
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/run', methods=['POST'])
def run_command():
command = request.json.get("command", "").strip()
result = {"output": "", "error": None, "audio_file": None}
try:
# If the command starts with 'python', execute as Python code
if command.startswith("python"):
script_code = command.replace("python", "").strip()
python_file = os.path.join(temp_dir, "temp_script.py")
with open(python_file, "w") as f:
f.write(script_code)
process = subprocess.run(
["python3", python_file],
capture_output=True,
text=True,
cwd=temp_dir
)
result["output"] = process.stdout
if process.stderr:
result["error"] = process.stderr
# Handle shell commands like `git clone`
else:
process = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
cwd=temp_dir
)
result["output"] = process.stdout
if process.stderr:
result["error"] = process.stderr
# Check for generated audio file
for file in os.listdir(temp_dir):
if file.endswith(".mp3"):
result["audio_file"] = file
except Exception as e:
result["error"] = str(e)
return jsonify(result)
@app.route('/download/<filename>')
def download_file(filename):
return send_from_directory(temp_dir, filename, as_attachment=True)
@app.route('/cleanup', methods=['POST'])
def cleanup():
shutil.rmtree(temp_dir)
os.makedirs(temp_dir, exist_ok=True)
return jsonify({"status": "Temporary files deleted."})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|