-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdashboard.py
More file actions
344 lines (290 loc) · 12 KB
/
Copy pathdashboard.py
File metadata and controls
344 lines (290 loc) · 12 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from flask import Flask, request, jsonify, send_from_directory, Response
import subprocess
import os
import json
import glob
import signal
import sys
import atexit
app = Flask(__name__)
PROMPTS_DIR = os.path.join("utils", "prompts")
current_process = None # Global variable to track the currently running process
def cleanup_process():
"""Cleanup function to ensure child processes are terminated on program exit"""
global current_process
if current_process and current_process.poll() is None:
print("[DASHBOARD] Terminating child process...")
try:
if os.name != "nt": # Unix systems
# Terminate the entire process group
os.killpg(os.getpgid(current_process.pid), signal.SIGTERM)
else: # Windows systems
current_process.terminate()
current_process.wait(timeout=5) # Wait for up to 5 seconds
except subprocess.TimeoutExpired:
print("[DASHBOARD] Forcefully killing the child process...")
if os.name != "nt":
os.killpg(os.getpgid(current_process.pid), signal.SIGKILL)
else:
current_process.kill()
except (ProcessLookupError, OSError):
pass
except Exception as e:
print(f"[DASHBOARD] Error while cleaning up the process: {e}")
finally:
current_process = None
def signal_handler(signum, frame):
"""Signal handler to handle Ctrl+C"""
print(f"\n[DASHBOARD] Received signal {signum}, cleaning up...")
cleanup_process()
print("[DASHBOARD] Cleanup complete, exiting...")
sys.exit(0)
# Register signal handler and exit handler
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
atexit.register(cleanup_process)
@app.route("/")
def serve_dashboard():
os.makedirs(PROMPTS_DIR, exist_ok=True)
return send_from_directory("tools", "tune-dashboard.html")
@app.route("/<path:filename>")
def serve_static_from_tools(filename):
"""
Serve static files from various locations.
- First, it checks for absolute paths to allow previewing any file on the system.
- Then, it checks for files relative to specific project directories ('utils', 'tools').
- Finally, it falls back to checking for files relative to the current working directory.
"""
# 1. Handle absolute paths (the requested bug fix).
# This allows previewing media from any location on the user's machine.
abs_path_candidate = filename
if os.name != 'nt' and not filename.startswith('/'):
abs_path_candidate = '/' + filename
if os.path.isabs(abs_path_candidate) and os.path.isfile(abs_path_candidate):
try:
directory, base_filename = os.path.split(abs_path_candidate)
return send_from_directory(directory, base_filename)
except Exception as e:
# Log the error and fall through to return a 404.
print(f"[DASHBOARD] Error serving absolute path '{abs_path_candidate}': {e}")
# 2. Handle special 'prompts/' path, which are located in the 'utils' directory.
if filename.startswith("prompts/"):
return send_from_directory("utils", filename)
# 3. Handle paths relative to the 'tools' directory.
tool_path = os.path.join("tools", filename)
if os.path.isfile(tool_path):
return send_from_directory("tools", filename)
# 4. Handle paths relative to the current working directory as a fallback.
# This allows serving files from places like the 'output' folder.
full_path = os.path.join(os.getcwd(), filename)
if os.path.isfile(full_path):
return send_from_directory(os.getcwd(), filename)
# If none of the above conditions are met, the file is not found.
return (
jsonify(
{
"error": "File not found",
"requested": filename,
}
),
404,
)
@app.route("/list-prompts")
def list_prompts():
try:
pattern = os.path.join(PROMPTS_DIR, "*.json")
files = glob.glob(pattern)
filenames = [os.path.basename(f) for f in files]
def get_version(filename):
"""Helper to get version number for sorting."""
if filename == "prompts.json":
return 0 # Default prompt is version 0
try:
# Extract number from prompts_v<number>.json
return int(filename.replace("prompts_v", "").replace(".json", ""))
except (ValueError, TypeError):
return -1 # Other files go to the bottom
# Sort by version number in descending order
filenames.sort(key=get_version, reverse=True)
return jsonify(filenames)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/save-prompt", methods=["POST"])
def save_prompt():
try:
data = request.json
if not data:
return jsonify({"error": "No prompt data provided"}), 400
# Find the next version number
pattern = os.path.join(PROMPTS_DIR, "prompts_v*.json")
files = glob.glob(pattern)
if not files:
next_version = 1
else:
versions = [
int(os.path.basename(f).replace("prompts_v", "").replace(".json", ""))
for f in files
]
next_version = max(versions) + 1
filename = f"prompts_v{next_version}.json"
filepath = os.path.join(PROMPTS_DIR, filename)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
return jsonify({"success": True, "filename": filename})
except Exception as e:
print(f"[DASHBOARD] Save failed: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/run-command", methods=["POST"])
def run_command():
global current_process
if current_process and current_process.poll() is None:
return (
jsonify({"success": False, "error": "A command is already running."}),
409,
)
data = request.json
command = data.get("command")
if not command:
return jsonify({"success": False, "error": "No command provided"}), 400
print(f"[DASHBOARD] Running command: {command}")
def generate_output():
global current_process
try:
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
bufsize=1,
preexec_fn=(os.setsid if os.name != "nt" else None),
)
current_process = process
for line in iter(process.stdout.readline, ""):
print(line, end="", flush=True)
yield line
process.stdout.close()
return_code = process.wait()
current_process = None
if return_code != 0:
print(f"[DASHBOARD] Command ended with error code {return_code}")
yield f"COMMAND_FAILED_WITH_CODE_{return_code}\n"
except Exception as e:
print(f"[DASHBOARD] Runtime exception: {e}")
yield f"COMMAND_EXCEPTION_{str(e)}\n"
finally:
current_process = None
return Response(generate_output(), mimetype="text/plain")
@app.route("/stop-command", methods=["POST"])
def stop_command():
global current_process
if current_process and current_process.poll() is None:
try:
if os.name != "nt":
# Attempt to gracefully terminate the process group
os.killpg(os.getpgid(current_process.pid), signal.SIGTERM)
else:
current_process.terminate()
current_process.wait(timeout=5) # Wait for up to 5 seconds
current_process = None
print("[DASHBOARD] Command has been terminated")
return jsonify({"success": True, "message": "Command terminated."})
except subprocess.TimeoutExpired:
print(
"[DASHBOARD] Process did not terminate within 5 seconds, forcefully killing..."
)
if os.name != "nt":
os.killpg(os.getpgid(current_process.pid), signal.SIGKILL)
else:
current_process.kill()
current_process = None
return jsonify({"success": True, "message": "Command forcefully killed."})
except (ProcessLookupError, OSError):
current_process = None
return jsonify({"success": True, "message": "Process already terminated."})
except Exception as e:
print(f"[DASHBOARD] Error while terminating the command: {e}")
return jsonify({"success": False, "error": str(e)}), 500
else:
return jsonify({"success": False, "error": "No command is running."}), 404
@app.route("/export-results", methods=["POST"])
def export_results():
"""Export results by calling export.py script"""
global current_process
if current_process and current_process.poll() is None:
return (
jsonify({"success": False, "error": "Another command is already running."}),
409,
)
data = request.json
output_folder = data.get("output_folder", "output_temp")
file_type = data.get("file_type", "mer")
export_path = data.get("export_path", "./")
# Validate file_type
valid_types = ["au", "image", "mer", "audio", "video"]
if file_type.lower() not in valid_types:
return (
jsonify(
{
"success": False,
"error": f"Invalid file type. Must be one of: {valid_types}",
}
),
400,
)
# Build the export command
command = f'python export.py --output_folder "{output_folder}" --file_type {file_type.lower()} --export_path "{export_path}" --export_csv'
print(f"[DASHBOARD] Running export command: {command}")
def generate_output():
global current_process
try:
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
bufsize=1,
preexec_fn=(os.setsid if os.name != "nt" else None),
)
current_process = process
for line in iter(process.stdout.readline, ""):
print(line, end="", flush=True)
yield line
process.stdout.close()
return_code = process.wait()
current_process = None
if return_code != 0:
print(f"[DASHBOARD] Export command failed with code {return_code}")
yield f"EXPORT_FAILED_WITH_CODE_{return_code}\n"
else:
print("[DASHBOARD] Export completed successfully")
yield f"EXPORT_COMPLETED_SUCCESSFULLY\n"
except Exception as e:
print(f"[DASHBOARD] Export exception: {e}")
yield f"EXPORT_EXCEPTION_{str(e)}\n"
finally:
current_process = None
return Response(generate_output(), mimetype="text/plain")
if __name__ == "__main__":
try:
app.run(debug=False, port=5000, use_reloader=False, threaded=True)
except KeyboardInterrupt:
print("\n[DASHBOARD] Keyboard interrupt received, shutting down...")
cleanup_process()
except OSError as e:
if "Address already in use" in str(e):
print(
f"[DASHBOARD] Port 5000 is in use, please free the port or use another one"
)
print("[DASHBOARD] You can try running: lsof -ti:5000 | xargs kill -9")
else:
print(f"[DASHBOARD] Error starting server: {e}")
cleanup_process()
except Exception as e:
print(f"[DASHBOARD] Runtime error: {e}")
cleanup_process()
finally:
print("[DASHBOARD] Server has been shut down")