chore: added config file and fixed updates not showing while training

This commit is contained in:
Andre Henriques 2024-04-08 15:47:31 +01:00
parent de0b430467
commit 2faf90f462
9 changed files with 91 additions and 50 deletions

2
config.toml Normal file
View File

@ -0,0 +1,2 @@
PORT=5002
HOSTNAME="https://testing.andr3h3nriqu3s.com"

1
go.mod
View File

@ -12,6 +12,7 @@ require (
)
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=

View File

@ -244,6 +244,7 @@ func trainDefinition(c *Context, model *BaseModel, definition_id string, load_pr
"SaveModelPath": path.Join(getDir(), result_path),
"Depth": classCount,
"StartPoint": 0,
"Host": (*c.Handle).Config.Hostname,
}); err != nil {
return
}
@ -500,6 +501,7 @@ func trainDefinitionExpandExp(c *Context, model *BaseModel, definition_id string
"SaveModelPath": path.Join(getDir(), result_path, "head", exp.Id),
"Depth": classCount,
"StartPoint": 0,
"Host": (*c.Handle).Config.Hostname,
}); err != nil {
return
}
@ -648,6 +650,7 @@ func trainDefinitionExp(c *Context, model *BaseModel, definition_id string, load
"SaveModelPath": path.Join(getDir(), result_path),
"Depth": classCount,
"StartPoint": 0,
"Host": (*c.Handle).Config.Hostname,
}); err != nil {
return
}

32
logic/utils/config.go Normal file
View File

@ -0,0 +1,32 @@
package utils
import (
"os"
toml "github.com/BurntSushi/toml"
"github.com/charmbracelet/log"
)
type Config struct {
Hostname string
Port int
}
func LoadConfig() Config {
log.Info("Loading the config file")
dat, err := os.ReadFile("./config.toml")
if err != nil {
log.Error("Failed to load config file", "err", err)
// Use default values
return Config{
Hostname: "localhost",
Port: 8000,
}
}
var conf Config
_, err = toml.Decode(string(dat), &conf)
return conf
}

View File

@ -42,6 +42,7 @@ type Handle struct {
gets []HandleFunc
posts []HandleFunc
deletes []HandleFunc
Config Config
}
func decodeBody(r *http.Request) (string, *Error) {
@ -138,6 +139,7 @@ type Context struct {
R *http.Request
Tx *sql.Tx
ShowMessage bool
Handle *Handle
}
func (c Context) Prepare(str string) (*sql.Stmt, error) {
@ -320,6 +322,7 @@ func (x Handle) createContext(handler *Handle, r *http.Request, w http.ResponseW
Writer: w,
R: r,
ShowMessage: true,
Handle: &x,
}, nil
}
@ -328,7 +331,7 @@ func (x Handle) createContext(handler *Handle, r *http.Request, w http.ResponseW
return nil, errors.Join(err, LogoffError)
}
return &Context{token, user, logger, handler.Db, w, r, nil, true}, nil
return &Context{token, user, logger, handler.Db, w, r, nil, true, &x}, nil
}
func contextlessLogoff(w http.ResponseWriter) {
@ -468,12 +471,12 @@ func (x Handle) ReadTypesFilesApi(pathTest string, baseFilePath string, fileType
})
}
func NewHandler(db *sql.DB) *Handle {
func NewHandler(db *sql.DB, config Config) *Handle {
var gets []HandleFunc
var posts []HandleFunc
var deletes []HandleFunc
x := &Handle{db, gets, posts, deletes}
x := &Handle{db, gets, posts, deletes, config}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -482,11 +485,11 @@ func NewHandler(db *sql.DB) *Handle {
w.Header().Add("Access-Control-Allow-Methods", "*")
// Decide answertype
if !(r.Header.Get("content-type") == "application/json" || r.Header.Get("response-type") == "application/json") {
/* if !(r.Header.Get("content-type") == "application/json" || r.Header.Get("response-type") == "application/json") {
w.WriteHeader(500)
w.Write([]byte("Please set content-type to application/json or set response-type to application/json\n"))
return
}
}*/
if !strings.HasPrefix(r.URL.Path, "/api") {
w.WriteHeader(404)
@ -528,10 +531,5 @@ func NewHandler(db *sql.DB) *Handle {
func (x Handle) Startup() {
log.Info("Starting up!\n")
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", x.Config.Port), nil))
}

View File

@ -33,8 +33,11 @@ func main() {
defer db.Close()
log.Info("Starting server on :5002!")
config := LoadConfig()
log.Info("Config loaded!", "config", config)
//TODO check if file structure exists to save data
handle := NewHandler(db)
handle := NewHandler(db, config)
// TODO remove this before commiting
_, err = db.Exec("update models set status=$1 where status=$2", models_utils.FAILED_TRAINING, models_utils.TRAINING)

View File

@ -9,9 +9,9 @@ import requests
class NotifyServerCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, log, *args, **kwargs):
{{ if .HeadId }}
requests.get(f'http://localhost:8000/api/model/head/epoch/update?epoch={epoch + 1}&accuracy={log["accuracy"]}&head_id={{.HeadId}}')
requests.get(f'{{ .Host }}/api/model/head/epoch/update?epoch={epoch + 1}&accuracy={log["accuracy"]}&head_id={{.HeadId}}')
{{ else }}
requests.get(f'http://localhost:8000/api/model/epoch/update?model_id={{.Model.Id}}&epoch={epoch + 1}&accuracy={log["accuracy"]}&definition={{.DefId}}')
requests.get(f'{{ .Host }}/api/model/epoch/update?model_id={{.Model.Id}}&epoch={epoch + 1}&accuracy={log["accuracy"]}&definition={{.DefId}}')
{{end}}

View File

@ -10,7 +10,7 @@ import numpy as np
class NotifyServerCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, log, *args, **kwargs):
requests.get(f'http://localhost:8000/api/model/head/epoch/update?epoch={epoch + 1}&accuracy={log["accuracy"]}&head_id={{.HeadId}}')
requests.get(f'{{ .Host }}/api/model/head/epoch/update?epoch={epoch + 1}&accuracy={log["accuracy"]}&head_id={{.HeadId}}')
DATA_DIR = "{{ .DataDir }}"