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 #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_TEXT_QA_BERT_QA_C_API_H_ 16 #define TENSORFLOW_LITE_SUPPORT_CC_TASK_TEXT_QA_BERT_QA_C_API_H_ 17 18 // -------------------------------------------------------------------------- 19 /// C API for BertQuestionAnswerer. 20 /// 21 /// The API leans towards simplicity and uniformity instead of convenience, as 22 /// most usage will be by language-specific wrappers. It provides largely the 23 /// same set of functionality as that of the C++ TensorFlow Lite 24 /// `BertQuestionAnswerer` API, but is useful for shared libraries where having 25 /// a stable ABI boundary is important. 26 /// 27 /// Usage: 28 /// <pre><code> 29 /// // Create the model and interpreter options. 30 /// BertQuestionAnswerer* qa_answerer = 31 /// BertQuestionAnswererFromFile("/path/to/model.tflite"); 32 /// 33 /// // answer a question. 34 /// QaAnswers* answers = Answer(qa_answerer, context, question); 35 /// 36 /// // Dispose of the API and QaAnswers objects. 37 /// BertQuestionAnswererDelete(qa_answerer); 38 /// BertQuestionAnswererQaAnswersDelete(answers); 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif // __cplusplus 43 44 typedef struct BertQuestionAnswerer BertQuestionAnswerer; 45 46 struct QaAnswer { 47 int start; 48 int end; 49 float logit; 50 char* text; 51 }; 52 53 struct QaAnswers { 54 int size; 55 struct QaAnswer* answers; 56 }; 57 58 // Creates BertQuestionAnswerer from model path, returns nullptr if the file 59 // doesn't exist or is not a well formatted TFLite model path. 60 extern BertQuestionAnswerer* BertQuestionAnswererFromFile( 61 const char* model_path); 62 63 // Invokes the encapsulated TFLite model and answers a question based on 64 // context. 65 extern struct QaAnswers* BertQuestionAnswererAnswer( 66 const BertQuestionAnswerer* question_answerer, const char* context, 67 const char* question); 68 69 extern void BertQuestionAnswererDelete( 70 BertQuestionAnswerer* bert_question_answerer); 71 72 extern void BertQuestionAnswererQaAnswersDelete(struct QaAnswers* qa_answers); 73 74 #ifdef __cplusplus 75 } // extern "C" 76 #endif // __cplusplus 77 78 #endif // TENSORFLOW_LITE_SUPPORT_CC_TASK_TEXT_QA_BERT_QA_C_API_H_ 79