areeb-h commited on
Commit
2cb6772
·
verified ·
1 Parent(s): 61031b9

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +45 -33
index.js CHANGED
@@ -9,44 +9,56 @@ const userInput = document.getElementById('user-input');
9
  const outputContainer = document.getElementById('output');
10
  const submitButton = document.getElementById('submit-button');
11
 
12
- // Load the text-generation pipeline
13
- status.textContent = 'Loading model...';
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  let generator;
16
- try {
17
- generator = await pipeline('text-generation', 'EleutherAI/gpt-neo-125M');
18
- status.textContent = 'Model loaded. Ready to chat!';
19
- } catch (error) {
20
- console.error('Error loading model:', error);
21
- status.textContent = 'Failed to load model. Check the console for details.';
22
- }
23
 
24
- // Add event listener to the submit button
25
- submitButton.addEventListener('click', async () => {
26
- const inputText = userInput.value.trim();
27
 
28
- if (!inputText) {
29
- outputContainer.innerText = 'Please enter a prompt.';
30
- return;
31
- }
32
 
33
- // Update status to show we're processing
34
- status.textContent = 'Generating response...';
35
 
36
- try {
37
- const response = await generator(inputText, {
38
- max_new_tokens: 100,
39
- temperature: 0.7,
40
- top_p: 0.95,
41
- });
42
-
43
- // Display the generated response
44
- outputContainer.innerText = response[0].generated_text;
45
- } catch (error) {
46
- console.error(error);
47
- outputContainer.innerText = 'Error generating response. Please try again.';
48
- }
49
 
50
- // Reset the status
51
- status.textContent = 'Model loaded. Ready to chat!';
 
 
 
 
 
 
 
 
 
 
 
52
  });
 
 
9
  const outputContainer = document.getElementById('output');
10
  const submitButton = document.getElementById('submit-button');
11
 
12
+ // Function to initialize and load the text-generation pipeline
13
+ async function loadModel() {
14
+ try {
15
+ status.textContent = 'Loading model...';
16
+ const generator = await pipeline('text-generation', 'EleutherAI/gpt-neo-125M');
17
+ status.textContent = 'Model loaded. Ready to chat!';
18
+ return generator;
19
+ } catch (error) {
20
+ console.error('Error loading model:', error);
21
+ status.textContent = 'Failed to load model. Please try again later.';
22
+ throw error; // Stop further execution if model loading fails
23
+ }
24
+ }
25
 
26
+ // Initialize the model
27
  let generator;
28
+ loadModel().then((loadedGenerator) => {
29
+ generator = loadedGenerator;
 
 
 
 
 
30
 
31
+ // Add event listener to the submit button after the model is ready
32
+ submitButton.addEventListener('click', async () => {
33
+ const inputText = userInput.value.trim();
34
 
35
+ if (!inputText) {
36
+ outputContainer.innerText = 'Please enter a prompt.';
37
+ return;
38
+ }
39
 
40
+ // Update status to show we're processing
41
+ status.textContent = 'Generating response...';
42
 
43
+ try {
44
+ const response = await generator(inputText, {
45
+ max_new_tokens: 100,
46
+ temperature: 0.7, // Controls randomness; lower = more deterministic
47
+ top_p: 0.95, // Nucleus sampling
48
+ });
 
 
 
 
 
 
 
49
 
50
+ // Display the generated response
51
+ outputContainer.innerText = response[0].generated_text;
52
+ } catch (error) {
53
+ console.error('Error generating response:', error);
54
+ outputContainer.innerText = 'Error generating response. Please try again.';
55
+ }
56
+
57
+ // Reset the status
58
+ status.textContent = 'Model loaded. Ready to chat!';
59
+ });
60
+ }).catch((error) => {
61
+ console.error('Model initialization failed:', error);
62
+ outputContainer.innerText = 'The application cannot proceed because the model failed to load.';
63
  });
64
+