-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmllm_ui.py
505 lines (428 loc) · 16.8 KB
/
mllm_ui.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
from flask import Flask, request, url_for, redirect, abort
import mllm_task_file
import mllm_make_task
import mllm_types
from lxml import html
import lxml.builder
import subprocess
import re, sys
import reactivedb
import argparse, yaml
from mllm_types import EnvConfig, GenerationOutput, GenerationEnv
import mllm_state
from lxml import etree
import datetime, token_auth
from mllm_state import apply_output, mark_as_done
from pathlib import Path
from urllib.parse import urlparse
import json
class _E:
def __getattr__(self, elem):
def elem_builder(*children, **kwargs):
real_children = []
for ch in children:
if isinstance(ch, dict):
kwargs.update(ch)
elif isinstance(ch, list):
real_children += ch
else:
real_children.append(ch)
for child in real_children:
assert child is not None
kwargs = {
k.replace('_', '-').rstrip('_'):v
for k, v in kwargs.items()
if v is not None
}
return getattr(lxml.builder.E, elem)(*real_children, **kwargs)
return elem_builder
E = _E()
app = Flask(__name__)
def parse_args():
parser = argparse.ArgumentParser(description='MLLM UI Server')
parser.add_argument('--task-file-path', type=Path, required=True,
help='Path to the tasks file')
parser.add_argument('--env-config-path', type=Path, required=True,
help='Path to env config')
parser.add_argument('--debug', action='store_true')
parser.add_argument('--port', type=int, default=5000)
return parser.parse_args()
args = parse_args()
def get_envconfig():
with open(args.env_config_path, 'r') as f: env_config_data = yaml.safe_load(f)
return EnvConfig.parse_obj(env_config_data)
def get_db():
env_config = get_envconfig()
db = reactivedb.Db(env_config.state_root / "db.sqlite3")
return db
get_db()
def get_systemd_units(task_id):
output = subprocess.check_output(
['systemctl', '--user', 'list-units', '--all', '--no-pager', '--no-legend'],
text=True
)
units = []
prefix = f'mllm-task-{task_id}-'
suffix = '.service'
for line in output.splitlines():
if not line.strip():
continue
columns = line.split()
if columns:
unit_name = columns[0]
if unit_name.startswith(prefix) and unit_name.endswith(suffix):
units.append(unit_name)
return units
def get_generation_outputs(task_id):
table = get_db().table(mllm_types.GenerationOutput)
result = table.query(task_id=task_id)
return list(result.values())
class CSRFError(Exception):
"""Custom exception for CSRF validation failures."""
pass
def validate_csrf(request):
"""Validates CSRF using Origin and Referer headers."""
origin = request.headers.get('Origin')
referer = request.headers.get('Referer')
if not origin or not referer:
raise CSRFError('CSRF validation failed: Origin or Referer headers missing')
expected_host = request.host_url
def get_base_url(url_string):
if not url_string: return None
parsed_url = urlparse(url_string)
scheme = parsed_url.scheme
netloc = parsed_url.netloc
if scheme == 'http' and netloc.endswith(':80'):
netloc = netloc[:-3]
elif scheme == 'https' and netloc.endswith(':443'):
netloc = netloc[:-4]
return f"{scheme}://{netloc}/"
base_origin = get_base_url(origin)
base_expected_host = get_base_url(expected_host)
if base_origin != base_expected_host:
raise CSRFError(f'CSRF validation failed: Origin does not match expected host. Got {base_origin} expected {base_expected_host}')
if not referer.startswith(base_expected_host):
raise CSRFError(f'CSRF validation failed: Referer does not match expected host. Got {referer} expected {base_expected_host}')
@app.route('/')
@token_auth.token_required
def show_tasks():
with open(args.task_file_path, 'r') as f:
task_file_content = f.read()
tasks = mllm_task_file.parse_text(task_file_content)
done_html_elements = []
undone_html_elements = []
for task in tasks:
task_id = task.task_id
if not task_id:
continue
content = '\n'.join(task.content)
lines = content.splitlines()
first_line = lines[0].strip().lower() if lines else ""
if first_line.startswith("done"):
task_div = E.div(
E.h2(
E.a(f"Task [{task_id}]", href=url_for('show_task_detail', task_id=task_id))
),
E.pre(content)
)
done_html_elements.append(task_div)
else:
has_running_units = bool(get_systemd_units(task_id))
status_marker = E.span("⚙️ ", style="color: #0066cc; animation: spin 1.5s linear infinite; display:inline-block; transform-origin: center; vertical-align: middle;") if has_running_units else ""
task_div = E.div(
E.h2(
status_marker,
E.a(f"Task [{task_id}]", href=url_for('show_task_detail', task_id=task_id))
),
E.pre(content),
E.form(
E.input(type='submit', value='Start', style='font-size: 70%',),
method='post',
action=url_for('start_task', task_id=task_id),
style='margin-top: -0.2em;'
)
)
undone_html_elements.append(task_div)
page = E.html(
E.head(
E.title("Tasks Overview"),
E.style("""
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
""")
),
E.body(
E.div(
E.a("Home", href=url_for("show_tasks"))
),
E.h1("Tasks Overview"),
E.h2("Open Tasks"),
*undone_html_elements,
E.h2("Done Tasks"),
*done_html_elements
)
)
return html.tostring(page, pretty_print=True, encoding='unicode')
@app.route('/task/<task_id>')
@token_auth.token_required
def show_task_detail(task_id):
with open(args.task_file_path, 'r') as f:
task_file_content = f.read()
tasks = mllm_task_file.parse_text(task_file_content)
task = next((t for t in tasks if t.task_id == task_id), None)
if not task:
return f"Task {task_id} not found", 404
content = '\n'.join(task.content)
units = get_systemd_units(task_id)
generation_outputs = get_generation_outputs(task_id)
task_div = E.div(
E.h2(f"Task [{task_id}]"),
E.pre(content)
)
task_div.append(E.h3("Running Units"))
if units:
units_list = E.ul(*[E.li(unit) for unit in units])
task_div.append(units_list)
else:
task_div.append(E.p("No running units"))
task_div.append(E.h3("Generation Outputs"))
if generation_outputs:
outputs_table = E.table(
E.tr(
E.th("ID"),
E.th("Created"),
E.th("Base Revision"),
E.th("Meta"),
E.th("Build Status"),
E.th("Changed Files"),
E.th("Applied")
),
style="border-collapse: collapse; width: 100%"
)
generation_outputs.sort(key=lambda o: -o.id)
for gen_output in generation_outputs:
build_status_text = "Unknown" if gen_output.build_successful is None else \
"Success" if gen_output.build_successful else "Failed"
build_status_style = {
None: "",
True: "color: green;",
False: "color: red;"
}[gen_output.build_successful]
created_date = datetime.datetime.fromtimestamp(gen_output.created).strftime('%Y-%m-%d %H:%M:%S')
meta_str = str(gen_output.meta) if gen_output.meta is not None else ""
applied_text = 'Yes' if gen_output.applied else 'No'
row = E.tr(
E.td(
E.a(
str(gen_output.id),
href=url_for('show_generation_output', id=gen_output.id)
)
),
E.td(created_date),
E.td(E.span((gen_output.base_revision or "")[:8], style='font-family: monospace')),
E.td(meta_str),
E.td(E.span(build_status_text, style=build_status_style)),
E.td(', '.join(sorted(gen_output.changed_files))),
E.td(applied_text)
)
outputs_table.append(row)
for td in outputs_table.xpath("//td"):
td.set("style", "border: 1px solid #ddd; padding: 8px;")
for th in outputs_table.xpath("//th"):
th.set("style", "border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2;")
task_div.append(outputs_table)
else:
task_div.append(E.p("No generation outputs"))
start_form = E.form(
E.input(type='submit', value='Start'),
method='post',
action=url_for('start_task', task_id=task_id)
)
task_div.append(start_form) # Add start task button to the task detail view
page = E.html(
E.head(E.title(f"Task {task_id} Details")),
E.body(
E.div(
E.a("Home", href=url_for("show_tasks"))
),
E.h1(f"Task {task_id} Details"),
task_div
)
)
return html.tostring(page, pretty_print=True, encoding='unicode')
@app.route('/task/<task_id>/start', methods=['POST'])
@token_auth.token_required
def start_task(task_id):
validate_csrf(request)
with open(args.task_file_path, 'r') as f:
task_file_content = f.read()
tasks = mllm_task_file.parse_text(task_file_content)
task = next((t for t in tasks if t.task_id == task_id), None)
if not task:
return f"Task {task_id} not found", 404
env_config = get_envconfig()
raw_task_prompt = "\n".join(task.content)
try:
mllm_make_task.parse_task_prompt(raw_task_prompt, args.env_config_path.parent.resolve())
except Exception as e:
return f"Error in parsing task prompt: {e}", 400
cwd = Path(__file__).parent
cmd = [
'systemd-run', '--user', '--unit=mllm-task-%s-start' % task.task_id, '--collect',
'--working-directory=%s' % cwd,
'--slice=mllm.slice',
sys.executable, 'mllm_make_task.py',
'--env-config', str(args.env_config_path),
'--task-file', str(args.task_file_path),
'--task-id', task_id,
'--and-start-prompt'
]
subprocess.check_call(cmd)
return redirect(url_for('show_task_detail', task_id=task_id))
def render_messages(messages):
elements = []
if not isinstance(messages, list):
elements.append(E.p("No messages to display"))
return elements
for message in messages:
if not isinstance(message, dict):
continue
role = message.get('role', 'unknown')
content = message.get('content', '')
message_div = E.div(
E.h4(f"Role: {role}"),
E.pre(content, style="max-height: 10em; overflow-y: auto")
)
elements.append(message_div)
return elements
def render_diff(gen_output):
diffs = mllm_state.generate_diff_for_output(get_envconfig(), gen_output, html=True)
return [ E.div(E.h1(fn), E.div(etree.fromstring(diff, etree.HTMLParser())) if diff else '(empty)')
for fn, diff in sorted(diffs.items()) ]
@app.route('/generation_output/<int:id>')
@token_auth.token_required
def show_generation_output(id):
db = get_db()
table = db.table(mllm_types.GenerationOutput)
gen_output = table.get(id=id)
siblings = list(table.query(task_id=gen_output.task_id).values())
siblings.sort(key=lambda x: x.id)
ids = [g.id for g in siblings]
current_index = ids.index(gen_output.id)
prev_id = ids[current_index - 1] if current_index > 0 else None
next_id = ids[current_index + 1] if current_index < len(ids) - 1 else None
if gen_output.build_successful is None:
build_status = 'unknown build status'
elif gen_output.build_successful:
build_status = E.div('build passed')
else:
build_status = E.div(
E.div('build failed', style='color: red'),
E.pre(gen_output.build_output))
created_date = datetime.datetime.fromtimestamp(gen_output.created).strftime('%Y-%m-%d %H:%M:%S')
if isinstance(gen_output.meta, dict):
meta_content = E.pre(json.dumps(gen_output.meta, indent=2))
elif gen_output.meta is not None:
meta_content = E.p(str(gen_output.meta))
else:
meta_content = E.p("No metadata available")
model_line = E.p(
E.span("Model: ", style="font-weight: bold;"),
meta_content)
applied_text = 'Yes' if gen_output.applied else 'No'
if not gen_output.applied:
apply_form = E.form(
E.input(type='submit', value='Apply'),
method='post',
action=url_for('apply_generation_output', id=id)
)
else:
apply_form = E.p("This output has already been applied.")
nav_links = []
nav_links.append(E.a("Home", href=url_for("show_tasks")))
nav_links.append(' ')
nav_links.append(E.a(f"Task {gen_output.task_id}", href=url_for('show_task_detail', task_id=gen_output.task_id)))
nav_links.append(' ')
if prev_id is not None:
nav_links.append(E.a("← Previous", href=url_for('show_generation_output', id=prev_id), id="prevOutputLink", title='Ctrl-LeftArrow'))
nav_links.append(' ')
if next_id is not None:
nav_links.append(E.a("Next →", href=url_for('show_generation_output', id=next_id), id="nextOutputLink", title='Ctrl-RightArrow'))
page = E.html(
E.head(
E.title(f"Generation Output {id}"),
E.script(
"""
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'ArrowLeft') {
let link = document.getElementById('prevOutputLink');
if (link) window.location.replace(link.getAttribute('href'));
}
if (e.ctrlKey && e.key === 'ArrowRight') {
let link = document.getElementById('nextOutputLink');
if (link) window.location.replace(link.getAttribute('href'));
}
});
"""
)
),
E.body(
E.div(*nav_links, style="margin-bottom: 1em;"),
E.h1(f"Generation Output {id}"),
E.p(E.span("Created: ", style="font-weight: bold;"), created_date),
model_line,
E.p(f"Applied: {applied_text}"),
build_status,
apply_form,
*render_diff(gen_output),
E.h2("Messages"),
*render_messages(gen_output.messages),
E.h2("Details"),
E.pre(str(gen_output))
)
)
return html.tostring(page, pretty_print=True, encoding='unicode')
@app.route('/generation_output/<int:id>/apply', methods=['POST'])
@token_auth.token_required
def apply_generation_output(id):
validate_csrf(request)
force = request.args.get('force')
db = get_db()
table = db.table(mllm_types.GenerationOutput)
gen_output = table.get(id=id)
if not gen_output:
return f"GenerationOutput {id} not found", 404
if gen_output.applied:
return f"GenerationOutput {id} has already been applied", 400
env_config = get_envconfig()
root_path = args.env_config_path.parent.resolve()
try:
apply_output(env_config, root_path, gen_output, force)
except mllm_state.MergeConflictError as conflict_error:
return render_conflict_resolution_page(conflict_error.conflict_files, gen_output)
gen_output.applied = True
table.set(gen_output)
db.commit()
mark_as_done(args.task_file_path, gen_output.task_id)
return redirect('/')
def render_conflict_resolution_page(conflict_files, gen_output):
conflict_list_items = [E.li(file) for file in conflict_files]
page = E.html(
E.head(E.title(f"Merge Conflicts for Generation Output {gen_output.id}")),
E.body(
E.h1(f"Merge Conflicts Detected"),
E.p("The following files have merge conflicts:"),
E.ul(*conflict_list_items),
E.form(
E.input(type='submit', value='Accept with Conflicts'),
method='post',
action=f"/generation_output/{gen_output.id}/apply?force=true"
)
)
)
return html.tostring(page, pretty_print=True, encoding='unicode')
if __name__ == '__main__':
token_auth.install(app, socketio=None, app_name="mllm_ui")
app.run(debug=args.debug, port=args.port)