1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBTEXTCLASSIFIER_ACTIONS_LUA_ACTIONS_H_ 18 #define LIBTEXTCLASSIFIER_ACTIONS_LUA_ACTIONS_H_ 19 20 #include "actions/actions_model_generated.h" 21 #include "actions/types.h" 22 #include "utils/lua-utils.h" 23 #include "utils/tensor-view.h" 24 #include "utils/tflite-model-executor.h" 25 26 namespace libtextclassifier3 { 27 28 // Lua backed actions suggestions. 29 class LuaActionsSuggestions : public LuaEnvironment { 30 public: 31 static std::unique_ptr<LuaActionsSuggestions> CreateLuaActionsSuggestions( 32 const std::string& snippet, const Conversation& conversation, 33 const TfLiteModelExecutor* model_executor, 34 const TensorflowLiteModelSpec* model_spec, 35 const tflite::Interpreter* interpreter, 36 const reflection::Schema* actions_entity_data_schema, 37 const reflection::Schema* annotations_entity_data_schema); 38 39 bool SuggestActions(std::vector<ActionSuggestion>* actions); 40 41 private: 42 LuaActionsSuggestions( 43 const std::string& snippet, const Conversation& conversation, 44 const TfLiteModelExecutor* model_executor, 45 const TensorflowLiteModelSpec* model_spec, 46 const tflite::Interpreter* interpreter, 47 const reflection::Schema* actions_entity_data_schema, 48 const reflection::Schema* annotations_entity_data_schema); 49 50 bool Initialize(); 51 52 template <typename T> PushTensor(const TensorView<T> * tensor)53 void PushTensor(const TensorView<T>* tensor) const { 54 PushIterator(tensor ? tensor->size() : 0, 55 [this, tensor](const int64 index) { 56 Push(tensor->data()[index]); 57 return 1; // Num. values pushed. 58 }); 59 } 60 61 const std::string& snippet_; 62 const Conversation& conversation_; 63 TensorView<float> actions_scores_; 64 TensorView<float> smart_reply_scores_; 65 TensorView<float> sensitivity_score_; 66 TensorView<float> triggering_score_; 67 const std::vector<std::string> smart_replies_; 68 const reflection::Schema* actions_entity_data_schema_; 69 const reflection::Schema* annotations_entity_data_schema_; 70 }; 71 72 } // namespace libtextclassifier3 73 74 #endif // LIBTEXTCLASSIFIER_ACTIONS_LUA_ACTIONS_H_ 75