huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "actions/lua-actions.h"
#include "utils/base/logging.h"
#include "utils/lua-utils.h"
 
#ifdef __cplusplus
extern "C" {
#endif
#include "lauxlib.h"
#include "lualib.h"
#ifdef __cplusplus
}
#endif
 
namespace libtextclassifier3 {
namespace {
TensorView<float> GetTensorViewForOutput(
    const TfLiteModelExecutor* model_executor,
    const tflite::Interpreter* interpreter, int output) {
  if (output < 0 || model_executor == nullptr || interpreter == nullptr) {
    return TensorView<float>::Invalid();
  }
  return model_executor->OutputView<float>(output, interpreter);
}
}  // namespace
 
int LuaActionsSuggestions::TensorViewIterator::Item(
    const TensorView<float>* tensor, const int64 index,
    lua_State* state) const {
  lua_pushnumber(state, tensor->data()[index]);
  return 1;
}
 
std::unique_ptr<LuaActionsSuggestions>
LuaActionsSuggestions::CreateLuaActionsSuggestions(
    const std::string& snippet, const Conversation& conversation,
    const TfLiteModelExecutor* model_executor,
    const TensorflowLiteModelSpec* model_spec,
    const tflite::Interpreter* interpreter,
    const reflection::Schema* actions_entity_data_schema,
    const reflection::Schema* annotations_entity_data_schema) {
  auto lua_actions =
      std::unique_ptr<LuaActionsSuggestions>(new LuaActionsSuggestions(
          snippet, conversation, model_executor, model_spec, interpreter,
          actions_entity_data_schema, annotations_entity_data_schema));
  if (!lua_actions->Initialize()) {
    TC3_LOG(ERROR)
        << "Could not initialize lua environment for actions suggestions.";
    return nullptr;
  }
  return lua_actions;
}
 
LuaActionsSuggestions::LuaActionsSuggestions(
    const std::string& snippet, const Conversation& conversation,
    const TfLiteModelExecutor* model_executor,
    const TensorflowLiteModelSpec* model_spec,
    const tflite::Interpreter* interpreter,
    const reflection::Schema* actions_entity_data_schema,
    const reflection::Schema* annotations_entity_data_schema)
    : snippet_(snippet),
      conversation_(conversation),
      conversation_iterator_(annotations_entity_data_schema, this),
      actions_scores_(
          model_spec == nullptr
              ? TensorView<float>::Invalid()
              : GetTensorViewForOutput(model_executor, interpreter,
                                       model_spec->output_actions_scores())),
      smart_reply_scores_(
          model_spec == nullptr
              ? TensorView<float>::Invalid()
              : GetTensorViewForOutput(model_executor, interpreter,
                                       model_spec->output_replies_scores())),
      sensitivity_score_(model_spec == nullptr
                             ? TensorView<float>::Invalid()
                             : GetTensorViewForOutput(
                                   model_executor, interpreter,
                                   model_spec->output_sensitive_topic_score())),
      triggering_score_(
          model_spec == nullptr
              ? TensorView<float>::Invalid()
              : GetTensorViewForOutput(model_executor, interpreter,
                                       model_spec->output_triggering_score())),
      actions_entity_data_schema_(actions_entity_data_schema),
      annotations_entity_data_schema_(annotations_entity_data_schema) {}
 
bool LuaActionsSuggestions::Initialize() {
  return RunProtected([this] {
           LoadDefaultLibraries();
 
           // Expose conversation message stream.
           conversation_iterator_.NewIterator("messages",
                                              &conversation_.messages, state_);
           lua_setglobal(state_, "messages");
 
           // Expose ML model output.
           lua_newtable(state_);
           {
             tensor_iterator_.NewIterator("actions_scores", &actions_scores_,
                                          state_);
             lua_setfield(state_, /*idx=*/-2, "actions_scores");
           }
           {
             tensor_iterator_.NewIterator("reply_scores", &smart_reply_scores_,
                                          state_);
             lua_setfield(state_, /*idx=*/-2, "reply_scores");
           }
           {
             tensor_iterator_.NewIterator("sensitivity", &sensitivity_score_,
                                          state_);
             lua_setfield(state_, /*idx=*/-2, "sensitivity");
           }
           {
             tensor_iterator_.NewIterator("triggering_score",
                                          &triggering_score_, state_);
             lua_setfield(state_, /*idx=*/-2, "triggering_score");
           }
           lua_setglobal(state_, "model");
 
           return LUA_OK;
         }) == LUA_OK;
}
 
bool LuaActionsSuggestions::SuggestActions(
    std::vector<ActionSuggestion>* actions) {
  if (luaL_loadbuffer(state_, snippet_.data(), snippet_.size(),
                      /*name=*/nullptr) != LUA_OK) {
    TC3_LOG(ERROR) << "Could not load actions suggestions snippet.";
    return false;
  }
 
  if (lua_pcall(state_, /*nargs=*/0, /*nargs=*/1, /*errfunc=*/0) != LUA_OK) {
    TC3_LOG(ERROR) << "Could not run actions suggestions snippet.";
    return false;
  }
 
  if (RunProtected(
          [this, actions] {
            return ReadActions(actions_entity_data_schema_,
                               annotations_entity_data_schema_, this, actions);
          },
          /*num_args=*/1) != LUA_OK) {
    TC3_LOG(ERROR) << "Could not read lua result.";
    return false;
  }
  return true;
}
 
}  // namespace libtextclassifier3