MRasheq commited on
Commit
30067d2
·
verified ·
1 Parent(s): dd3ebc2

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +92 -30
index.js CHANGED
@@ -1,32 +1,94 @@
1
  import { pipeline } from "@huggingface/transformers";
2
 
3
- // Create a text-generation pipeline
4
- const generator = await pipeline(
5
- "text-generation",
6
- "HuggingFaceTB/SmolLM2-360M", // You can replace this with other models like "EleutherAI/gpt-neo-125M" or "facebook/opt-125m"
7
- { device: "webgpu" }
8
- );
9
-
10
- // Generate text
11
- const prompts = [
12
- "Once upon a time",
13
- "The artificial intelligence"
14
- ];
15
-
16
- const results = await generator(prompts, {
17
- max_length: 50,
18
- num_return_sequences: 1,
19
- temperature: 0.7,
20
- top_p: 0.9
21
- });
22
-
23
- console.log(results);
24
- // Will output something like:
25
- // [
26
- // [{
27
- // "generated_text": "Once upon a time there was a young princess who lived in a castle..."
28
- // }],
29
- // [{
30
- // "generated_text": "The artificial intelligence revolution has transformed the way we..."
31
- // }]
32
- // ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { pipeline } from "@huggingface/transformers";
2
 
3
+ // Get DOM elements
4
+ const loadingContainer = document.getElementById('loadingContainer');
5
+ const chatInterface = document.getElementById('chatInterface');
6
+ const progressFill = document.getElementById('progressFill');
7
+ const progressPercent = document.getElementById('progressPercent');
8
+ const questionInput = document.getElementById('questionInput');
9
+ const sendButton = document.getElementById('sendButton');
10
+ const response = document.getElementById('response');
11
+
12
+ async function initializeModel() {
13
+ try {
14
+ // Create a text-generation pipeline with WebGPU
15
+ const generator = await pipeline(
16
+ "text-generation",
17
+ "HuggingFaceTB/SmolLM2-360M",
18
+ {
19
+ device: "webgpu",
20
+ progress_callback: (progress) => {
21
+ // Calculate percentage
22
+ const percentage = Math.round(progress.progress * 100);
23
+
24
+ // Update progress bar and text
25
+ progressFill.style.width = `${percentage}%`;
26
+ progressPercent.textContent = `${percentage}%`;
27
+
28
+ // When loading is complete
29
+ if (progress.progress === 1) {
30
+ setTimeout(() => {
31
+ loadingContainer.style.display = 'none';
32
+ chatInterface.style.display = 'block';
33
+ }, 500);
34
+ }
35
+ }
36
+ }
37
+ );
38
+
39
+ // Add event listener for send button
40
+ sendButton.addEventListener('click', async () => {
41
+ const question = questionInput.value.trim();
42
+ if (!question) return;
43
+
44
+ try {
45
+ // Disable input while generating
46
+ sendButton.disabled = true;
47
+ questionInput.disabled = true;
48
+ response.textContent = 'Generating response...';
49
+
50
+ const results = await generator(question, {
51
+ max_length: 50,
52
+ num_return_sequences: 1,
53
+ temperature: 0.7,
54
+ top_p: 0.9
55
+ });
56
+
57
+ // Display the generated text
58
+ response.textContent = results[0].generated_text;
59
+
60
+ } catch (error) {
61
+ response.textContent = 'Error generating response: ' + error.message;
62
+ } finally {
63
+ // Re-enable input
64
+ sendButton.disabled = false;
65
+ questionInput.disabled = false;
66
+ }
67
+ });
68
+
69
+ } catch (error) {
70
+ progressPercent.textContent = 'Error loading model: ' + error.message;
71
+ }
72
+ }
73
+
74
+ // Check for WebGPU support
75
+ async function checkWebGPUSupport() {
76
+ if (!navigator.gpu) {
77
+ loadingContainer.innerHTML = `
78
+ <div class="error-message">
79
+ WebGPU is not supported in your browser.
80
+ Please use a browser that supports WebGPU, such as Chrome Canary with WebGPU flags enabled.
81
+ </div>`;
82
+ return false;
83
+ }
84
+ return true;
85
+ }
86
+
87
+ // Initialize when page loads
88
+ async function init() {
89
+ if (await checkWebGPUSupport()) {
90
+ initializeModel();
91
+ }
92
+ }
93
+
94
+ init();