File size: 11,703 Bytes
6a50e97
 
 
 
 
 
 
 
 
 
 
 
 
 
1c47f1e
6a50e97
 
 
 
 
 
1c47f1e
6a50e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0c156e
6a50e97
1c47f1e
 
 
6a50e97
1c47f1e
 
 
 
 
6a50e97
 
 
 
 
1c47f1e
6a50e97
 
f0c156e
6a50e97
 
1c47f1e
 
 
6a50e97
 
1c47f1e
 
 
6a50e97
1c47f1e
 
 
 
 
6a50e97
 
 
 
 
1c47f1e
 
6a50e97
f0c156e
6a50e97
 
1c47f1e
 
 
6a50e97
 
1c47f1e
 
 
6a50e97
1c47f1e
 
 
 
 
 
 
6a50e97
 
 
 
 
1c47f1e
 
6a50e97
 
 
 
 
 
 
1c47f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
 
 
 
1c47f1e
6a50e97
 
1c47f1e
6a50e97
 
 
 
1c47f1e
6a50e97
1c47f1e
 
6a50e97
 
 
 
 
 
1c47f1e
 
 
 
6a50e97
 
 
 
 
 
1c47f1e
 
6a50e97
1c47f1e
 
6a50e97
 
 
 
 
 
 
 
 
 
1c47f1e
6a50e97
 
 
 
1c47f1e
 
6a50e97
1c47f1e
 
6a50e97
 
 
 
 
1c47f1e
6a50e97
 
1c47f1e
 
 
 
6a50e97
 
1c47f1e
6a50e97
1c47f1e
 
 
 
6a50e97
 
 
1c47f1e
 
 
 
 
 
6a50e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c47f1e
 
 
6a50e97
1c47f1e
 
6a50e97
1c47f1e
 
 
 
 
 
 
 
 
 
 
 
6a50e97
1c47f1e
6a50e97
 
1c47f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
1c47f1e
 
 
 
 
 
 
 
 
 
6a50e97
 
1c47f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
1c47f1e
 
6a50e97
 
1c47f1e
6a50e97
1c47f1e
6a50e97
 
1c47f1e
 
6a50e97
1c47f1e
6a50e97
1c47f1e
6a50e97
 
 
 
 
 
1c47f1e
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { useState, useEffect } from 'react'
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import { 
  BookOpen, 
  Brain, 
  Zap, 
  Download, 
  Trash2, 
  Loader2,
  Info,
  CheckCircle,
  Cloud,
  HardDrive
} from 'lucide-react'

interface ModelInfo {
  model_name: string
  name: string
  supports_thinking: boolean
  ram_required_gb: string
  size_gb: string
  is_loaded: boolean
  type: 'local' | 'api'
}

interface ModelsResponse {
  models: ModelInfo[]
  current_model: string
}

