mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
package vision
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/ai/classify"
|
||||
"github.com/photoprism/photoprism/pkg/clean"
|
||||
)
|
||||
|
||||
// ApiResponse represents a Vision API service response.
|
||||
type ApiResponse struct {
|
||||
Id string `yaml:"Id,omitempty" json:"id,omitempty"`
|
||||
Model *Model `yaml:"Model,omitempty" json:"model"`
|
||||
Result *ApiResult `yaml:"Result,omitempty" json:"result"`
|
||||
Code int `yaml:"Code,omitempty" json:"code,omitempty"`
|
||||
Error string `yaml:"Error,omitempty" json:"error,omitempty"`
|
||||
Model *Model `yaml:"Model,omitempty" json:"model,omitempty"`
|
||||
Result *ApiResult `yaml:"Result,omitempty" json:"result,omitempty"`
|
||||
}
|
||||
|
||||
// ApiResult represents the model response(s) to a Vision API service
|
||||
@@ -33,6 +38,15 @@ type LabelResult struct {
|
||||
Topicality float32 `yaml:"Topicality,omitempty" json:"topicality,omitempty"`
|
||||
}
|
||||
|
||||
// NewApiError generates a Vision API error response based on the specified HTTP status code.
|
||||
func NewApiError(id string, code int) ApiResponse {
|
||||
return ApiResponse{
|
||||
Id: clean.Type(id),
|
||||
Code: code,
|
||||
Error: http.StatusText(code),
|
||||
}
|
||||
}
|
||||
|
||||
// NewLabelsResponse generates a new Vision API image classification service response.
|
||||
func NewLabelsResponse(id string, model *Model, results classify.Labels) ApiResponse {
|
||||
if model == nil {
|
||||
@@ -46,7 +60,8 @@ func NewLabelsResponse(id string, model *Model, results classify.Labels) ApiResp
|
||||
}
|
||||
|
||||
return ApiResponse{
|
||||
Id: id,
|
||||
Id: clean.Type(id),
|
||||
Code: http.StatusOK,
|
||||
Model: model,
|
||||
Result: &ApiResult{Labels: labels},
|
||||
}
|
||||
|
||||
@@ -7604,6 +7604,12 @@
|
||||
1000000,
|
||||
1000000000,
|
||||
60000000000,
|
||||
3600000000000,
|
||||
1,
|
||||
1000,
|
||||
1000000,
|
||||
1000000000,
|
||||
60000000000,
|
||||
3600000000000
|
||||
],
|
||||
"x-enum-varnames": [
|
||||
@@ -7622,6 +7628,12 @@
|
||||
"Millisecond",
|
||||
"Second",
|
||||
"Minute",
|
||||
"Hour",
|
||||
"Nanosecond",
|
||||
"Microsecond",
|
||||
"Millisecond",
|
||||
"Second",
|
||||
"Minute",
|
||||
"Hour"
|
||||
]
|
||||
},
|
||||
@@ -7651,6 +7663,12 @@
|
||||
"vision.ApiResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {
|
||||
"type": "integer"
|
||||
},
|
||||
"error": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
@@ -32,17 +32,19 @@ func PostVisionCaption(router *gin.RouterGroup) {
|
||||
|
||||
// Assign and validate request form values.
|
||||
if err := c.BindJSON(&request); err != nil {
|
||||
AbortBadRequest(c)
|
||||
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
// Generate Vision API service response.
|
||||
response := vision.ApiResponse{
|
||||
Id: request.GetId(),
|
||||
Model: &vision.Model{Name: "Caption", Version: "Test", Resolution: 224},
|
||||
Result: &vision.ApiResult{Caption: &vision.CaptionResult{Text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", Confidence: 0.42424}},
|
||||
Code: http.StatusNotImplemented,
|
||||
Error: http.StatusText(http.StatusNotImplemented),
|
||||
Model: &vision.Model{Name: "Caption"},
|
||||
Result: &vision.ApiResult{Caption: &vision.CaptionResult{Text: "This is a test.", Confidence: 0.14159265359}},
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
c.JSON(http.StatusNotImplemented, response)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,17 +32,19 @@ func PostVisionFaces(router *gin.RouterGroup) {
|
||||
|
||||
// Assign and validate request form values.
|
||||
if err := c.BindJSON(&request); err != nil {
|
||||
AbortBadRequest(c)
|
||||
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
// Generate Vision API service response.
|
||||
response := vision.ApiResponse{
|
||||
Id: request.GetId(),
|
||||
Model: &vision.Model{Name: "Faces", Version: "Test", Resolution: 224},
|
||||
Result: &vision.ApiResult{Faces: &[]string{}},
|
||||
Code: http.StatusNotImplemented,
|
||||
Error: http.StatusText(http.StatusNotImplemented),
|
||||
Model: &vision.Model{Name: "Faces"},
|
||||
Result: &vision.ApiResult{},
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
c.JSON(http.StatusNotImplemented, response)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func PostVisionLabels(router *gin.RouterGroup) {
|
||||
|
||||
// Assign and validate request form values.
|
||||
if err := c.BindJSON(&request); err != nil {
|
||||
AbortBadRequest(c)
|
||||
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func PostVisionLabels(router *gin.RouterGroup) {
|
||||
labels, err := vision.Labels(request.Images)
|
||||
|
||||
if err != nil {
|
||||
AbortBadRequest(c)
|
||||
c.JSON(http.StatusBadRequest, vision.NewApiError(request.GetId(), http.StatusBadRequest))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user