Utkarsh Verma commited on
Commit
dd124d4
·
1 Parent(s): 8bc5476

Changes in UI

Browse files
Files changed (1) hide show
  1. templates/index.html +82 -64
templates/index.html CHANGED
@@ -8,132 +8,150 @@
8
  body {
9
  background-color: #121212;
10
  color: #f1f1f1;
11
- font-family: Arial, sans-serif;
12
  display: flex;
13
  flex-direction: column;
14
  align-items: center;
15
  padding: 20px;
16
  }
17
 
18
- h1 {
19
- color: #00ffff;
20
- }
21
-
22
  #chat-box {
23
- width: 100%;
24
- max-width: 600px;
25
  border: 1px solid #444;
26
- padding: 10px;
 
 
27
  height: 400px;
28
  overflow-y: auto;
29
- background-color: #1e1e1e;
30
- border-radius: 8px;
31
  margin-bottom: 10px;
32
  }
33
 
34
  #user-input {
35
- width: 75%;
36
  padding: 10px;
37
- border-radius: 6px;
38
- border: 1px solid #555;
39
- background-color: #2a2a2a;
40
- color: white;
41
  }
42
 
43
- button {
44
  padding: 10px 15px;
45
- background-color: #00ffff;
46
- color: black;
47
  border: none;
48
- border-radius: 6px;
 
49
  cursor: pointer;
50
- margin-left: 5px;
 
51
  }
52
 
53
- .bot-typing {
54
- color: #aaa;
 
 
 
55
  font-style: italic;
 
56
  }
57
 
58
- .mic-button {
59
- background-color: #444;
60
- color: white;
61
- border-radius: 50%;
62
- padding: 8px 10px;
63
- margin-left: 5px;
64
- border: 1px solid #666;
65
  }
66
  </style>
67
  </head>
68
  <body>
69
- <h1>AI Chatbot</h1>
70
  <div id="chat-box"></div>
71
- <div>
 
72
  <input type="text" id="user-input" placeholder="Type a message..." />
73
- <button onclick="sendMessage()">Send</button>
74
- <button class="mic-button" onclick="startListening()">🎤</button>
75
  </div>
76
 
77
  <script>
78
- let chatHistory = [];
 
 
 
 
 
 
79
 
80
- function updateChatBox() {
 
81
  const chatBox = document.getElementById("chat-box");
82
- chatBox.innerHTML = "";
83
- chatHistory.forEach(({ sender, message }) => {
84
- chatBox.innerHTML += `<p><b>${sender}:</b> ${message}</p>`;
85
- });
 
 
 
86
  chatBox.scrollTop = chatBox.scrollHeight;
 
87
  }
88
 
89
  async function sendMessage() {
90
- const inputEl = document.getElementById("user-input");
91
- const message = inputEl.value.trim();
92
- if (!message) return;
93
 
94
- chatHistory.push({ sender: "You", message });
95
- updateChatBox();
96
- inputEl.value = "";
97
 
98
- // Show typing...
99
- chatHistory.push({ sender: "Bot", message: "<span class='bot-typing'>Typing...</span>" });
100
- updateChatBox();
 
 
 
 
 
101
 
102
  try {
103
  const response = await fetch("/chat", {
104
  method: "POST",
105
  headers: { "Content-Type": "application/json" },
106
- body: JSON.stringify({ message }),
107
  });
108
 
109
  const data = await response.json();
110
- chatHistory.pop(); // Remove "Typing..." message
111
- chatHistory.push({ sender: "Bot", message: data.reply || "Error in response" });
112
- updateChatBox();
113
- } catch (e) {
114
- chatHistory.pop();
115
- chatHistory.push({ sender: "Bot", message: "Failed to reach server." });
116
- updateChatBox();
117
  }
118
  }
119
 
120
- // 🎤 Speech-to-text
 
 
 
 
 
 
 
 
121
  function startListening() {
122
  const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
123
- recognition.lang = "en-US";
124
  recognition.interimResults = false;
125
- recognition.maxAlternatives = 1;
126
 
127
- recognition.onresult = (event) => {
128
- document.getElementById("user-input").value = event.results[0][0].transcript;
 
 
129
  };
130
 
131
- recognition.onerror = (event) => {
132
- alert("Speech recognition error: " + event.error);
133
  };
134
 
135
  recognition.start();
136
  }
137
  </script>
138
  </body>
139
- </html>
 
8
  body {
9
  background-color: #121212;
10
  color: #f1f1f1;
11
+ font-family: 'Segoe UI', sans-serif;
12
  display: flex;
13
  flex-direction: column;
14
  align-items: center;
15
  padding: 20px;
16
  }
