-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make cog train call trainings endpoint (#2013)
* Support different response types in _predict * Currently _predict assumes all response types are a PredictionResponse type. * In the case where _predict is called by a train endpoint this assumption is not true and we have to use a TrainingResponse type. * Feed in the response type to the _predict function so it can properly deserialise training responses. * Make cog train CLI call /trainings * Instead of relying on the /predictions endpoint to be overridden directly call the /trainings endpoint. * Report the correct command to the user in the event of a failure. * Explicitly check return code in test train * Allows better debugging of stderr et al * Correctly set TrainingResponse type in training idempotent * This call also requires the appropriate response type. * Use explicit argument index syntax in cog predict
- Loading branch information
Showing
5 changed files
with
85 additions
and
24 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 |
---|---|---|
|
@@ -43,19 +43,20 @@ type ValidationErrorResponse struct { | |
|
||
type Predictor struct { | ||
runOptions docker.RunOptions | ||
isTrain bool | ||
|
||
// Running state | ||
containerID string | ||
port int | ||
} | ||
|
||
func NewPredictor(runOptions docker.RunOptions) Predictor { | ||
func NewPredictor(runOptions docker.RunOptions, isTrain bool) Predictor { | ||
if global.Debug { | ||
runOptions.Env = append(runOptions.Env, "COG_LOG_LEVEL=debug") | ||
} else { | ||
runOptions.Env = append(runOptions.Env, "COG_LOG_LEVEL=warning") | ||
} | ||
return Predictor{runOptions: runOptions} | ||
return Predictor{runOptions: runOptions, isTrain: isTrain} | ||
} | ||
|
||
func (p *Predictor) Start(logsWriter io.Writer, timeout time.Duration) error { | ||
|
@@ -146,7 +147,7 @@ func (p *Predictor) Predict(inputs Inputs) (*Response, error) { | |
return nil, err | ||
} | ||
|
||
url := fmt.Sprintf("http://localhost:%d/predictions", p.port) | ||
url := p.url() | ||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBody)) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to create HTTP request to %s: %w", url, err) | ||
|
@@ -164,14 +165,14 @@ func (p *Predictor) Predict(inputs Inputs) (*Response, error) { | |
if resp.StatusCode == http.StatusUnprocessableEntity { | ||
errorResponse := &ValidationErrorResponse{} | ||
if err := json.NewDecoder(resp.Body).Decode(errorResponse); err != nil { | ||
return nil, fmt.Errorf("/predictions call returned status 422, and the response body failed to decode: %w", err) | ||
return nil, fmt.Errorf("/%s call returned status 422, and the response body failed to decode: %w", p.endpoint(), err) | ||
} | ||
|
||
return nil, buildInputValidationErrorMessage(errorResponse) | ||
return nil, p.buildInputValidationErrorMessage(errorResponse) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("/predictions call returned status %d", resp.StatusCode) | ||
return nil, fmt.Errorf("/%s call returned status %d", p.endpoint(), resp.StatusCode) | ||
} | ||
|
||
prediction := &Response{} | ||
|
@@ -197,30 +198,47 @@ func (p *Predictor) GetSchema() (*openapi3.T, error) { | |
return openapi3.NewLoader().LoadFromData(body) | ||
} | ||
|
||
func buildInputValidationErrorMessage(errorResponse *ValidationErrorResponse) error { | ||
func (p *Predictor) endpoint() string { | ||
if p.isTrain { | ||
return "trainings" | ||
} | ||
return "predictions" | ||
} | ||
|
||
func (p *Predictor) url() string { | ||
return fmt.Sprintf("http://localhost:%d/%s", p.port, p.endpoint()) | ||
} | ||
|
||
func (p *Predictor) buildInputValidationErrorMessage(errorResponse *ValidationErrorResponse) error { | ||
errorMessages := []string{} | ||
|
||
for _, validationError := range errorResponse.Detail { | ||
if len(validationError.Location) != 3 || validationError.Location[0] != "body" || validationError.Location[1] != "input" { | ||
responseBody, _ := json.MarshalIndent(errorResponse, "", "\t") | ||
return fmt.Errorf("/predictions call returned status 422, and there was an unexpected message in response:\n\n%s", responseBody) | ||
return fmt.Errorf("/%s call returned status 422, and there was an unexpected message in response:\n\n%s", p.endpoint(), responseBody) | ||
} | ||
|
||
errorMessages = append(errorMessages, fmt.Sprintf("- %s: %s", validationError.Location[2], validationError.Message)) | ||
} | ||
|
||
command := "predict" | ||
if p.isTrain { | ||
command = "train" | ||
} | ||
|
||
return fmt.Errorf( | ||
`The inputs you passed to cog predict could not be validated: | ||
`The inputs you passed to cog %[1]s could not be validated: | ||
%s | ||
%[2]s | ||
You can provide an input with -i. For example: | ||
cog predict -i blur=3.5 | ||
cog %[1]s -i blur=3.5 | ||
If your input is a local file, you need to prefix the path with @ to tell Cog to read the file contents. For example: | ||
cog predict -i [email protected]`, | ||
cog %[1]s -i [email protected]`, | ||
command, | ||
strings.Join(errorMessages, "\n"), | ||
) | ||
} |
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