Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cli/cmd/predict.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,31 @@ import (
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
)

var predictPrintJSON bool

func init() {
predictCmd.PersistentFlags().BoolVarP(&predictPrintJSON, "json", "j", false, "print the raw json response")
addAppNameFlag(predictCmd)
addEnvFlag(predictCmd)
}

type PredictResponse struct {
ResourceID string `json:"resource_id"`
ClassificationPredictions []ClassificationPrediction `json:"classification_predictions"`
RegressionPredictions []RegressionPrediction `json:"regression_predictions"`
ClassificationPredictions []ClassificationPrediction `json:"classification_predictions,omitempty"`
RegressionPredictions []RegressionPrediction `json:"regression_predictions,omitempty"`
}

type ClassificationPrediction struct {
PredictedClass int `json:"predicted_class"`
PredictedClassReversed interface{} `json:"predicted_class_reversed"`
Probabilities []float64 `json:"probabilities"`
TransformedSample interface{} `json:"transformed_sample"`
}

type RegressionPrediction struct {
PredictedValue float64 `json:"predicted_value"`
PredictedValueReversed interface{} `json:"predicted_value_reversed"`
TransformedSample interface{} `json:"transformed_sample"`
}

var predictCmd = &cobra.Command{
Expand Down Expand Up @@ -87,6 +92,16 @@ var predictCmd = &cobra.Command{
errors.Exit(err)
}

if predictPrintJSON {
prettyResp, err := json.Pretty(predictResponse)
if err != nil {
errors.Exit(err)
}

fmt.Println(prettyResp)
return
}

apiID := predictResponse.ResourceID
api := resourcesRes.APIStatuses[apiID]

Expand Down
1 change: 1 addition & 0 deletions docs/operator/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Flags:
-a, --app string app name
-e, --env string environment (default "dev")
-h, --help help for predict
-j, --json print the raw json response
```

The `predict` command converts samples from a JSON file into prediction requests and outputs the response. This command is useful for quickly testing model output.
Expand Down
9 changes: 9 additions & 0 deletions pkg/lib/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ func WriteJSON(obj interface{}, outPath string) error {
}
return nil
}

func Pretty(obj interface{}) (string, error) {
b, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return "", err
}

return string(b), nil
}
1 change: 1 addition & 0 deletions pkg/workloads/tf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def run_predict(sample):
prediction_request = create_prediction_request(transformed_sample)
response_proto = local_cache["stub"].Predict(prediction_request, timeout=10.0)
result = parse_response_proto(response_proto)
result["transformed_sample"] = transformed_sample
util.log_indent("Raw sample:", indent=4)
util.log_pretty(sample, indent=6)
util.log_indent("Transformed sample:", indent=4)
Expand Down