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/qa/bert_qa_c_api.h" 17 18 #include <memory> 19 20 #include "tensorflow_lite_support/cc/task/text/qa/bert_question_answerer.h" 21 #include "tensorflow_lite_support/cc/task/text/qa/question_answerer.h" 22 23 using BertQuestionAnswererCPP = ::tflite::task::text::qa::BertQuestionAnswerer; 24 using QaAnswerCPP = ::tflite::task::text::qa::QaAnswer; 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif // __cplusplus 29 30 struct BertQuestionAnswerer { 31 std::unique_ptr<BertQuestionAnswererCPP> impl; 32 }; 33 BertQuestionAnswererFromFile(const char * model_path)34BertQuestionAnswerer* BertQuestionAnswererFromFile(const char* model_path) { 35 auto bert_qa_status = 36 BertQuestionAnswererCPP::CreateFromFile(std::string(model_path)); 37 if (bert_qa_status.ok()) { 38 return new BertQuestionAnswerer{ 39 .impl = std::unique_ptr<BertQuestionAnswererCPP>( 40 dynamic_cast<BertQuestionAnswererCPP*>( 41 bert_qa_status.value().release()))}; 42 } else { 43 return nullptr; 44 } 45 } 46 BertQuestionAnswererAnswer(const BertQuestionAnswerer * question_answerer,const char * context,const char * question)47QaAnswers* BertQuestionAnswererAnswer( 48 const BertQuestionAnswerer* question_answerer, const char* context, 49 const char* question) { 50 std::vector<QaAnswerCPP> answers = question_answerer->impl->Answer( 51 absl::string_view(context).data(), absl::string_view(question).data()); 52 size_t size = answers.size(); 53 auto* qa_answers = new QaAnswer[size]; 54 55 for (size_t i = 0; i < size; ++i) { 56 qa_answers[i].start = answers[i].pos.start; 57 qa_answers[i].end = answers[i].pos.end; 58 qa_answers[i].logit = answers[i].pos.logit; 59 qa_answers[i].text = strdup(answers[i].text.c_str()); 60 } 61 62 auto* c_answers = new QaAnswers; 63 c_answers->size = size; 64 c_answers->answers = qa_answers; 65 return c_answers; 66 } 67 BertQuestionAnswererDelete(BertQuestionAnswerer * bert_question_answerer)68void BertQuestionAnswererDelete(BertQuestionAnswerer* bert_question_answerer) { 69 delete bert_question_answerer; 70 } 71 BertQuestionAnswererQaAnswersDelete(QaAnswers * qa_answers)72void BertQuestionAnswererQaAnswersDelete(QaAnswers* qa_answers) { 73 delete[] qa_answers->answers; 74 delete qa_answers; 75 } 76 77 #ifdef __cplusplus 78 } // extern "C" 79 #endif // __cplusplus 80