-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
219 lines (198 loc) · 11.4 KB
/
index.html
File metadata and controls
219 lines (198 loc) · 11.4 KB
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
<!DOCTYPE html><html lang="en"><head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" crossorigin="anonymous">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Assistant</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.3/dist/tailwind.min.css" rel="stylesheet">
<style>
.chat-message {
display: block;
word-wrap: break-word;
margin-bottom: 2px;
}
.spinner {
display: none;
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-top-color: #3498db;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
#chatbox::-webkit-scrollbar {
width: 8px;
}
.chatbox::-webkit-scrollbar-track {
background-color: #F3F4F6;
}
.chatbox::-webkit-scrollbar-thumb {
background-color: #6B7280;
border-radius: 4px;
}
</style>
</head>
<body class="bg-gray-900">
<div class="container mx-auto px-4 h-full relative max-w-md">
<div class=" py-6 px-8 rounded max-w-md mx-auto h-full flex flex-col">
<div id="chatbox" class="flex flex-col h-full overflow-y-scroll pb-16 flex-grow mb-20 space-y-2">
</div>
<div class="fixed bottom-0 left-0 w-full bg-gray-800 py-2 px-4 max-w-md mx-auto ml-auto mr-auto md:bottom-0 md:left-auto md:right-auto md:transform" style="z-index: 10;">
<form id="chatForm">
<div class="flex w-full">
<input id="chatInput" type="text" class="w-full py-2 px-4 mb-4 rounded-l bg-gray-700 text-white border border-gray-600" placeholder="Type your message">
<button id="infoButton" type="button" class="rounded-r bg-gray-700 text-white border border-gray-600 px-4 mb-4 relative" onclick="showInfoPopup(event)">
<div id="infoTooltip" class="absolute hidden -left-40 bottom-full mb-2 bg-gray-700 text-white text-xs p-2 rounded-lg shadow" style="width: 200px;">Simply ask for an image by describing it or type <strong>/image</strong> followed by the description to generate an image.</div>
<i class="fas fa-image"></i>
</button>
</div>
<button id="sendButton" class="w-full py-2 px-4 bg-green-500 text-white font-bold rounded">Send</button>
</form>
</div>
<div class="spinner absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 z-20"></div>
</div>
</div>
<script>
function displayLargeImage(imageUrl) {
const largeImageContainer = document.createElement('div');
largeImageContainer.className = 'fixed top-0 left-0 w-screen p-2 h-screen bg-black bg-opacity-70 flex justify-center items-center z-30';
largeImageContainer.onclick = (e) => {
e.stopPropagation();
largeImageContainer.remove();
}
const largeImage = document.createElement('img');
largeImage.src = imageUrl;
largeImage.className = 'max-w-full max-h-full rounded-lg';
const downloadLargeButton = document.createElement("button");
downloadLargeButton.innerHTML = "Download";
downloadLargeButton.className = "absolute bottom-6 md:bottom-10 left-1/2 p-2 text-white rounded bg-green-500 bg-opacity-80 hover:bg-opacity-100 transform -translate-x-1/2";
downloadLargeButton.onclick = (e) => {
e.stopPropagation();
const a = document.createElement('a');
a.href = imageUrl;
a.download = 'generated-image.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
const closeButton = document.createElement('button');
closeButton.innerHTML = '<i class="fas fa-times"></i>';
closeButton.className = 'absolute top-6 right-6 p-2 text-red-500 rounded-full bg-opacity-80 hover:bg-opacity-100';
closeButton.onclick = (e) => {
e.stopPropagation();
largeImageContainer.remove();
};
largeImageContainer.appendChild(largeImage);
largeImageContainer.appendChild(downloadLargeButton);
largeImageContainer.appendChild(closeButton);
document.body.appendChild(largeImageContainer);
}
const chatForm = document.getElementById("chatForm");
const sendButton = document.getElementById("sendButton");
const chatInput = document.getElementById('chatInput');
chatInput.focus();
const chatbox = document.getElementById('chatbox');
const spinner = document.querySelector('.spinner');
function displayMessage(message, isUser) {
const msgElem = document.createElement('p');
msgElem.textContent = message;
msgElem.className = isUser ? 'chat-message block text-right text-white mt-2 bg-green-500 rounded-tl-lg rounded-br-lg rounded-bl-sm px-4 py-2' : 'chat-message block text-white mt-2 bg-gray-600 rounded-tr-lg rounded-br-lg rounded-bl-md px-4 py-2';
chatbox.appendChild(msgElem);
}
async function callApi(apiUrl, prompt, prependPersona = false) {
if (prependPersona) {
prompt = "Follow instructions precisely! If the user asks to generate, create or make an image, photo, or picture by describing it, You will reply with '/image' + description. Otherwise, You will respond normally. Avoid additional explanations." + prompt;
}
chatInput.value = apiUrl === "https://backend.buildpicoapps.com/aero/run/image-generation-api?pk=v1-Z0FBQUFBQm53RVBIYVdDR3JMbGczdnlpcnpWVGRmOUhkV2dnUUpxQzRFYk1QYml2Zm1hczZkYU93a1dKeGdWTGctOTRKSnp0R2pST0dmZjR3Y3FDNHF0eDBDaERwS0dEUkE9PQ==" ? "Generating..." : "Typing...";
chatInput.disabled = true;
sendButton.disabled = true;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({prompt})
});
chatInput.value = "";
chatInput.disabled = false;
sendButton.disabled = false;
chatInput.focus();
return response.json();
}
function handleError(error) {
console.error('Error:', error);
displayMessage('An error occurred. Please try again.', false);
}
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) return;
displayMessage(message, true);
chatInput.value = '';
const apiUrl = message.startsWith('/image') ? 'https://backend.buildpicoapps.com/aero/run/image-generation-api?pk=v1-Z0FBQUFBQm53RVBIYVdDR3JMbGczdnlpcnpWVGRmOUhkV2dnUUpxQzRFYk1QYml2Zm1hczZkYU93a1dKeGdWTGctOTRKSnp0R2pST0dmZjR3Y3FDNHF0eDBDaERwS0dEUkE9PQ=='
: 'https://backend.buildpicoapps.com/aero/run/llm-api?pk=v1-Z0FBQUFBQm53RVBIYVdDR3JMbGczdnlpcnpWVGRmOUhkV2dnUUpxQzRFYk1QYml2Zm1hczZkYU93a1dKeGdWTGctOTRKSnp0R2pST0dmZjR3Y3FDNHF0eDBDaERwS0dEUkE9PQ==';
try {
const prependPersona = apiUrl === 'https://backend.buildpicoapps.com/aero/run/llm-api?pk=v1-Z0FBQUFBQm53RVBIYVdDR3JMbGczdnlpcnpWVGRmOUhkV2dnUUpxQzRFYk1QYml2Zm1hczZkYU93a1dKeGdWTGctOTRKSnp0R2pST0dmZjR3Y3FDNHF0eDBDaERwS0dEUkE9PQ==';
const data = await callApi(apiUrl, message.startsWith('/image') ? message.substring(6).trim() : message, prependPersona);
if (data.status === 'success') {
if (message.toLowerCase().startsWith('/image')) {
handleImageResponse(data);
} else {
if (data.text.trim().toLowerCase().startsWith('/image')) {
const imageDescription = data.text.substring(data.text.toLowerCase().indexOf('/image') + 6).trim();
const imageData = await callApi('https://backend.buildpicoapps.com/aero/run/image-generation-api?pk=v1-Z0FBQUFBQm53RVBIYVdDR3JMbGczdnlpcnpWVGRmOUhkV2dnUUpxQzRFYk1QYml2Zm1hczZkYU93a1dKeGdWTGctOTRKSnp0R2pST0dmZjR3Y3FDNHF0eDBDaERwS0dEUkE9PQ==', imageDescription, false);
handleImageResponse(imageData);
} else {
displayMessage(data.text, false);
}
}
} else {
handleError(data);
}
function handleImageResponse(imageData) {
if (imageData.status === 'success') {
const imageContainer = document.createElement('div');
imageContainer.className = 'mx-auto mt-2 relative w-48 h-48 bg-center border-4 rounded-xl border-gray-700 bg-cover';
imageContainer.style.backgroundImage = `url(${imageData.imageUrl})`;
imageContainer.onclick = (e) => {
e.stopPropagation();
displayLargeImage(imageData.imageUrl);
}
const downloadButton = document.createElement('button');
downloadButton.innerHTML = '';
downloadButton.className = 'absolute bottom-0 right-0 p-2 text-yellow-500 rounded-tl hover:bg-blue-500 hover:text-white';
downloadButton.onclick = () => {
const a = document.createElement('a');
a.href = imageData.imageUrl;
a.download = 'generated-image.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
imageContainer.appendChild(downloadButton);
chatbox.appendChild(imageContainer);
} else {
handleError(imageData);
}
}
} catch (error) {
handleError(error);
}
});
function showInfoPopup(e) {
e.preventDefault();
const tooltip = document.getElementById("infoTooltip");
if (tooltip.classList.contains("hidden")) {
tooltip.classList.remove("hidden");
} else {
tooltip.classList.add("hidden");
}
}
</script>
<script data-name="BMC-Widget" data-cfasync="false" src="https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js" data-id="Sajkhan" data-description="Support me on Buy me a coffee!" data-message="" data-color="#FF813F" data-position="Right" data-x_margin="18" data-y_margin="18"></script>
</body></html>