Skip to content

Commit

Permalink
Added support for --compiled-grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
anikocharyan committed May 27, 2024
1 parent 30b6e96 commit 01d97c5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.14)
project(cli_client VERSION 0.0.1)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)

set(CMAKE_CXX_STANDARD 20)

option(USE_CXX11_ABI_0 "" ON)
if (USE_CXX11_ABI_0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cli_client
```
command like:
```shell
cli_client -a audiofile.wav -T generic -t my.token -l en-US --asr-version V1 -H us.speechcenter.verbio.com -s 16000 --client-id my-client-id --client-secret my-client-secret
cli_client -a audiofile.wav -T GENERIC -t my.token -l en-US --asr-version V1 -H us.speechcenter.verbio.com -s 16000 --client-id my-client-id --client-secret my-client-secret
```

Which will give an output along these lines:
Expand Down Expand Up @@ -269,9 +269,9 @@ There are three options available to provide a grammar:

- The inline grammar option expects a grammar passed inline as a string.
- The grammar URI option expects a URI, either pointing to a built-in grammar or to a grammar that is being hosted externally.
- The compiled grammar expects a filename of the compiled grammar binary.
- The compiled grammar expects a filename (a .tar.xz file) of the previously compiled grammar.

> **THIS FEATURE IS STILL IN DEVELOPMENT, PLEASE ONLY USE THE GRAMMAR URI OPTION WITH BUILTIN GRAMMARS, OR AN ERROR WILL BE GIVEN.**
> **THE INLINE GRAMMAR OPTION IS NOT IMPLEMENTED YET.**
#### Language

Expand Down
4 changes: 3 additions & 1 deletion headers/Grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class Grammar {
std::vector<char> getCompiledBytes() const;

private:
void readCompiledGrammar();

GrammarType type;
std::string content;
std::vector<char> compiledBytes;
};


#endif//SPEECHCENTER_GRAMMAR_H
#endif //SPEECHCENTER_GRAMMAR_H
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ target_link_libraries(speech-center-client PUBLIC
SndFile::sndfile
cpr::cpr
jwt-cpp::jwt-cpp
nlohmann_json::nlohmann_json)
nlohmann_json::nlohmann_json
stdc++fs
)

add_executable(cli_client
main.cpp)
Expand Down
8 changes: 4 additions & 4 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ void Configuration::parse(int argc, char **argv) {
cxxopts::Options options(argv[0], "Verbio Technlogies S.L. - Speech Center client example");
options.set_width(180).allow_unrecognised_options().add_options()
("a,audio",
"Path to a .wav audio in 8kHz or 16kHz sampling rate and PCM16 encoding to use for the recognition",
"Path to a .wav audio in 8kHz or 16kHz sampling rate and PCM16 encoding to use for the recognition.",
cxxopts::value(audioPath), "file")
("I,inline-grammar", "ABNF Grammar to use for the recognition passed as a string", cxxopts::value(grammarInline), "string")
("G,grammar-uri", "Grammar URI to use for the recognition (builtin or externally served)", cxxopts::value(grammarUri), "uri")
("C,compiled-grammar", "Path to the compiled grammar file to use for the recognition", cxxopts::value(grammarCompiled), "file")
("I,inline-grammar", "ABNF Grammar to use for the recognition passed as a string.", cxxopts::value(grammarInline), "string")
("G,grammar-uri", "Grammar URI to use for the recognition (builtin or externally served).", cxxopts::value(grammarUri), "uri")
("C,compiled-grammar", "Path to the compiled grammar file (a .tar.xz file) to use for the recognition.", cxxopts::value(grammarCompiled), "file")
("T,topic",
"Topic to use for the recognition when a grammar is not provided. Must be GENERIC | BANKING | TELCO | INSURANCE",
cxxopts::value(topic))
Expand Down
25 changes: 20 additions & 5 deletions src/Grammar.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "Grammar.h"

#include <fstream>
#include <filesystem>

Grammar::Grammar() : type{NONE}, content{} {}

Grammar::Grammar(const GrammarType type, const std::string content) : type(type), content(content) {
if (type == COMPILED) {
std::ifstream input(content, std::ios::binary);
compiledBytes = std::vector<char> (
(std::istreambuf_iterator<char>(input)),
(std::istreambuf_iterator<char>()));
input.close();
readCompiledGrammar();
}
}

Expand All @@ -25,4 +23,21 @@ std::string Grammar::getContent() const {

std::vector<char> Grammar::getCompiledBytes() const {
return compiledBytes;
}

void Grammar::readCompiledGrammar() {
if(!std::filesystem::exists(content)) {
throw std::invalid_argument("Compiled grammar file '" + content + "' does not exist.");
}
const auto kXzExtension = std::filesystem::path(content).extension();
const auto kFileNameWithoutXzExtension = std::filesystem::path(content).stem();
const auto kTarExtension = std::filesystem::path(kFileNameWithoutXzExtension).extension();
if(kXzExtension != ".xz" || kTarExtension != ".tar") {
throw std::invalid_argument("Compiled grammar file '" + content + "' extension is not .tar.xz.");
}
std::ifstream input(content, std::ios::binary);
compiledBytes = std::vector<char> (
(std::istreambuf_iterator<char>(input)),
(std::istreambuf_iterator<char>()));
input.close();
}
21 changes: 18 additions & 3 deletions src/RecognitionClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ using namespace speechcenter::recognizer::v1;
typedef RecognitionStreamingRequest Request;
typedef RecognitionStreamingResponse Response;

namespace {

std::string buildLogString(Request request) {
if (request.config().has_resource() &&
request.config().resource().has_grammar() &&
request.config().resource().grammar().has_compiled_grammar()) {
GrammarResource* resource = request.mutable_config()->mutable_resource()->mutable_grammar();
std::string str = "Compiled Grammar";
std::vector<char> bytes(str.begin(), str.end());
resource->set_compiled_grammar(bytes.data(), bytes.size());
}
return request.DebugString();
}

}

void RecognitionClient::write(
std::shared_ptr<grpc::ClientReaderWriter<Request,
Response>>
stream) {

INFO("Writing to stream...");
Request recognitionConfig = buildRecognitionConfig();
INFO("Sending config: \n{} ", recognitionConfig.DebugString());
INFO("Sending config: \n{} ", buildLogString(recognitionConfig));
bool streamFail = !stream->Write(recognitionConfig);
if (streamFail) {
auto status = stream->Finish();
Expand Down Expand Up @@ -308,5 +324,4 @@ RecognitionConfig_AsrVersion RecognitionClient::buildAsrVersion() {
}

return topicIter->second;
}

}

0 comments on commit 01d97c5

Please sign in to comment.