-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
311 lines (294 loc) · 10.4 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<html>
<head>
<meta charset="UTF-8">
<title>SamirAI</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com?plugins=forms,typography"></script>
<script src="https://unpkg.com/unlazy@0.11.3/dist/unlazy.with-hashing.iife.js" defer init></script>
<script type="text/javascript">
window.tailwind.config = {
darkMode: ['class'],
theme: {
extend: {
colors: {
border: 'hsl(var(--border))',
input: 'hsl(var(--input))',
ring: 'hsl(var(--ring))',
background: 'hsl(var(--background))',
foreground: 'hsl(var(--foreground))',
primary: {
DEFAULT: 'hsl(var(--primary))',
foreground: 'hsl(var(--primary-foreground))'
},
secondary: {
DEFAULT: 'hsl(var(--secondary))',
foreground: 'hsl(var(--secondary-foreground))'
},
destructive: {
DEFAULT: 'hsl(var(--destructive))',
foreground: 'hsl(var(--destructive-foreground))'
},
muted: {
DEFAULT: 'hsl(var(--muted))',
foreground: 'hsl(var(--muted-foreground))'
},
accent: {
DEFAULT: 'hsl(var(--accent))',
foreground: 'hsl(var(--accent-foreground))'
},
popover: {
DEFAULT: 'hsl(var(--popover))',
foreground: 'hsl(var(--popover-foreground))'
},
card: {
DEFAULT: 'hsl(var(--card))',
foreground: 'hsl(var(--card-foreground))'
},
user: {
DEFAULT: 'hsl(210, 100%, 50%)', /* Blue background for user messages */
foreground: 'hsl(0, 0%, 100%)' /* White text for user messages */
}
},
}
}
}
</script>
<style type="text/tailwindcss">
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 0 0% 3.9%;
--card: 0 0% 100%;
--card-foreground: 0 0% 3.9%;
--popover: 0 0% 100%;
--popover-foreground: 0 0% 3.9%;
--primary: 0 0% 9%;
--primary-foreground: 0 0% 98%;
--secondary: 0 0% 96.1%;
--secondary-foreground: 0 0% 9%;
--muted: 0 0% 96.1%;
--muted-foreground: 0 0% 45.1%;
--accent: 0 0% 96.1%;
--accent-foreground: 0 0% 9%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 0 0% 98%;
--border: 0 0% 89.8%;
--input: 0 0% 89.8%;
--ring: 0 0% 3.9%;
}
.dark {
--background: 0 0% 3.9%;
--foreground: 0 0% 98%;
--card: 0 0% 3.9%;
--card-foreground: 0 0% 98%;
--popover: 0 0% 3.9%;
--popover-foreground: 0 0% 98%;
--primary: 0 0% 98%;
--primary-foreground: 0 0% 9%;
--secondary: 0 0% 14.9%;
--secondary-foreground: 0 0% 98%;
--muted: 0 0% 14.9%;
--muted-foreground: 0 0% 63.9%;
--accent: 0 0% 14.9%;
--accent-foreground: 0 0% 98%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 0 0% 98%;
--border: 0 0% 14.9%;
--input: 0 0% 14.9%;
--ring: 0 0% 83.1%;
}
}
/* Style for code blocks */
.code-block {
background-color: #f5f5f5;
border-left: 4px solid #007acc;
padding: 1rem;
margin: 1rem 0;
overflow-x: auto;
white-space: pre-wrap; /* Preserve whitespace */
}
.code-block pre {
margin: 0;
}
.code-block code {
display: block;
font-family: monospace;
}
/* Typing indicator styles */
.typing-indicator {
display: flex;
gap: 4px;
margin: 0 auto;
width: 60px;
justify-content: center;
align-items: center;
height: 24px;
}
.typing-indicator div {
width: 6px;
height: 6px;
background-color: hsl(var(--foreground));
border-radius: 50%;
animation: jump 1s infinite;
}
.typing-indicator div:nth-child(1) {
animation-delay: 0s;
}
.typing-indicator div:nth-child(2) {
animation-delay: 0.2s;
}
.typing-indicator div:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes jump {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
}
/* Loader styles */
.loader {
border: 4px solid #f3f3f3;
border-top: 4px solid hsl(var(--foreground));
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
margin: 10px auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Copy button styles */
.copy-button {
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
background-color: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
padding: 4px;
margin-left: 8px;
}
.copy-button img {
width: 16px;
height: 16px;
}
</style>
</head>
<body>
<div class="flex flex-col h-screen bg-background">
<div class="flex justify-between items-center p-4 bg-primary shadow-lg rounded-b-lg">
<h1 class="text-2xl font-semibold text-primary-foreground">SamirAI</h1>
<button class="bg-secondary text-secondary-foreground hover:bg-secondary/80 p-2 rounded-lg transition duration-300" id="newChatButton">
New Chat
</button>
</div>
<div class="flex-1 overflow-y-auto p-4 bg-card rounded-lg shadow-md" id="chatContainer">
<!-- Messages will be appended here -->
</div>
<div class="p-4 bg-card rounded-lg shadow-md">
<input type="text" id="userInput" placeholder="Type your message here" class="w-full p-2 border border-border rounded-lg focus:outline-none focus:ring focus:ring-ring transition duration-300" />
<button id="sendButton" class="bg-accent text-accent-foreground hover:bg-accent/80 p-2 rounded-lg mt-2 transition duration-300">
<img aria-hidden="true" alt="send-icon" src="https://openui.fly.dev/openui/24x24.svg?text=📤" />
</button>
</div>
</div>
<script>
let uid = generateUID();
let isGenerating = false;
document.getElementById('sendButton').addEventListener('click', async () => {
const message = document.getElementById('userInput').value;
if (!message || isGenerating) return;
// Disable input and send button
disableUserInput();
appendMessage('You', message, 'user');
document.getElementById('userInput').value = '';
showLoader();
try {
const response = await fetch(`https://www.samirxpikachu.run.place/Gemini?text=${encodeURIComponent(message)}&uid=${uid}`, {
method: 'GET',
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const reply = data.candidates[0].content.parts[0].text;
appendMessage('SamirAI', formatMarkdown(reply));
} catch (error) {
console.error('Error:', error);
appendMessage('SamirAI', 'Sorry, something went wrong. Please try again later.');
} finally {
hideLoader();
// Enable input and send button
enableUserInput();
}
});
document.getElementById('newChatButton').addEventListener('click', () => {
uid = generateUID();
document.getElementById('chatContainer').innerHTML = '';
});
function appendMessage(sender, message, type = 'response') {
const messageDiv = document.createElement('div');
messageDiv.className = `p-3 rounded-lg mb-2 transition duration-300 ${type === 'user' ? 'bg-user text-user-foreground' : 'bg-muted text-muted-foreground'}`;
messageDiv.innerHTML = `
<span class="font-bold text-accent">${sender}:</span>
<p>${message}</p>
${type === 'response' ? `<button class="copy-button" onclick="copyToClipboard(this.previousElementSibling.innerText)">
<img aria-hidden="true" alt="copy-icon" src="https://openui.fly.dev/openui/24x24.svg?text=📋" />
</button>` : ''}
`;
document.getElementById('chatContainer').appendChild(messageDiv);
document.getElementById('chatContainer').scrollTop = document.getElementById('chatContainer').scrollHeight;
}
function formatMarkdown(text) {
// Convert **bold** to <strong>
text = text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
// Convert `code` to <code> inside <pre>
text = text.replace(/```(.*?)```/gs, '<div class="code-block"><pre><code>$1</code></pre></div>');
// Convert `inline code` to <code>
text = text.replace(/`([^`]*)`/g, '<code>$1</code>');
// Convert newlines to <br> for line breaks
text = text.replace(/\n/g, '<br>');
return text;
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
function showLoader() {
const loader = document.createElement('div');
loader.className = 'loader';
loader.id = 'loader';
document.getElementById('chatContainer').appendChild(loader);
document.getElementById('chatContainer').scrollTop = document.getElementById('chatContainer').scrollHeight;
isGenerating = true;
}
function hideLoader() {
const loader = document.getElementById('loader');
if (loader) {
loader.remove();
}
isGenerating = false;
}
function disableUserInput() {
document.getElementById('userInput').disabled = true;
document.getElementById('sendButton').disabled = true;
}
function enableUserInput() {
document.getElementById('userInput').disabled = false;
document.getElementById('sendButton').disabled = false;
}
function generateUID() {
// Generate a random UID (for simplicity, using a timestamp + random number)
return Date.now() + Math.floor(Math.random() * 1000);
}
</script>
</body>
</html>