-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
264 lines (223 loc) · 6.24 KB
/
main.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/calvinmclean/babyapi"
"github.com/calvinmclean/babyapi/extensions"
"github.com/calvinmclean/babyapi/html"
"github.com/go-chi/render"
)
const (
replicateURL string = "https://homepage.replicate.com/"
model string = "black-forest-labs/flux-schnell"
version string = "f2ab8a5bfe79f02f0789a146cf5e73d2a4ff2684a98c2b303d1e1ff3814271db"
allResults html.Template = "allResults"
allResultsTemplate string = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Results</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/uikit.min.css"/>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/sse.js"></script>
</head>
<style>
tr.htmx-swapping td {
opacity: 0;
transition: opacity 1s ease-out;
}
.prompt {
font-style: italic;
}
</style>
<body>
<table class="uk-table uk-table-divider uk-margin-left uk-margin-right">
<colgroup>
<col>
<col style="width: 300px;">
</colgroup>
<thead>
<tr>
<th>Prompt</th>
<th></th>
</tr>
</thead>
<tbody hx-ext="sse" sse-connect="/results/listen" sse-swap="newResult" hx-swap="afterend">
<form hx-post="/results" hx-swap="none" hx-on::after-request="this.reset()">
<td>
<input class="uk-input" name="Prompt" type="text">
</td>
<td>
<button type="submit" class="uk-button uk-button-primary">GENERATE</button>
</td>
</form>
{{ range . }}
{{ template "resultRow" . }}
{{ end }}
</tbody>
</table>
</body>
</html>`
resultRow html.Template = "resultRow"
resultRowTemplate string = `<tr hx-target="this" hx-swap="innerHTML">
<td>
{{ range .Images }}<a href="{{ . }}"><img src="{{ . }}" alt="{{ . }}" style="width:300px; height:auto; margin-right:10px;"/></a>{{ end }}
<p class="prompt">{{ .Prompt }}</p>
</td>
<td>
<button class="uk-button uk-button-danger" hx-delete="/results/{{ .ID }}" hx-swap="swap:1s">
Delete
</button>
</td>
</tr>`
)
type PredictionResponse struct {
ID string `json:"id"`
}
type PollResponse struct {
Status string `json:"status"`
Output []string `json:"output"`
Error string `json:"error,omitempty"`
}
type Result struct {
babyapi.DefaultResource
Prompt string
Images []string
}
func (t *Result) HTML(_ http.ResponseWriter, r *http.Request) string {
return resultRow.Render(r, t)
}
type AllResults struct {
babyapi.ResourceList[*Result]
}
func (at AllResults) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
func (at AllResults) HTML(_ http.ResponseWriter, r *http.Request) string {
return allResults.Render(r, at.Items)
}
func createAPI() *babyapi.API[*Result] {
api := babyapi.NewAPI("API", "/results", func() *Result { return &Result{} })
api.AddCustomRootRoute(http.MethodGet, "/", http.RedirectHandler("/results", http.StatusFound))
api.SetGetAllResponseWrapper(func(results []*Result) render.Renderer {
return AllResults{ResourceList: babyapi.ResourceList[*Result]{Items: results}}
})
api.ApplyExtension(extensions.HTMX[*Result]{})
resultChan := api.AddServerSentEventHandler("/listen")
api.SetOnCreateOrUpdate(func(w http.ResponseWriter, r *http.Request, t *Result) *babyapi.ErrResponse {
if r.Method != http.MethodPost {
return nil
}
t.Images = []string{}
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func(prompt string, wg *sync.WaitGroup) error {
defer wg.Done()
pollID, err := getID(prompt)
if err != nil {
errb := new(babyapi.ErrResponse)
errb.Err = err
errb.ErrorText = err.Error()
errb.HTTPStatusCode = http.StatusInternalServerError
return errb
}
if pollID == "" {
errb := new(babyapi.ErrResponse)
errb.Err = errors.New("no ID found in response")
errb.ErrorText = errb.Err.Error()
errb.HTTPStatusCode = http.StatusInternalServerError
return errb
}
image, err := getImageURL(pollID)
if err != nil {
errb := new(babyapi.ErrResponse)
errb.Err = err
errb.ErrorText = err.Error()
errb.HTTPStatusCode = http.StatusInternalServerError
return errb
}
t.Images = append(t.Images, image)
return nil
}(t.Prompt, &wg)
}
wg.Wait()
select {
case resultChan <- &babyapi.ServerSentEvent{Event: "newResult", Data: t.HTML(w, r)}:
default:
logger := babyapi.GetLoggerFromContext(r.Context())
logger.Info("no listeners for server-sent event")
}
return nil
})
html.SetMap(map[string]string{
string(allResults): allResultsTemplate,
string(resultRow): resultRowTemplate,
})
return api
}
func main() {
api := createAPI()
api.RunCLI()
}
func getID(prompt string) (string, error) {
requestBody := fmt.Sprintf(`{
"model": "`+model+`",
"version": "`+version+`",
"input": {
"prompt": "%s"
}
}`, prompt)
response, err := http.Post(replicateURL+"api/prediction", "application/json", bytes.NewBufferString(requestBody))
if err != nil {
return "", err
}
defer response.Body.Close()
var predictionData PredictionResponse
content, _ := io.ReadAll(response.Body)
if err := json.Unmarshal(content, &predictionData); err != nil {
return "", err
}
return predictionData.ID, nil
}
func getImageURL(id string) (string, error) {
timeout := time.NewTimer(60 * time.Second)
ticker := time.NewTicker(5 * time.Second)
defer timeout.Stop()
defer ticker.Stop()
for {
select {
case <-timeout.C:
return "", errors.New("timeout")
case <-ticker.C:
url := fmt.Sprintf("%s/api/poll?id=%s", replicateURL, id)
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
var pollData PollResponse
content, _ := io.ReadAll(response.Body)
if err := json.Unmarshal(content, &pollData); err != nil {
return "", err
}
switch pollData.Status {
case "succeeded":
if len(pollData.Output) > 0 {
return pollData.Output[0], nil
}
return "", errors.New("output array is empty or not available")
case "failed":
return "", fmt.Errorf("prediction failed: %v", pollData.Error)
default:
}
}
}
}