17
 
 
 
 
 
18
  #chat-box {
 
 
19
  border: 1px solid #444;
20
+ background-color: #1e1e1e;
21
+ width: 80%;
22
+ max-width: 800px;
23
  height: 400px;
24
  overflow-y: auto;
25
+ padding: 10px;
26
+ border-radius: 10px;
27
  margin-bottom: 10px;
28
  }
29
 
30
  #user-input {
31
+ width: 60%;
32
  padding: 10px;
33
+ border-radius: 5px;
34
+ border: none;
35
+ margin-right: 10px;
 
36
  }
37
 
38
+ #send-btn, #mic-btn {
39
  padding: 10px 15px;
 
 
40
  border: none;
41
+ background-color: #1db954;
42
+ color: white;
43
  cursor: pointer;
44
+ border-radius: 5px;
45
+ margin-right: 5px;
46
  }
47
 
48
+ #send-btn:hover, #mic-btn:hover {
49
+ background-color: #1ed760;
50
+ }
51
+
52
+ .typing {
53
  font-style: italic;
54
+ opacity: 0.7;
55
  }
56
 
57
+ .chat-wrapper {
58
+ display: flex;
59
+ align-items: center;
60
+ width: 80%;
61
+ max-width: 800px;
 
 
62
  }
63
  </style>
64
  </head>
65
  <body>
66
+ <h1>🧠 AI Chatbot</h1>
67
  <div id="chat-box"></div>
68
+
69
+ <div class="chat-wrapper">
70
  <input type="text" id="user-input" placeholder="Type a message..." />
71
+ <button id="send-btn" onclick="sendMessage()">Send</button>
72
+ <button id="mic-btn" onclick="startListening()">🎤</button>
73
  </div>
74
 
75
  <script>
76
+ // Load saved chat history
77
+ window.onload = function () {
78
+ const history = localStorage.getItem("chatHistory");
79
+ if (history) {
80
+ document.getElementById("chat-box").innerHTML = history;
81
+ }
82
+ };
83
 
84
+ // Save chat history
85
+ function saveHistory() {
86
  const chatBox = document.getElementById("chat-box");
87
+ localStorage.setItem("chatHistory", chatBox.innerHTML);
88
+ }
89
+
90
+ function updateChatBox(role, message) {
91
+ let chatBox = document.getElementById("chat-box");
92
+ const tag = role === "user" ? "You" : "Bot";
93
+ chatBox.innerHTML += `<p><b>${tag}:</b> ${message}</p>`;
94
  chatBox.scrollTop = chatBox.scrollHeight;
95
+ saveHistory();
96
  }
97
 
98
  async function sendMessage() {
99
+ let userInput = document.getElementById("user-input").value.trim();
100
+ if (!userInput) return;
 
101
 
102
+ updateChatBox("user", userInput);
 
 
103
 
104
+ document.getElementById("user-input").value = "";
105
+
106
+ const chatBox = document.getElementById("chat-box");
107
+ const typingIndicator = document.createElement("p");
108
+ typingIndicator.className = "typing";
109
+ typingIndicator.innerText = "Bot is typing...";
110
+ chatBox.appendChild(typingIndicator);
111
+ chatBox.scrollTop = chatBox.scrollHeight;
112
 
113
  try {
114
  const response = await fetch("/chat", {
115
  method: "POST",
116
  headers: { "Content-Type": "application/json" },
117
+ body: JSON.stringify({ message: userInput })
118
  });
119
 
120
  const data = await response.json();
121
+ chatBox.removeChild(typingIndicator);
122
+ updateChatBox("bot", data.reply || "Error in response");
123
+ } catch (err) {
124
+ chatBox.removeChild(typingIndicator);
125
+ updateChatBox("bot", "Something went wrong.");
 
 
126
  }
127
  }
128
 
129
+ // Send with Enter key
130
+ document.getElementById("user-input").addEventListener("keydown", function (event) {
131
+ if (event.key === "Enter") {
132
+ event.preventDefault();
133
+ sendMessage();
134
+ }
135
+ });
136
+
137
+ // Speech to text
138
  function startListening() {
139
  const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
140
+ recognition.lang = 'en-US';
141
  recognition.interimResults = false;
 
142
 
143
+ recognition.onresult = function (event) {
144
+ const transcript = event.results[0][0].transcript;
145
+ document.getElementById("user-input").value = transcript;
146
+ sendMessage();
147
  };
148
 
149
+ recognition.onerror = function () {
150
+ updateChatBox("bot", "Speech recognition failed.");
151
  };
152
 
153
  recognition.start();
154
  }
155
  </script>
156
  </body>
157
+ </html>