Skip to content

Commit

Permalink
update quickstart to v2 using both global and regional connections
Browse files Browse the repository at this point in the history
  • Loading branch information
scotthart committed Mar 8, 2024
1 parent 51375d3 commit c744329
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
2 changes: 2 additions & 0 deletions ci/etc/integration-tests-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export GOOGLE_CLOUD_CPP_USER_PROJECT="${GOOGLE_CLOUD_PROJECT}"
export GOOGLE_CLOUD_CPP_TEST_REGION="us-central1"
# Some quickstart programs require a zone.
export GOOGLE_CLOUD_CPP_TEST_ZONE="us-central1-a"
# Some tests and quickstarts need to specify a location as "global".
export GOOGLE_CLOUD_CPP_TEST_GLOBAL="global"
# Some quickstart programs require an organization.
export GOOGLE_CLOUD_CPP_TEST_ORGANIZATION="1006341795026"

Expand Down
24 changes: 16 additions & 8 deletions google/cloud/speech/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
target_link_libraries(speech_quickstart PRIVATE google-cloud-cpp::speech)
google_cloud_cpp_add_common_options(speech_quickstart)
add_test(
NAME speech_quickstart
COMMAND cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake"
$<TARGET_FILE:speech_quickstart>)
set_tests_properties(
speech_quickstart
PROPERTIES
LABELS "integration-test;quickstart" ENVIRONMENT
GOOGLE_CLOUD_CPP_USER_PROJECT=$ENV{GOOGLE_CLOUD_CPP_USER_PROJECT})
NAME speech_quickstart_global
COMMAND
cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake"
$<TARGET_FILE:speech_quickstart> GOOGLE_CLOUD_PROJECT
GOOGLE_CLOUD_CPP_TEST_GLOBAL)
set_tests_properties(speech_quickstart_global
PROPERTIES LABELS "integration-test;quickstart")
add_test(
NAME speech_quickstart_regional
COMMAND
cmake -P "${PROJECT_SOURCE_DIR}/cmake/quickstart-runner.cmake"
$<TARGET_FILE:speech_quickstart> GOOGLE_CLOUD_PROJECT
GOOGLE_CLOUD_CPP_TEST_REGION)
set_tests_properties(speech_quickstart_regional
PROPERTIES LABELS "integration-test;quickstart")

endif ()
45 changes: 33 additions & 12 deletions google/cloud/speech/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,49 @@ top-level [README](/README.md#building-and-installing).
<!-- inject-quickstart-start -->

```cc
#include "google/cloud/speech/v1/speech_client.h"
#include "google/cloud/speech/v2/speech_client.h"
#include "google/cloud/project.h"
#include <iostream>

// Configure a simple recognizer for en-US.
void ConfigureRecognizer(google::cloud::speech::v2::RecognizeRequest& request) {
*request.mutable_config()->add_language_codes() = "en-US";
request.mutable_config()->set_model("short");
*request.mutable_config()->mutable_auto_decoding_config() = {};
}

int main(int argc, char* argv[]) try {
auto constexpr kDefaultUri = "gs://cloud-samples-data/speech/hello.wav";
if (argc > 2) {
std::cerr << "Usage: " << argv[0] << " [gcs-uri]\n"
if (argc > 4) {
std::cerr << "Usage: " << argv[0] << " project <region>|global [gcs-uri]\n"
<< " Specify the region desired or \"global\"\n"
<< " The gcs-uri must be in gs://... format. It defaults to "
<< kDefaultUri << "\n";
return 1;
}
auto uri = std::string{argc == 2 ? argv[1] : kDefaultUri};

namespace speech = ::google::cloud::speech_v1;
auto client = speech::SpeechClient(speech::MakeSpeechConnection());
std::string const project = argv[1];
std::string const location = argv[2];
std::cout << "project=" << project << "; location=" << location << "\n";
auto const uri = std::string{argc == 4 ? argv[3] : kDefaultUri};
namespace speech = ::google::cloud::speech_v2;

std::shared_ptr<speech::SpeechConnection> connection;
google::cloud::speech::v2::RecognizeRequest request;
ConfigureRecognizer(request);
request.set_uri(uri);

if (location == "global") {
connection = speech::MakeSpeechConnection();
request.set_recognizer("projects/" + project +
"/locations/global/recognizers/_");
} else {
connection = speech::MakeSpeechConnection(location);
request.set_recognizer("projects/" + project + "/locations/" + location +
"/recognizers/_");
}

google::cloud::speech::v1::RecognitionConfig config;
config.set_language_code("en-US");
google::cloud::speech::v1::RecognitionAudio audio;
audio.set_uri(uri);
auto response = client.Recognize(config, audio);
auto client = speech::SpeechClient(connection);
auto response = client.Recognize(request);
if (!response) throw std::move(response).status();
std::cout << response->DebugString() << "\n";

Expand Down
42 changes: 31 additions & 11 deletions google/cloud/speech/quickstart/quickstart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,48 @@
// limitations under the License.

//! [all]
#include "google/cloud/speech/v1/speech_client.h"
#include "google/cloud/speech/v2/speech_client.h"
#include "google/cloud/project.h"
#include <iostream>

// Configure a simple recognizer for en-US.
void ConfigureRecognizer(google::cloud::speech::v2::RecognizeRequest& request) {
*request.mutable_config()->add_language_codes() = "en-US";
request.mutable_config()->set_model("short");
*request.mutable_config()->mutable_auto_decoding_config() = {};
}

int main(int argc, char* argv[]) try {
auto constexpr kDefaultUri = "gs://cloud-samples-data/speech/hello.wav";
if (argc > 2) {
std::cerr << "Usage: " << argv[0] << " [gcs-uri]\n"
if (argc > 4) {
std::cerr << "Usage: " << argv[0] << " project <region>|global [gcs-uri]\n"
<< " Specify the region desired or \"global\"\n"
<< " The gcs-uri must be in gs://... format. It defaults to "
<< kDefaultUri << "\n";
return 1;
}
auto uri = std::string{argc == 2 ? argv[1] : kDefaultUri};
std::string const project = argv[1];
std::string const location = argv[2];
auto const uri = std::string{argc == 4 ? argv[3] : kDefaultUri};
namespace speech = ::google::cloud::speech_v2;

namespace speech = ::google::cloud::speech_v1;
auto client = speech::SpeechClient(speech::MakeSpeechConnection());
std::shared_ptr<speech::SpeechConnection> connection;
google::cloud::speech::v2::RecognizeRequest request;
ConfigureRecognizer(request);
request.set_uri(uri);

if (location == "global") {
connection = speech::MakeSpeechConnection();
request.set_recognizer("projects/" + project +
"/locations/global/recognizers/_");
} else {
connection = speech::MakeSpeechConnection(location);
request.set_recognizer("projects/" + project + "/locations/" + location +
"/recognizers/_");
}

google::cloud::speech::v1::RecognitionConfig config;
config.set_language_code("en-US");
google::cloud::speech::v1::RecognitionAudio audio;
audio.set_uri(uri);
auto response = client.Recognize(config, audio);
auto client = speech::SpeechClient(connection);
auto response = client.Recognize(request);
if (!response) throw std::move(response).status();
std::cout << response->DebugString() << "\n";

Expand Down

0 comments on commit c744329

Please sign in to comment.