File size: 9,185 Bytes
0573fbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import {
    Box,
    Typography,
    Card,
    Chip,
    Button,
    Alert,
    Dialog,
    DialogTitle,
    DialogContent,
    DialogActions,
    Snackbar,
} from '@mui/material';
import { CloudDownload as CloudDownloadIcon, Trash2 as DeleteIcon } from 'lucide-react';
import api from '../api';
import { checkpointManagerStyles } from '../theme';

export default function CheckpointManager({ model, onRefresh }) {
    const [loadingStates, setLoadingStates] = useState({});
    const [error, setError] = useState(null);
    const [deleteTarget, setDeleteTarget] = useState(null);
    const [toast, setToast] = useState({
        open: false,
        message: '',
        severity: 'success'
    });

    const handleUnwrapCheckpoint = async (checkpoint) => {
        const checkpointId = checkpoint.path;
        setLoadingStates(prev => ({ ...prev, [checkpointId]: { unwrapping: true } }));
        setError(null);
        try {
            await api.post('/api/unwrap-model', {
                model_config: model.config_path,
                ckpt_path: checkpoint.path,
                name: `${checkpoint.name}_unwrapped`
            });
            setError(null);
            setToast({
                open: true,
                message: `Checkpoint "${checkpoint.name}" unwrapped successfully.`,
                severity: 'success'
            });
            onRefresh();
        } catch (err) {
            setError(`Failed to unwrap ${checkpoint.name}: ${err.response?.data?.error || err.message}`);
        } finally {
            setLoadingStates(prev => ({ ...prev, [checkpointId]: { unwrapping: false } }));
        }
    };

    const handleDeleteCheckpoint = async () => {
        if (!deleteTarget) {
            return;
        }

        const checkpointId = deleteTarget.path;
        setLoadingStates(prev => ({ ...prev, [checkpointId]: { deleting: true } }));
        setError(null);

        try {
            await api.post('/api/delete-checkpoint', {
                checkpoint_path: deleteTarget.path
            });
            setToast({
                open: true,
                message: `Checkpoint "${deleteTarget.name}" deleted successfully.`,
                severity: 'success'
            });
            onRefresh();
        } catch (err) {
            setError(`Failed to delete ${deleteTarget.name}: ${err.response?.data?.error || err.message}`);
        } finally {
            setDeleteTarget(null);
            setLoadingStates(prev => ({ ...prev, [checkpointId]: { deleting: false } }));
        }
    };

    const closeToast = (_, reason) => {
        if (reason === 'clickaway') {
            return;
        }
        setToast(prev => ({ ...prev, open: false }));
    };

    const checkpoints = model.checkpoints || [];

    return (
        <>
            <Box sx={checkpointManagerStyles.root}>
                <Typography variant="subtitle2" color="textSecondary">
                    Checkpoints ({checkpoints.length})
                </Typography>

                {checkpoints.length === 0 ? (
                    <Typography variant="caption" color="textSecondary" sx={checkpointManagerStyles.emptyText}>
                        No checkpoints yet.
                    </Typography>
                ) : (
                    <Box sx={checkpointManagerStyles.checkpointsList}>
                        {checkpoints.map((checkpoint, index) => {
                            const checkpointId = checkpoint.path;
                            const isUnwrapping = loadingStates[checkpointId]?.unwrapping;
                            const isDeleting = loadingStates[checkpointId]?.deleting;

                            const hasUnwrappedVersion = model.unwrapped_models?.some(unwrapped =>
                                unwrapped.name.includes(checkpoint.name) ||
                                checkpoint.name.includes(unwrapped.name.replace('_unwrapped', ''))
                            );

                            return (
                                <Card key={index} sx={checkpointManagerStyles.checkpointCard}>
                                    <Box sx={checkpointManagerStyles.checkpointRow}>
                                        <Box sx={checkpointManagerStyles.checkpointInfo}>
                                            <Typography variant="body2" sx={checkpointManagerStyles.checkpointName}>
                                                {checkpoint.name}
                                                {hasUnwrappedVersion && (
                                                    <Chip
                                                        label="Unwrapped"
                                                        size="small"
                                                        color="success"
                                                        sx={checkpointManagerStyles.unwrappedChip}
                                                    />
                                                )}
                                            </Typography>
                                            <Typography variant="caption" color="textSecondary">
                                                {checkpoint.size_mb} MB
                                            </Typography>
                                        </Box>
                                        <Box sx={checkpointManagerStyles.actions}>
                                            {!hasUnwrappedVersion && (
                                                <Button
                                                    variant="outlined"
                                                    color="primary"
                                                    size="small"
                                                    startIcon={<CloudDownloadIcon />}
                                                    onClick={() => handleUnwrapCheckpoint(checkpoint)}
                                                    disabled={isUnwrapping || isDeleting}
                                                >
                                                    {isUnwrapping ? 'Unwrapping...' : 'Unwrap'}
                                                </Button>
                                            )}

                                            {hasUnwrappedVersion && (
                                                <Button
                                                    variant="outlined"
                                                    color="error"
                                                    size="small"
                                                    startIcon={<DeleteIcon />}
                                                    onClick={() => setDeleteTarget(checkpoint)}
                                                    disabled={isDeleting || isUnwrapping}
                                                >
                                                    {isDeleting ? 'Deleting...' : 'Delete'}
                                                </Button>
                                            )}
                                        </Box>
                                    </Box>
                                </Card>
                            );
                        })}
                    </Box>
                )}

                {error && (
                    <Alert severity="error" sx={checkpointManagerStyles.errorAlert}>{error}</Alert>
                )}
            </Box>

            <Dialog
                open={Boolean(deleteTarget)}
                onClose={() => setDeleteTarget(null)}
                aria-labelledby="delete-checkpoint-dialog-title"
            >
                <DialogTitle id="delete-checkpoint-dialog-title">
                    Delete Wrapped Checkpoint
                </DialogTitle>
                <DialogContent>
                    <Typography sx={checkpointManagerStyles.deleteDialogText}>
                        {deleteTarget
                            ? `Delete "${deleteTarget.name}"? This action cannot be undone.`
                            : 'Delete this checkpoint?'}
                    </Typography>
                </DialogContent>
                <DialogActions>
                    <Button onClick={() => setDeleteTarget(null)}>
                        Cancel
                    </Button>
                    <Button
                        variant="contained"
                        color="error"
                        onClick={handleDeleteCheckpoint}
                    >
                        Delete
                    </Button>
                </DialogActions>
            </Dialog>

            <Snackbar
                open={toast.open}
                autoHideDuration={3200}
                onClose={closeToast}
                anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
            >
                <Alert onClose={closeToast} severity={toast.severity} variant="filled" sx={checkpointManagerStyles.snackbarAlert}>
                    {toast.message}
                </Alert>
            </Snackbar>
        </>
    );
}