-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcx.py
262 lines (222 loc) · 8.49 KB
/
cx.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import os, time, uuid, sys
from tqdm import tqdm
from google.cloud import dialogflowcx as df
from google.protobuf.field_mask_pb2 import FieldMask
from sklearn.metrics import precision_recall_fscore_support
#from my_config import *
from config import *
intents_client = df.IntentsClient()
flows_client = df.FlowsClient()
def get_intent_list(agent):
df_intents = {}
listIntents = df.ListIntentsRequest(parent=agent)
intents = intents_client.list_intents(listIntents)
for intent in intents:
df_intents[intent.display_name] = intent
return df_intents
def delete_intent(intent):
print(intent.display_name, intent.name)
if "00000000-0000-0000-0000-" in intent.name:
return
deleteInent = df.DeleteIntentRequest(name=intent.name)
intents_client.delete_intent(deleteInent)
print("deleted",intent.display_name, intent.name)
time.sleep(1)
def delete_all_intents(intents):
for intent_name, intent in intents.items():
delete_intent(intent)
def create_intent(agent, intent_name):
intent = df.Intent(
display_name = intent_name
)
createIntent = df.CreateIntentRequest(intent=intent, parent=agent)
intent = intents_client.create_intent(createIntent)
time.sleep(1)
return intent
def add_training_data(intents, train_data):
for i, row in train_data.iterrows():
part = df.Intent.TrainingPhrase.Part(text = row[TEXT_COLUMN])
training_phrase = df.Intent.TrainingPhrase(parts=[part], repeat_count=1)
intent = intents[row[INTENT_COLUMN]]
intent.training_phrases.extend([training_phrase])
for df_intent in tqdm(intents, desc="adding training data"):
updateIntent = df.UpdateIntentRequest(intent=intents[df_intent])
response = intents_client.update_intent(updateIntent)
time.sleep(1)
def detect_intent(agent, text_input):
try:
text_input = text_input[:255] #dialog
DIALOGFLOW_LANGUAGE_CODE = "en"
SESSION_ID = uuid.uuid4()
session_path = f"{agent}/sessions/{SESSION_ID}"
session_client = df.SessionsClient()
text_input = df.TextInput(text=text_input)
query_input = df.QueryInput(text=text_input, language_code=DIALOGFLOW_LANGUAGE_CODE)
detectIntent = df.DetectIntentRequest(session=session_path,query_input=query_input)
response = session_client.detect_intent(request=detectIntent)
return response.query_result.intent.display_name
except:
print("exception while input")
print(text_input)
print("Unexpected error:", sys.exc_info()[0])
return ""
def create_agent(agent_name, location_path):
agent = df.Agent(display_name=agent_name, default_language_code=LANGUAGE_CODE, time_zone=TIME_ZONE)
createAgentRequest = df.CreateAgentRequest(agent=agent, parent=location_path)
agentsClient = df.AgentsClient()
agent = agentsClient.create_agent(request=createAgentRequest)
return agent
def create_all_intents(agent, train_data):
print("creating intents")
df_intents = get_intent_list(agent)
intent_names = train_data.intent.unique()
for intent_name in intent_names:
if intent_name in df_intents:
continue
print(intent_name)
intent = create_intent(agent, intent_name)
df_intents[intent_name] = intent
add_training_data(df_intents, train_data)
def do_test(agent, test_data):
#test_data = test_data[:10]
actuals = []
expected = []
for i, row in tqdm(test_data.iterrows()):
text_input = row[TEXT_COLUMN]
intent_name = row[INTENT_COLUMN]
expected.append(intent_name)
actual_intent = detect_intent(agent, text_input)
if actual_intent.strip() == "":
actual_intent = "None"
actuals.append(actual_intent)
print("type, precision, recall, fscore, support")
precision, recall, fscore, support = precision_recall_fscore_support(expected, actuals, average="micro")
print("micro", precision, recall, fscore, support)
precision, recall, fscore, support = precision_recall_fscore_support(expected, actuals, average="weighted")
print("weighted", precision, recall, fscore, support)
precision, recall, fscore, support = precision_recall_fscore_support(expected, actuals, average="macro")
print("macro", precision, recall, fscore, support)
test_data["actual"] = actuals
success_series = test_data["actual"] == test_data[INTENT_COLUMN]
total_count = len(test_data)
success_count = len(test_data[success_series])
success_ratio = round(success_count/total_count, 2)
print("total", total_count, "success", success_count, "success %", success_ratio)
test_data.to_csv("results.csv",index=False)
def get_flow(agent, flowId):
flowPath = f"{agent}/flows/{flowId}"
flowRequest = df.GetFlowRequest(name=flowPath)
flow = flows_client.get_flow(flowRequest)
return flow
def update_transition_routes(flow, intents):
routes = flow.transition_routes
existing_routes = set()
for route in routes:
intent_name = route.intent
existing_routes.add(intent_name)
for intent_name, intent in intents.items():
if intent.name in existing_routes:
continue
if "00000000-0000-0000" in intent.name:
continue
resText = df.ResponseMessage.Text(text=[f"{intent_name} intent"])
rm = df.ResponseMessage(text=resText)
ff = df.Fulfillment(messages=[rm])
route = df.TransitionRoute(intent=intent.name, trigger_fulfillment=ff)
flow.transition_routes.append(route)
existing_routes.add(intent.name)
mask = FieldMask()
mask.FromJsonString("transitionRoutes")
flowRequest = df.UpdateFlowRequest(flow=flow, update_mask=mask)
flows_client.update_flow(request=flowRequest)
#print(flow.transition_routes)
def update_nlu_type(flow, nlu_type):
flowPath = flow.name
#nlu_settings = df.NluSettings(model_type=nlu_type)
flow.nlu_settings.model_type = nlu_type
mask = FieldMask()
mask.FromJsonString("nluSettings")
flowRequest = df.UpdateFlowRequest(flow=flow, update_mask=mask)
flows_client.update_flow(request=flowRequest)
def train_flow(flow):
print("training started")
trainFlowRequest = df.TrainFlowRequest(name=flow.name)
flowsClient = df.FlowsClient()
train_operation = flowsClient.train_flow(request=trainFlowRequest)
result = train_operation.result()
print("training Completed")
def create_and_test():
locationPath = f"projects/{PROJECT_ID}/locations/{LOCATION_ID}"
print("creating agent", AGENT_NAME)
agent = create_agent(AGENT_NAME, locationPath)
print("agent created", agent.name)
train_data = pd.read_csv(TRAIN_FILE)
create_all_intents(agent.name, train_data)
df_intents = get_intent_list(agent.name)
flow = get_flow(agent.name, DEFAULT_FLOW_ID)
update_transition_routes(flow, df_intents)
update_nlu_type(flow, df.NluSettings.ModelType.MODEL_TYPE_ADVANCED)
train_flow(flow)
test_data = pd.read_csv(TEST_FILE)
do_test(agent.name, test_data)
def test_existing(train=False):
agent_name = input("give me agent Id")
agent_name = agent_name.strip().lower()
locationPath = f"projects/{PROJECT_ID}/locations/{LOCATION_ID}"
print("getting agents at location", locationPath)
agentsClient = df.AgentsClient()
listAgentsRequest = df.ListAgentsRequest(parent=locationPath)
agents = agentsClient.list_agents(request=listAgentsRequest).agents
agent = None
for a in agents:
print(a.name, a.display_name)
name = a.display_name.lower()
if name == agent_name:
agent = a
break
if train:
flow = get_flow(agent.name, DEFAULT_FLOW_ID)
train_flow(flow)
pass
test_data = pd.read_csv(TEST_FILE)
do_test(agent.name, test_data)
def delete_agent():
agent_name = input("give me agent Id")
agent_name = agent_name.strip().lower()
locationPath = f"projects/{PROJECT_ID}/locations/{LOCATION_ID}"
print("getting agents at location", locationPath)
agentsClient = df.AgentsClient()
listAgentsRequest = df.ListAgentsRequest(parent=locationPath)
agents = agentsClient.list_agents(request=listAgentsRequest).agents
deleteAgent = None
for agent in agents:
print(agent.name, agent.display_name)
name = agent.display_name.lower()
if name == agent_name:
deleteAgent = agent
deleteAgentRequest = df.DeleteAgentRequest()
deleteAgentRequest.name=deleteAgent.name
agentsClient.delete_agent(request=deleteAgentRequest)
print("agent deleted", deleteAgent.name)
if __name__ == "__main__":
create_and_test()
#test_existing()
#delete_agent()
'''delete_intents = input("Do you want to delete intents first?")
if delete_intents.strip().lower() in ["y", "yes", "yeah", "sure"]:
df_intents = get_intent_list(agentPath)
delete_all_intents(df_intents)
create_all_intents(agentPath)'''
#test_data = pd.read_csv(TEST_FILE)
#do_test(agentPath, test_data)
#df_intents = get_intent_list()
#flow = get_flow()
#update_transition_routes(flow, df_intents)
#flow = get_flow()
#print(flow)
#print("\n\n\n")
#print(flow.transition_routes)
#print(type(flow.transition_routes[0]))