@@ -3,7 +3,9 @@ package routes
3
3
import (
4
4
"fmt"
5
5
"html/template"
6
+ "math"
6
7
"sort"
8
+ "strconv"
7
9
"strings"
8
10
9
11
"github.com/mudler/LocalAI/core/config"
@@ -126,6 +128,8 @@ func RegisterUIRoutes(app *fiber.App,
126
128
// Show the Models page (all models)
127
129
app .Get ("/browse" , func (c * fiber.Ctx ) error {
128
130
term := c .Query ("term" )
131
+ page := c .Query ("page" )
132
+ items := c .Query ("items" )
129
133
130
134
models , _ := gallery .AvailableGalleryModels (appConfig .Galleries , appConfig .ModelPath )
131
135
@@ -164,13 +168,64 @@ func RegisterUIRoutes(app *fiber.App,
164
168
// "ApplicationConfig": appConfig,
165
169
}
166
170
171
+ if page == "" {
172
+ page = "1"
173
+ }
174
+
175
+ if page != "" {
176
+ log .Debug ().Msgf ("page : %+v\n " , page )
177
+ // return a subset of the models
178
+ pageNum , err := strconv .Atoi (page )
179
+ if err != nil {
180
+ return c .Status (fiber .StatusBadRequest ).SendString ("Invalid page number" )
181
+ }
182
+
183
+ if pageNum == 0 {
184
+ return c .Render ("views/models" , summary )
185
+ }
186
+
187
+ itemsNum , err := strconv .Atoi (items )
188
+ if err != nil {
189
+ itemsNum = 21
190
+ }
191
+
192
+ totalPages := int (math .Ceil (float64 (len (models )) / float64 (itemsNum )))
193
+
194
+ models = models .Paginate (pageNum , itemsNum )
195
+
196
+ log .Debug ().Msgf ("number of models : %+v\n " , len (models ))
197
+ prevPage := pageNum - 1
198
+ nextPage := pageNum + 1
199
+ if prevPage < 1 {
200
+ prevPage = 1
201
+ }
202
+ if nextPage > totalPages {
203
+ nextPage = totalPages
204
+ }
205
+ if prevPage != pageNum {
206
+ summary ["PrevPage" ] = prevPage
207
+ }
208
+ summary ["NextPage" ] = nextPage
209
+ summary ["TotalPages" ] = totalPages
210
+ summary ["CurrentPage" ] = pageNum
211
+ summary ["Models" ] = template .HTML (elements .ListModels (models , processingModels , galleryService ))
212
+
213
+ log .Debug ().Msgf ("totalPages : %+v\n " , totalPages )
214
+ log .Debug ().Msgf ("prevPage : %+v\n " , prevPage )
215
+ log .Debug ().Msgf ("nextPage : %+v\n " , nextPage )
216
+ log .Debug ().Msgf ("CurrentPage : %+v\n " , pageNum )
217
+ }
218
+
167
219
// Render index
168
220
return c .Render ("views/models" , summary )
169
221
})
170
222
171
223
// Show the models, filtered from the user input
172
224
// https://htmx.org/examples/active-search/
173
225
app .Post ("/browse/search/models" , func (c * fiber.Ctx ) error {
226
+ page := c .Query ("page" )
227
+ items := c .Query ("items" )
228
+
174
229
form := struct {
175
230
Search string `form:"search"`
176
231
}{}
@@ -180,7 +235,26 @@ func RegisterUIRoutes(app *fiber.App,
180
235
181
236
models , _ := gallery .AvailableGalleryModels (appConfig .Galleries , appConfig .ModelPath )
182
237
183
- return c .SendString (elements .ListModels (gallery .GalleryModels (models ).Search (form .Search ), processingModels , galleryService ))
238
+ if page != "" {
239
+ // return a subset of the models
240
+ pageNum , err := strconv .Atoi (page )
241
+ if err != nil {
242
+ return c .Status (fiber .StatusBadRequest ).SendString ("Invalid page number" )
243
+ }
244
+
245
+ itemsNum , err := strconv .Atoi (items )
246
+ if err != nil {
247
+ itemsNum = 21
248
+ }
249
+
250
+ models = models .Paginate (pageNum , itemsNum )
251
+ }
252
+
253
+ if form .Search != "" {
254
+ models = models .Search (form .Search )
255
+ }
256
+
257
+ return c .SendString (elements .ListModels (models , processingModels , galleryService ))
184
258
})
185
259
186
260
/*
0 commit comments