export function Models() {
  const [models, setModels] = useState<ModelInfo[]>([])
  const [loading, setLoading] = useState(true)
  const [modelLoading, setModelLoading] = useState<string | null>(null)

  useEffect(() => {
    fetchModels()
  }, [])

  const fetchModels = async () => {
    try {
      const baseUrl = `${window.location.protocol}//${window.location.host}`
      const res = await fetch(`${baseUrl}/models`)
      
      if (!res.ok) {
        throw new Error(`Failed to fetch models: ${res.status}`)
      }
      
      const data: ModelsResponse = await res.json()
      setModels(data.models)
    } catch (error) {
      console.error('Error fetching models:', error)
    } finally {
      setLoading(false)
    }
  }

  const loadModel = async (modelName: string) => {
    setModelLoading(modelName)
    try {
      const baseUrl = `${window.location.protocol}//${window.location.host}`
      const res = await fetch(`${baseUrl}/load-model`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ model_name: modelName }),
      })

      if (!res.ok) {
        throw new Error(`Failed to load model: ${res.status}`)
      }

      // Refresh models list
      fetchModels()
    } catch (error) {
      console.error('Error loading model:', error)
    } finally {
      setModelLoading(null)
    }
  }

  const unloadModel = async (modelName: string) => {
    setModelLoading(modelName)
    try {
      const baseUrl = `${window.location.protocol}//${window.location.host}`
      const res = await fetch(`${baseUrl}/unload-model`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ model_name: modelName }),
      })

      if (!res.ok) {
        throw new Error(`Failed to unload model: ${res.status}`)
      }

      // Refresh models list
      fetchModels()
    } catch (error) {
      console.error('Error unloading model:', error)
    } finally {
      setModelLoading(null)
    }
  }

  if (loading) {
    return (
      <div className="min-h-screen bg-background flex items-center justify-center">
        <Loader2 className="h-8 w-8 animate-spin" />
      </div>
    )
  }

  return (
    <div className="min-h-screen bg-background">
      {/* Header */}
      <div className="border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
        <div className="max-w-6xl mx-auto p-6">
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-3">
              <div className="w-8 h-8 bg-blue-600 rounded-lg flex items-center justify-center">
                <BookOpen className="h-5 w-5 text-white" />
              </div>
              <div>
                <h1 className="text-2xl font-bold">Model Catalog</h1>
                <p className="text-sm text-muted-foreground">
                  Browse and manage AI models for your conversations
                </p>
              </div>
            </div>
            <Button 
              onClick={() => window.location.href = '/playground'} 
              className="flex items-center gap-2"
            >
              <Zap className="h-4 w-4" />
              Go to Playground
            </Button>
          </div>
        </div>
      </div>

      <div className="flex-1 p-6">
        <div className="max-w-6xl mx-auto space-y-6">
          
          {/* Info Card */}
          <Card className="bg-blue-50 border-blue-200">
            <CardContent className="pt-6">
              <div className="flex items-start gap-3">
                <Info className="h-5 w-5 text-blue-600 mt-0.5" />
                <div>
                  <h3 className="font-medium text-blue-900">Model Management</h3>
                  <p className="text-sm text-blue-700 mt-1">
                    Load models to use them in the playground. Models are cached locally for faster access.
                    Each model requires significant storage space and initial download time.
                  </p>
                </div>
              </div>
            </CardContent>
          </Card>

          {/* API Models Section */}
          <div>
            <h2 className="text-xl font-semibold mb-4 flex items-center gap-2">
              <Cloud className="h-5 w-5" />
              API Models
              <Badge variant="outline" className="text-xs">Cloud-Powered</Badge>
            </h2>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
              {models.filter(m => m.type === 'api').map((model) => (
                <ModelCard 
                  key={model.model_name}
                  model={model}
                  modelLoading={modelLoading}
                  onLoad={loadModel}
                  onUnload={unloadModel}
                />
              ))}
            </div>
          </div>

          {/* Local Models Section */}
          <div>
            <h2 className="text-xl font-semibold mb-4 flex items-center gap-2">
              <HardDrive className="h-5 w-5" />
              Local Models
              <Badge variant="outline" className="text-xs">Self-Hosted</Badge>
            </h2>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
              {models.filter(m => m.type === 'local').map((model) => (
                <ModelCard 
                  key={model.model_name}
                  model={model}
                  modelLoading={modelLoading}
                  onLoad={loadModel}
                  onUnload={unloadModel}
                />
              ))}
            </div>
          </div>

          {/* Model Statistics */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <Brain className="h-5 w-5" />
                Model Statistics
              </CardTitle>
            </CardHeader>
            <CardContent>
              <div className="grid grid-cols-2 md:grid-cols-4 gap-6">
                <div className="text-center">
                  <div className="text-2xl font-bold text-blue-600">
                    {models.filter(m => m.type === 'api').length}
                  </div>
                  <div className="text-sm text-muted-foreground">API Models</div>
                </div>
                <div className="text-center">
                  <div className="text-2xl font-bold text-green-600">
                    {models.filter(m => m.type === 'local').length}
                  </div>
                  <div className="text-sm text-muted-foreground">Local Models</div>
                </div>
                <div className="text-center">
                  <div className="text-2xl font-bold text-orange-600">
                    {models.filter(m => m.is_loaded).length}
                  </div>
                  <div className="text-sm text-muted-foreground">Loaded Models</div>
                </div>
                <div className="text-center">
                  <div className="text-2xl font-bold text-purple-600">
                    {models.filter(m => m.supports_thinking).length}
                  </div>
                  <div className="text-sm text-muted-foreground">Thinking Models</div>
                </div>
              </div>
            </CardContent>
          </Card>
        </div>
      </div>
    </div>
  )
}

