-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.hs
More file actions
130 lines (112 loc) · 4.01 KB
/
Copy pathapi_server.hs
File metadata and controls
130 lines (112 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module APIServer where
import qualified Network.Wai as Wai
import qualified Network.Wai.Handler.Warp as Warp
import qualified Network.HTTP.Types as HTTP
import Data.Aeson (FromJSON, ToJSON, encode, decode)
import GHC.Generics
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Pipeline as P
import qualified Genetics as GA
-- | API request types
data TrainRequest = TrainRequest
{ configPath :: String
, epochs :: Int
} deriving (Show, Generic)
instance FromJSON TrainRequest
instance ToJSON TrainRequest
data PredictRequest = PredictRequest
{ modelPath :: String
, dataPath :: String
} deriving (Show, Generic)
instance FromJSON PredictRequest
instance ToJSON PredictRequest
data EvolveRequest = EvolveRequest
{ config :: String
, generations :: Int
} deriving (Show, Generic)
instance FromJSON EvolveRequest
instance ToJSON EvolveRequest
-- | API response types
data APIResponse = APIResponse
{ status :: String
, message :: String
, data_ :: Maybe String
} deriving (Show, Generic)
instance FromJSON APIResponse
instance ToJSON APIResponse
-- | Start the REST API server
startServer :: Int -> IO ()
startServer port = do
putStrLn $ "Starting NeuralForge API server on port " ++ show port
putStrLn "Available endpoints:"
putStrLn " POST /api/train - Train a new model"
putStrLn " POST /api/predict - Make predictions"
putStrLn " POST /api/evolve - Evolve architecture"
putStrLn " GET /api/health - Health check"
Warp.run port application
-- | Main application handler
application :: Wai.Application
application request respond = do
let path = Wai.pathInfo request
method = Wai.requestMethod request
case (method, path) of
("GET", ["api", "health"]) ->
respond $ jsonResponse HTTP.status200 $ APIResponse
{ status = "ok"
, message = "NeuralForge API is running"
, data_ = Nothing
}
("POST", ["api", "train"]) -> do
body <- Wai.strictRequestBody request
case decode body :: Maybe TrainRequest of
Nothing -> respond $ jsonResponse HTTP.status400 $ APIResponse
{ status = "error"
, message = "Invalid request body"
, data_ = Nothing
}
Just req -> do
-- Run training asynchronously
respond $ jsonResponse HTTP.status200 $ APIResponse
{ status = "success"
, message = "Training started"
, data_ = Just $ "Training with " ++ show (epochs req) ++ " epochs"
}
("POST", ["api", "predict"]) -> do
body <- Wai.strictRequestBody request
case decode body :: Maybe PredictRequest of
Nothing -> respond $ jsonResponse HTTP.status400 $ APIResponse
{ status = "error"
, message = "Invalid request body"
, data_ = Nothing
}
Just req -> do
respond $ jsonResponse HTTP.status200 $ APIResponse
{ status = "success"
, message = "Predictions generated"
, data_ = Just $ "Model: " ++ modelPath req
}
("POST", ["api", "evolve"]) -> do
body <- Wai.strictRequestBody request
case decode body :: Maybe EvolveRequest of
Nothing -> respond $ jsonResponse HTTP.status400 $ APIResponse
{ status = "error"
, message = "Invalid request body"
, data_ = Nothing
}
Just req -> do
respond $ jsonResponse HTTP.status200 $ APIResponse
{ status = "success"
, message = "Evolution started"
, data_ = Just $ "Running " ++ show (generations req) ++ " generations"
}
_ -> respond $ jsonResponse HTTP.status404 $ APIResponse
{ status = "error"
, message = "Endpoint not found"
, data_ = Nothing
}
-- | Helper to create JSON responses
jsonResponse :: HTTP.Status -> APIResponse -> Wai.Response
jsonResponse status resp =
Wai.responseLBS status [("Content-Type", "application/json")] (encode resp)