• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier_c_api.h"
17 
18 #include <memory>
19 
20 #include "absl/strings/string_view.h"
21 #include "tensorflow_lite_support/cc/task/core/category.h"
22 #include "tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier.h"
23 
24 using CategoryCPP = ::tflite::task::core::Category;
25 using NLClassifierCPP = ::tflite::task::text::nlclassifier::NLClassifier;
26 using NLClassifierOptionsCPP =
27     ::tflite::task::text::nlclassifier::NLClassifierOptions;
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif  // __cplusplus
32 
33 struct NLClassifier {
34   std::unique_ptr<NLClassifierCPP> impl;
35 };
36 
NLClassifierFromFileAndOptions(const char * model_path,const NLClassifierOptions * options)37 NLClassifier* NLClassifierFromFileAndOptions(
38     const char* model_path, const NLClassifierOptions* options) {
39   auto classifier_status = NLClassifierCPP::CreateFromFileAndOptions(
40       std::string(model_path),
41       {
42           .input_tensor_index = options->input_tensor_index,
43           .output_score_tensor_index = options->output_score_tensor_index,
44           .output_label_tensor_index = options->output_label_tensor_index,
45           .input_tensor_name = !options->input_tensor_name
46                                    ? ""
47                                    : std::string(options->input_tensor_name),
48           .output_score_tensor_name =
49               !options->output_score_tensor_name
50                   ? ""
51                   : std::string(options->output_score_tensor_name),
52           .output_label_tensor_name =
53               !options->output_label_tensor_name
54                   ? ""
55                   : std::string(options->output_label_tensor_name),
56       });
57 
58   if (classifier_status.ok()) {
59     return new NLClassifier{
60         .impl = std::unique_ptr<NLClassifierCPP>(dynamic_cast<NLClassifierCPP*>(
61             classifier_status.value().release()))};
62   } else {
63     return nullptr;
64   }
65 }
66 
NLClassifierClassify(const NLClassifier * classifier,const char * text)67 Categories* NLClassifierClassify(const NLClassifier* classifier,
68                                  const char* text) {
69   std::vector<CategoryCPP> results =
70       classifier->impl->Classify(absl::string_view(text).data());
71   size_t size = results.size();
72   auto* categories = new Category[size];
73 
74   for (size_t i = 0; i < size; ++i) {
75     categories[i].text = strdup(results[i].class_name.c_str());
76     categories[i].score = results[i].score;
77   }
78 
79   auto* c_categories = new Categories;
80   c_categories->size = size;
81   c_categories->categories = categories;
82   return c_categories;
83 }
84 
NLClassifierDelete(NLClassifier * classifier)85 void NLClassifierDelete(NLClassifier* classifier) { delete classifier; }
86 
87 #ifdef __cplusplus
88 }  // extern "C"
89 #endif  // __cplusplus
90