-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark_gui.py
More file actions
845 lines (696 loc) · 32.5 KB
/
Copy pathbenchmark_gui.py
File metadata and controls
845 lines (696 loc) · 32.5 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#!/usr/bin/env python3
"""
GUI for Context Tester Benchmark Tool
Provides a user-friendly interface for configuring and running benchmarks.
"""
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
from pathlib import Path
import threading
import sys
import os
import json
from datetime import datetime
# Import parameter schema
from src.parameter_schema import PARAMETER_SCHEMA, get_gui_params_by_section, get_cli_name, get_var_name
# Set matplotlib backend before importing pyplot
import matplotlib
matplotlib.use('TkAgg')
class BenchmarkGUI:
"""GUI for running context tester benchmarks."""
def __init__(self, root):
self.root = root
self.root.title("Context Tester - Benchmark Tool")
self.root.geometry("1080x900")
# Settings file
self.settings_file = Path.home() / ".context_tester_gui_settings.json"
# Configuration variables (auto-generated from parameter_schema.py)
self.text_file = tk.StringVar()
# Dynamically create StringVars for all parameters in schema
for param_name, spec in PARAMETER_SCHEMA.items():
if not spec.get('gui', True) or spec.get('type') == 'positional':
continue
var_name = get_var_name(param_name)
default = spec.get('default')
if default is not None:
setattr(self, var_name, tk.StringVar(value=str(default)))
else:
setattr(self, var_name, tk.StringVar())
# Runtime state
self.running = False
self.benchmark_thread = None
self.recent_hosts = []
self.available_models = []
# Load settings first
self.load_settings()
self.setup_ui()
# Apply environment variables after UI is created (so we can update the label)
self.apply_env_variables()
# Save settings on close
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
def _get_base_url_and_endpoint(self, api_url):
"""Get base URL and endpoint path based on API type.
Args:
api_url: The API URL string
Returns:
tuple: (base_url, endpoint_path)
"""
# Detect Z.AI endpoint
if 'z.ai' in api_url.lower() or '/api/paas/v4' in api_url:
endpoint_path = '/api/paas/v4'
base_url = api_url.replace('/api/paas/v4/chat/completions', '').replace('/chat/completions', '').rstrip('/')
else:
# Standard OpenAI-compatible endpoint
endpoint_path = '/v1'
base_url = api_url.replace('/v1/chat/completions', '').rstrip('/')
return base_url, endpoint_path
def setup_ui(self):
"""Setup the main UI layout."""
# Create main container with scrollbar
main_container = ttk.Frame(self.root)
main_container.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Left side: Configuration
left_frame = ttk.Frame(main_container)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
# Right side: Output console
right_frame = ttk.Frame(main_container)
right_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(5, 0))
# Setup sections
self.setup_text_file_section(left_frame)
self.setup_api_section(left_frame)
self.setup_test_params_section(left_frame)
self.setup_generation_params_section(left_frame)
self.setup_run_controls(left_frame)
self.setup_output_console(right_frame)
def setup_text_file_section(self, parent):
"""Setup text file selection section."""
frame = ttk.LabelFrame(parent, text="Input Text", padding=10)
frame.pack(fill=tk.X, pady=(0, 10))
# File selection
file_frame = ttk.Frame(frame)
file_frame.pack(fill=tk.X)
ttk.Label(file_frame, text="Text File:").pack(side=tk.LEFT, padx=(0, 5))
entry = ttk.Entry(file_frame, textvariable=self.text_file, width=10)
entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
ttk.Button(
file_frame,
text="Browse...",
command=self.browse_text_file
).pack(side=tk.LEFT)
def setup_api_section(self, parent):
"""Setup API configuration section."""
frame = ttk.LabelFrame(parent, text="API Configuration", padding=10)
frame.pack(fill=tk.X, pady=(0, 10))
# API URL with dropdown
url_frame = ttk.Frame(frame)
url_frame.pack(fill=tk.X, pady=2)
ttk.Label(url_frame, text="API URL:", width=15).pack(side=tk.LEFT)
# Combobox for recent hosts
self.url_combo = ttk.Combobox(url_frame, textvariable=self.api_url)
self.url_combo.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
self.url_combo['values'] = self.recent_hosts
# Detect API button
ttk.Button(
url_frame,
text="Detect",
command=self.detect_api_type,
width=8
).pack(side=tk.LEFT)
# API Password/Key
pwd_frame = ttk.Frame(frame)
pwd_frame.pack(fill=tk.X, pady=2)
ttk.Label(pwd_frame, text="API Key:", width=15).pack(side=tk.LEFT)
ttk.Entry(pwd_frame, textvariable=self.api_password, show="*").pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
# Environment variable indicator
self.env_key_label = ttk.Label(pwd_frame, text="", foreground="green", font=('TkDefaultFont', 8))
self.env_key_label.pack(side=tk.LEFT)
# Track when user manually changes API password
self.api_password.trace_add('write', self._on_api_password_changed)
# Model Name with combobox and fetch button
model_frame = ttk.Frame(frame)
model_frame.pack(fill=tk.X, pady=2)
ttk.Label(model_frame, text="Model Name:", width=15).pack(side=tk.LEFT)
self.model_combo = ttk.Combobox(model_frame, textvariable=self.model_name)
self.model_combo.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
self.model_combo['values'] = self.available_models
# Fetch models button
ttk.Button(
model_frame,
text="Fetch",
command=self.fetch_models,
width=8
).pack(side=tk.LEFT)
# Tokenizer Model with combobox
tokenizer_frame = ttk.Frame(frame)
tokenizer_frame.pack(fill=tk.X, pady=2)
ttk.Label(tokenizer_frame, text="Tokenizer Model:", width=15).pack(side=tk.LEFT)
self.tokenizer_combo = ttk.Combobox(tokenizer_frame, textvariable=self.tokenizer_model)
self.tokenizer_combo.pack(side=tk.LEFT, fill=tk.X, expand=True)
self.tokenizer_combo['values'] = self.available_models
# Help text
self.api_help_label = ttk.Label(
frame,
text="Click 'Detect' to auto-configure for KoboldCpp\n"
"For other APIs: Set Model Name and Tokenizer Model manually\n"
"Tokenizer is Huggingface Tokenizer name and may not match!",
font=('TkDefaultFont', 8),
foreground='gray')
self.api_help_label.pack(fill=tk.X, pady=(5, 0))
def setup_test_params_section(self, parent):
"""Setup test parameters section (auto-generated from parameter_schema)."""
frame = ttk.LabelFrame(parent, text="Test Parameters", padding=10)
frame.pack(fill=tk.X, pady=(0, 10))
# Dynamically generate fields from schema
params = get_gui_params_by_section('test')
for param_name, spec in params.items():
var_name = get_var_name(param_name)
gui_label = spec.get('gui_label', param_name.replace('_', ' ').title())
gui_type = spec.get('gui_type', 'entry')
gui_width = spec.get('gui_width')
gui_hint = spec.get('gui_hint')
# Create frame for this field
field_frame = ttk.Frame(frame)
field_frame.pack(fill=tk.X, pady=2)
ttk.Label(field_frame, text=f"{gui_label}:", width=15).pack(side=tk.LEFT)
# Create widget based on type
if gui_type == 'checkbox':
ttk.Checkbutton(field_frame, variable=getattr(self, var_name)).pack(side=tk.LEFT)
elif gui_type == 'combobox':
combo = ttk.Combobox(field_frame, textvariable=getattr(self, var_name))
# Set dropdown values if specified
if spec.get('gui_values'):
combo['values'] = spec['gui_values']
if gui_width:
combo.pack(side=tk.LEFT, fill=tk.X, expand=False)
else:
combo.pack(side=tk.LEFT, fill=tk.X, expand=True)
else: # entry
if gui_width:
ttk.Entry(field_frame, textvariable=getattr(self, var_name), width=gui_width).pack(side=tk.LEFT)
else:
ttk.Entry(field_frame, textvariable=getattr(self, var_name)).pack(side=tk.LEFT, fill=tk.X, expand=True)
# Add hint text if provided
if gui_hint:
ttk.Label(field_frame, text=gui_hint, font=('TkDefaultFont', 8), foreground='gray').pack(side=tk.LEFT, padx=(5, 0))
def setup_generation_params_section(self, parent):
"""Setup generation parameters section (auto-generated from parameter_schema)."""
frame = ttk.LabelFrame(parent, text="Generation Parameters", padding=10)
frame.pack(fill=tk.X, pady=(0, 10))
# Dynamically generate fields from schema
params = get_gui_params_by_section('generation')
for param_name, spec in params.items():
var_name = get_var_name(param_name)
gui_label = spec.get('gui_label', param_name.replace('_', ' ').title())
gui_type = spec.get('gui_type', 'entry')
gui_width = spec.get('gui_width')
gui_hint = spec.get('gui_hint')
# Create frame for this field
field_frame = ttk.Frame(frame)
field_frame.pack(fill=tk.X, pady=2)
ttk.Label(field_frame, text=f"{gui_label}:", width=15).pack(side=tk.LEFT)
# Create widget based on type
if gui_type == 'checkbox':
ttk.Checkbutton(field_frame, variable=getattr(self, var_name)).pack(side=tk.LEFT)
elif gui_type == 'combobox':
combo = ttk.Combobox(field_frame, textvariable=getattr(self, var_name))
# Set dropdown values if specified
if spec.get('gui_values'):
combo['values'] = spec['gui_values']
if gui_width:
combo.pack(side=tk.LEFT, fill=tk.X, expand=True)
else:
combo.pack(side=tk.LEFT, fill=tk.X, expand=True)
else: # entry
if gui_width:
ttk.Entry(field_frame, textvariable=getattr(self, var_name), width=gui_width).pack(side=tk.LEFT)
else:
ttk.Entry(field_frame, textvariable=getattr(self, var_name)).pack(side=tk.LEFT, fill=tk.X, expand=True)
# Add hint text if provided
if gui_hint:
ttk.Label(field_frame, text=gui_hint, font=('TkDefaultFont', 8), foreground='gray').pack(side=tk.LEFT, padx=(5, 0))
def setup_run_controls(self, parent):
"""Setup run button and controls."""
frame = ttk.Frame(parent)
frame.pack(fill=tk.X, pady=(0, 10))
self.run_button = ttk.Button(
frame,
text="Start Benchmark",
command=self.start_benchmark,
style='Accent.TButton'
)
self.run_button.pack(side=tk.LEFT, padx=(0, 5))
self.stop_button = ttk.Button(
frame,
text="Stop",
command=self.stop_benchmark,
state=tk.DISABLED
)
self.stop_button.pack(side=tk.LEFT)
# Status label
self.status_label = ttk.Label(frame, text="Ready", foreground="gray")
self.status_label.pack(side=tk.LEFT, padx=(10, 0))
def setup_output_console(self, parent):
"""Setup output console for real-time feedback."""
frame = ttk.LabelFrame(parent, text="Output Console", padding=10)
frame.pack(fill=tk.BOTH, expand=True)
# Scrolled text widget
self.console = scrolledtext.ScrolledText(
frame,
wrap=tk.WORD,
width=60,
height=40,
font=('Courier', 9),
bg='#1e1e1e',
fg='#d4d4d4',
insertbackground='white'
)
self.console.pack(fill=tk.BOTH, expand=True)
# Configure tags for colored output
self.console.tag_config('info', foreground='#4ec9b0')
self.console.tag_config('warning', foreground='#dcdcaa')
self.console.tag_config('error', foreground='#f48771')
self.console.tag_config('success', foreground='#4fc1ff')
# Make read-only
self.console.config(state=tk.DISABLED)
# Clear button
ttk.Button(
frame,
text="Clear Console",
command=self.clear_console
).pack(pady=(5, 0))
def browse_text_file(self):
"""Browse for text file."""
filename = filedialog.askopenfilename(
title="Select text file",
filetypes=[
("Text files", "*.txt"),
("PDF files", "*.pdf"),
("HTML files", "*.html *.htm"),
("All files", "*.*")
]
)
if filename:
self.text_file.set(filename)
def fetch_models(self):
"""Fetch available models from the API endpoint."""
if not self.api_url.get():
messagebox.showwarning("No URL", "Please enter an API URL first")
return
self.log_to_console("Fetching available models...", 'info')
try:
import requests
base_url, endpoint_path = self._get_base_url_and_endpoint(self.api_url.get())
models_url = f"{base_url}{endpoint_path}/models"
self.log_to_console(f"Querying: {models_url}", 'info')
headers = {}
if self.api_password.get():
headers['Authorization'] = f"Bearer {self.api_password.get()}"
response = requests.get(models_url, headers=headers, timeout=10)
if response.status_code == 200:
data = response.json()
# Extract model IDs
models = []
if 'data' in data:
for model in data['data']:
if 'id' in model:
models.append(model['id'])
elif 'models' in data:
models = data['models']
elif isinstance(data, list):
models = [m.get('id', m) for m in data if isinstance(m, dict)]
if models:
self.available_models = sorted(models)
self.model_combo['values'] = self.available_models
self.tokenizer_combo['values'] = self.available_models
self.log_to_console(f"✓ Found {len(models)} models", 'success')
# Show in dialog
model_list = '\n'.join(models[:20]) # Show first 20
if len(models) > 20:
model_list += f"\n... and {len(models) - 20} more"
messagebox.showinfo(
"Models Found",
f"Found {len(models)} available models:\n\n{model_list}\n\n"
f"Select from dropdown or type to search."
)
else:
self.log_to_console("No models found in response", 'warning')
messagebox.showwarning(
"No Models",
"API responded but no models were found.\n\n"
"You may need to manually enter the model name."
)
else:
self.log_to_console(f"Failed: HTTP {response.status_code}", 'error')
messagebox.showerror(
"Fetch Failed",
f"Could not fetch models.\n\n"
f"HTTP {response.status_code}: {response.text[:200]}"
)
except requests.exceptions.Timeout:
self.log_to_console("Request timed out", 'error')
messagebox.showerror("Timeout", "Request timed out. Check your API URL and connection.")
except Exception as e:
self.log_to_console(f"Error fetching models: {str(e)}", 'error')
import traceback
self.log_to_console(traceback.format_exc(), 'error')
messagebox.showerror("Error", f"Could not fetch models:\n{str(e)}")
def detect_api_type(self):
"""Detect if the API is KoboldCpp and update help text."""
if not self.api_url.get():
messagebox.showwarning("No URL", "Please enter an API URL first")
return
self.log_to_console("Detecting API type...", 'info')
try:
import requests
import json
# Detect KoboldCpp without creating full client (to avoid tokenizer issues)
base_url, endpoint_path = self._get_base_url_and_endpoint(self.api_url.get())
version_url = f"{base_url}/api/extra/version"
self.log_to_console(f"Checking: {version_url}", 'info')
is_kobold = False
try:
response = requests.get(version_url, timeout=5)
if response.status_code == 200:
version_data = response.json()
# Check if response has KoboldCpp-specific fields
if 'result' in version_data or 'version' in version_data:
is_kobold = True
self.log_to_console(f"KoboldCpp version info: {version_data}", 'info')
except requests.exceptions.Timeout:
self.log_to_console("Request timed out - assuming not KoboldCpp", 'warning')
except requests.exceptions.RequestException as e:
self.log_to_console(f"Connection failed: {str(e)} - assuming not KoboldCpp", 'warning')
if is_kobold:
self.log_to_console("✓ Detected KoboldCpp API", 'success')
self.api_help_label.config(
text="✓ KoboldCpp detected - Model Name and Tokenizer will be auto-detected",
foreground='green'
)
# Clear model fields since they'll be auto-detected
self.model_name.set("")
self.tokenizer_model.set("")
messagebox.showinfo(
"KoboldCpp Detected",
"This is a KoboldCpp API.\n\n"
"Model Name and Tokenizer fields have been cleared.\n"
"They will be auto-detected when you run the benchmark."
)
else:
self.log_to_console("✗ Not a KoboldCpp API (likely NVIDIA NIM or other)", 'warning')
self.api_help_label.config(
text="Not KoboldCpp - Please set Model Name and Tokenizer Model manually",
foreground='orange'
)
messagebox.showinfo(
"Not KoboldCpp",
"This does not appear to be a KoboldCpp API.\n\n"
"Please manually configure:\n"
"- Model Name\n"
"- Tokenizer Model\n\n"
"For NVIDIA NIM, check nim_models.md for known working configurations."
)
except Exception as e:
self.log_to_console(f"Error detecting API: {str(e)}", 'error')
import traceback
self.log_to_console(traceback.format_exc(), 'error')
messagebox.showerror("Detection Error", f"Could not detect API type:\n{str(e)}")
def log_to_console(self, message, tag='info'):
"""Log message to console with optional color tag."""
self.console.config(state=tk.NORMAL)
timestamp = datetime.now().strftime("%H:%M:%S")
self.console.insert(tk.END, f"[{timestamp}] ", 'info')
self.console.insert(tk.END, f"{message}\n", tag)
self.console.see(tk.END)
self.console.config(state=tk.DISABLED)
def clear_console(self):
"""Clear the console."""
self.console.config(state=tk.NORMAL)
self.console.delete(1.0, tk.END)
self.console.config(state=tk.DISABLED)
def validate_inputs(self):
"""Validate user inputs before running benchmark."""
errors = []
if not self.text_file.get():
errors.append("Please select a text file")
elif not Path(self.text_file.get()).exists():
errors.append("Text file does not exist")
if not self.api_url.get():
errors.append("API URL is required")
# Check if tokenizer/model are required (not KoboldCpp)
if self.api_url.get():
try:
import requests
import json
# Quick check for KoboldCpp without creating full client
base_url, endpoint_path = self._get_base_url_and_endpoint(self.api_url.get())
version_url = f"{base_url}/api/extra/version"
is_kobold = False
try:
response = requests.get(version_url, timeout=3)
if response.status_code == 200:
version_data = response.json()
if 'result' in version_data or 'version' in version_data:
is_kobold = True
except:
pass
# If not KoboldCpp, require model name and tokenizer
if not is_kobold:
if not self.model_name.get():
errors.append("Model Name is required for non-KoboldCpp APIs\n(Click 'Detect' to check API type)")
if not self.tokenizer_model.get():
errors.append("Tokenizer Model is required for non-KoboldCpp APIs\n(Click 'Detect' to check API type)")
except Exception:
# If detection fails, warn user only if fields are empty
if not self.model_name.get() and not self.tokenizer_model.get():
errors.append("Could not detect API type. For non-KoboldCpp APIs,\nplease provide Model Name and Tokenizer Model")
try:
max_ctx = int(self.max_context.get())
if max_ctx <= 0:
errors.append("Max context must be positive")
except ValueError:
errors.append("Max context must be a number")
try:
rounds = int(self.rounds.get())
if rounds <= 0:
errors.append("Rounds must be positive")
except ValueError:
errors.append("Rounds must be a number")
try:
max_tokens = int(self.max_tokens.get())
if max_tokens <= 0:
errors.append("Max tokens must be positive")
except ValueError:
errors.append("Max tokens must be a number")
try:
temp = float(self.temperature.get())
if temp < 0:
errors.append("Temperature must be non-negative")
except ValueError:
errors.append("Temperature must be a number")
return errors
def build_command_args(self):
"""Build command-line arguments (auto-generated from parameter_schema)."""
args = [self.text_file.get()]
# Dynamically build arguments from schema
for param_name, spec in PARAMETER_SCHEMA.items():
if not spec.get('gui', True) or spec.get('type') == 'positional':
continue
var_name = get_var_name(param_name)
cli_name = get_cli_name(param_name, spec)
value = getattr(self, var_name).get()
if spec['type'] == 'bool':
# Boolean flags
if value == 'True':
args.append(f'--{cli_name}')
elif param_name == 'api_password':
# Special handling for API password
if value and not self._is_from_env_variable():
args.extend([f'--{cli_name}', value])
else:
# Regular parameters
if value:
args.extend([f'--{cli_name}', value])
return args
def start_benchmark(self):
"""Start the benchmark in a separate thread."""
# Validate inputs
errors = self.validate_inputs()
if errors:
# Log to console as well
for error in errors:
self.log_to_console(f"Validation Error: {error}", 'error')
messagebox.showerror("Validation Error", "\n".join(errors))
return
# Update UI state
self.running = True
self.run_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.status_label.config(text="Running...", foreground="green")
# Clear console
self.clear_console()
self.log_to_console("Starting benchmark...", 'success')
# Build command
args = self.build_command_args()
self.log_to_console(f"Command: main.py {' '.join(args)}", 'info')
# Run in thread
self.benchmark_thread = threading.Thread(target=self.run_benchmark_thread, args=(args,))
self.benchmark_thread.daemon = True
self.benchmark_thread.start()
def run_benchmark_thread(self, args):
"""Run the benchmark in a background thread."""
try:
# Import main benchmark module
from src.config import parse_args
from main import run_benchmark
# Redirect stdout to console
import io
class ConsoleRedirector:
def __init__(self, gui, tag='info'):
self.gui = gui
self.tag = tag
def write(self, message):
if message.strip():
self.gui.root.after(0, lambda: self.gui.log_to_console(message.strip(), self.tag))
def flush(self):
pass
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = ConsoleRedirector(self, 'info')
sys.stderr = ConsoleRedirector(self, 'error')
# Parse arguments and run
parsed_args = parse_args(args)
run_benchmark(parsed_args)
# Restore stdout/stderr
sys.stdout = old_stdout
sys.stderr = old_stderr
# Success
self.root.after(0, lambda: self.log_to_console("Benchmark completed successfully!", 'success'))
except Exception as e:
import traceback
error_msg = traceback.format_exc()
self.root.after(0, lambda: self.log_to_console(f"Error: {str(e)}", 'error'))
self.root.after(0, lambda: self.log_to_console(error_msg, 'error'))
finally:
# Reset UI state
self.root.after(0, self.benchmark_finished)
def stop_benchmark(self):
"""Stop the running benchmark."""
if self.running:
self.log_to_console("Stopping benchmark (may take a moment)...", 'warning')
self.running = False
# Request stop via main.py's global flag
try:
from main import request_stop
request_stop()
except Exception as e:
self.log_to_console(f"Error requesting stop: {e}", 'error')
def benchmark_finished(self):
"""Called when benchmark finishes."""
self.running = False
self.run_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.status_label.config(text="Ready", foreground="gray")
def load_settings(self):
"""Load settings from JSON file (auto-generated from parameter_schema)."""
try:
if self.settings_file.exists():
with open(self.settings_file, 'r') as f:
settings = json.load(f)
# Load recent hosts
self.recent_hosts = settings.get('recent_hosts', [])
# Dynamically load last used values from schema
last_values = settings.get('last_values', {})
for param_name, spec in PARAMETER_SCHEMA.items():
if not spec.get('gui', True) or spec.get('type') == 'positional':
continue
var_name = get_var_name(param_name)
if last_values.get(var_name):
getattr(self, var_name).set(last_values[var_name])
print(f"Loaded settings from {self.settings_file}")
except Exception as e:
print(f"Could not load settings: {e}")
def save_settings(self):
"""Save settings to JSON file (auto-generated from parameter_schema)."""
try:
# Update recent hosts
current_url = self.api_url.get()
if current_url and current_url not in self.recent_hosts:
self.recent_hosts.insert(0, current_url)
# Keep only last 10 hosts
self.recent_hosts = self.recent_hosts[:10]
elif current_url in self.recent_hosts:
# Move to front
self.recent_hosts.remove(current_url)
self.recent_hosts.insert(0, current_url)
# Dynamically build last_values from schema
last_values = {}
for param_name, spec in PARAMETER_SCHEMA.items():
if not spec.get('gui', True) or spec.get('type') == 'positional':
continue
# Don't save API password to disk
if param_name == 'api_password':
continue
var_name = get_var_name(param_name)
last_values[var_name] = getattr(self, var_name).get()
settings = {
'recent_hosts': self.recent_hosts,
'last_values': last_values
}
with open(self.settings_file, 'w') as f:
json.dump(settings, f, indent=2)
print(f"Saved settings to {self.settings_file}")
except Exception as e:
print(f"Could not save settings: {e}")
def apply_env_variables(self):
"""Apply API key from environment variables if available."""
if not self.api_password.get():
# Check for API key in environment
api_key = os.environ.get('API_KEY') or \
os.environ.get('API_PASSWORD') or \
os.environ.get('OPENAI_API_KEY') or \
os.environ.get('NVIDIA_API_KEY') or \
os.environ.get('NVAPI_KEY')
if api_key:
# Temporarily disable the trace to avoid triggering during programmatic set
self._setting_env_key = True
self.api_password.set(api_key)
self._env_api_key = api_key # Remember it came from env
self._setting_env_key = False
print(f"Loaded API key from environment variable")
# Update indicator after UI is created
if hasattr(self, 'env_key_label'):
self.env_key_label.config(text="✓ from env")
else:
self._env_api_key = None
else:
self._env_api_key = None
def _is_from_env_variable(self):
"""Check if current API password came from environment variable."""
return hasattr(self, '_env_api_key') and \
self._env_api_key and \
self.api_password.get() == self._env_api_key
def _on_api_password_changed(self, *args):
"""Called when API password field is modified."""
# Skip if we're programmatically setting the env key
if hasattr(self, '_setting_env_key') and self._setting_env_key:
return
# If the current value differs from the env variable, clear the env flag
if hasattr(self, '_env_api_key') and self._env_api_key:
current_value = self.api_password.get()
if current_value != self._env_api_key:
# User manually changed it, clear the env flag
self._env_api_key = None
# Update the indicator
if hasattr(self, 'env_key_label'):
self.env_key_label.config(text="")
def on_closing(self):
"""Handle window close event."""
self.save_settings()
self.root.destroy()
def main():
"""Main entry point."""
root = tk.Tk()
app = BenchmarkGUI(root)
root.mainloop()
if __name__ == "__main__":
main()