Subbu1304 commited on
Commit
a3a8858
·
verified ·
1 Parent(s): 9847f61

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +55 -5
static/script.js CHANGED
@@ -1,8 +1,58 @@
 
1
  function sendMessage() {
2
- const input = document.getElementById('user-input').value;
3
- if (input) {
4
- // Send message to Flask server (This would be integrated with Gradio's response mechanism)
5
- // Placeholder for now
6
- alert('Message sent: ' + input);
7
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
 
 
 
 
 
 
 
 
1
+ // This will handle sending the message and displaying the response in the Gradio chatbot
2
  function sendMessage() {
3
+ const input = document.getElementById("user-input").value;
4
+ const chatbox = document.getElementById("chatbot");
5
+
6
+ if (input.trim() === "") {
7
+ return; // Ignore if no input is provided
8
  }
9
+
10
+ // Add the user's message to the chatbox
11
+ chatbox.innerHTML += `
12
+ <div class="user-message">
13
+ <p>${input}</p>
14
+ </div>
15
+ `;
16
+
17
+ // Clear the input box
18
+ document.getElementById("user-input").value = "";
19
+
20
+ // Scroll to the bottom of the chat for new message
21
+ chatbox.scrollTop = chatbox.scrollHeight;
22
+
23
+ // Send the input to Gradio and get the bot response
24
+ fetch('/process_message', {
25
+ method: 'POST',
26
+ headers: {
27
+ 'Content-Type': 'application/json',
28
+ },
29
+ body: JSON.stringify({ message: input })
30
+ })
31
+ .then(response => response.json())
32
+ .then(data => {
33
+ // Add the bot's response to the chatbox
34
+ chatbox.innerHTML += `
35
+ <div class="bot-message">
36
+ <p>${data.response}</p>
37
+ </div>
38
+ `;
39
+
40
+ // Scroll to the bottom again after the bot's response
41
+ chatbox.scrollTop = chatbox.scrollHeight;
42
+ })
43
+ .catch(error => {
44
+ console.error('Error:', error);
45
+ chatbox.innerHTML += `
46
+ <div class="bot-message">
47
+ <p>Sorry, something went wrong.</p>
48
+ </div>
49
+ `;
50
+ });
51
  }
52
+
53
+ // Add an event listener for the Enter key to send messages
54
+ document.getElementById("user-input").addEventListener("keypress", function(e) {
55
+ if (e.key === "Enter") {
56
+ sendMessage();
57
+ }
58
+ });