File size: 6,050 Bytes
c41cd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Main application logic
class ChatAssistant {
    constructor() {
        this.pipeline = null;
        this.isModelLoaded = false;
        this.worker = null;
        this.messageHistory = [];
        
        this.initializeElements();
        this.setupEventListeners();
        this.loadModel();
    }
    
    initializeElements() {
        this.chatMessages = document.getElementById('chatMessages');
        this.userInput = document.getElementById('userInput');
        this.sendButton = document.getElementById('sendButton');
        this.modelStatus = document.getElementById('modelStatus');
        this.progressFill = document.getElementById('progressFill');
        this.progressText = document.getElementById('progressText');
    }
    
    setupEventListeners() {
        this.sendButton.addEventListener('click', () => this.sendMessage());
        this.userInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                this.sendMessage();
            }
        });
    }
    
    async loadModel() {
        try {
            this.modelStatus.textContent = 'Loading model...';
            this.updateProgress(0, 'Initializing...');
            
            // Create a Web Worker for model loading
            this.worker = new Worker('worker.js');
            
            // Listen for messages from the worker
            this.worker.onmessage = (event) => {
                const { type, data, progress } = event.data;
                
                if (type === 'progress') {
                    this.updateProgress(progress * 100, `Downloading: ${Math.round(progress * 100)}%`);
                } else if (type === 'ready') {
                    this.pipeline = data;
                    this.isModelLoaded = true;
                    this.modelStatus.textContent = 'Ready';
                    this.updateProgress(100, 'Model loaded!');
                    this.addMessage('assistant', 'Hola! I\'m ready to chat. How can I help you today?');
                } else if (type === 'error') {
                    this.handleError(data);
                }
            };
            
            // Start model loading in worker
            this.worker.postMessage({ type: 'loadModel' });
            
        } catch (error) {
            this.handleError(error);
        }
    }
    
    updateProgress(percentage, text) {
        this.progressFill.style.width = `${percentage}%`;
        this.progressText.textContent = text;
    }
    
    async sendMessage() {
        const userInput = this.userInput.value.trim();
        if (!userInput || !this.isModelLoaded) return;
        
        // Add user message to chat
        this.addMessage('user', userInput);
        this.userInput.value = '';
        this.userInput.disabled = true;
        this.sendButton.disabled = true;
        
        try {
            // Show typing indicator
            const typingIndicator = this.addTypingIndicator();
            
            // Generate response
            const response = await this.generateResponse(userInput);
            
            // Remove typing indicator and add response
            typingIndicator.remove();
            this.addMessage('assistant', response);
            
        } catch (error) {
            this.handleError(error);
            this.userInput.disabled = false;
            this.sendButton.disabled = false;
        }
        
        this.userInput.disabled = false;
        this.sendButton.disabled = false;
        this.userInput.focus();
    }
    
    async generateResponse(input) {
        if (!this.pipeline) {
            throw new Error('Model not loaded');
        }
        
        try {
            const result = await this.pipeline(input, {
                max_new_tokens: 100,
                temperature: 0.7,
                top_p: 0.9,
                do_sample: true,
                return_full_text: false
            });
            
            return result[0].generated_text.trim();
        } catch (error) {
            throw new Error('Failed to generate response: ' + error.message);
        }
    }
    
    addMessage(sender, content) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${sender}`;
        
        const messageContent = document.createElement('div');
        messageContent.className = 'message-content';
        messageContent.textContent = content;
        
        messageDiv.appendChild(messageContent);
        this.chatMessages.appendChild(messageDiv);
        
        // Scroll to bottom
        this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
        
        this.messageHistory.push({ sender, content });
    }
    
    addTypingIndicator() {
        const typingDiv = document.createElement('div');
        typingDiv.className = 'message assistant';
        
        const typingContent = document.createElement('div');
        typingContent.className = 'message-content typing';
        
        const dots = document.createElement('span');
        dots.textContent = '...';
        typingContent.appendChild(dots);
        
        typingDiv.appendChild(typingContent);
        this.chatMessages.appendChild(typingDiv);
        
        this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
        
        return typingDiv;
    }
    
    handleError(error) {
        console.error('Error:', error);
        this.modelStatus.textContent = 'Error';
        this.addMessage('assistant', 'Sorry, I encountered an error. Please try again later.');
        
        // Show error message to user
        const errorMessage = document.createElement('div');
        errorMessage.className = 'error-message';
        errorMessage.textContent = 'An error occurred: ' + error.message;
        this.chatMessages.appendChild(errorMessage);
    }
}

// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    new ChatAssistant();
});