{
  "openapi": "3.1.0",
  "info": {
    "title": "Sylvester ML API",
    "description": "Cat comfort/discomfort detection from images.",
    "version": "2"
  },
  "servers": [
    {
      "url": "/v2"
    }
  ],
  "paths": {
    "/predict": {
      "post": {
        "summary": "Predict comfort/discomfort with the latest model",
        "operationId": "predict",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PredictRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PredictResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "422": {
            "description": "Unprocessable Entity",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "ApiKeyAuth": []
          }
        ]
      }
    },
    "/models/{model_id}/predict": {
      "post": {
        "summary": "Predict comfort/discomfort with a specific model",
        "operationId": "predict_with_model",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "parameters": [
          {
            "name": "model_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "description": "Model identifier, e.g. bc-v1.",
              "examples": [
                "bc-v1"
              ],
              "title": "Model Id"
            },
            "description": "Model identifier, e.g. bc-v1."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PredictRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PredictResponse"
                }
              }
            }
          },
          "400": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            },
            "description": "Bad Request"
          },
          "401": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            },
            "description": "Unauthorized"
          },
          "404": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            },
            "description": "Not Found"
          },
          "422": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            },
            "description": "Unprocessable Entity"
          },
          "500": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            },
            "description": "Internal Server Error"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "BoundingBox": {
        "properties": {
          "x_min": {
            "type": "integer",
            "title": "X Min"
          },
          "y_min": {
            "type": "integer",
            "title": "Y Min"
          },
          "x_max": {
            "type": "integer",
            "title": "X Max"
          },
          "y_max": {
            "type": "integer",
            "title": "Y Max"
          }
        },
        "type": "object",
        "required": [
          "x_min",
          "y_min",
          "x_max",
          "y_max"
        ],
        "title": "BoundingBox"
      },
      "Cat": {
        "properties": {
          "bounding_box": {
            "allOf": [
              {
                "$ref": "#/components/schemas/BoundingBox"
              }
            ],
            "description": "Pixel coordinates in the original image."
          },
          "detection_confidence": {
            "type": "number",
            "title": "Detection Confidence",
            "description": "Detector confidence for this cat (0.0-1.0)."
          }
        },
        "type": "object",
        "required": [
          "bounding_box",
          "detection_confidence"
        ],
        "title": "Cat"
      },
      "Detection": {
        "type": "string",
        "enum": [
          "detected",
          "low_confidence",
          "not_detected"
        ],
        "title": "Detection"
      },
      "ErrorDetail": {
        "properties": {
          "code": {
            "type": "string",
            "title": "Code"
          },
          "message": {
            "type": "string",
            "title": "Message"
          },
          "details": {
            "type": "object",
            "title": "Details"
          }
        },
        "type": "object",
        "required": [
          "code",
          "message"
        ],
        "title": "ErrorDetail"
      },
      "ErrorResponse": {
        "properties": {
          "request_id": {
            "type": "string",
            "title": "Request Id"
          },
          "error": {
            "$ref": "#/components/schemas/ErrorDetail"
          }
        },
        "type": "object",
        "required": [
          "request_id",
          "error"
        ],
        "title": "ErrorResponse"
      },
      "ImageInfo": {
        "properties": {
          "width": {
            "type": "integer",
            "title": "Width"
          },
          "height": {
            "type": "integer",
            "title": "Height"
          }
        },
        "type": "object",
        "required": [
          "width",
          "height"
        ],
        "title": "ImageInfo"
      },
      "Label": {
        "type": "string",
        "enum": [
          "comfort",
          "discomfort"
        ],
        "title": "Label"
      },
      "ModelInfo": {
        "properties": {
          "id": {
            "type": "string",
            "title": "Id",
            "description": "Model identifier, e.g. bc-v1."
          },
          "version": {
            "type": "string",
            "title": "Version",
            "description": "Service version from manifest.yaml."
          }
        },
        "type": "object",
        "required": [
          "id",
          "version"
        ],
        "title": "ModelInfo"
      },
      "PredictRequest": {
        "properties": {
          "image": {
            "type": "string",
            "minLength": 1,
            "title": "Image",
            "description": "Base64-encoded image. An optional data-URI prefix (data:image/jpeg;base64,) is accepted."
          },
          "threshold": {
            "anyOf": [
              {
                "type": "number",
                "maximum": 1.0,
                "minimum": 0.0
              },
              {
                "type": "null"
              }
            ],
            "title": "Threshold",
            "description": "Discomfort threshold override (0.0-1.0). Defaults to the model's trained threshold."
          }
        },
        "type": "object",
        "required": [
          "image"
        ],
        "title": "PredictRequest"
      },
      "PredictResponse": {
        "properties": {
          "request_id": {
            "type": "string",
            "title": "Request Id"
          },
          "model": {
            "$ref": "#/components/schemas/ModelInfo"
          },
          "detection": {
            "$ref": "#/components/schemas/Detection"
          },
          "prediction": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/Prediction"
              },
              {
                "type": "null"
              }
            ],
            "description": "Present only when detection is 'detected'."
          },
          "cat": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/Cat"
              },
              {
                "type": "null"
              }
            ],
            "description": "Detected cat, or best candidate when detection is 'low_confidence'."
          },
          "image": {
            "$ref": "#/components/schemas/ImageInfo"
          }
        },
        "type": "object",
        "required": [
          "request_id",
          "model",
          "detection",
          "prediction",
          "cat",
          "image"
        ],
        "title": "PredictResponse"
      },
      "Prediction": {
        "properties": {
          "label": {
            "allOf": [
              {
                "$ref": "#/components/schemas/Label"
              }
            ],
            "description": "discomfort when score > threshold, otherwise comfort."
          },
          "score": {
            "type": "number",
            "title": "Score",
            "description": "Probability of discomfort (0.0-1.0)."
          },
          "threshold": {
            "type": "number",
            "title": "Threshold",
            "description": "Threshold applied to score."
          }
        },
        "type": "object",
        "required": [
          "label",
          "score",
          "threshold"
        ],
        "title": "Prediction"
      }
    },
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "description": "API key issued by Sylvester.",
        "in": "header",
        "name": "api-key"
      }
    }
  }
}
