use of de.tudarmstadt.ukp.inception.recommendation.imls.external.v1.messages.TrainingRequest in project inception by inception-project.
the class ExternalRecommender method train.
@Override
public void train(RecommenderContext aContext, List<CAS> aCasses) throws RecommendationException {
TrainingRequest trainingRequest = new TrainingRequest();
List<Document> documents = new ArrayList<>();
// We assume that the type system for all CAS are the same
String typeSystem = serializeTypeSystem(aCasses.get(0));
trainingRequest.setTypeSystem(typeSystem);
// Fill in metadata. We use the type system of the first CAS in the list
// for all the other CAS. It could happen that training happens while
// the type system changes, e.g. by adding a layer or feature during training.
// Then the type system of the first CAS might not match the type system
// of the other CAS. This should happen really rarely, therefore this potential
// error is neglected.
trainingRequest.setMetadata(buildMetadata(aCasses.get(0)));
for (CAS cas : aCasses) {
documents.add(buildDocument(cas));
}
trainingRequest.setDocuments(documents);
HttpRequest request = //
HttpRequest.newBuilder().uri(//
URI.create(appendIfMissing(traits.getRemoteUrl(), "/")).resolve("train")).header(HttpHeaders.CONTENT_TYPE, //
APPLICATION_JSON_VALUE).timeout(properties.getReadTimeout()).POST(BodyPublishers.ofString(toJson(trainingRequest), UTF_8)).build();
HttpResponse<String> response = sendRequest(request);
if (response.statusCode() == HTTP_TOO_MANY_REQUESTS) {
LOG.info("External recommender is already training");
} else // then it does not make sense to go on and try to decode the XMI
if (response.statusCode() >= HTTP_BAD_REQUEST) {
String responseBody = getResponseBody(response);
String msg = format("Request was not successful: [%d] - [%s]", response.statusCode(), responseBody);
throw new RecommendationException(msg);
}
aContext.put(KEY_TRAINING_COMPLETE, true);
}
use of de.tudarmstadt.ukp.inception.recommendation.imls.external.v1.messages.TrainingRequest in project inception by inception-project.
the class ExternalRecommenderIntegrationTest method thatTrainingSendsCorrectRequest.
@Test
public void thatTrainingSendsCorrectRequest() throws Exception {
List<CAS> casses = loadDevelopmentData();
sut.train(context, casses);
TrainingRequest request = fromJsonString(TrainingRequest.class, requestBodies.get(0));
//
assertThat(request.getMetadata()).hasNoNullFieldsOrProperties().hasFieldOrPropertyWithValue("projectId", PROJECT_ID).hasFieldOrPropertyWithValue("layer", recommender.getLayer().getName()).hasFieldOrPropertyWithValue("feature", recommender.getFeature().getName()).hasFieldOrPropertyWithValue("crossSentence", CROSS_SENTENCE).hasFieldOrPropertyWithValue("anchoringMode", ANCHORING_MODE.getId());
for (int i = 0; i < request.getDocuments().size(); i++) {
Document doc = request.getDocuments().get(i);
assertThat(doc).hasFieldOrPropertyWithValue("documentId", (long) i).hasFieldOrPropertyWithValue("userId", USER_NAME);
}
}
use of de.tudarmstadt.ukp.inception.recommendation.imls.external.v1.messages.TrainingRequest in project inception by inception-project.
the class RemoteStringMatchingNerRecommender method train.
public void train(String aTrainingRequestJson) throws UIMAException, SAXException, IOException, RecommendationException {
TrainingRequest request = deserializeTrainingRequest(aTrainingRequestJson);
List<CAS> casses = new ArrayList<>();
for (Document doc : request.getDocuments()) {
CAS cas = deserializeCas(doc.getXmi(), request.getTypeSystem());
casses.add(cas);
}
recommendationEngine.train(context, casses);
}
Aggregations