Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
import tempfile
|
|
@@ -7,6 +6,7 @@ from zipfile import ZipFile
|
|
| 7 |
import logging
|
| 8 |
import json
|
| 9 |
import psutil
|
|
|
|
| 10 |
import threading
|
| 11 |
|
| 12 |
# Configure logging
|
|
@@ -14,8 +14,6 @@ logging.basicConfig(level=logging.INFO)
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Initialize Flask app
|
| 17 |
-
from flask import Flask, request, jsonify
|
| 18 |
-
|
| 19 |
app = Flask(__name__)
|
| 20 |
|
| 21 |
connected_cpus = {}
|
|
@@ -60,16 +58,23 @@ def run_script(script_name, folder_path):
|
|
| 60 |
return log_output
|
| 61 |
|
| 62 |
# Function to handle file uploads and script execution
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Create a temporary directory to store uploaded files
|
| 65 |
temp_dir = tempfile.mkdtemp()
|
| 66 |
|
| 67 |
# Save the uploaded folder contents to the temporary directory
|
| 68 |
folder_path = os.path.join(temp_dir, 'uploaded_folder')
|
| 69 |
os.makedirs(folder_path, exist_ok=True)
|
| 70 |
-
for
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
# Run the script
|
| 75 |
log_output = run_script(script_name, folder_path)
|
|
@@ -81,43 +86,24 @@ def handle_upload(folder, script_name):
|
|
| 81 |
for file in files:
|
| 82 |
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
|
| 83 |
|
| 84 |
-
return log_output, zip_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
#
|
|
|
|
| 87 |
def get_cpu_info():
|
| 88 |
info = []
|
| 89 |
for host, data in connected_cpus.items():
|
| 90 |
info.append(f"{host}: {data['cpu_count']} CPUs, {data['usage']}% usage")
|
| 91 |
-
return "\n".join(info)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
# Function to launch Gradio interface
|
| 98 |
-
def launch_gradio_interface():
|
| 99 |
-
iface = gr.Interface(
|
| 100 |
-
fn=handle_upload,
|
| 101 |
-
inputs=[
|
| 102 |
-
gr.File(label="Upload Folder", file_count="multiple", file_types=['file']),
|
| 103 |
-
gr.Textbox(label="Python Script Name")
|
| 104 |
-
],
|
| 105 |
-
outputs=[
|
| 106 |
-
gr.Textbox(label="Log Output", interactive=False),
|
| 107 |
-
gr.File(label="Download Output Folder"),
|
| 108 |
-
gr.Textbox(label="Connected CPUs Info", interactive=False)
|
| 109 |
-
],
|
| 110 |
-
live=True,
|
| 111 |
-
theme="light" # Specify a theme that works, "light" is an example
|
| 112 |
-
)
|
| 113 |
-
iface.launch()
|
| 114 |
-
|
| 115 |
-
# Start Flask app in a separate thread
|
| 116 |
if __name__ == "__main__":
|
| 117 |
-
|
| 118 |
-
flask_thread.start()
|
| 119 |
-
|
| 120 |
-
# Launch Gradio interface in the main thread
|
| 121 |
-
launch_gradio_interface()
|
| 122 |
-
|
| 123 |
-
flask_thread.join()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
|
|
|
| 6 |
import logging
|
| 7 |
import json
|
| 8 |
import psutil
|
| 9 |
+
from flask import Flask, request, jsonify, render_template, send_file
|
| 10 |
import threading
|
| 11 |
|
| 12 |
# Configure logging
|
|
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Initialize Flask app
|
|
|
|
|
|
|
| 17 |
app = Flask(__name__)
|
| 18 |
|
| 19 |
connected_cpus = {}
|
|
|
|
| 58 |
return log_output
|
| 59 |
|
| 60 |
# Function to handle file uploads and script execution
|
| 61 |
+
@app.route('/upload', methods=['POST'])
|
| 62 |
+
def handle_upload():
|
| 63 |
+
if 'file' not in request.files or 'script_name' not in request.form:
|
| 64 |
+
return jsonify({"status": "error", "message": "File or script name not provided"}), 400
|
| 65 |
+
|
| 66 |
+
files = request.files.getlist('file')
|
| 67 |
+
script_name = request.form['script_name']
|
| 68 |
+
|
| 69 |
# Create a temporary directory to store uploaded files
|
| 70 |
temp_dir = tempfile.mkdtemp()
|
| 71 |
|
| 72 |
# Save the uploaded folder contents to the temporary directory
|
| 73 |
folder_path = os.path.join(temp_dir, 'uploaded_folder')
|
| 74 |
os.makedirs(folder_path, exist_ok=True)
|
| 75 |
+
for file_obj in files:
|
| 76 |
+
file_path = os.path.join(folder_path, file_obj.filename)
|
| 77 |
+
file_obj.save(file_path)
|
| 78 |
|
| 79 |
# Run the script
|
| 80 |
log_output = run_script(script_name, folder_path)
|
|
|
|
| 86 |
for file in files:
|
| 87 |
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), folder_path))
|
| 88 |
|
| 89 |
+
return jsonify({"status": "success", "log_output": log_output, "download_url": f"/download/{os.path.basename(zip_path)}"})
|
| 90 |
+
|
| 91 |
+
@app.route('/download/<filename>')
|
| 92 |
+
def download_file(filename):
|
| 93 |
+
return send_file(os.path.join(tempfile.gettempdir(), filename), as_attachment=True)
|
| 94 |
|
| 95 |
+
# Endpoint to get connected CPUs information
|
| 96 |
+
@app.route('/cpu_info', methods=['GET'])
|
| 97 |
def get_cpu_info():
|
| 98 |
info = []
|
| 99 |
for host, data in connected_cpus.items():
|
| 100 |
info.append(f"{host}: {data['cpu_count']} CPUs, {data['usage']}% usage")
|
| 101 |
+
return jsonify({"status": "success", "cpu_info": "\n".join(info)})
|
| 102 |
|
| 103 |
+
# Main interface
|
| 104 |
+
@app.route('/')
|
| 105 |
+
def index():
|
| 106 |
+
return render_template('index.html')
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
app.run(host='0.0.0.0', port=7860, threaded=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|