MosbergControl commited on
Commit
6bdd515
·
1 Parent(s): da83245

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +39 -0
script.js CHANGED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.getElementById('send-button').addEventListener('click', function() {
2
+ let userInput = document.getElementById('user-input').value.trim();
3
+ if (userInput) {
4
+ displayMessage(userInput, 'user');
5
+ sendMessage(userInput);
6
+ document.getElementById('user-input').value = '';
7
+ }
8
+ });
9
+
10
+ function sendMessage(message) {
11
+ fetch('https://huggingface.co/spaces/Mosberg-Control/CodeNinjaOpenChat', {
12
+ method: 'POST',
13
+ headers: {
14
+ 'Content-Type': 'application/json',
15
+ },
16
+ body: JSON.stringify({ message: message })
17
+ })
18
+ .then(response => {
19
+ if (!response.ok) {
20
+ throw new Error(`HTTP error! status: ${response.status}`);
21
+ }
22
+ return response.json();
23
+ })
24
+ .then(data => {
25
+ displayMessage(data.response, 'bot');
26
+ })
27
+ .catch((error) => {
28
+ console.error('Error:', error);
29
+ displayMessage(`Sorry, there was an error processing your request. Error: ${error.message}`, 'error');
30
+ });
31
+ }
32
+
33
+ function displayMessage(message, sender) {
34
+ let chatBox = document.getElementById('chat-box');
35
+ let newMessage = document.createElement('div');
36
+ newMessage.classList.add('message', sender);
37
+ newMessage.textContent = message;
38
+ chatBox.appendChild(newMessage);
39
+ }