feat: removed non svelte front page
This commit is contained in:
@@ -2,9 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
@@ -118,143 +116,14 @@ func runModelExp(c *Context, model *BaseModel, def_id string, inputImage *tf.Ten
|
||||
}
|
||||
|
||||
func handleRun(handle *Handle) {
|
||||
handle.Post("/models/run", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
||||
if !CheckAuthLevel(1, w, r, c) {
|
||||
handle.Post("/models/run", func(c *Context) *Error {
|
||||
if !c.CheckAuthLevel(1) {
|
||||
return nil
|
||||
}
|
||||
if c.Mode == JSON {
|
||||
|
||||
read_form, err := r.MultipartReader()
|
||||
if err != nil {
|
||||
// TODO improve message
|
||||
return ErrorCode(nil, 400, nil)
|
||||
}
|
||||
|
||||
var id string
|
||||
var file []byte
|
||||
|
||||
for {
|
||||
part, err_part := read_form.NextPart()
|
||||
if err_part == io.EOF {
|
||||
break
|
||||
} else if err_part != nil {
|
||||
return c.JsonBadRequest("Invalid multipart data")
|
||||
}
|
||||
if part.FormName() == "id" {
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(part)
|
||||
id = buf.String()
|
||||
}
|
||||
if part.FormName() == "file" {
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(part)
|
||||
file = buf.Bytes()
|
||||
}
|
||||
}
|
||||
|
||||
model, err := GetBaseModel(handle.Db, id)
|
||||
if err == ModelNotFoundError {
|
||||
return c.JsonBadRequest("Models not found")
|
||||
} else if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
if model.Status != READY {
|
||||
return c.JsonBadRequest("Model not ready to run images")
|
||||
}
|
||||
|
||||
def := JustId{}
|
||||
err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id)
|
||||
if err == NotFoundError {
|
||||
return c.JsonBadRequest("Could not find definition")
|
||||
} else if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
def_id := def.Id
|
||||
|
||||
// TODO create a database table with tasks
|
||||
run_path := path.Join("/tmp", model.Id, "runs")
|
||||
os.MkdirAll(run_path, os.ModePerm)
|
||||
img_path := path.Join(run_path, "img."+model.Format)
|
||||
|
||||
img_file, err := os.Create(img_path)
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
defer img_file.Close()
|
||||
img_file.Write(file)
|
||||
|
||||
if !testImgForModel(c, model, img_path) {
|
||||
return c.JsonBadRequest("Provided image does not match the model")
|
||||
}
|
||||
|
||||
root := tg.NewRoot()
|
||||
|
||||
var tf_img *image.Image = nil
|
||||
|
||||
switch model.Format {
|
||||
case "png":
|
||||
tf_img = ReadPNG(root, img_path, int64(model.ImageMode))
|
||||
case "jpeg":
|
||||
tf_img = ReadJPG(root, img_path, int64(model.ImageMode))
|
||||
default:
|
||||
panic("Not sure what to do with '" + model.Format + "'")
|
||||
}
|
||||
|
||||
exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{})
|
||||
inputImage, err := tf.NewTensor(exec_results[0].Value())
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
vi := -1
|
||||
var confidence float32 = 0
|
||||
|
||||
if model.ModelType == 2 {
|
||||
c.Logger.Info("Running model normal", "model", model.Id, "def", def_id)
|
||||
vi, confidence, err = runModelExp(c, model, def_id, inputImage)
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
} else {
|
||||
c.Logger.Info("Running model normal", "model", model.Id, "def", def_id)
|
||||
vi, confidence, err = runModelNormal(c, model, def_id, inputImage)
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
}
|
||||
|
||||
os.RemoveAll(run_path)
|
||||
|
||||
rows, err := handle.Db.Query("select name from model_classes where model_id=$1 and class_order=$2;", model.Id, vi)
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
if !rows.Next() {
|
||||
return c.SendJSON(nil)
|
||||
}
|
||||
|
||||
var name string
|
||||
if err = rows.Scan(&name); err != nil {
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
returnValue := struct {
|
||||
Class string `json:"class"`
|
||||
Confidence float32 `json:"confidence"`
|
||||
}{
|
||||
Class: name,
|
||||
Confidence: confidence,
|
||||
}
|
||||
|
||||
return c.SendJSON(returnValue)
|
||||
}
|
||||
|
||||
read_form, err := r.MultipartReader()
|
||||
read_form, err := c.R.MultipartReader()
|
||||
if err != nil {
|
||||
// TODO improve message
|
||||
return ErrorCode(nil, 400, nil)
|
||||
return c.JsonBadRequest("Invalid muilpart body")
|
||||
}
|
||||
|
||||
var id string
|
||||
@@ -265,7 +134,7 @@ func handleRun(handle *Handle) {
|
||||
if err_part == io.EOF {
|
||||
break
|
||||
} else if err_part != nil {
|
||||
return &Error{Code: http.StatusBadRequest}
|
||||
return c.JsonBadRequest("Invalid multipart data")
|
||||
}
|
||||
if part.FormName() == "id" {
|
||||
buf := new(bytes.Buffer)
|
||||
@@ -281,27 +150,21 @@ func handleRun(handle *Handle) {
|
||||
|
||||
model, err := GetBaseModel(handle.Db, id)
|
||||
if err == ModelNotFoundError {
|
||||
return ErrorCode(nil, http.StatusNotFound, AnyMap{
|
||||
"NotFoundMessage": "Model not found",
|
||||
"GoBackLink": "/models",
|
||||
})
|
||||
return c.JsonBadRequest("Models not found")
|
||||
} else if err != nil {
|
||||
return Error500(err)
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
if model.Status != READY {
|
||||
// TODO improve this
|
||||
return ErrorCode(nil, 400, c.AddMap(nil))
|
||||
return c.JsonBadRequest("Model not ready to run images")
|
||||
}
|
||||
|
||||
def := JustId{}
|
||||
err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id)
|
||||
if err == NotFoundError {
|
||||
// TODO improve this
|
||||
fmt.Printf("Could not find definition\n")
|
||||
return ErrorCode(nil, 400, c.AddMap(nil))
|
||||
return c.JsonBadRequest("Could not find definition")
|
||||
} else if err != nil {
|
||||
return Error500(err)
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
def_id := def.Id
|
||||
@@ -313,19 +176,13 @@ func handleRun(handle *Handle) {
|
||||
|
||||
img_file, err := os.Create(img_path)
|
||||
if err != nil {
|
||||
return Error500(err)
|
||||
return c.Error500(err)
|
||||
}
|
||||
defer img_file.Close()
|
||||
img_file.Write(file)
|
||||
|
||||
if !testImgForModel(c, model, img_path) {
|
||||
LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{
|
||||
"Model": model,
|
||||
"NotFound": false,
|
||||
"Result": nil,
|
||||
"ImageError": true,
|
||||
}))
|
||||
return nil
|
||||
return c.JsonBadRequest("Provided image does not match the model")
|
||||
}
|
||||
|
||||
root := tg.NewRoot()
|
||||
@@ -344,7 +201,7 @@ func handleRun(handle *Handle) {
|
||||
exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{})
|
||||
inputImage, err := tf.NewTensor(exec_results[0].Value())
|
||||
if err != nil {
|
||||
return Error500(err)
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
vi := -1
|
||||
@@ -368,27 +225,25 @@ func handleRun(handle *Handle) {
|
||||
|
||||
rows, err := handle.Db.Query("select name from model_classes where model_id=$1 and class_order=$2;", model.Id, vi)
|
||||
if err != nil {
|
||||
return Error500(err)
|
||||
return c.Error500(err)
|
||||
}
|
||||
if !rows.Next() {
|
||||
LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{
|
||||
"Model": model,
|
||||
"NotFound": true,
|
||||
"Result": nil,
|
||||
"Confidence": confidence,
|
||||
}))
|
||||
return nil
|
||||
return c.SendJSON(nil)
|
||||
}
|
||||
|
||||
var name string
|
||||
if err = rows.Scan(&name); err != nil {
|
||||
return nil
|
||||
return c.Error500(err)
|
||||
}
|
||||
|
||||
LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{
|
||||
"Model": model,
|
||||
"Result": name,
|
||||
}))
|
||||
return nil
|
||||
returnValue := struct {
|
||||
Class string `json:"class"`
|
||||
Confidence float32 `json:"confidence"`
|
||||
}{
|
||||
Class: name,
|
||||
Confidence: confidence,
|
||||
}
|
||||
|
||||
return c.SendJSON(returnValue)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user