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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package flux
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"within.website/x/web"
)
// Struct definitions based on the OpenAPI schema
type Input struct {
Prompt string `json:"prompt"`
Image string `json:"image,omitempty"`
AspectRatio string `json:"aspect_ratio,omitempty"`
NumOutputs int `json:"num_outputs"`
GuidanceScale float64 `json:"guidance_scale"`
MaxSequenceLength int `json:"max_sequence_length"`
NumInferenceSteps int `json:"num_inference_steps"`
PromptStrength float64 `json:"prompt_strength"`
Seed *int `json:"seed,omitempty"`
OutputFormat string `json:"output_format"`
OutputQuality int `json:"output_quality"`
}
type Output []string
type PredictionRequest struct {
Input Input `json:"input"`
ID string `json:"id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
OutputFilePrefix string `json:"output_file_prefix,omitempty"`
Webhook string `json:"webhook,omitempty"`
WebhookEventsFilter []string `json:"webhook_events_filter,omitempty"`
}
type PredictionResponse struct {
Input Input `json:"input"`
Output Output `json:"output"`
ID string `json:"id"`
Version string `json:"version"`
CreatedAt string `json:"created_at"`
StartedAt string `json:"started_at"`
CompletedAt string `json:"completed_at"`
Logs string `json:"logs"`
Error string `json:"error"`
Status string `json:"status"`
Metrics map[string]interface{} `json:"metrics"`
}
type HTTPValidationError struct {
Detail []ValidationError `json:"detail"`
}
type ValidationError struct {
Loc []interface{} `json:"loc"`
Msg string `json:"msg"`
Type string `json:"type"`
}
// HealthCheckResponse represents the response structure for the health check endpoint.
type HealthCheckResponse struct {
Status string `json:"status"`
}
// Client struct
type Client struct {
BaseURL string
HTTPClient *http.Client
}
// NewClient creates a new API client
func NewClient(baseURL string) *Client {
return &Client{
BaseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Minute},
}
}
// Methods to interact with the API endpoints
func (c *Client) Predict(predictionReq PredictionRequest) (*PredictionResponse, error) {
body, err := json.Marshal(predictionReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/predictions", c.BaseURL), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var predictionResp PredictionResponse
err = json.NewDecoder(resp.Body).Decode(&predictionResp)
if err != nil {
return nil, err
}
return &predictionResp, nil
}
func (c *Client) PredictIdempotent(predictionID string, predictionReq PredictionRequest) (*PredictionResponse, error) {
body, err := json.Marshal(predictionReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/predictions/%s", c.BaseURL, predictionID), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var predictionResp PredictionResponse
err = json.NewDecoder(resp.Body).Decode(&predictionResp)
if err != nil {
return nil, err
}
return &predictionResp, nil
}
func (c *Client) CancelPrediction(predictionID string) (*http.Response, error) {
req, err := http.NewRequest("POST", fmt.Sprintf("%s/predictions/%s/cancel", c.BaseURL, predictionID), nil)
if err != nil {
return nil, err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
return resp, nil
}
// HealthCheck checks the health of the service
func (c *Client) HealthCheck() (*HealthCheckResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/health-check", c.BaseURL), nil)
if err != nil {
return nil, err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(resp.StatusCode, resp)
}
var healthResp HealthCheckResponse
err = json.NewDecoder(resp.Body).Decode(&healthResp)
if err != nil {
return nil, err
}
return &healthResp, nil
}
|