// ModelCard component for reusability
interface ModelCardProps {
  model: ModelInfo
  modelLoading: string | null
  onLoad: (modelName: string) => void
  onUnload: (modelName: string) => void
}

function ModelCard({ model, modelLoading, onLoad, onUnload }: ModelCardProps) {
  const isApiModel = model.type === 'api'
  const isLoading = modelLoading === model.model_name
  const isLoaded = model.is_loaded

  return (
    <Card className="hover:shadow-md transition-shadow">
      <CardHeader className="pb-3">
        <div className="flex items-start justify-between">
          <div className="flex-1">
            <CardTitle className="text-base flex items-center gap-2">
              {isApiModel ? (
                <Cloud className="h-4 w-4 text-blue-600" />
              ) : (
                <HardDrive className="h-4 w-4 text-green-600" />
              )}
              {model.name}
            </CardTitle>
            <p className="text-xs text-muted-foreground mt-1">
              {model.model_name}
            </p>
          </div>
          {isLoaded && <CheckCircle className="h-5 w-5 text-green-600" />}
        </div>
      </CardHeader>
      
      <CardContent className="space-y-3">
        {/* Model Info */}
        <div className="space-y-2 text-sm">
          {!isApiModel && (
            <>
              <div className="flex justify-between">
                <span className="text-muted-foreground">Size:</span>
                <Badge variant="outline" className="text-xs">
                  {model.size_gb}
                </Badge>
              </div>
              <div className="flex justify-between">
                <span className="text-muted-foreground">RAM Required:</span>
                <Badge variant="outline" className="text-xs">
                  {model.ram_required_gb}
                </Badge>
              </div>
            </>
          )}
          
          <div className="flex justify-between">
            <span className="text-muted-foreground">Type:</span>
            <Badge variant={isApiModel ? "default" : "secondary"} className="text-xs">
              {isApiModel ? 'API' : 'Local'}
            </Badge>
          </div>
          
          {model.supports_thinking && (
            <div className="flex justify-between">
              <span className="text-muted-foreground">Features:</span>
              <Badge variant="outline" className="text-xs">
                <Brain className="h-3 w-3 mr-1" />
                Thinking
              </Badge>
            </div>
          )}
        </div>

        {/* Action Button */}
        <div className="pt-2">
          {isLoaded ? (
            <Button
              size="sm"
              variant="outline"
              onClick={() => onUnload(model.model_name)}
              disabled={isLoading}
              className="w-full"
            >
              {isLoading ? (
                <Loader2 className="h-4 w-4 mr-2 animate-spin" />
              ) : (
                <Trash2 className="h-4 w-4 mr-2" />
              )}
              {isLoading ? 'Unloading...' : 'Unload Model'}
            </Button>
          ) : (
            <Button
              size="sm"
              onClick={() => onLoad(model.model_name)}
              disabled={isLoading}
              className="w-full"
            >
              {isLoading ? (
                <Loader2 className="h-4 w-4 mr-2 animate-spin" />
              ) : (
                <Download className="h-4 w-4 mr-2" />
              )}
              {isLoading ? 'Loading...' : 'Load Model'}
            </Button>
          )}
        </div>
      </CardContent>
    </Card>
  )
}