File size: 8,862 Bytes
b206cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * API Adapter for Flask Backend (android.py)
 *
 * This file provides a pywebview.api compatible interface that works with
 * the Flask REST API instead of the pywebview Python bridge.
 *
 * Include this BEFORE app.js when running on Android/Flask:
 * <script src="api-adapter.js"></script>
 * <script src="app.js"></script>
 */

(function() {
    'use strict';

    // API base URL - will be automatically set to current origin
    const API_BASE = window.location.origin;

    /**
     * Make API call to Flask backend
     */
    async function apiCall(endpoint, data = null) {
        const url = `${API_BASE}/api/${endpoint}`;

        const options = {
            method: data ? 'POST' : 'GET',
            headers: {
                'Content-Type': 'application/json',
            }
        };

        if (data) {
            options.body = JSON.stringify(data);
        }

        try {
            const response = await fetch(url, options);

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const result = await response.json();
            return result;
        } catch (error) {
            console.error(`API call failed: ${endpoint}`, error);
            throw error;
        }
    }

    /**
     * Create pywebview.api compatible interface
     */
    window.pywebview = {
        api: {
            // ==================== Synthetic Data Generation ====================

            generate_simplex_points: async function(n, d, k, seed) {
                return await apiCall('generate_simplex_points', { n, d, k, seed });
            },

            save_synthetic_dataset: async function(points) {
                return await apiCall('save_synthetic_dataset', { points });
            },

            // ==================== MNIST Dataset ====================

            load_mnist: async function(max_samples, subset) {
                return await apiCall('load_mnist', { max_samples, subset });
            },

            // ==================== Upload Handling ====================

            upload_csv: async function(name, content, delimiter) {
                return await apiCall('upload_csv', { name, content, delimiter });
            },

            upload_images: async function(files) {
                return await apiCall('upload_images', { files });
            },

            list_datasets: async function() {
                return await apiCall('list_datasets');
            },

            prepare_csv_dataset: async function(dataset_id, selected_columns, handle_missing) {
                return await apiCall('prepare_csv_dataset', {
                    dataset_id,
                    selected_columns,
                    handle_missing
                });
            },

            // ==================== Embeddings ====================

            compute_embeddings: async function(dataset_id, method) {
                return await apiCall('compute_embeddings', { dataset_id, method });
            },

            // ==================== t-SNE ====================

            run_tsne: async function(dataset_id, perplexity, learning_rate, n_iter,
                                    early_exaggeration, momentum, init_method, init_data, seed) {
                return await apiCall('run_tsne', {
                    dataset_id,
                    perplexity,
                    learning_rate,
                    n_iter,
                    early_exaggeration,
                    momentum,
                    init_method,
                    init_data,
                    seed
                });
            },

            stop_tsne: async function() {
                // Note: stop functionality needs to be implemented in Flask backend
                return { success: true };
            },

            // ==================== Clustering ====================

            run_clustering: async function(dataset_id, method, k, eps, min_samples) {
                return await apiCall('run_clustering', {
                    dataset_id,
                    method,
                    k,
                    eps,
                    min_samples
                });
            },

            // ==================== Export ====================

            export_results: async function(dataset_id) {
                return await apiCall('export_results', { dataset_id });
            },

            get_image_at_index: async function(dataset_id, index) {
                return await apiCall('get_image_at_index', { dataset_id, index });
            }
        },

        // ==================== API Status ====================

        /**
         * Check if we're running in Flask/Android mode
         */
        isFlaskMode: function() {
            return true;
        },

        /**
         * Check MCP connection status
         */
        checkMCPStatus: async function() {
            try {
                const status = await apiCall('mcp_status');
                return status;
            } catch (error) {
                return { connected: false, available: false };
            }
        }
    };

    // ==================== Progress Updates ====================

    /**
     * Progress updates for t-SNE
     * In Flask mode, we use polling instead of callbacks
     */
    let progressInterval = null;

    window.startProgressPolling = function() {
        if (progressInterval) {
            clearInterval(progressInterval);
        }

        // Poll for progress updates every 500ms
        progressInterval = setInterval(async () => {
            try {
                const progress = await apiCall('tsne_progress');
                if (progress && progress.current !== undefined) {
                    window.updateProgress(progress.current, progress.total, progress.message);

                    // Stop polling when complete
                    if (progress.current >= progress.total) {
                        clearInterval(progressInterval);
                        progressInterval = null;
                    }
                }
            } catch (error) {
                // Silently fail if progress endpoint not available
            }
        }, 500);
    };

    window.stopProgressPolling = function() {
        if (progressInterval) {
            clearInterval(progressInterval);
            progressInterval = null;
        }
    };

    // ==================== Connection Status Indicator ====================

    /**
     * Show connection status in UI
     */
    async function showConnectionStatus() {
        try {
            const health = await apiCall('health');
            const statusDiv = document.createElement('div');
            statusDiv.id = 'connection-status';
            statusDiv.style.cssText = `
                position: fixed;
                top: 10px;
                right: 10px;
                padding: 8px 12px;
                background: ${health.mcp_connected ? '#10b981' : '#f59e0b'};
                color: white;
                border-radius: 6px;
                font-size: 0.85em;
                font-weight: 600;
                z-index: 9999;
                box-shadow: 0 2px 8px rgba(0,0,0,0.2);
            `;
            statusDiv.innerHTML = health.mcp_connected
                ? '🟢 MCP Connected'
                : '🟡 Local Mode';

            document.body.appendChild(statusDiv);

            // Add tooltip
            statusDiv.title = health.mcp_connected
                ? 'Connected to MCP server for heavy computations'
                : 'Using local fallback (computations may be slower)';

        } catch (error) {
            console.error('Failed to check connection status:', error);
        }
    }

    // ==================== Initialization ====================

    /**
     * Initialize Flask API adapter
     */
    function initAdapter() {
        console.log('Flask API Adapter initialized');
        console.log(`API Base URL: ${API_BASE}`);

        // Show connection status
        showConnectionStatus();

        // Dispatch ready event
        window.dispatchEvent(new Event('pywebviewready'));
    }

    // Wait for DOM to be ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initAdapter);
    } else {
        initAdapter();
    }

    // ==================== Helper Functions ====================

    /**
     * Download file helper (for CSV export)
     */
    window.downloadFile = function(content, filename, type) {
        const blob = new Blob([content], { type: type });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = filename;
        a.click();
        URL.revokeObjectURL(url);
    };

})();