Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,64 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import tempfile
|
| 3 |
-
import shutil
|
| 4 |
-
from zipfile import ZipFile
|
| 5 |
-
import logging
|
| 6 |
-
import psutil
|
| 7 |
import subprocess
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# Initialize Flask app
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def handle_upload():
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# Save the script content to a file
|
| 53 |
-
script_path = os.path.join(folder_path, 'user_script.py')
|
| 54 |
-
with open(script_path, 'w') as script_file:
|
| 55 |
-
script_file.write(script_content)
|
| 56 |
-
|
| 57 |
-
# Run the script using multiprocessing
|
| 58 |
-
log_output = run_script(script_path, folder_path)
|
| 59 |
-
|
| 60 |
-
# Create a zip file of the entire folder
|
| 61 |
-
zip_path = os.path.join(temp_dir, 'output_folder.zip')
|
| 62 |
-
with ZipFile(zip_path, 'w') as zipf:
|
| 63 |
-
for root, _, files in os.walk(folder_path):
|
| 64 |
-
for file in files:
|
| 65 |
-
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
|
| 66 |
-
|
| 67 |
-
return jsonify({"status": "success", "log_output": log_output, "download_url": f"/download/{os.path.basename(zip_path)}"})
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
-
|
| 71 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
| 72 |
|
| 73 |
-
@app.route('/download/<filename>')
|
| 74 |
-
def download_file(filename):
|
| 75 |
-
try:
|
| 76 |
-
return send_file(os.path.join(tempfile.gettempdir(), filename), as_attachment=True)
|
| 77 |
-
except Exception as e:
|
| 78 |
-
logger.error(f"Error in download_file: {e}")
|
| 79 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
| 80 |
-
|
| 81 |
-
# Endpoint to get connected CPUs information
|
| 82 |
@app.route('/cpu_info', methods=['GET'])
|
| 83 |
-
def
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
for host, data in connected_cpus.items():
|
| 87 |
-
info.append(f"{host}: {data['cpu_count']} CPUs, {data['usage']}% usage")
|
| 88 |
-
return jsonify({"status": "success", "cpu_info": "\n".join(info)})
|
| 89 |
-
except Exception as e:
|
| 90 |
-
logger.error(f"Error in get_cpu_info: {e}")
|
| 91 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
| 92 |
-
|
| 93 |
-
# Endpoint to execute commands
|
| 94 |
-
@app.route('/execute_command', methods=['POST'])
|
| 95 |
-
def execute_command():
|
| 96 |
-
try:
|
| 97 |
-
command = request.form['command']
|
| 98 |
-
if not command:
|
| 99 |
-
return jsonify({"status": "error", "message": "No command provided"}), 400
|
| 100 |
-
|
| 101 |
-
# Ensure commands are executed in a safe environment
|
| 102 |
-
allowed_commands = ['pip install']
|
| 103 |
-
if not any(command.startswith(cmd) for cmd in allowed_commands):
|
| 104 |
-
return jsonify({"status": "error", "message": "Command not allowed"}), 400
|
| 105 |
-
|
| 106 |
-
output_log = tempfile.TemporaryFile(mode='w+t')
|
| 107 |
-
try:
|
| 108 |
-
result = subprocess.run(command.split(), stdout=output_log, stderr=subprocess.STDOUT)
|
| 109 |
-
output_log.seek(0)
|
| 110 |
-
log_output = output_log.read()
|
| 111 |
-
except Exception as e:
|
| 112 |
-
log_output = str(e)
|
| 113 |
-
finally:
|
| 114 |
-
output_log.close()
|
| 115 |
-
return jsonify({"status": "success", "log_output": log_output})
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
logger.error(f"Error in execute_command: {e}")
|
| 119 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
| 120 |
-
|
| 121 |
-
# Main interface
|
| 122 |
-
@app.route('/')
|
| 123 |
-
def index():
|
| 124 |
-
return render_template('index.html')
|
| 125 |
-
|
| 126 |
-
def run_script(script_path, folder_path):
|
| 127 |
-
# Collect all available CPUs including the local host CPU
|
| 128 |
-
total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values()) + 1
|
| 129 |
-
|
| 130 |
-
# Use multiprocessing to run the script
|
| 131 |
-
with multiprocessing.Pool(total_cpus) as pool:
|
| 132 |
-
log_outputs = pool.starmap(target_function, [(script_path, folder_path)] * total_cpus)
|
| 133 |
-
|
| 134 |
-
return '\n'.join(log_outputs)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
-
app.run(host='0.0.0.0', port=7860
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import subprocess
|
| 4 |
+
import tempfile
|
| 5 |
+
import json
|
| 6 |
+
import multiprocessing
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
# Dictionary to store donated CPU information
|
| 11 |
+
donated_cpus = {}
|
| 12 |
+
total_donated_cpus = 0
|
| 13 |
|
| 14 |
+
@app.route('/donate_cpu', methods=['POST'])
|
| 15 |
+
def donate_cpu():
|
| 16 |
+
global total_donated_cpus
|
| 17 |
+
data = request.get_json()
|
| 18 |
+
host = data['host']
|
| 19 |
+
cpu_count = data['cpu_count']
|
| 20 |
+
donated_cpus[host] = {"cpu_count": cpu_count, "usage": 0.0}
|
| 21 |
+
total_donated_cpus += cpu_count
|
| 22 |
+
return jsonify({"status": "success", "message": f"CPU donated by {host}"})
|
| 23 |
+
|
| 24 |
+
@app.route('/update_cpu_usage', methods=['POST'])
|
| 25 |
+
def update_cpu_usage():
|
| 26 |
+
data = request.get_json()
|
| 27 |
+
host = data['host']
|
| 28 |
+
usage = data['usage']
|
| 29 |
+
if host in donated_cpus:
|
| 30 |
+
donated_cpus[host]['usage'] = usage
|
| 31 |
+
return jsonify({"status": "success", "message": f"Updated CPU usage for {host}: {usage}%"})
|
| 32 |
+
return jsonify({"status": "error", "message": "Host not found"}), 404
|
| 33 |
|
| 34 |
+
@app.route('/execute_script', methods=['POST'])
|
| 35 |
+
def execute_script():
|
|
|
|
| 36 |
try:
|
| 37 |
+
data = request.get_json()
|
| 38 |
+
script_content = data['script_content']
|
| 39 |
|
| 40 |
+
# Save the script to a temporary file
|
| 41 |
+
script_file = tempfile.NamedTemporaryFile(delete=False, suffix='.py')
|
| 42 |
+
script_file.write(script_content.encode())
|
| 43 |
+
script_file.close()
|
| 44 |
|
| 45 |
+
# Run the script using the donated CPUs
|
| 46 |
+
cpu_count = total_donated_cpus or 1 # Default to 1 if no CPUs are donated
|
| 47 |
+
result = subprocess.run(['python', script_file.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=60)
|
| 48 |
|
| 49 |
+
os.remove(script_file.name)
|
| 50 |
+
return jsonify({
|
| 51 |
+
"stdout": result.stdout,
|
| 52 |
+
"stderr": result.stderr,
|
| 53 |
+
"returncode": result.returncode
|
| 54 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
@app.route('/cpu_info', methods=['GET'])
|
| 59 |
+
def cpu_info():
|
| 60 |
+
info = [{"host": host, "cpu_count": data['cpu_count'], "usage": data['usage']} for host, data in donated_cpus.items()]
|
| 61 |
+
return jsonify({"cpu_info": info})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
app.run(host='0.0.0.0', port=7860)
|