ApprikatAI commited on
Commit
c41cd83
·
verified ·
1 Parent(s): cd18a85

Upload index.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. index.js +175 -0
index.js ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Main application logic
2
+ class ChatAssistant {
3
+ constructor() {
4
+ this.pipeline = null;
5
+ this.isModelLoaded = false;
6
+ this.worker = null;
7
+ this.messageHistory = [];
8
+
9
+ this.initializeElements();
10
+ this.setupEventListeners();
11
+ this.loadModel();
12
+ }
13
+
14
+ initializeElements() {
15
+ this.chatMessages = document.getElementById('chatMessages');
16
+ this.userInput = document.getElementById('userInput');
17
+ this.sendButton = document.getElementById('sendButton');
18
+ this.modelStatus = document.getElementById('modelStatus');
19
+ this.progressFill = document.getElementById('progressFill');
20
+ this.progressText = document.getElementById('progressText');
21
+ }
22
+
23
+ setupEventListeners() {
24
+ this.sendButton.addEventListener('click', () => this.sendMessage());
25
+ this.userInput.addEventListener('keypress', (e) => {
26
+ if (e.key === 'Enter' && !e.shiftKey) {
27
+ e.preventDefault();
28
+ this.sendMessage();
29
+ }
30
+ });
31
+ }
32
+
33
+ async loadModel() {
34
+ try {
35
+ this.modelStatus.textContent = 'Loading model...';
36
+ this.updateProgress(0, 'Initializing...');
37
+
38
+ // Create a Web Worker for model loading
39
+ this.worker = new Worker('worker.js');
40
+
41
+ // Listen for messages from the worker
42
+ this.worker.onmessage = (event) => {
43
+ const { type, data, progress } = event.data;
44
+
45
+ if (type === 'progress') {
46
+ this.updateProgress(progress * 100, `Downloading: ${Math.round(progress * 100)}%`);
47
+ } else if (type === 'ready') {
48
+ this.pipeline = data;
49
+ this.isModelLoaded = true;
50
+ this.modelStatus.textContent = 'Ready';
51
+ this.updateProgress(100, 'Model loaded!');
52
+ this.addMessage('assistant', 'Hola! I\'m ready to chat. How can I help you today?');
53
+ } else if (type === 'error') {
54
+ this.handleError(data);
55
+ }
56
+ };
57
+
58
+ // Start model loading in worker
59
+ this.worker.postMessage({ type: 'loadModel' });
60
+
61
+ } catch (error) {
62
+ this.handleError(error);
63
+ }
64
+ }
65
+
66
+ updateProgress(percentage, text) {
67
+ this.progressFill.style.width = `${percentage}%`;
68
+ this.progressText.textContent = text;
69
+ }
70
+
71
+ async sendMessage() {
72
+ const userInput = this.userInput.value.trim();
73
+ if (!userInput || !this.isModelLoaded) return;
74
+
75
+ // Add user message to chat
76
+ this.addMessage('user', userInput);
77
+ this.userInput.value = '';
78
+ this.userInput.disabled = true;
79
+ this.sendButton.disabled = true;
80
+
81
+ try {
82
+ // Show typing indicator
83
+ const typingIndicator = this.addTypingIndicator();
84
+
85
+ // Generate response
86
+ const response = await this.generateResponse(userInput);
87
+
88
+ // Remove typing indicator and add response
89
+ typingIndicator.remove();
90
+ this.addMessage('assistant', response);
91
+
92
+ } catch (error) {
93
+ this.handleError(error);
94
+ this.userInput.disabled = false;
95
+ this.sendButton.disabled = false;
96
+ }
97
+
98
+ this.userInput.disabled = false;
99
+ this.sendButton.disabled = false;
100
+ this.userInput.focus();
101
+ }
102
+
103
+ async generateResponse(input) {
104
+ if (!this.pipeline) {
105
+ throw new Error('Model not loaded');
106
+ }
107
+
108
+ try {
109
+ const result = await this.pipeline(input, {
110
+ max_new_tokens: 100,
111
+ temperature: 0.7,
112
+ top_p: 0.9,
113
+ do_sample: true,
114
+ return_full_text: false
115
+ });
116
+
117
+ return result[0].generated_text.trim();
118
+ } catch (error) {
119
+ throw new Error('Failed to generate response: ' + error.message);
120
+ }
121
+ }
122
+
123
+ addMessage(sender, content) {
124
+ const messageDiv = document.createElement('div');
125
+ messageDiv.className = `message ${sender}`;
126
+
127
+ const messageContent = document.createElement('div');
128
+ messageContent.className = 'message-content';
129
+ messageContent.textContent = content;
130
+
131
+ messageDiv.appendChild(messageContent);
132
+ this.chatMessages.appendChild(messageDiv);
133
+
134
+ // Scroll to bottom
135
+ this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
136
+
137
+ this.messageHistory.push({ sender, content });
138
+ }
139
+
140
+ addTypingIndicator() {
141
+ const typingDiv = document.createElement('div');
142
+ typingDiv.className = 'message assistant';
143
+
144
+ const typingContent = document.createElement('div');
145
+ typingContent.className = 'message-content typing';
146
+
147
+ const dots = document.createElement('span');
148
+ dots.textContent = '...';
149
+ typingContent.appendChild(dots);
150
+
151
+ typingDiv.appendChild(typingContent);
152
+ this.chatMessages.appendChild(typingDiv);
153
+
154
+ this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
155
+
156
+ return typingDiv;
157
+ }
158
+
159
+ handleError(error) {
160
+ console.error('Error:', error);
161
+ this.modelStatus.textContent = 'Error';
162
+ this.addMessage('assistant', 'Sorry, I encountered an error. Please try again later.');
163
+
164
+ // Show error message to user
165
+ const errorMessage = document.createElement('div');
166
+ errorMessage.className = 'error-message';
167
+ errorMessage.textContent = 'An error occurred: ' + error.message;
168
+ this.chatMessages.appendChild(errorMessage);
169
+ }
170
+ }
171
+
172
+ // Initialize the application when DOM is loaded
173
+ document.addEventListener('DOMContentLoaded', () => {
174
+ new ChatAssistant();
175
+ });