Add error handling to C API
PiperOrigin-RevId: 570094642
This commit is contained in:
parent
c7402efe5e
commit
a00759007d
|
@ -30,6 +30,7 @@ cc_library(
|
||||||
"//mediapipe/tasks/c/core:base_options_converter",
|
"//mediapipe/tasks/c/core:base_options_converter",
|
||||||
"//mediapipe/tasks/cc/text/text_classifier",
|
"//mediapipe/tasks/cc/text/text_classifier",
|
||||||
"@com_google_absl//absl/log:absl_log",
|
"@com_google_absl//absl/log:absl_log",
|
||||||
|
"@com_google_absl//absl/status",
|
||||||
],
|
],
|
||||||
alwayslink = 1,
|
alwayslink = 1,
|
||||||
)
|
)
|
||||||
|
@ -78,7 +79,6 @@ cc_test(
|
||||||
"//mediapipe/framework/deps:file_path",
|
"//mediapipe/framework/deps:file_path",
|
||||||
"//mediapipe/framework/port:gtest",
|
"//mediapipe/framework/port:gtest",
|
||||||
"//mediapipe/tasks/c/components/containers:category",
|
"//mediapipe/tasks/c/components/containers:category",
|
||||||
"//mediapipe/tasks/cc/components/containers:category",
|
|
||||||
"@com_google_absl//absl/flags:flag",
|
"@com_google_absl//absl/flags:flag",
|
||||||
"@com_google_absl//absl/strings",
|
"@com_google_absl//absl/strings",
|
||||||
"@com_google_googletest//:gtest_main",
|
"@com_google_googletest//:gtest_main",
|
||||||
|
|
|
@ -19,6 +19,7 @@ limitations under the License.
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "absl/log/absl_log.h"
|
#include "absl/log/absl_log.h"
|
||||||
|
#include "absl/status/status.h"
|
||||||
#include "mediapipe/tasks/c/components/containers/classification_result_converter.h"
|
#include "mediapipe/tasks/c/components/containers/classification_result_converter.h"
|
||||||
#include "mediapipe/tasks/c/components/processors/classifier_options.h"
|
#include "mediapipe/tasks/c/components/processors/classifier_options.h"
|
||||||
#include "mediapipe/tasks/c/components/processors/classifier_options_converter.h"
|
#include "mediapipe/tasks/c/components/processors/classifier_options_converter.h"
|
||||||
|
@ -38,9 +39,18 @@ using ::mediapipe::tasks::c::components::processors::
|
||||||
CppConvertToClassifierOptions;
|
CppConvertToClassifierOptions;
|
||||||
using ::mediapipe::tasks::c::core::CppConvertToBaseOptions;
|
using ::mediapipe::tasks::c::core::CppConvertToBaseOptions;
|
||||||
using ::mediapipe::tasks::text::text_classifier::TextClassifier;
|
using ::mediapipe::tasks::text::text_classifier::TextClassifier;
|
||||||
|
|
||||||
|
int CppProcessError(absl::Status status, char** error_msg) {
|
||||||
|
if (error_msg) {
|
||||||
|
*error_msg = strdup(status.ToString().c_str());
|
||||||
|
}
|
||||||
|
return status.raw_code();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TextClassifier* CppTextClassifierCreate(const TextClassifierOptions& options) {
|
TextClassifier* CppTextClassifierCreate(const TextClassifierOptions& options,
|
||||||
|
char** error_msg) {
|
||||||
auto cpp_options = std::make_unique<
|
auto cpp_options = std::make_unique<
|
||||||
::mediapipe::tasks::text::text_classifier::TextClassifierOptions>();
|
::mediapipe::tasks::text::text_classifier::TextClassifierOptions>();
|
||||||
|
|
||||||
|
@ -52,49 +62,53 @@ TextClassifier* CppTextClassifierCreate(const TextClassifierOptions& options) {
|
||||||
if (!classifier.ok()) {
|
if (!classifier.ok()) {
|
||||||
ABSL_LOG(ERROR) << "Failed to create TextClassifier: "
|
ABSL_LOG(ERROR) << "Failed to create TextClassifier: "
|
||||||
<< classifier.status();
|
<< classifier.status();
|
||||||
|
CppProcessError(classifier.status(), error_msg);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return classifier->release();
|
return classifier->release();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CppTextClassifierClassify(void* classifier, const char* utf8_str,
|
int CppTextClassifierClassify(void* classifier, const char* utf8_str,
|
||||||
TextClassifierResult* result) {
|
TextClassifierResult* result, char** error_msg) {
|
||||||
auto cpp_classifier = static_cast<TextClassifier*>(classifier);
|
auto cpp_classifier = static_cast<TextClassifier*>(classifier);
|
||||||
auto cpp_result = cpp_classifier->Classify(utf8_str);
|
auto cpp_result = cpp_classifier->Classify(utf8_str);
|
||||||
if (!cpp_result.ok()) {
|
if (!cpp_result.ok()) {
|
||||||
ABSL_LOG(ERROR) << "Classification failed: " << cpp_result.status();
|
ABSL_LOG(ERROR) << "Classification failed: " << cpp_result.status();
|
||||||
return false;
|
return CppProcessError(cpp_result.status(), error_msg);
|
||||||
}
|
}
|
||||||
CppConvertToClassificationResult(*cpp_result, result);
|
CppConvertToClassificationResult(*cpp_result, result);
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppTextClassifierCloseResult(TextClassifierResult* result) {
|
void CppTextClassifierCloseResult(TextClassifierResult* result) {
|
||||||
CppCloseClassificationResult(result);
|
CppCloseClassificationResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppTextClassifierClose(void* classifier) {
|
int CppTextClassifierClose(void* classifier, char** error_msg) {
|
||||||
auto cpp_classifier = static_cast<TextClassifier*>(classifier);
|
auto cpp_classifier = static_cast<TextClassifier*>(classifier);
|
||||||
auto result = cpp_classifier->Close();
|
auto result = cpp_classifier->Close();
|
||||||
if (!result.ok()) {
|
if (!result.ok()) {
|
||||||
ABSL_LOG(ERROR) << "Failed to close TextClassifier: " << result;
|
ABSL_LOG(ERROR) << "Failed to close TextClassifier: " << result;
|
||||||
|
return CppProcessError(result, error_msg);
|
||||||
}
|
}
|
||||||
delete cpp_classifier;
|
delete cpp_classifier;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mediapipe::tasks::c::text::text_classifier
|
} // namespace mediapipe::tasks::c::text::text_classifier
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
void* text_classifier_create(struct TextClassifierOptions* options) {
|
void* text_classifier_create(struct TextClassifierOptions* options,
|
||||||
|
char** error_msg) {
|
||||||
return mediapipe::tasks::c::text::text_classifier::CppTextClassifierCreate(
|
return mediapipe::tasks::c::text::text_classifier::CppTextClassifierCreate(
|
||||||
*options);
|
*options, error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
int text_classifier_classify(void* classifier, const char* utf8_str,
|
int text_classifier_classify(void* classifier, const char* utf8_str,
|
||||||
TextClassifierResult* result) {
|
TextClassifierResult* result, char** error_msg) {
|
||||||
return mediapipe::tasks::c::text::text_classifier::CppTextClassifierClassify(
|
return mediapipe::tasks::c::text::text_classifier::CppTextClassifierClassify(
|
||||||
classifier, utf8_str, result);
|
classifier, utf8_str, result, error_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void text_classifier_close_result(TextClassifierResult* result) {
|
void text_classifier_close_result(TextClassifierResult* result) {
|
||||||
|
@ -102,9 +116,9 @@ void text_classifier_close_result(TextClassifierResult* result) {
|
||||||
result);
|
result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void text_classifier_close(void* classifier) {
|
int text_classifier_close(void* classifier, char** error_ms) {
|
||||||
mediapipe::tasks::c::text::text_classifier::CppTextClassifierClose(
|
return mediapipe::tasks::c::text::text_classifier::CppTextClassifierClose(
|
||||||
classifier);
|
classifier, error_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|
|
@ -42,18 +42,31 @@ struct TextClassifierOptions {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Creates a TextClassifier from the provided `options`.
|
// Creates a TextClassifier from the provided `options`.
|
||||||
MP_EXPORT void* text_classifier_create(struct TextClassifierOptions* options);
|
// Returns a pointer to the text classifier on success.
|
||||||
|
// If an error occurs, returns `nullptr` and sets the error parameter to an
|
||||||
|
// an error message (if `error_msg` is not nullptr). You must free the memory
|
||||||
|
// allocated for the error message.
|
||||||
|
MP_EXPORT void* text_classifier_create(struct TextClassifierOptions* options,
|
||||||
|
char** error_msg = nullptr);
|
||||||
|
|
||||||
// Performs classification on the input `text`.
|
// Performs classification on the input `text`. Returns `0` on success.
|
||||||
|
// If an error occurs, returns an error code and sets the error parameter to an
|
||||||
|
// an error message (if `error_msg` is not nullptr). You must free the memory
|
||||||
|
// allocated for the error message.
|
||||||
MP_EXPORT int text_classifier_classify(void* classifier, const char* utf8_str,
|
MP_EXPORT int text_classifier_classify(void* classifier, const char* utf8_str,
|
||||||
TextClassifierResult* result);
|
TextClassifierResult* result,
|
||||||
|
char** error_msg = nullptr);
|
||||||
|
|
||||||
// Frees the memory allocated inside a TextClassifierResult result. Does not
|
// Frees the memory allocated inside a TextClassifierResult result. Does not
|
||||||
// free the result pointer itself.
|
// free the result pointer itself.
|
||||||
MP_EXPORT void text_classifier_close_result(TextClassifierResult* result);
|
MP_EXPORT void text_classifier_close_result(TextClassifierResult* result);
|
||||||
|
|
||||||
// Shuts down the TextClassifier when all the work is done. Frees all memory.
|
// Shuts down the TextClassifier when all the work is done. Frees all memory.
|
||||||
MP_EXPORT void text_classifier_close(void* classifier);
|
// If an error occurs, returns an error code and sets the error parameter to an
|
||||||
|
// an error message (if `error_msg` is not nullptr). You must free the memory
|
||||||
|
// allocated for the error message.
|
||||||
|
MP_EXPORT int text_classifier_close(void* classifier,
|
||||||
|
char** error_msg = nullptr);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern C
|
} // extern C
|
||||||
|
|
|
@ -15,17 +15,20 @@ limitations under the License.
|
||||||
|
|
||||||
#include "mediapipe/tasks/c/text/text_classifier/text_classifier.h"
|
#include "mediapipe/tasks/c/text/text_classifier/text_classifier.h"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "absl/flags/flag.h"
|
#include "absl/flags/flag.h"
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
#include "mediapipe/framework/deps/file_path.h"
|
#include "mediapipe/framework/deps/file_path.h"
|
||||||
|
#include "mediapipe/framework/port/gmock.h"
|
||||||
#include "mediapipe/framework/port/gtest.h"
|
#include "mediapipe/framework/port/gtest.h"
|
||||||
#include "mediapipe/tasks/c/components/containers/category.h"
|
#include "mediapipe/tasks/c/components/containers/category.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using ::mediapipe::file::JoinPath;
|
using ::mediapipe::file::JoinPath;
|
||||||
|
using testing::HasSubstr;
|
||||||
|
|
||||||
constexpr char kTestDataDirectory[] = "/mediapipe/tasks/testdata/text/";
|
constexpr char kTestDataDirectory[] = "/mediapipe/tasks/testdata/text/";
|
||||||
constexpr char kTestBertModelPath[] = "bert_text_classifier.tflite";
|
constexpr char kTestBertModelPath[] = "bert_text_classifier.tflite";
|
||||||
|
@ -67,4 +70,21 @@ TEST(TextClassifierTest, SmokeTest) {
|
||||||
text_classifier_close(classifier);
|
text_classifier_close(classifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(TextClassifierTest, ErrorHandling) {
|
||||||
|
// It is an error to set neither the asset buffer nor the path.
|
||||||
|
TextClassifierOptions options = {
|
||||||
|
/* base_options= */ {/* model_asset_buffer= */ nullptr,
|
||||||
|
/* model_asset_path= */ nullptr},
|
||||||
|
/* classifier_options= */ {},
|
||||||
|
};
|
||||||
|
|
||||||
|
char* error_msg;
|
||||||
|
void* classifier = text_classifier_create(&options, &error_msg);
|
||||||
|
EXPECT_EQ(classifier, nullptr);
|
||||||
|
|
||||||
|
EXPECT_THAT(error_msg, HasSubstr("INVALID_ARGUMENT"));
|
||||||
|
|
||||||
|
free(error_msg);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
Loading…
Reference in New Issue
Block a user