| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- from dash.dependencies import Input, Output, State, ALL
- from dash import html, callback_context
- from utils.file_utils import list_files_in_directory, save_file, delete_file, ensure_directory_exists
- from components.hotwords import generate_training_data_table, HotwordsLayout, ManageHotwordLayout
- import os
- import dash_bootstrap_components as dbc
- TRAINING_HOTWORD_DIR = './training/hotword/'
- def register_hotwords_callbacks(app):
- @app.callback(
- Output("new-hotword-input", "children"),
- Input("add-new-hotword", "n_clicks"),
- prevent_initial_call=True
- )
- def display_new_hotword_input(n_clicks):
- if n_clicks:
- return dbc.Input(id="new-hotword-name", placeholder="Enter new hotword name", type="text")
- @app.callback(
- Output("page-manage-content-nav", "children"),
- [
- Input("new-hotword-name", "n_submit"),
- Input({'type': 'delete-hotword', 'index': ALL}, 'n_clicks')
- ],
- [
- State("new-hotword-name", "value"),
- State({'type': 'delete-hotword', 'index': ALL}, 'id')
- ],
- prevent_initial_call=True
- )
- def manage_hotwords(n_submit, n_clicks_delete, hotword_name, delete_id):
- ctx = callback_context
- if not ctx.triggered:
- return html.Div()
- triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
- if 'new-hotword-name' in triggered_id and hotword_name:
- new_hotword_path = os.path.join(TRAINING_HOTWORD_DIR, hotword_name)
- ensure_directory_exists(new_hotword_path)
- return HotwordsLayout()
- if 'delete-hotword' in triggered_id:
- model_name = delete_id[0]['index']
- hotword_path = os.path.join(TRAINING_HOTWORD_DIR, model_name)
- if os.path.exists(hotword_path):
- os.rmdir(hotword_path)
- return HotwordsLayout()
- return html.Div()
- @app.callback(
- Output('data-table-body', 'children'),
- [Input('data-table-pagination', 'active_page')],
- [State('url', 'pathname')]
- )
- def update_table(page, pathname):
- model_name = pathname.split('/')[-1]
- filepath = f"{TRAINING_HOTWORD_DIR}/{model_name}/"
- training_data = list_files_in_directory(filepath, ".wav")
- rows = generate_training_data_table(model_name, training_data, page, page_size=10)
- return rows
|