-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
192 lines (180 loc) · 5.68 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Kids Chat Bot</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: 'Comic Sans MS', 'Comic Sans', cursive;
background-color: #f0f8ff;
}
.chat-container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
border-radius: 10px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.chat-box {
height: 400px;
overflow-y: scroll;
border: 1px solid #ccc;
border-radius: 10px;
padding: 10px;
background-color: #e6f7ff;
}
.chat-box div {
margin: 10px 0;
}
.question {
font-weight: bold;
color: #ff6347;
}
.answer {
margin-left: 20px;
color: #4682b4;
}
.input-box {
margin-top: 20px;
}
.input-box input[type="text"] {
width: calc(100% - 90px);
padding: 10px;
border-radius: 20px;
border: 2px solid #87ceeb;
}
.submit-btn {
padding: 10px 20px;
border-radius: 20px;
border: none;
background-color: #87ceeb;
color: #fff;
cursor: pointer;
transition: background-color 0.3s, transform 0.3s;
}
.submit-btn:hover {
background-color: #4682b4;
transform: scale(1.1);
}
.input-box input[type="submit"] {
padding: 10px 20px;
border-radius: 20px;
border: none;
background-color: #87ceeb;
color: #fff;
cursor: pointer;
transition: background-color 0.3s;
}
.input-box input[type="submit"]:hover {
background-color: #4682b4;
}
.button-box {
margin-top: 10px;
}
.button-box button {
padding: 10px 20px;
border-radius: 20px;
border: none;
background-color: #87ceeb;
color: #fff;
cursor: pointer;
transition: background-color 0.3s;
margin: 5px;
}
.button-box button:hover {
background-color: #4682b4;
}
.api-key-box {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container chat-container">
<h1 class="text-center mb-4">Ask Your Questions</h1>
<div class="api-key-box text-center">
<input type="text" id="apiKey" class="form-control d-inline" placeholder="Enter your Gemini API key...">
<button class="btn d-inline submit-btn" onclick="submitApiKey()">Submit API Key</button>
</div>
<div class="chat-box" id="chat-box"></div>
<div class="input-box text-center">
<input type="text" id="question" class="form-control d-inline" placeholder="Type your question here..." onkeypress="handleKeyPress(event)">
<input type="submit" class="btn d-inline" value="Send" onclick="sendQuestion()">
</div>
<div class="button-box text-center">
<button onclick="clearChat()">Clear</button>
<button onclick="downloadChat()">Download Chat</button>
</div>
</div>
<script>
let geminiToken = "";
function submitApiKey() {
geminiToken = document.getElementById('apiKey').value.trim();
if (!geminiToken) {
alert('Please enter a valid API key!');
return;
}
document.getElementById('apiKey').value = ''; // Clear the input field after submission
document.getElementById('apiKey').placeholder = '******'; // Change placeholder after submission
document.getElementById('apiKey').disabled = true; // Disable the input field after submission
alert('API Key submitted successfully!');
}
async function sendQuestion() {
const question = document.getElementById('question').value;
if (!question) return;
const chatBox = document.getElementById('chat-box');
const userQuestionDiv = document.createElement('div');
userQuestionDiv.className = 'question';
userQuestionDiv.textContent = 'You: ' + question;
chatBox.appendChild(userQuestionDiv);
const payload = {
contents: [{ parts: [{ text: question }] }],
generationConfig: { maxOutputTokens: 10000 }
};
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${geminiToken}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (response.ok) {
const result = await response.json();
const answer = result.candidates[0].content.parts[0].text;
const botAnswerDiv = document.createElement('div');
botAnswerDiv.className = 'answer';
botAnswerDiv.textContent = 'Bot: ' + answer;
chatBox.appendChild(botAnswerDiv);
chatBox.scrollTop = chatBox.scrollHeight; // Scroll to the bottom
document.getElementById('question').value = ''; // Clear the input field
} else {
const errorDiv = document.createElement('div');
errorDiv.className = 'answer text-danger';
errorDiv.textContent = 'Error: Unable to get a response from the API';
chatBox.appendChild(errorDiv);
}
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendQuestion();
}
}
function clearChat() {
const chatBox = document.getElementById('chat-box');
chatBox.innerHTML = '';
}
function downloadChat() {
const chatBox = document.getElementById('chat-box');
const chatText = chatBox.innerText;
const blob = new Blob([chatText], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'chat.txt';
a.click();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>