-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(JAQPOT-114): send request to inference api (#25)
* feat(JAQPOT-114): send request to inference api * feat: send test request to jaqpot-inference * fix: compilation error * feat: complete end-to-end with prediction request * fix: make async method run properly async
- Loading branch information
Showing
19 changed files
with
321 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.jaqpot.api.entity | ||
|
||
enum class DataEntryRole { | ||
INPUT, | ||
RESULTS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jaqpot.api.mapper | ||
|
||
import org.jaqpot.api.entity.DataEntry | ||
import org.jaqpot.api.entity.DataEntryRole | ||
import org.jaqpot.api.entity.Dataset | ||
import org.jaqpot.api.model.DataEntryDto | ||
|
||
fun DataEntry.toDto(): DataEntryDto { | ||
return DataEntryDto( | ||
this.type.toDto(), | ||
this.values, | ||
this.id, | ||
this.createdAt, | ||
this.updatedAt | ||
) | ||
} | ||
|
||
fun DataEntryDto.toEntity(dataset: Dataset, dataEntryRole: DataEntryRole): DataEntry { | ||
return DataEntry( | ||
this.id, | ||
dataset, | ||
this.type.toEntity(), | ||
dataEntryRole, | ||
this.propertyValues, | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/org/jaqpot/api/mapper/DataEntryTypeMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.jaqpot.api.mapper | ||
|
||
import org.jaqpot.api.entity.DataEntryType | ||
import org.jaqpot.api.model.DataEntryDto | ||
|
||
fun DataEntryDto.Type.toEntity(): DataEntryType { | ||
return when (this) { | ||
DataEntryDto.Type.ARRAY -> DataEntryType.ARRAY | ||
} | ||
} | ||
|
||
fun DataEntryType.toDto(): DataEntryDto.Type { | ||
return when (this) { | ||
DataEntryType.ARRAY -> DataEntryDto.Type.ARRAY | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.jaqpot.api.mapper | ||
|
||
import org.jaqpot.api.entity.DataEntryRole | ||
import org.jaqpot.api.entity.Dataset | ||
import org.jaqpot.api.entity.Model | ||
import org.jaqpot.api.model.DatasetDto | ||
|
||
|
||
fun Dataset.toDto(): DatasetDto { | ||
return DatasetDto( | ||
this.type.toDto(), | ||
this.input.map { it.toDto() }, | ||
this.id, | ||
this.results.map { it.toDto() }, | ||
this.createdAt, | ||
this.updatedAt | ||
) | ||
} | ||
|
||
fun DatasetDto.toEntity(model: Model, userId: String): Dataset { | ||
val d = Dataset( | ||
this.id, | ||
model, | ||
userId, | ||
this.type.toEntity(), | ||
mutableListOf(), | ||
mutableListOf() | ||
) | ||
|
||
d.input.addAll(this.input.map { it -> it.toEntity(d, DataEntryRole.INPUT) }) | ||
d.results.addAll(this.results?.map { it -> it.toEntity(d, DataEntryRole.RESULTS) } ?: emptyList()) | ||
|
||
return d | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/org/jaqpot/api/mapper/DatasetTypeMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.jaqpot.api.mapper | ||
|
||
import org.jaqpot.api.entity.DatasetType | ||
import org.jaqpot.api.model.DatasetDto | ||
|
||
fun DatasetDto.Type.toEntity(): DatasetType { | ||
return when (this) { | ||
DatasetDto.Type.PREDICTION -> DatasetType.PREDICTION | ||
} | ||
} | ||
|
||
fun DatasetType.toDto(): DatasetDto.Type { | ||
return when (this) { | ||
DatasetType.PREDICTION -> DatasetDto.Type.PREDICTION | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/org/jaqpot/api/repository/DatasetRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.jaqpot.api.repository | ||
|
||
import org.jaqpot.api.entity.Dataset | ||
import org.springframework.data.repository.CrudRepository | ||
|
||
interface DatasetRepository : CrudRepository<Dataset, Long> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/org/jaqpot/api/service/model/PredictionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.jaqpot.api.service.model | ||
|
||
import org.jaqpot.api.entity.* | ||
import org.jaqpot.api.mapper.toDto | ||
import org.jaqpot.api.repository.DatasetRepository | ||
import org.jaqpot.api.service.model.dto.PredictionRequestDto | ||
import org.jaqpot.api.service.model.dto.PredictionResponseDto | ||
import org.jaqpot.api.service.runtime.RuntimeResolver | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.scheduling.annotation.Async | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
import java.util.* | ||
|
||
|
||
@Service | ||
class PredictionService( | ||
private val datasetRepository: DatasetRepository, | ||
private val runtimeResolver: RuntimeResolver | ||
) { | ||
|
||
@Async | ||
fun executePredictionAndSaveResults(model: Model, dataset: Dataset) { | ||
val rawModel = Base64.getEncoder().encodeToString(model.actualModel) | ||
val request: HttpEntity<PredictionRequestDto> = | ||
HttpEntity(PredictionRequestDto(listOf(rawModel), dataset.toDto())) | ||
|
||
val results: List<Any> = makePredictionRequest(model, request) | ||
|
||
storeResults(dataset, results) | ||
} | ||
|
||
private fun storeResults(dataset: Dataset, results: List<Any>) { | ||
dataset.results.clear() | ||
dataset.results.addAll( | ||
listOf( | ||
DataEntry( | ||
null, | ||
dataset, | ||
DataEntryType.ARRAY, | ||
DataEntryRole.RESULTS, | ||
results | ||
) | ||
) | ||
) | ||
|
||
datasetRepository.save(dataset) | ||
} | ||
|
||
private fun makePredictionRequest( | ||
model: Model, | ||
request: HttpEntity<PredictionRequestDto> | ||
): List<Any> { | ||
val restTemplate = RestTemplate() | ||
val inferenceUrl = "${runtimeResolver.resolveRuntime(model)}/predict/" | ||
val response = restTemplate.postForEntity(inferenceUrl, request, PredictionResponseDto::class.java) | ||
|
||
val results: List<Any> = response.body?.predictions ?: emptyList() | ||
return results | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/org/jaqpot/api/service/model/dto/PredictionRequestDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.jaqpot.api.service.model.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import org.jaqpot.api.model.DatasetDto | ||
|
||
@JsonInclude(JsonInclude.Include.ALWAYS) | ||
class PredictionRequestDto( | ||
val rawModel: List<String>, | ||
val dataset: DatasetDto, | ||
val additionalInfo: String? = null, | ||
val doaMatrix: String? = null | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/org/jaqpot/api/service/model/dto/PredictionResponseDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.jaqpot.api.service.model.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
|
||
@JsonInclude(JsonInclude.Include.ALWAYS) | ||
class PredictionResponseDto( | ||
val predictions: List<Map<String, Any>> | ||
) |
6 changes: 4 additions & 2 deletions
6
src/main/kotlin/org/jaqpot/api/service/runtime/RuntimeResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package org.jaqpot.api.service.runtime | ||
|
||
import org.jaqpot.api.entity.Model | ||
import org.jaqpot.api.service.runtime.config.RuntimeProvider | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class RuntimeResolver(val runtimeProvider: RuntimeProvider) { | ||
|
||
fun resolveRuntime(): String { | ||
return runtimeProvider.jaqpotpyPretrainedUrl; | ||
fun resolveRuntime(model: Model): String { | ||
return runtimeProvider.jaqpotpyPretrainedUrl | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ot/api/service/runtime/RuntimeProvider.kt → ...service/runtime/config/RuntimeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.