summaryrefslogtreecommitdiffstats
path: root/Whisper/WhisperCLI/WhisperCLI.cpp
blob: af0a59fbfbdcb83ccbe29ab6fc0802d49d9aa399 (plain)
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
#define WIN32_LEAN_AND_MEAN

#include <Unknwn.h>
#include <windows.h>

#include "Whisper/API/whisperWindows.h"

#include <iostream>
#include <locale>
#include <set>
#include <string>
#include <string_view>

using std::cout;
using std::cerr;
using std::endl;
using namespace Whisper;

struct Config {
	std::wstring audio_path = L"input.wav";
	std::wstring model_path = L"model.bin";
	eSamplingStrategy decode_method = eSamplingStrategy::BeamSearch;
};

bool hasArg(int argc, int shift, char* arg) {
	if (shift + 1 >= argc) {
		cerr << "Error: " << arg << " is missing argument" << endl;
		return false;
	}
	return true;
}

std::wstring cstrToWstr(char* c_str) {
	int length = MultiByteToWideChar(CP_UTF8, 0, c_str, -1, NULL, 0);
	std::wstring result(length, 0);
	MultiByteToWideChar(CP_UTF8, 0, c_str, -1, result.data(), result.size());
	return result;
}


bool parseArgs(int argc, char* argv[], Config& c) {
	int shift = 1;
	while (shift < argc) {
		if (std::string_view(argv[shift]) == "--audio_path") {
			if (!hasArg(argc, shift, argv[shift])) {
				return false;
			}
			c.audio_path = cstrToWstr(argv[shift + 1]);
			shift += 2;
			continue;
		}
		if (std::string_view(argv[shift]) == "--model_path") {
			if (!hasArg(argc, shift, argv[shift])) {
				return false;
			}
			c.model_path = cstrToWstr(argv[shift + 1]);
			shift += 2;
			continue;
		}
		if (std::string_view(argv[shift]) == "--decode_method") {
			if (!hasArg(argc, shift, argv[shift])) {
				return false;
			}
			std::string_view decode_method(argv[shift + 1]);
			if (decode_method == "greedy") {
				cerr << "Using greedy decode " << endl;
				c.decode_method = eSamplingStrategy::Greedy;
			}
			else if (decode_method == "beam") {
				cerr << "Using beam decode " << endl;
				c.decode_method = eSamplingStrategy::BeamSearch;
			}
			else {
				cerr << "Unsupported decode method: " << decode_method << endl;
				return false;
			}
			shift += 2;
			continue;
		}
		cerr << "Unrecognized argument: \"" << argv[shift] << '"' << endl;
		return false;
	}
	return true;
}

int main(int argc, char* argv[])
{
	Config c;
	if (!parseArgs(argc, argv, c)) {
		cerr << "Failed to parse args";
		return 1;
	}

	iMediaFoundation* f = nullptr;
	HRESULT err = initMediaFoundation(&f);
	if (FAILED(err)) {
		cerr << "Failed to init media foundation: " << err << endl;
		return 1;
	}

	Whisper::iAudioBuffer* buffer = nullptr;
	err = f->loadAudioFile(c.audio_path.c_str(), /*stereo=*/false, &buffer);
	if (FAILED(err)) {
		cerr << "Failed to load audio file 'input.wav': " << err << endl;
		return 1;
	}

	Whisper::iModel* model = nullptr;
	err = Whisper::loadModel(c.model_path.c_str(), eModelImplementation::GPU, /*flags=*/0, /*callbacks=*/nullptr, &model);
	if (FAILED(err)) {
		cerr << "Failed to open model 'model.bin': " << err << endl;
		return 1;
	}

	Whisper::iContext* context = nullptr;
	err = model->createContext(&context);
	if (FAILED(err)) {
		cerr << "Failed to create context: " << err << endl;
		return 1;
	}

	Whisper::sFullParams wparams{};
	context->fullDefaultParams(c.decode_method, &wparams);
	if (c.decode_method == eSamplingStrategy::BeamSearch) {
		wparams.beam_search.beam_width = 5;
		wparams.beam_search.n_best = 5;
	}
	wparams.language = Whisper::makeLanguageKey("en");
	wparams.n_max_text_ctx = 100;

	err = context->runFull(wparams, buffer);
	if (FAILED(err)) {
		cerr << "Failed to transcribe: " << err << endl;
		return 1;
	}

	Whisper::iTranscribeResult* result = nullptr;
	err = context->getResults(eResultFlags::Tokens, &result);
	if (FAILED(err)) {
		cerr << "Failed to get transcription results: " << err << endl;
		return 1;
	}

	std::set<int> special_tokens;
	{
		Whisper::SpecialTokens st;
		err = model->getSpecialTokens(st);
		if (FAILED(err)) {
			cerr << "Failed to get special tokens: " << err << endl;
		}
		special_tokens.insert(st.Not);
		special_tokens.insert(st.PreviousWord);
		special_tokens.insert(st.SentenceStart);
		special_tokens.insert(st.TaskTranscribe);
		special_tokens.insert(st.TaskTranslate);
		special_tokens.insert(st.TranscriptionBegin);
		special_tokens.insert(st.TranscriptionEnd);
		special_tokens.insert(st.TranscriptionStart);
	}

	sTranscribeLength length;
	err = result->getSize(length);
	if (FAILED(err)) {
		cerr << "Failed to get transcription length: " << err << endl;
	}
	auto* segments = result->getSegments();
	auto* tokens = result->getTokens();
	bool is_metadata = false;
	for (int i = 0; i < length.countSegments; i++) {
		auto& segment = segments[i];
		for (int j = 0; j < segment.countTokens; j++) {
			const sToken& tok = tokens[segment.firstToken + j];
			if (special_tokens.contains(tok.id)) {
				continue;
			}
			std::string_view tok_str(tok.text);
			if (tok_str.starts_with("[") ||
				tok_str.starts_with(" [")) {
				if (tok_str.ends_with("]")) {
					continue;
				}
				is_metadata = true;
				continue;
			}
			if (is_metadata &&
				tok_str.ends_with("]")) {
				is_metadata = false;
				continue;
			}
			cout << tok.text;
		}
	}
	cout << endl;

	return 0;
}