Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,75 @@
|
|
1 |
-
|
2 |
-
from flask import Flask, request, jsonify, render_template, stream_with_context, Response
|
3 |
import os
|
4 |
-
import subprocess
|
5 |
import tempfile
|
6 |
import shutil
|
7 |
-
import
|
8 |
-
import
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
-
logging.basicConfig(level=logging.DEBUG)
|
12 |
|
13 |
-
#
|
14 |
temp_dir = tempfile.mkdtemp()
|
15 |
-
os.
|
|
|
16 |
|
17 |
-
@app.route(
|
18 |
def index():
|
19 |
-
return render_template(
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
def execute_shell_command(command):
|
22 |
-
"""Executes a shell command and streams output with improved error handling."""
|
23 |
try:
|
24 |
-
#
|
25 |
-
if command.startswith(
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
text=True,
|
33 |
-
cwd=temp_dir
|
34 |
-
env={'GIT_TERMINAL_PROMPT': '0'} # Prevent git from prompting
|
35 |
)
|
|
|
|
|
|
|
|
|
|
|
36 |
else:
|
37 |
-
process = subprocess.
|
38 |
-
command,
|
39 |
-
shell=True,
|
40 |
-
|
41 |
-
stderr=subprocess.PIPE,
|
42 |
text=True,
|
43 |
cwd=temp_dir
|
44 |
)
|
|
|
|
|
|
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
if output:
|
52 |
-
yield f"{output}<br>"
|
53 |
-
if error:
|
54 |
-
yield f"<span style='color: red'>Error: {error}</span><br>"
|
55 |
-
|
56 |
-
# Check if process has finished
|
57 |
-
if output == '' and error == '' and process.poll() is not None:
|
58 |
-
break
|
59 |
-
|
60 |
-
# Get return code
|
61 |
-
return_code = process.poll()
|
62 |
-
if return_code != 0:
|
63 |
-
yield f"<span style='color: red'>Command failed with return code {return_code}</span><br>"
|
64 |
-
else:
|
65 |
-
yield "Command completed successfully.<br>"
|
66 |
|
67 |
except Exception as e:
|
68 |
-
|
69 |
-
finally:
|
70 |
-
if process:
|
71 |
-
process.stdout.close()
|
72 |
-
process.stderr.close()
|
73 |
|
74 |
-
|
75 |
-
def execute_code():
|
76 |
-
command = request.json.get("code", "").strip()
|
77 |
-
if not command:
|
78 |
-
return jsonify({"result": "Error: No command provided."})
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
# Log the command being executed
|
84 |
-
app.logger.debug(f"Executing shell command: {shell_command}")
|
85 |
-
return Response(
|
86 |
-
stream_with_context(execute_shell_command(shell_command)),
|
87 |
-
content_type='text/html'
|
88 |
-
)
|
89 |
-
else:
|
90 |
-
# Handle Python code execution
|
91 |
-
process = subprocess.run(
|
92 |
-
[sys.executable, "-c", command],
|
93 |
-
stdout=subprocess.PIPE,
|
94 |
-
stderr=subprocess.PIPE,
|
95 |
-
text=True,
|
96 |
-
cwd=temp_dir
|
97 |
-
)
|
98 |
-
return jsonify({"result": process.stdout + process.stderr})
|
99 |
-
except Exception as e:
|
100 |
-
app.logger.error(f"Error executing command: {str(e)}")
|
101 |
-
return jsonify({"result": f"Error: {str(e)}"})
|
102 |
|
103 |
-
@app.route(
|
104 |
def cleanup():
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
109 |
-
temp_dir = tempfile.mkdtemp()
|
110 |
-
os.chmod(temp_dir, 0o777)
|
111 |
-
return jsonify({"result": "Temporary files cleaned up."})
|
112 |
-
except Exception as e:
|
113 |
-
return jsonify({"result": f"Error during cleanup: {str(e)}"})
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
-
|
117 |
-
os.chmod(temp_dir, 0o777)
|
118 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
|
|
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 |
+
command = request.json.get("command", "").strip()
|
22 |
+
result = {"output": "", "error": None, "audio_file": None}
|
23 |
|
|
|
|
|
24 |
try:
|
25 |
+
# If the command starts with 'python', execute as Python code
|
26 |
+
if command.startswith("python"):
|
27 |
+
script_code = command.replace("python", "").strip()
|
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 |
+
result["error"] = str(e)
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
return jsonify(result)
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
@app.route('/download/<filename>')
|
65 |
+
def download_file(filename):
|
66 |
+
return send_from_directory(temp_dir, filename, as_attachment=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__ == "__main__":
|
75 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|