hantech commited on
Commit
209ec3a
·
verified ·
1 Parent(s): 1eafeae

Update worker.js

Browse files
Files changed (1) hide show
  1. worker.js +21 -7
worker.js CHANGED
@@ -17,25 +17,39 @@ let extractor = null;
17
  let generator = null;
18
  let vectorStore = []; // Lưu trữ chunks và vectors: { text: string, vector: number[] }
19
 
20
- // Khởi tạo Models
21
  async function initModels() {
22
  try {
23
- console.log("Đang tải Embedding Model...");
 
 
24
  extractor = await pipeline('feature-extraction', EMBEDDING_MODEL_ID, {
25
  device: 'webgpu',
26
- dtype: 'fp32', // Embedding thường cần độ chính xác
 
27
  });
28
 
29
- console.log("Đang tải LLM Granite 4.0...");
 
30
  generator = await pipeline('text-generation', LLM_MODEL_ID, {
31
  device: 'webgpu',
32
- dtype: 'q4', // Quantization 4-bit để chạy mượt trên browser
33
- use_external_data_format: true
 
 
 
 
 
 
 
 
 
 
34
  });
35
 
36
  self.postMessage({ type: 'init_complete' });
37
  } catch (e) {
38
- self.postMessage({ type: 'error', payload: "Lỗi tải model: " + e.message });
 
39
  }
40
  }
41
 
 
17
  let generator = null;
18
  let vectorStore = []; // Lưu trữ chunks và vectors: { text: string, vector: number[] }
19
 
 
20
  async function initModels() {
21
  try {
22
+ self.postMessage({ type: 'status', payload: "Đang tải Embedding Model (có thể lâu lần đầu)..." });
23
+
24
+ // Thêm cờ { use_cache: true } dù mặc định nó đã có, để đảm bảo
25
  extractor = await pipeline('feature-extraction', EMBEDDING_MODEL_ID, {
26
  device: 'webgpu',
27
+ dtype: 'fp32',
28
+ use_cache: true
29
  });
30
 
31
+ self.postMessage({ type: 'status', payload: "Đang tải LLM Granite 4.0 (Model nặng, vui lòng chờ)..." });
32
+
33
  generator = await pipeline('text-generation', LLM_MODEL_ID, {
34
  device: 'webgpu',
35
+ dtype: 'q4',
36
+ use_external_data_format: true,
37
+ // Thêm progress_callback để UI không tưởng là bị treo
38
+ progress_callback: (data) => {
39
+ if (data.status === 'progress') {
40
+ // Gửi tiến độ về main thread để hiện loading bar nếu cần
41
+ self.postMessage({
42
+ type: 'download_progress',
43
+ payload: { file: data.file, progress: data.progress }
44
+ });
45
+ }
46
+ }
47
  });
48
 
49
  self.postMessage({ type: 'init_complete' });
50
  } catch (e) {
51
+ console.error(e); // Log chi tiết ra console
52
+ self.postMessage({ type: 'error', payload: "Lỗi tải model (Kiểm tra Console F12): " + e.message });
53
  }
54
  }
55