forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimal_server.go
More file actions
314 lines (276 loc) · 7.64 KB
/
Copy pathminimal_server.go
File metadata and controls
314 lines (276 loc) · 7.64 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
)
// Simple model structure
type Model struct {
Name string `json:"name"`
Description string `json:"description"`
Size int64 `json:"size"`
Parameters int64 `json:"parameters"`
Tags []string `json:"tags"`
Category string `json:"category"`
IsDownloaded bool `json:"downloaded"`
KCRecommended bool `json:"kc_recommended"`
}
// Download status structure
type DownloadStatus struct {
ModelName string `json:"model_name"`
Progress float64 `json:"progress"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
Completed bool `json:"completed"`
Downloaded int64 `json:"downloaded_bytes"`
TotalSize int64 `json:"total_bytes"`
}
var (
models = []Model{
{
Name: "mistral-7b",
Description: "Mistral 7B is a powerful general-purpose language model with 7.3B parameters",
Size: 4500000000,
Parameters: 7000000000,
Tags: []string{"language", "general", "chat"},
Category: "recommended",
IsDownloaded: false,
KCRecommended: true,
},
{
Name: "llava",
Description: "LLaVA (Large Language and Vision Assistant) is a multimodal model for visual understanding",
Size: 4200000000,
Parameters: 7000000000,
Tags: []string{"multimodal", "vision", "chat"},
Category: "recommended",
IsDownloaded: false,
KCRecommended: true,
},
{
Name: "moondream",
Description: "Moondream is optimized for vision tasks",
Size: 1200000000,
Parameters: 1500000000,
Tags: []string{"vision", "efficient"},
Category: "recommended",
IsDownloaded: false,
KCRecommended: true,
},
}
downloadMutex sync.Mutex
downloads = make(map[string]*DownloadStatus)
)
func main() {
port := getPort()
fmt.Printf("Starting minimal server on http://0.0.0.0:%s\n", port)
http.HandleFunc("/api/models", getModelsHandler)
http.HandleFunc("/api/models/download", downloadModelHandler)
http.HandleFunc("/api/models/status", getDownloadStatusHandler)
http.HandleFunc("/api/models/remove", removeModelHandler)
http.HandleFunc("/api/health", healthCheckHandler)
http.HandleFunc("/api/updates", checkUpdatesHandler)
http.HandleFunc("/api/updates/apply", applyUpdateHandler)
http.ListenAndServe(":"+port, nil)
}
func getPort() string {
// Default to port 5000
port := "5000"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
return port
}
func getModelsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(models)
}
func downloadModelHandler(w http.ResponseWriter, r *http.Request) {
modelName := r.URL.Query().Get("name")
if modelName == "" {
http.Error(w, "Model name is required", http.StatusBadRequest)
return
}
// Check if model exists
var modelExists bool
for _, model := range models {
if model.Name == modelName {
modelExists = true
break
}
}
if !modelExists {
http.Error(w, "Model not found", http.StatusNotFound)
return
}
// Check if already downloading
downloadMutex.Lock()
status, exists := downloads[modelName]
if !exists {
// Create new download
status = &DownloadStatus{
ModelName: modelName,
Progress: 0,
Status: "downloading",
Completed: false,
Downloaded: 0,
TotalSize: 0,
}
downloads[modelName] = status
// Start download in background
go simulateDownload(modelName)
}
downloadMutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
}
func getDownloadStatusHandler(w http.ResponseWriter, r *http.Request) {
modelName := r.URL.Query().Get("name")
if modelName == "" {
http.Error(w, "Model name is required", http.StatusBadRequest)
return
}
// Check if model is already downloaded
for i, model := range models {
if model.Name == modelName && model.IsDownloaded {
status := &DownloadStatus{
ModelName: modelName,
Progress: 100,
Status: "completed",
Completed: true,
Downloaded: model.Size,
TotalSize: model.Size,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
return
}
}
// Check if currently downloading
downloadMutex.Lock()
status, exists := downloads[modelName]
downloadMutex.Unlock()
if !exists {
// Not downloaded and not in progress
status = &DownloadStatus{
ModelName: modelName,
Progress: 0,
Status: "not_started",
Completed: false,
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
}
func removeModelHandler(w http.ResponseWriter, r *http.Request) {
modelName := r.URL.Query().Get("name")
if modelName == "" {
http.Error(w, "Model name is required", http.StatusBadRequest)
return
}
// Find the model and update its status
var modelFound bool
for i, model := range models {
if model.Name == modelName {
modelFound = true
if !model.IsDownloaded {
http.Error(w, "Model is not downloaded", http.StatusBadRequest)
return
}
models[i].IsDownloaded = false
break
}
}
if !modelFound {
http.Error(w, "Model not found", http.StatusNotFound)
return
}
// Remove from downloads
downloadMutex.Lock()
delete(downloads, modelName)
downloadMutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"message": "Model removed successfully",
})
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "healthy",
"version": "0.1.0",
})
}
func checkUpdatesHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"available": true,
"current_version": "0.1.0",
"new_version": "0.2.0",
"release_date": time.Now().Format(time.RFC3339),
"release_notes": "Bug fixes and performance improvements",
})
}
func applyUpdateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"message": "Update applied successfully",
})
}
func simulateDownload(modelName string) {
// Get model size
var modelSize int64
for _, model := range models {
if model.Name == modelName {
modelSize = model.Size
break
}
}
// Update status
downloadMutex.Lock()
status := downloads[modelName]
status.TotalSize = modelSize
downloadMutex.Unlock()
// Simulate download progress
totalChunks := 20
for i := 1; i <= totalChunks; i++ {
// Check if we should cancel
downloadMutex.Lock()
if status, exists := downloads[modelName]; exists && status.Status == "cancelled" {
downloadMutex.Unlock()
return
}
// Update progress
progress := float64(i) * 100 / float64(totalChunks)
downloaded := int64(float64(modelSize) * progress / 100)
status := downloads[modelName]
status.Progress = progress
status.Downloaded = downloaded
downloadMutex.Unlock()
// Sleep to simulate network delay
time.Sleep(500 * time.Millisecond)
}
// Mark as completed
downloadMutex.Lock()
status = downloads[modelName]
status.Progress = 100
status.Status = "completed"
status.Completed = true
status.Downloaded = modelSize
// Update model
for i, model := range models {
if model.Name == modelName {
models[i].IsDownloaded = true
break
}
}
downloadMutex.Unlock()
// Log completion
log.Printf("Download of model %s completed\n", modelName)
}