-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.py
More file actions
423 lines (382 loc) · 14.6 KB
/
settings.py
File metadata and controls
423 lines (382 loc) · 14.6 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
import flet as ft
import json
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from services.audio_processor import AudioProcessor
from utils.model_manager import WhisperModelManager
def show_settings(page, audio_processor: AudioProcessor):
model_manager = WhisperModelManager()
# Load settings
def load_settings():
try:
if Path("app_settings.json").exists():
with open("app_settings.json", "r") as f:
return json.load(f)
else:
# Return default settings if the file doesn't exist
return {
"max_workers": 15,
"batch_size": 10,
"retry_attempts": 3,
"auto_cleanup": True,
"voice": "en-US-EmmaNeural",
"rate": "+0%",
"volume": "+0%",
"pitch": "+0Hz",
"words_per_minute": None,
"whisper_model": "base",
}
except (FileNotFoundError, json.JSONDecodeError):
# Return default settings if there's an error reading the file
return {
"max_workers": 15,
"batch_size": 10,
"retry_attempts": 3,
"auto_cleanup": True,
"voice": "en-US-EmmaNeural",
"rate": "+0%",
"volume": "+0%",
"pitch": "+0Hz",
"words_per_minute": None,
"whisper_model": "base",
}
settings = load_settings()
# Settings fields with descriptions
workers_field = ft.TextField(
label="Max Workers (1-50)",
value=str(settings.get("max_workers", 15)),
width=300,
helper_text="Number of parallel processing threads",
text_size=14,
border_color=ft.colors.BLUE_200,
height=65,
)
batch_field = ft.TextField(
label="Batch Size (1-100)",
value=str(settings.get("batch_size", 10)),
width=300,
helper_text="Number of segments to process at once",
text_size=14,
border_color=ft.colors.BLUE_200,
height=65,
)
retry_field = ft.TextField(
label="Retry Attempts (1-10)",
value=str(settings.get("retry_attempts", 3)),
width=300,
helper_text="Number of retries on failure",
text_size=14,
border_color=ft.colors.BLUE_200,
height=65,
)
auto_cleanup_switch = ft.Switch(
label="Auto Cleanup Temporary Files",
value=settings.get("auto_cleanup", True),
label_position=ft.LabelPosition.LEFT,
active_color=ft.colors.BLUE_700,
active_track_color=ft.colors.BLUE_200,
)
# Voice settings
voices = audio_processor.voice_manager.voices.get('voices', [])
voice_dropdown = ft.Dropdown(
label="Voice",
options=[ft.dropdown.Option(voice) for voice in voices],
value=settings.get("voice", "en-US-EmmaNeural"),
width=300,
helper_text="Select voice for text-to-speech conversion",
border_color=ft.colors.BLUE_200,
height=65,
)
# Sliders for voice adjustments
rate_slider = ft.Slider(
min=-50,
max=50,
value=float(settings.get("rate", "+0%").rstrip("%")) / 100 * 50,
label="{value}",
divisions=20,
width=300,
)
rate_label = ft.Text("Speech Rate:", size=14, weight=ft.FontWeight.BOLD)
rate_value = ft.Text(settings.get("rate", "+0%"), size=14, color=ft.colors.BLUE)
def on_rate_change(e):
try:
if e.data and e.data.strip():
val = float(e.data)
percentage = int((val / 50) * 100)
rate_value.value = f"{percentage:+d}%"
page.update()
except ValueError:
pass
rate_slider.on_change = on_rate_change
volume_slider = ft.Slider(
min=-50,
max=50,
value=float(settings.get("volume", "+0%").rstrip("%")) / 100 * 50,
label="{value}",
divisions=20,
width=300,
)
volume_label = ft.Text("Volume:", size=14, weight=ft.FontWeight.BOLD)
volume_value = ft.Text(settings.get("volume", "+0%"), size=14, color=ft.colors.BLUE)
def on_volume_change(e):
try:
if e.data and e.data.strip():
val = float(e.data)
percentage = int((val / 50) * 100)
volume_value.value = f"{percentage:+d}%"
page.update()
except ValueError:
pass
volume_slider.on_change = on_volume_change
pitch_slider = ft.Slider(
min=-50,
max=50,
value=float(settings.get("pitch", "+0Hz").rstrip("Hz")),
label="{value}",
divisions=20,
width=300,
)
pitch_label = ft.Text("Pitch:", size=14, weight=ft.FontWeight.BOLD)
pitch_value = ft.Text(settings.get("pitch", "+0Hz"), size=14, color=ft.colors.BLUE)
def on_pitch_change(e):
try:
if e.data and e.data.strip():
val = float(e.data)
pitch_value.value = f"{int(val):+d}Hz"
page.update()
except ValueError:
pass
pitch_slider.on_change = on_pitch_change
wpm_field = ft.TextField(
label="Words per Minute",
value=str(settings.get("words_per_minute", "")) if settings.get("words_per_minute") else "",
width=300,
helper_text="Optional: 50-600 WPM (leave empty for default)",
text_size=14,
border_color=ft.colors.BLUE_200,
height=65,
)
# Whisper Model settings
whisper_model_dropdown = ft.Dropdown(
label="Whisper Model",
options=[
ft.dropdown.Option("tiny", text="Tiny (Fastest)"),
ft.dropdown.Option("base", text="Base (Fast)"),
ft.dropdown.Option("small", text="Small (Balanced)"),
ft.dropdown.Option("medium", text="Medium (Accurate)"),
ft.dropdown.Option("large", text="Large (Most Accurate)"),
],
value=settings.get("whisper_model", "base"),
width=300,
)
download_progress = ft.ProgressBar(width=300, visible=False)
model_status_text = ft.Text("", size=14)
def update_model_status():
model_name = whisper_model_dropdown.value
is_downloaded = model_manager.is_model_downloaded(model_name)
if is_downloaded:
model_status_text.value = "Model already downloaded"
model_status_text.color = ft.colors.GREEN
else:
model_status_text.value = "Model not downloaded"
model_status_text.color = ft.colors.RED
page.update()
def handle_download_click(e):
def download_model():
try:
download_progress.visible = True
model_status_text.value = "Downloading model..."
page.update()
model_name = whisper_model_dropdown.value
model_manager.get_model(model_name)
model_status_text.value = "Model downloaded successfully!"
model_status_text.color = ft.colors.GREEN
except Exception as ex:
model_status_text.value = f"Error downloading model: {str(ex)}"
model_status_text.color = ft.colors.RED
finally:
download_progress.visible = False
page.update()
# Run the download in a separate thread
import threading
threading.Thread(target=download_model).start()
# Update model status on dropdown change
def on_model_selection_change(e):
update_model_status()
whisper_model_dropdown.on_change = on_model_selection_change
# Create tabs for settings
processing_tab = ft.Tab(
text="Processing",
content=ft.Container(
content=ft.Column([
ft.Container(
content=ft.Column([
ft.Container(
content=workers_field,
padding=ft.padding.only(bottom=15)
),
ft.Container(
content=batch_field,
padding=ft.padding.only(bottom=15)
),
ft.Container(
content=retry_field,
padding=ft.padding.only(bottom=15)
),
ft.Container(
content=auto_cleanup_switch,
padding=ft.padding.only(bottom=10)
),
]),
padding=20
),
]),
padding=10,
),
)
voice_tab = ft.Tab(
text="Voice",
content=ft.Container(
content=ft.Column([
ft.Container(
content=ft.Column([
voice_dropdown,
ft.Container(height=20),
ft.Container(
content=ft.Column([
rate_label,
ft.Row([rate_slider, rate_value], alignment=ft.MainAxisAlignment.SPACE_BETWEEN),
ft.Container(height=10),
volume_label,
ft.Row([volume_slider, volume_value], alignment=ft.MainAxisAlignment.SPACE_BETWEEN),
ft.Container(height=10),
pitch_label,
ft.Row([pitch_slider, pitch_value], alignment=ft.MainAxisAlignment.SPACE_BETWEEN),
], spacing=10),
),
ft.Container(height=20),
wpm_field,
], spacing=10),
padding=20
),
]),
padding=10,
),
)
whisper_tab = ft.Tab(
text="Whisper",
content=ft.Container(
content=ft.Column([
whisper_model_dropdown,
ft.ElevatedButton(
"Download Selected Model",
on_click=handle_download_click,
),
download_progress,
model_status_text,
], spacing=20),
padding=30
),
)
settings_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Settings", size=20, weight=ft.FontWeight.BOLD, color=ft.colors.BLUE_700),
content=ft.Container(
content=ft.Column([
ft.Tabs(
selected_index=0,
animation_duration=300,
tabs=[
processing_tab,
voice_tab,
whisper_tab,
],
expand=True,
),
]),
width=600,
height=500,
),
actions=[
ft.Row([
ft.TextButton(
"Reset to Defaults",
on_click=lambda e: reset_to_defaults(),
style=ft.ButtonStyle(color=ft.colors.BLUE_700)
),
ft.Row([
ft.TextButton(
"Cancel",
on_click=lambda e: close_settings(),
style=ft.ButtonStyle(color=ft.colors.GREY_700)
),
ft.TextButton(
"Save",
on_click=lambda e: save_settings(),
style=ft.ButtonStyle(color=ft.colors.BLUE_700)
),
]),
], alignment=ft.MainAxisAlignment.SPACE_BETWEEN),
],
actions_alignment=ft.MainAxisAlignment.END,
on_dismiss=lambda e: close_settings(),
)
def close_settings():
settings_dialog.open = False
page.update()
def reset_to_defaults():
workers_field.value = "15"
batch_field.value = "10"
retry_field.value = "3"
voice_dropdown.value = "en-US-EmmaNeural"
auto_cleanup_switch.value = True
rate_slider.value = 0
volume_slider.value = 0
pitch_slider.value = 0
wpm_field.value = ""
whisper_model_dropdown.value = "base"
page.update()
def save_settings():
try:
new_workers = int(workers_field.value)
new_batch = int(batch_field.value)
new_retry = int(retry_field.value)
new_wpm = int(wpm_field.value) if wpm_field.value.strip() else None
new_auto_cleanup = auto_cleanup_switch.value
if not (1 <= new_workers <= 50):
raise ValueError("Max workers must be between 1 and 50")
if not (1 <= new_batch <= 100):
raise ValueError("Batch size must be between 1 and 100")
if not (1 <= new_retry <= 10):
raise ValueError("Retry attempts must be between 1 and 10")
if new_wpm is not None and not (50 <= new_wpm <= 600):
raise ValueError("Words per minute must be between 50 and 600")
# Format voice settings with proper signs
rate_val = float(rate_slider.value)
volume_val = float(volume_slider.value)
pitch_val = float(pitch_slider.value)
settings = {
"max_workers": new_workers,
"batch_size": new_batch,
"retry_attempts": new_retry,
"voice": voice_dropdown.value,
"rate": f"{int((rate_val / 50) * 100):+d}%",
"volume": f"{int((volume_val / 50) * 100):+d}%",
"pitch": f"{int(pitch_val):+d}Hz",
"words_per_minute": new_wpm,
"auto_cleanup": new_auto_cleanup,
"whisper_model": whisper_model_dropdown.value,
}
with open("app_settings.json", "w") as f:
json.dump(settings, f)
audio_processor.settings = settings
audio_processor.executor = ThreadPoolExecutor(max_workers=new_workers)
close_settings()
page.show_snack_bar(ft.SnackBar(content=ft.Text("Settings saved successfully!")))
except ValueError as e:
page.show_snack_bar(ft.SnackBar(content=ft.Text(str(e)), bgcolor=ft.colors.ERROR))
page.update()
# Add the dialog to the page
page.dialog = settings_dialog
settings_dialog.open = True
page.update()