Athspi / app.py
Artificial-superintelligence's picture
Update app.py
1917e67 verified
raw
history blame
2.3 kB
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)