-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel-worker.js
More file actions
239 lines (202 loc) · 6.25 KB
/
model-worker.js
File metadata and controls
239 lines (202 loc) · 6.25 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
// model-worker.js - Web Worker for Transformers.js model inference
// This runs in a separate thread to avoid blocking the UI
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
// Configure Transformers.js environment
env.allowLocalModels = false; // Use CDN models
env.allowRemoteModels = true;
// Global model instance
let modelPipeline = null;
let modelLoading = false;
let modelLoaded = false;
// Model configuration
const MODEL_CONFIG = {
// Primary model - Smaller BLIP model for faster inference
primary: {
name: 'Xenova/blip-image-captioning-base',
task: 'image-to-text',
options: {
device: 'auto', // WebGPU > WebGL > WASM
}
},
// Fallback model - Even smaller if available
fallback: {
name: 'Xenova/vit-gpt2-image-captioning',
task: 'image-to-text',
options: {
device: 'auto',
}
}
};
/**
* Initialize the model pipeline
* @param {string} modelChoice - 'primary' or 'fallback'
* @returns {Promise<void>}
*/
async function initializeModel(modelChoice = 'primary') {
if (modelLoaded) {
return;
}
if (modelLoading) {
// Wait for existing load to complete
while (modelLoading) {
await new Promise(resolve => setTimeout(resolve, 100));
}
return;
}
modelLoading = true;
try {
const config = MODEL_CONFIG[modelChoice];
console.log(`[Model Worker] Loading ${config.name}...`);
// Report progress
self.postMessage({
type: 'progress',
status: 'downloading',
message: `Downloading ${config.name} model... (first time only, ~80-500MB)`,
progress: 0
});
// Initialize pipeline with progress tracking
modelPipeline = await pipeline(
config.task,
config.name,
config.options
);
modelLoaded = true;
console.log(`[Model Worker] Model ${config.name} loaded successfully`);
self.postMessage({
type: 'progress',
status: 'ready',
message: 'Model loaded and ready',
progress: 100
});
} catch (error) {
console.error('[Model Worker] Error loading model:', error);
// Try fallback if primary failed
if (modelChoice === 'primary') {
console.log('[Model Worker] Trying fallback model...');
self.postMessage({
type: 'progress',
status: 'downloading',
message: 'Primary model failed, trying alternative...',
progress: 0
});
modelLoading = false;
return await initializeModel('fallback');
}
self.postMessage({
type: 'error',
error: `Failed to load model: ${error.message}`
});
throw error;
} finally {
modelLoading = false;
}
}
/**
* Process an image and generate explanation
* @param {string} imageData - Base64 encoded image data URL
* @param {number[]} coords - [x1, y1, x2, y2] coordinates (optional, for cropping)
* @returns {Promise<string>}
*/
async function processImage(imageData, coords = null) {
try {
// Ensure model is loaded
if (!modelLoaded) {
await initializeModel();
}
console.log('[Model Worker] Processing image...');
self.postMessage({
type: 'progress',
status: 'processing',
message: 'Analyzing code...',
progress: 50
});
// Crop image if coordinates provided
let processedImage = imageData;
if (coords && coords.length === 4) {
processedImage = await cropImage(imageData, coords);
}
// Generate explanation
const prompt = 'Analyze this image and describe any code, programming syntax, or technical content you see. Identify the programming language, explain what the code does, and highlight key concepts.';
const result = await modelPipeline(processedImage, {
prompt: prompt,
max_new_tokens: 150,
temperature: 0.2,
do_sample: true,
});
console.log('[Model Worker] Processing complete');
// Extract text from result
let explanation = '';
if (Array.isArray(result)) {
explanation = result[0]?.generated_text || result[0]?.text || '';
} else if (result.generated_text) {
explanation = result.generated_text;
} else if (result.text) {
explanation = result.text;
} else {
explanation = String(result);
}
explanation = explanation.trim();
if (!explanation) {
explanation = 'No explanation generated. The model may not have recognized any code in the selection.';
}
return explanation;
} catch (error) {
console.error('[Model Worker] Error processing image:', error);
throw error;
}
}
/**
* Crop image to specified coordinates using Canvas API
* @param {string} imageData - Base64 encoded image data URL
* @param {number[]} coords - [x1, y1, x2, y2]
* @returns {Promise<string>} Cropped image as data URL
*/
async function cropImage(imageData, coords) {
// Note: Canvas API not available in Web Workers
// This will be handled in the main thread before sending to worker
// For now, return the original image
return imageData;
}
/**
* Get model status
* @returns {Object}
*/
function getModelStatus() {
return {
loaded: modelLoaded,
loading: modelLoading,
};
}
// Message handler
self.addEventListener('message', async (event) => {
const { type, data } = event.data;
try {
switch (type) {
case 'initialize':
await initializeModel(data?.modelChoice);
self.postMessage({ type: 'initialized', status: getModelStatus() });
break;
case 'process':
const explanation = await processImage(data.imageData, data.coords);
self.postMessage({
type: 'result',
explanation: explanation,
croppedImage: data.imageData // Return cropped image for display
});
break;
case 'status':
self.postMessage({ type: 'status', status: getModelStatus() });
break;
default:
self.postMessage({ type: 'error', error: `Unknown message type: ${type}` });
}
} catch (error) {
self.postMessage({
type: 'error',
error: error.message || 'Unknown error occurred'
});
}
});
// Initialize on worker start
console.log('[Model Worker] Worker started, ready to initialize model');
self.postMessage({ type: 'ready' });