forked from ANSHIKA-26/WordWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.html
277 lines (249 loc) · 7.67 KB
/
chatbot.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!-- Author (C) @rutikakengal -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Word Wise - Chat Bot</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"
rel="stylesheet"
/>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/a11y-dark.min.css"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/12.0.1/marked.min.js"></script>
<style>
body {
background-color: #f0f2f5;
display: flex;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.chat-container {
width: 100%;
max-width: 800px;
background-color: white;
margin: auto;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
height: 90vh;
position: relative;
height: 100vh;
}
#output-field {
text-align: center;
font-size: 1.25rem;
font-weight: bold;
padding: 15px;
background-color: #f0f2f5;
border-top-left-radius: 12px;
border-top-right-radius: 12px;
}
#output-container {
flex: 1;
overflow-y: auto;
padding: 20px;
display: flex;
flex-direction: column;
background-image: url(./images/background.jpg);
}
.user-message, .ai-message {
max-width: 95%;
padding: 10px 15px;
border-radius: 18px;
margin-bottom: 10px;
word-wrap: break-word;
display: inline-block; /* Add this line */
}
.user-message {
align-self: flex-end;
background-color: #0084ff;
color: white;
}
.ai-message {
align-self: flex-start;
background-color: #e4e6eb;
color: black;
}
p{
margin-bottom: 0;
}
.input-group {
margin-top: auto;
padding: 15px;
background-color: #f0f2f5;
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
}
#prompt-input {
flex: 1;
border-radius: 20px;
border: 1px solid #ccc;
padding: 10px 15px;
}
#generate-btn {
border-radius: 20px;
margin-left: 10px;
}
.loading {
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
}
.loading-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background-color: #007bff;
margin: 0 5px;
animation: loading 0.6s infinite alternate;
}
@keyframes loading {
0% { transform: scale(1); }
100% { transform: scale(1.5); }
}
</style>
</head>
<body>
<div id="Loader"></div>
<div class="chat-container">
<div id="output-field" class="text-center">
How can I help you today?
</div>
<div id="output-container"></div>
<div class="input-group mb-0">
<input
type="text"
id="prompt-input"
class="form-control"
placeholder="Type your prompt here..."
aria-label="Message input"
/>
<button id="generate-btn" class="btn btn-primary">Send</button>
</div>
</div>
<script type="importmap">
{
"imports": {
"@google/generative-ai": "https://esm.run/@google/generative-ai"
}
}
</script>
<script type="module">
import {
GoogleGenerativeAI,
HarmBlockThreshold,
HarmCategory,
} from "@google/generative-ai";
const API_KEY = "AIzaSyBF265bY6o63VuPypal-5w0-b7sr0N_O3A"; // Replace with your gemini-api actual API key
const genAI = new GoogleGenerativeAI(API_KEY);
let chat;
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
async function sendMessage(prompt) {
let model;
clearGreeting(); // Clear the greeting after sending the message
if (!chat) {
chat = await genAI
.getGenerativeModel({ model: "gemini-pro", safetySettings })
.startChat({
history: [],
generationConfig: {
maxOutputTokens: 4000, // maxOutputTokens Limit around 4096
},
});
}
model = chat;
clearInputs(); //clear the input field imidiately
try {
const result = await model.sendMessage(prompt);
const response = await result.response;
if (response) {
const text = await response.text();
displayMessage(text, "ai");
} else {
displayMessage("This content is not safe for display based on current settings.", "ai");
}
} catch (error) {
console.error("Error during message generation:", error);
displayMessage("This content is not safe for display based on current settings or an internal error.", "ai");
}
// clearInputs();
}
function displayMessage(message, sender) {
const outputContainer = document.getElementById("output-container");
const msgDiv = document.createElement("div");
msgDiv.classList.add(sender === "user" ? "user-message" : "ai-message");
if (sender === "ai") {
// Show loading animation for AI messages
msgDiv.innerHTML =
'<div class="loading">' +
'<div class="loading-dot"></div>' +
'<div class="loading-dot"></div>' +
'<div class="loading-dot"></div>' +
"</div>";
outputContainer.appendChild(msgDiv);
// Simulate processing delay
setTimeout(() => {
// Clear loading animation
msgDiv.innerHTML = marked.parse(message);
outputContainer.scrollTop = outputContainer.scrollHeight; // Scroll to bottom
}, 1500);
} else {
msgDiv.innerHTML = marked.parse(message);
outputContainer.appendChild(msgDiv);
}
// Ensure the latest message is visible
outputContainer.scrollTop = outputContainer.scrollHeight;
}
function clearInputs() {
document.getElementById("prompt-input").value = "";
}
document.getElementById("generate-btn").addEventListener("click", async () => {
const prompt = document.getElementById("prompt-input").value;
if (prompt.trim() !== "") {
displayMessage(prompt, "user");
await sendMessage(prompt);
}
});
// Function to send msg by pressing Enter button
document.getElementById("prompt-input").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault(); // Prevent the default form submission
const prompt = this.value;
if (prompt.trim() !== "") {
displayMessage(prompt, "user");
sendMessage(prompt);
}
}
});
function clearGreeting() {
const outputField = document.getElementById("output-field");
if (outputField) {
outputField.style.display = "none"; // Hide the field completely
}
}
</script>
<script>
var loader = document.getElementById("Loader");
window.addEventListener("load", function () {
loader.style.display = "none";
});
</script>
</body>
</html>