|
| 1 | +// Package deeplearning provides Go client for PyTorch Deep Learning Service |
| 2 | +package deeplearning |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// Client is the deep learning service client |
| 14 | +type Client struct { |
| 15 | + baseURL string |
| 16 | + httpClient *http.Client |
| 17 | +} |
| 18 | + |
| 19 | +// NewClient creates a new deep learning service client |
| 20 | +func NewClient(baseURL string) *Client { |
| 21 | + return &Client{ |
| 22 | + baseURL: baseURL, |
| 23 | + httpClient: &http.Client{ |
| 24 | + Timeout: 30 * time.Second, |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// ModelType represents the type of deep learning model |
| 30 | +type ModelType string |
| 31 | + |
| 32 | +const ( |
| 33 | + ModelTypeAutoencoder ModelType = "autoencoder" |
| 34 | + ModelTypeLSTMAutoencoder ModelType = "lstm_autoencoder" |
| 35 | +) |
| 36 | + |
| 37 | +// ModelConfig holds configuration for model creation |
| 38 | +type ModelConfig struct { |
| 39 | + InputDim int `json:"input_dim"` |
| 40 | + LatentDim int `json:"latent_dim,omitempty"` |
| 41 | + HiddenDim int `json:"hidden_dim,omitempty"` |
| 42 | + HiddenDims []int `json:"hidden_dims,omitempty"` |
| 43 | + NumLayers int `json:"num_layers,omitempty"` |
| 44 | + Bidirectional bool `json:"bidirectional,omitempty"` |
| 45 | + LearningRate float64 `json:"learning_rate,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// TrainingParams holds training parameters |
| 49 | +type TrainingParams struct { |
| 50 | + Epochs int `json:"epochs,omitempty"` |
| 51 | + BatchSize int `json:"batch_size,omitempty"` |
| 52 | + ValidationSplit float64 `json:"validation_split,omitempty"` |
| 53 | + EarlyStoppingPatience int `json:"early_stopping_patience,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// TrainRequest is the request for training a model |
| 57 | +type TrainRequest struct { |
| 58 | + ModelType ModelType `json:"model_type"` |
| 59 | + Config ModelConfig `json:"config"` |
| 60 | + TrainingData [][]float64 `json:"training_data"` |
| 61 | + TrainingParams TrainingParams `json:"training_params,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +// TrainResponse is the response from training |
| 65 | +type TrainResponse struct { |
| 66 | + Status string `json:"status"` |
| 67 | + ModelName string `json:"model_name"` |
| 68 | + ModelType string `json:"model_type"` |
| 69 | + ModelPath string `json:"model_path"` |
| 70 | + Threshold *float64 `json:"threshold"` |
| 71 | + TrainLosses []float64 `json:"train_losses"` |
| 72 | + ValLosses []float64 `json:"val_losses"` |
| 73 | +} |
| 74 | + |
| 75 | +// LoadRequest is the request for loading a model |
| 76 | +type LoadRequest struct { |
| 77 | + ModelType string `json:"model_type"` |
| 78 | + ModelPath string `json:"model_path,omitempty"` |
| 79 | +} |
| 80 | + |
| 81 | +// LoadResponse is the response from loading a model |
| 82 | +type LoadResponse struct { |
| 83 | + Status string `json:"status"` |
| 84 | + ModelName string `json:"model_name"` |
| 85 | + ModelType string `json:"model_type"` |
| 86 | + Threshold *float64 `json:"threshold"` |
| 87 | +} |
| 88 | + |
| 89 | +// PredictRequest is the request for making predictions |
| 90 | +type PredictRequest struct { |
| 91 | + Data interface{} `json:"data"` // [][]float64 or [][][]float64 for sequences |
| 92 | + ReturnProba bool `json:"return_proba,omitempty"` |
| 93 | +} |
| 94 | + |
| 95 | +// PredictResponse is the response from predictions |
| 96 | +type PredictResponse struct { |
| 97 | + Predictions []float64 `json:"predictions"` |
| 98 | + ReconstructionError []float64 `json:"reconstruction_errors"` |
| 99 | + Threshold *float64 `json:"threshold"` |
| 100 | + NumAnomalies int `json:"num_anomalies"` |
| 101 | +} |
| 102 | + |
| 103 | +// EvaluateRequest is the request for model evaluation |
| 104 | +type EvaluateRequest struct { |
| 105 | + Data [][]float64 `json:"data"` |
| 106 | + Labels []int `json:"labels"` |
| 107 | +} |
| 108 | + |
| 109 | +// EvaluateResponse is the response from evaluation |
| 110 | +type EvaluateResponse struct { |
| 111 | + Accuracy float64 `json:"accuracy"` |
| 112 | + Precision float64 `json:"precision"` |
| 113 | + Recall float64 `json:"recall"` |
| 114 | + F1Score float64 `json:"f1_score"` |
| 115 | + ROCAUC float64 `json:"roc_auc,omitempty"` |
| 116 | + ConfusionMatrix map[string]int `json:"confusion_matrix"` |
| 117 | +} |
| 118 | + |
| 119 | +// HealthResponse is the response from health check |
| 120 | +type HealthResponse struct { |
| 121 | + Status string `json:"status"` |
| 122 | + LoadedModels []string `json:"loaded_models"` |
| 123 | +} |
| 124 | + |
| 125 | +// Health checks if the service is healthy |
| 126 | +func (c *Client) Health() (*HealthResponse, error) { |
| 127 | + resp, err := c.httpClient.Get(c.baseURL + "/health") |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("health check failed: %w", err) |
| 130 | + } |
| 131 | + defer resp.Body.Close() |
| 132 | + |
| 133 | + if resp.StatusCode != http.StatusOK { |
| 134 | + body, _ := io.ReadAll(resp.Body) |
| 135 | + return nil, fmt.Errorf("health check failed with status %d: %s", resp.StatusCode, string(body)) |
| 136 | + } |
| 137 | + |
| 138 | + var result HealthResponse |
| 139 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 140 | + return nil, fmt.Errorf("failed to decode health response: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + return &result, nil |
| 144 | +} |
| 145 | + |
| 146 | +// Train trains a new model |
| 147 | +func (c *Client) Train(modelName string, req TrainRequest) (*TrainResponse, error) { |
| 148 | + body, err := json.Marshal(req) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + resp, err := c.httpClient.Post( |
| 154 | + fmt.Sprintf("%s/models/%s/train", c.baseURL, modelName), |
| 155 | + "application/json", |
| 156 | + bytes.NewBuffer(body), |
| 157 | + ) |
| 158 | + if err != nil { |
| 159 | + return nil, fmt.Errorf("train request failed: %w", err) |
| 160 | + } |
| 161 | + defer resp.Body.Close() |
| 162 | + |
| 163 | + if resp.StatusCode != http.StatusOK { |
| 164 | + body, _ := io.ReadAll(resp.Body) |
| 165 | + return nil, fmt.Errorf("train failed with status %d: %s", resp.StatusCode, string(body)) |
| 166 | + } |
| 167 | + |
| 168 | + var result TrainResponse |
| 169 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 170 | + return nil, fmt.Errorf("failed to decode train response: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + return &result, nil |
| 174 | +} |
| 175 | + |
| 176 | +// Load loads a trained model |
| 177 | +func (c *Client) Load(modelName string, req LoadRequest) (*LoadResponse, error) { |
| 178 | + body, err := json.Marshal(req) |
| 179 | + if err != nil { |
| 180 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 181 | + } |
| 182 | + |
| 183 | + resp, err := c.httpClient.Post( |
| 184 | + fmt.Sprintf("%s/models/%s/load", c.baseURL, modelName), |
| 185 | + "application/json", |
| 186 | + bytes.NewBuffer(body), |
| 187 | + ) |
| 188 | + if err != nil { |
| 189 | + return nil, fmt.Errorf("load request failed: %w", err) |
| 190 | + } |
| 191 | + defer resp.Body.Close() |
| 192 | + |
| 193 | + if resp.StatusCode != http.StatusOK { |
| 194 | + body, _ := io.ReadAll(resp.Body) |
| 195 | + return nil, fmt.Errorf("load failed with status %d: %s", resp.StatusCode, string(body)) |
| 196 | + } |
| 197 | + |
| 198 | + var result LoadResponse |
| 199 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 200 | + return nil, fmt.Errorf("failed to decode load response: %w", err) |
| 201 | + } |
| 202 | + |
| 203 | + return &result, nil |
| 204 | +} |
| 205 | + |
| 206 | +// Predict makes predictions with a loaded model |
| 207 | +func (c *Client) Predict(modelName string, req PredictRequest) (*PredictResponse, error) { |
| 208 | + body, err := json.Marshal(req) |
| 209 | + if err != nil { |
| 210 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 211 | + } |
| 212 | + |
| 213 | + resp, err := c.httpClient.Post( |
| 214 | + fmt.Sprintf("%s/models/%s/predict", c.baseURL, modelName), |
| 215 | + "application/json", |
| 216 | + bytes.NewBuffer(body), |
| 217 | + ) |
| 218 | + if err != nil { |
| 219 | + return nil, fmt.Errorf("predict request failed: %w", err) |
| 220 | + } |
| 221 | + defer resp.Body.Close() |
| 222 | + |
| 223 | + if resp.StatusCode != http.StatusOK { |
| 224 | + body, _ := io.ReadAll(resp.Body) |
| 225 | + return nil, fmt.Errorf("predict failed with status %d: %s", resp.StatusCode, string(body)) |
| 226 | + } |
| 227 | + |
| 228 | + var result PredictResponse |
| 229 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 230 | + return nil, fmt.Errorf("failed to decode predict response: %w", err) |
| 231 | + } |
| 232 | + |
| 233 | + return &result, nil |
| 234 | +} |
| 235 | + |
| 236 | +// Evaluate evaluates model performance |
| 237 | +func (c *Client) Evaluate(modelName string, req EvaluateRequest) (*EvaluateResponse, error) { |
| 238 | + body, err := json.Marshal(req) |
| 239 | + if err != nil { |
| 240 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 241 | + } |
| 242 | + |
| 243 | + resp, err := c.httpClient.Post( |
| 244 | + fmt.Sprintf("%s/models/%s/evaluate", c.baseURL, modelName), |
| 245 | + "application/json", |
| 246 | + bytes.NewBuffer(body), |
| 247 | + ) |
| 248 | + if err != nil { |
| 249 | + return nil, fmt.Errorf("evaluate request failed: %w", err) |
| 250 | + } |
| 251 | + defer resp.Body.Close() |
| 252 | + |
| 253 | + if resp.StatusCode != http.StatusOK { |
| 254 | + body, _ := io.ReadAll(resp.Body) |
| 255 | + return nil, fmt.Errorf("evaluate failed with status %d: %s", resp.StatusCode, string(body)) |
| 256 | + } |
| 257 | + |
| 258 | + var result EvaluateResponse |
| 259 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 260 | + return nil, fmt.Errorf("failed to decode evaluate response: %w", err) |
| 261 | + } |
| 262 | + |
| 263 | + return &result, nil |
| 264 | +} |
| 265 | + |
| 266 | +// Unload unloads a model from memory |
| 267 | +func (c *Client) Unload(modelName string) error { |
| 268 | + resp, err := c.httpClient.Post( |
| 269 | + fmt.Sprintf("%s/models/%s/unload", c.baseURL, modelName), |
| 270 | + "application/json", |
| 271 | + nil, |
| 272 | + ) |
| 273 | + if err != nil { |
| 274 | + return fmt.Errorf("unload request failed: %w", err) |
| 275 | + } |
| 276 | + defer resp.Body.Close() |
| 277 | + |
| 278 | + if resp.StatusCode != http.StatusOK { |
| 279 | + body, _ := io.ReadAll(resp.Body) |
| 280 | + return fmt.Errorf("unload failed with status %d: %s", resp.StatusCode, string(body)) |
| 281 | + } |
| 282 | + |
| 283 | + return nil |
| 284 | +} |
0 commit comments