-
Notifications
You must be signed in to change notification settings - Fork 9
/
job_services.py
120 lines (97 loc) · 4.15 KB
/
job_services.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
# coding: utf-8
#
# Copyright 2017 The Oppia Authors. All Rights Reserved.
#
# 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.
"""This module contains functions used for polling, training and saving jobs."""
from core.classifiers import algorithm_registry
from core.services import remote_access_services
# pylint: disable=too-many-branches
def _validate_job_data(job_data):
if not isinstance(job_data, dict):
raise Exception('Invalid format of job data')
if 'job_id' not in job_data:
raise Exception('job data should contain job id')
if 'training_data' not in job_data:
raise Exception('job data should contain training data')
if 'algorithm_id' not in job_data:
raise Exception('job data should contain algorithm id')
if not isinstance(job_data['job_id'], str):
raise Exception(
'Expected job id to be a string, received %s' %
job_data['job_id'])
if not isinstance(job_data['algorithm_id'], str):
raise Exception(
'Expected algorithm id to be a string, received %s' %
job_data['algorithm_id'])
if not isinstance(job_data['training_data'], list):
raise Exception(
'Expected training data to be a list, received %s' %
job_data['training_data'])
algorithm_ids = (
algorithm_registry.Registry.get_all_classifier_algorithm_ids())
if job_data['algorithm_id'] not in algorithm_ids:
raise Exception('Invalid algorithm id %s' % job_data['algorithm_id'])
for grouped_answers in job_data['training_data']:
if 'answer_group_index' not in grouped_answers:
raise Exception(
'Expected answer_group_index to be a key in training_data',
' list item')
if 'answers' not in grouped_answers:
raise Exception(
'Expected answers to be a key in training_data list item')
if not isinstance(grouped_answers['answer_group_index'], int):
raise Exception(
'Expected answer_group_index to be an int, received %s' %
grouped_answers['answer_group_index'])
if not isinstance(grouped_answers['answers'], list):
raise Exception(
'Expected answers to be a list, received %s' %
grouped_answers['answers'])
def get_next_job():
"""Get next job request.
Returns: dict. A dictionary containing job data.
"""
job_data = remote_access_services.fetch_next_job_request()
_validate_job_data(job_data)
return job_data
def train_classifier(algorithm_id, training_data):
"""Train classifier associated with 'algorithm_id' using 'training_data'.
Args:
algorithm_id: str. ID of classifier algorithm.
training_data: list(dict). A list containing training data. Each dict
stores 'answer_group_index' and 'answers'.
Returns:
dict. Result of trained classifier algorithm.
"""
classifier = algorithm_registry.Registry.get_classifier_by_algorithm_id(
algorithm_id)
classifier.train(training_data)
classifier_data = classifier.to_dict()
classifier.validate(classifier_data)
return classifier_data
def store_job_result(job_id, classifier_data):
"""Store result of job in the Oppia server.
Args:
job_id: str. ID of the job whose result is to be stored.
classifier_data: dict. A dictionary representing result of the job.
Returns:
int. Status code of response.
"""
job_result_dict = {
'job_id': job_id,
'classifier_data': classifier_data
}
status = remote_access_services.store_trained_classifier_model(
job_result_dict)
return status