This repository was archived by the owner on Nov 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbothub_api.py
More file actions
205 lines (167 loc) · 5.88 KB
/
bothub_api.py
File metadata and controls
205 lines (167 loc) · 5.88 KB
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
import json
import sys
import time
import requests
from decouple import config
repository = config('REPOSITORY_UUID')
repository_version = config('REPOSITORY_VERSION_ID')
base_url = config('BOTHUB_API_URL')
params = {'repository_uuid': repository}
headers = {
'Authorization': f'Token {config("ACCOUNT_API_TOKEN")}',
'Content-Type': 'application/json'
}
def get_examples_count(repository_uuid, *args):
return json.loads(requests.get(
f'{base_url}/repository/info/{repository_uuid}/{repository_version}/',
headers=headers
).content).get('examples__count')
def delete_repository(repository_uuid, *args):
return requests.delete(
f'{base_url}/repository/info/{repository_uuid}/{repository_version}/',
headers=headers
)
def get_all_examples(headers, next_call, params, *args):
while next_call is not None:
response = requests.get(next_call, headers=headers, params=params)
response_json = response.json()
next_call = response_json.get('next')
yield response_json.get('results', None)
def delete_example(example_id, *args):
try:
response = requests.delete(
f'{base_url}/repository/example/{example_id}', headers=headers)
if response.status_code == 204:
print("Example deleted!")
return response.status_code
except Exception as error:
print(error)
print(f'Failed to delete this sentence!\nID: {example_id}')
return
def create_example(example, *args):
entities = []
if len(example.get('entities')) > 0:
for entity in example.get('entities'):
if entity:
entities.append({
"entity": entity.get('entity'),
"start": entity.get('start'),
"end": entity.get('end')
})
data = {
"repository": repository,
"repository_version": repository_version,
"text": example.get('text'),
"intent": example.get('intent'),
"entities": entities
}
try:
response = requests.post(
f'{base_url}/repository/example/', headers=headers, data=json.dumps(data))
print("Example created!")
return response.status_code
except Exception as error:
print(error)
print(f'Failed to train this sentence!\nText: {example.get("text")}')
return
def delete_evaluate_example(example_id, *args):
response = requests.delete(
f'{base_url}/repository/evaluate/{example_id}/?repository_uuid={repository}/{repository_version}/', headers=headers)
if response.status_code == 204:
print("Example deleted!")
return response.status_code
def delete_all_examples(*args):
results = get_all_examples(
headers=headers,
next_call=f'{base_url}/repository/evaluate/',
params=params
)
for result in results:
for item in result:
time.sleep(1)
response = delete_evaluate_example(item.get('id'))
print(response)
def create_evaluate_examples(*args):
with open('examples/rasa-dataset-testing.json', encoding="utf-8") as json_file:
examples = json.load(json_file)
count = len(examples)
index = 0
for example in examples:
data = {
"is_corrected": False,
"repository": repository,
"repository_version": int(repository_version),
"text": example['text'],
"language": example['language'],
"intent": example['intent'],
"entities": []
}
response = requests.post(
f'{base_url}/repository/evaluate/', headers=headers, data=json.dumps(data))
if response.status_code == 201:
print("\nEvaluate example created!")
print(f"%.2f%%" % ((index*100)/count))
def sentences_count(intent=None, *args):
def count_intent(examples, intent):
count = 0
for example in examples:
if example['intent'] == intent:
count += 1
return count
with open('examples/rasa_dataset_training.json') as json_file:
examples = json.load(json_file)['rasa_nlu_data']['common_examples']
if intent:
count = count_intent(examples, intent)
print(count)
return
intents = []
total = 0
for example in examples:
if example['intent'] not in intents:
count = count_intent(examples, example['intent'])
print(f'{count} - {example["intent"]}')
total += count
intents.append(example['intent'])
print(f'{total} - total')
def delete_all(*args):
count = get_examples_count(repository)
results = get_all_examples(
headers=headers,
next_call=f'{base_url}/repository/examples/',
params=params
)
index = 0
for result in results:
for item in result:
time.sleep(1)
delete_example(item.get('id'))
index += 1
print(f"%.2f%%" % ((index*100)/count))
def delete_by_intent(intent, *args):
count = get_examples_count(repository)
index = 0
results = get_all_examples(
headers=headers,
next_call=f'{base_url}/repository/examples/',
params=params
)
for result in results:
for item in result:
if item.get('intent') == intent:
time.sleep(1)
delete_example(item.get('id'))
index += 1
print(f"%.2f%%" % ((index*100)/count))
def main():
print('welcome to Chatito-Bothub-Extension!')
if __name__ == '__main__':
args = sys.argv
if len(args) > 1:
try:
func = eval(f'{args[1]}')
except Exception:
print("function doesn't exists!")
else:
func(*args[2:])
else:
main()