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
|
#include "stdafx.h"
#include "LoadModelDlg.h"
#include "Utils/miscUtils.h"
#include "Utils/logger.h"
#include "ModelAdvancedDlg.h"
constexpr int progressMaxInteger = 1024 * 8;
HRESULT LoadModelDlg::show()
{
auto res = DoModal( nullptr );
if( res == -1 )
return HRESULT_FROM_WIN32( GetLastError() );
if( res == IDOK )
{
HRESULT hr = appState.lastScreenLoad();
switch( hr )
{
case SCREEN_TRANSCRIBE:
case SCREEN_CAPTURE:
return hr;
default:
return SCREEN_TRANSCRIBE;
}
}
return S_OK;
}
LRESULT LoadModelDlg::OnInitDialog( UINT nMessage, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
// First DDX call, hooks up variables to controls.
DoDataExchange( false );
cbConsole.initialize( m_hWnd, IDC_CONSOLE, appState );
implPopulateCombobox( cbModelType, appState.source.impl );
modelPath.SetWindowTextW( appState.source.path );
HRESULT hr = work.create( this );
if( FAILED( hr ) )
{
CString text = L"CreateThreadpoolWork failed\n";
text += formatErrorMessage( hr );
::MessageBox( m_hWnd, text, L"Unable to load the model", MB_OK | MB_ICONWARNING );
return TRUE;
}
editorsWindows.reserve( 6 );
editorsWindows = { modelPath, cbModelType, GetDlgItem( IDC_BROWSE ), GetDlgItem( IDC_MODEL_ADV ), GetDlgItem( IDOK ), GetDlgItem( IDCANCEL ) };
pendingWindows.reserve( 2 );
pendingWindows = { GetDlgItem( IDC_PENDING_TEXT ), progressBar };
progressBar.SetRange32( 0, progressMaxInteger );
progressBar.SetStep( 1 );
appState.setupIcon( this );
ATLVERIFY( CenterWindow() );
if( !appState.source.found || !appState.automaticallyLoadModel )
return 0;
// AppState.findModelSource() method has located model parameters in registry;
// Post a notification identical to the "OK" button click event.
PostMessage( WM_COMMAND, IDOK, (LPARAM)( GetDlgItem( IDOK ).m_hWnd ) );
return 0;
}
LRESULT LoadModelDlg::OnBrowse( UINT, INT, HWND, BOOL& bHandled )
{
bHandled = TRUE;
CString path;
modelPath.GetWindowText( path );
if( !getOpenFileName( m_hWnd, L"Select a GGML Model File", L"Binary files (*.bin)\0*.bin\0\0", path ) )
return 0;
modelPath.SetWindowText( path );
appState.source.path = path;
return 0;
}
LRESULT LoadModelDlg::validationError( LPCTSTR message )
{
reportError( m_hWnd, message, L"Unable to load the model" );
return 0;
}
LRESULT LoadModelDlg::validationError( LPCTSTR message, HRESULT hr )
{
reportError( m_hWnd, message, L"Unable to load the model", hr );
return 0;
}
void LoadModelDlg::setPending( bool nowPending )
{
const BOOL enable = nowPending ? FALSE : TRUE;
for( HWND w : editorsWindows )
::EnableWindow( w, enable );
const int show = nowPending ? SW_NORMAL : SW_HIDE;
for( HWND w : pendingWindows )
::ShowWindow( w, show );
if( nowPending )
progressBar.SetMarquee( TRUE, 0 );
else
progressBar.SetMarquee( FALSE, 0 );
}
LRESULT LoadModelDlg::OnOk( UINT, INT, HWND, BOOL& bHandled )
{
modelPath.GetWindowText( path );
if( path.GetLength() <= 0 )
return validationError( L"Please select a model GGML file" );
{
CAtlFile file;
HRESULT hr = file.Create( path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING );
if( FAILED( hr ) )
return validationError( L"Unable to open the model file", hr );
ULONGLONG cb = 0;
file.GetSize( cb );
appState.source.sizeInBytes = cb;
}
impl = implGetValue( cbModelType );
if( impl == (Whisper::eModelImplementation)0 )
return validationError( L"Please select a model type" );
setPending( true );
work.post();
return 0;
}
void __stdcall LoadModelDlg::poolCallback() noexcept
{
CComPtr<Whisper::iModel> model;
clearLastError();
loadError = L"";
Whisper::sLoadModelCallbacks lmcb;
lmcb.cancel = nullptr;
lmcb.progress = &LoadModelDlg::progressCallback;
lmcb.pv = this;
const uint32_t flags = appState.gpuFlagsLoad();
HRESULT hr = Whisper::loadModel( path, impl, flags, &lmcb, &model );
if( SUCCEEDED( hr ) )
appState.model = model;
else
getLastError( loadError );
this->PostMessage( WM_CALLBACK_STATUS, (WPARAM)hr );
}
HRESULT __stdcall LoadModelDlg::progressCallback( double val, void* pv ) noexcept
{
LoadModelDlg& dialog = *(LoadModelDlg*)pv;
constexpr double mul = progressMaxInteger;
int pos = lround( mul * val );
dialog.progressBar.PostMessage( PBM_SETPOS, pos, 0 );
return S_OK;
}
LRESULT LoadModelDlg::OnCallbackStatus( UINT, WPARAM wParam, LPARAM, BOOL& bHandled )
{
setPending( false );
bHandled = TRUE;
const HRESULT hr = (HRESULT)wParam;
if( FAILED( hr ) )
{
LPCTSTR failMessage = L"Error loading the model";
if( loadError.GetLength() > 0 )
{
CString tmp = failMessage;
tmp += L"\n";
tmp += loadError;
return validationError( tmp, hr );
}
else
return validationError( failMessage, hr );
}
appState.source.path = path;
appState.source.impl = impl;
appState.saveModelSource();
EndDialog( IDOK );
return 0;
}
LRESULT LoadModelDlg::OnHyperlink( int idCtrl, LPNMHDR pnmh, BOOL& bHandled )
{
const UINT code = pnmh->code;
switch( code )
{
case NM_CLICK:
case NM_RETURN:
break;
default:
return 0;
}
PNMLINK pNMLink = (PNMLINK)pnmh;
LPCTSTR url = pNMLink->item.szUrl;
ShellExecute( NULL, L"open", url, NULL, NULL, SW_SHOW );
bHandled = TRUE;
return 0;
}
void LoadModelDlg::onModelAdvanced()
{
ModelAdvancedDlg dlg{ appState };
dlg.show( m_hWnd );
}
|