ehristoforu commited on
Commit
71e4da3
1 Parent(s): 0cd79b5

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +642 -19
index.html CHANGED
@@ -1,19 +1,642 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Solatiam ASE</title>
7
+ <link rel="icon" href="https://huggingface.co/spaces/gramota/ai/resolve/main/assets/logo_global.png" type="image/png">
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
+ <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
11
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
12
+ <style>
13
+ *{box-sizing:border-box}
14
+ body{margin:0;padding:0;font-family:"Poppins",sans-serif;background-color:#f8f9fa}
15
+ a{text-decoration:none;color:#1a0dab}
16
+ a:hover{text-decoration:underline}
17
+ .main-content{display:flex;flex-direction:column;align-items:center;padding:50px 20px}
18
+ .search-container{width:100%;max-width:700px;position:relative;width:60%;margin-bottom:20px}
19
+ .search-box{width:100%;padding:12px 16px;border:2px solid #4285f4;border-radius:24px;box-shadow:0 2px 4px rgba(0,0,0,0.1);transition:box-shadow .2s ease-in-out,width .3s ease,border-color .3s ease;display:flex;align-items:center}
20
+ .search-box:focus-within{box-shadow:0 4px 8px rgba(32,33,36,0.35);border-color:#ea4335}
21
+ #search-query{width:calc(100% - 40px);border:none;outline:0;font-size:16px;padding:4px 0;transition:font-size .2s ease}
22
+ #search-query::placeholder{color:#9aa0a6;transition:color .2s ease}
23
+ #search-query:focus{font-size:18px}
24
+ #search-query:focus::placeholder{color:transparent}
25
+ #search-form button{background:0 0;border:none;cursor:pointer;padding:8px;margin-left:10px;transition:transform .2s ease}
26
+ #search-form button:hover{transform:scale(1.1)}
27
+ #search-form button svg{display:none}
28
+ #search-form button::after{content:"\f002";color:#9aa0a6;transition:color .2s ease,transform .2s ease;font:900 1.2em "Font Awesome 5 Free"}
29
+ #search-form button:hover::after{color:#4285f4;transform:scale(1.1)}
30
+ #suggestions{width:calc(80% - 32px);background-color:#fff;border:none;border-radius:8px;box-shadow:0 4px 6px rgba(32,33,36,0.28);display:none;position:absolute;top:100%;left:0;z-index:10;opacity:0;transform:translateY(10px);transition:opacity .3s ease,transform .3s ease;padding:10px 0}
31
+ @keyframes spin {
32
+ 0%{transform:rotate(0)}
33
+ 100%{transform:rotate(360deg)}
34
+ }
35
+ #suggestions ul{list-style-type:none;padding:0;margin:0}
36
+ #suggestions li{padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;transition:background-color .2s ease}
37
+ #suggestions li:hover{background-color:#e9e9e9}
38
+ #suggestions li.selected{background-color:#f0f0f0}
39
+ .search-box:focus-within+#suggestions,.search-box:hover+#suggestions{display:block;opacity:1;transform:translateY(0)}
40
+ #results{width:100%;max-width:700px;margin-top:20px}
41
+ .result,.ai-result{margin-bottom:20px;padding:15px;border-radius:8px;background-color:#fff;box-shadow:0 2px 4px rgba(0,0,0,0.1);opacity:0;transform:translateY(10px);transition:opacity .3s ease,transform .3s ease;animation:fadeInUp .5s ease forwards}
42
+ .ai-result{background-color:#f0f0f5;width:100%;max-width:800px}
43
+ @keyframes fadeInUp {
44
+ from{opacity:0;transform:translateY(20px)}
45
+ to{opacity:1;transform:translateY(0)}
46
+ }
47
+ .result.show,.ai-result.show{opacity:1;transform:translateY(0)}
48
+ .result:hover,.ai-result:hover{box-shadow:0 4px 8px rgba(0,0,0,0.2)}
49
+ .result h3{margin:0 0 5px;font-size:1.2rem;color:#222}
50
+ .result .url{color:#202124;font-size:.9rem;margin-bottom:8px;display:block;max-width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
51
+ .result p{color:#555;font-size:.9rem;line-height:1.6em;margin:0}
52
+ .loading-overlay{display:none;position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0);z-index:1000}
53
+ .loading-spinner{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:80px;height:80px;border-radius:50%;border:5px solid #f3f3f3;border-top:5px solid #3498db;animation:spin 1.2s linear infinite}
54
+ @keyframes spin {
55
+ 0%{transform:rotate(0deg)}
56
+ 100%{transform:rotate(360deg)}
57
+ }
58
+ .loading-spinner{width:40px;height:40px;border-radius:50%;border:5px solid #f3f3f3;border-top:5px solid #3498db;animation:spin 1.2s linear infinite}
59
+ .loading-text{margin-left:10px;font-size:1rem;color:#333}
60
+ #no-results{display:none;text-align:center;padding:20px;font-size:1.1em;color:#555}
61
+ .result .actions button{background-color:#f2f2f2;color:#000;border:1px solid #ddd;padding:8px 16px;border-radius:20px;font-size:.9rem;font-weight:700;cursor:pointer;transition:background-color .2s ease,box-shadow .2s ease;margin-right:10px}
62
+ .result .actions button:hover{background-color:#e0e0e0;box-shadow:0 2px 4px rgba(0,0,0,0.1)}
63
+ .summary-popup,.answer-popup{border-radius:16px;box-shadow:0 4px 12px rgba(0,0,0,0.25);background-color:#f8f9fa;color:#333;font-family:'Poppins',sans-serif;padding:20px;position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);max-width:80%;max-height:80%;overflow-y:auto;z-index:1001}
64
+ .summary-popup .close,.answer-popup .close{position:absolute;top:15px;right:15px;cursor:pointer;font-size:1.5em;color:#666;transition:color .2s ease}
65
+ .summary-popup .close:hover,.answer-popup .close:hover{color:#333}
66
+ .summary-popup .loading,.answer-popup .loading{display:flex;justify-content:center;align-items:center;height:100px;display:none}
67
+ .summary-popup .loading-spinner,.answer-popup .loading-spinner{width:60px;height:60px;border-radius:50%;border:5px solid #f3f3f3;border-top:5px solid #3498db;animation:spin 1.2s linear infinite}
68
+ .summary-popup .content,.answer-popup .content{padding:20px;font-size:1rem;line-height:1.5}
69
+ .summary-popup #summaryContent,.answer-popup #answerContent{font-family:'Poppins',sans-serif;margin-bottom:20px}
70
+ #no-results{display:none;text-align:center;padding:20px;font-size:1.1em;color:#555}
71
+ #loading-more{display:none;text-align:center;padding:10px}
72
+ #loading-more.active{display:block}
73
+ .ai-result h2{margin:0 0 10px;font-size:1.5rem;font-weight:700;color:#333}
74
+ .ai-result p{color:#444;font-size:.9rem;line-height:1.5em;margin:0;display:flex;flex-direction:column}
75
+ .ai-result .actions {
76
+ display: flex;
77
+ justify-content: flex-end; /* Align buttons to the right */
78
+ margin-top: 10px;
79
+ }
80
+
81
+ .ai-result .actions button {
82
+ background-color: #f2f2f2;
83
+ color: #000;
84
+ border: 1px solid #ddd;
85
+ padding: 8px 12px; /* Adjust padding if needed */
86
+ border-radius: 20px;
87
+ font-size: .9rem;
88
+ font-weight: 700;
89
+ cursor: pointer;
90
+ transition: background-color .2s ease, box-shadow .2s ease;
91
+ margin-left: 10px; /* Add space between buttons */
92
+ }
93
+
94
+ .ai-result .actions button:hover {
95
+ background-color: #e0e0e0;
96
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
97
+ }
98
+
99
+ .ai-result .actions button i { /* Style Font Awesome icons */
100
+ font-size: 1.2rem; /* Adjust icon size as needed */
101
+ color: #333;
102
+ }
103
+ </style>
104
+ </head>
105
+ <body>
106
+ <div class="main-content">
107
+ <h1><center>🔎 Solatium Advanced Search Engine</center></h1>
108
+ <div class="search-container">
109
+ <form id="search-form">
110
+ <div class="search-box">
111
+ <input type="text" id="search-query" placeholder="Считанный или считаный?" autocomplete="on">
112
+ <button type="submit"></button>
113
+ </div>
114
+ <div id="suggestions"></div>
115
+ </form>
116
+ </div>
117
+ <div id="ai-response"></div>
118
+ <div id="results-info" style="text-align: center;"></div>
119
+ <div id="results"></div>
120
+ <div id="no-results">
121
+ <p></p>
122
+ </div>
123
+ <div id="loading-more"></div>
124
+ <div class="loading-overlay">
125
+ <div class="loading-spinner"></div>
126
+ </div>
127
+ </div>
128
+ <div id="summaryPopup" class="summary-popup" style="display: none;">
129
+ <span class="close">×</span>
130
+ <div class="content">
131
+ <div class="loading">
132
+ <div class="loading-spinner"></div>
133
+ <p class="loading-text">Загрузка...</p>
134
+ </div>
135
+ <div id="summaryContent"></div>
136
+ </div>
137
+ </div>
138
+ <div id="answerPopup" class="answer-popup" style="display: none;">
139
+ <span class="close">×</span>
140
+ <div class="content">
141
+ <div class="loading">
142
+ <div class="loading-spinner"></div>
143
+ <p class="loading-text">Загрузка...</p>
144
+ </div>
145
+ <div id="answerContent"></div>
146
+ </div>
147
+ </div>
148
+ <script>
149
+ const BASE_URL = "https://oevortex-webscout-api.hf.space";
150
+ const searchForm = document.getElementById("search-form");
151
+ const searchQueryInput = document.getElementById("search-query");
152
+ const resultsContainer = document.getElementById("results");
153
+ const suggestionsContainer = document.getElementById("suggestions");
154
+ const loadingOverlay = document.querySelector('.loading-overlay');
155
+ const noResultsMessage = document.getElementById('no-results');
156
+ const loadingMoreIndicator = document.getElementById('loading-more');
157
+ const aiResponseContainer = document.getElementById('ai-response');
158
+ const INITIAL_RESULTS = 5;
159
+ const CACHED_RESULTS = 50;
160
+ const RESULTS_PER_PAGE = 5;
161
+ let allResultsFetched = false;
162
+ const seenUrls = new Set();
163
+ let selectedSuggestionIndex = -1;
164
+ let suggestionRequestTimeout;
165
+ let cachedSearchResults = [];
166
+ const suggestionCache = {};
167
+ let prefetchTimeout;
168
+ let allResults = [];
169
+ let startTime;
170
+
171
+ function debounce(func, delay) {
172
+ return function() {
173
+ clearTimeout(suggestionRequestTimeout);
174
+ suggestionRequestTimeout = setTimeout(() => {
175
+ func.apply(this, arguments);
176
+ }, delay);
177
+ };
178
+ }
179
+ async function fetchSuggestions(query) {
180
+ if (suggestionCache[query]) {
181
+ return suggestionCache[query];
182
+ }
183
+ try {
184
+ const response = await fetch(`${BASE_URL}/api/suggestions?q=${encodeURIComponent(query)}`);
185
+ if (response.ok) {
186
+ const suggestions = await response.json();
187
+ suggestionCache[query] = suggestions;
188
+ return suggestions;
189
+ } else {
190
+ console.error("Error fetching suggestions:", response.status);
191
+ return [];
192
+ }
193
+ } catch (error) {
194
+ console.error("Error fetching suggestions:", error);
195
+ return [];
196
+ }
197
+ }
198
+ searchQueryInput.addEventListener("input", () => {
199
+ clearTimeout(prefetchTimeout);
200
+ const searchQuery = searchQueryInput.value.trim();
201
+ if (searchQuery === "") {
202
+ suggestionsContainer.style.display = "none";
203
+ return;
204
+ }
205
+ prefetchTimeout = setTimeout(async () => {
206
+ const suggestions = await fetchSuggestions(searchQuery);
207
+ displaySuggestions(suggestions);
208
+ }, 100);
209
+ });
210
+
211
+ function displaySuggestions(suggestions) {
212
+ suggestionsContainer.innerHTML = "";
213
+ if (suggestions.length === 0 || searchQueryInput.value.trim() === "") {
214
+ suggestionsContainer.style.display = "none";
215
+ return;
216
+ }
217
+ const suggestionList = document.createElement("ul");
218
+ suggestions.forEach((suggestion, index) => {
219
+ const listItem = document.createElement("li");
220
+ listItem.textContent = suggestion.phrase;
221
+ listItem.addEventListener("click", () => {
222
+ searchQueryInput.value = suggestion.phrase;
223
+ suggestionsContainer.style.display = "none";
224
+ performSearch(suggestion.phrase);
225
+ });
226
+ listItem.addEventListener("focus", () => {
227
+ selectedSuggestionIndex = index;
228
+ updateSuggestionSelection();
229
+ });
230
+ suggestionList.appendChild(listItem);
231
+ });
232
+ suggestionsContainer.appendChild(suggestionList);
233
+ suggestionsContainer.style.display = "block";
234
+ }
235
+
236
+ function updateSuggestionSelection() {
237
+ const suggestionItems = suggestionsContainer.querySelectorAll("li");
238
+ suggestionItems.forEach((item, index) => {
239
+ item.classList.toggle("selected", index === selectedSuggestionIndex);
240
+ });
241
+ }
242
+
243
+ function showLoading() {
244
+ loadingOverlay.style.display = 'block';
245
+ }
246
+
247
+ function hideLoading() {
248
+ loadingOverlay.style.display = 'none';
249
+ }
250
+ async function performSearch(query) {
251
+ showLoading();
252
+ aiResponseContainer.style.display = 'none';
253
+ suggestionsContainer.style.display = "none";
254
+ startTime = performance.now();
255
+ seenUrls.clear();
256
+ allResultsFetched = false;
257
+ resultsContainer.innerHTML = '';
258
+ noResultsMessage.style.display = 'none';
259
+ loadingMoreIndicator.classList.remove('active');
260
+ speechSynthesis.cancel();
261
+ const initialResults = await fetchResults(query, INITIAL_RESULTS);
262
+ displayResults(initialResults);
263
+ hideLoading();
264
+ fetchResults(query, CACHED_RESULTS).then(cachedResults => {
265
+ cachedSearchResults = removeDuplicateResults(cachedResults);
266
+ allResults = allResults.concat(cachedSearchResults);
267
+ displayResults(cachedSearchResults.slice(INITIAL_RESULTS, RESULTS_PER_PAGE), true);
268
+ if (cachedSearchResults.length > RESULTS_PER_PAGE) {
269
+ allResultsFetched = false;
270
+ loadingMoreIndicator.classList.add('active');
271
+ }
272
+ });
273
+ fetchAIResponse(query).then(aiResponse => {
274
+ displayAIResponse(aiResponse);
275
+ aiResponseContainer.style.display = 'block';
276
+ }).catch(error => {
277
+ console.error("Error fetching AI response:", error);
278
+ });
279
+ updateURLWithQuery(query);
280
+ }
281
+ async function fetchAIResponse(query) {
282
+ try {
283
+ const encodedQuery = encodeURIComponent(query);
284
+ const websiteURL = 'https://google.com/search?q=${encodedQuery}'
285
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=https://google.com/search?q=${encodedQuery}&question=Answer this question from google search result ${encodedQuery}&model=gpt-4o-mini`);
286
+ if (response.ok) {
287
+ const aiResponse = await response.json();
288
+ return aiResponse;
289
+ } else {
290
+ console.error("Error fetching AI response from website:", response.status);
291
+ return null;
292
+ }
293
+ } catch (error) {
294
+ console.error("Error fetching AI response from website:", error);
295
+ return null;
296
+ }
297
+ }
298
+
299
+ function displayAIResponse(response) {
300
+ aiResponseContainer.innerHTML = '';
301
+ if (response) {
302
+ const aiResultElement = document.createElement('div');
303
+ aiResultElement.classList.add('ai-result');
304
+ const aiHeading = document.createElement('h2');
305
+ aiHeading.textContent = "❇️ Вот так говорят словари и справочные материалы:";
306
+ aiResultElement.appendChild(aiHeading);
307
+ const aiText = document.createElement('p');
308
+ const decodedResponse = decodeHtml(response);
309
+ const msg = new SpeechSynthesisUtterance(decodedResponse);
310
+ speechSynthesis.speak(msg);
311
+ aiText.textContent = decodedResponse;
312
+ const pauseButton = document.createElement('button');
313
+ pauseButton.id = 'pause';
314
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
315
+ const stopButton = document.createElement('button');
316
+ stopButton.id = 'stop';
317
+ stopButton.innerHTML = '<i class="fas fa-stop"></i>';
318
+ let isPaused = false;
319
+ let isStoped = false;
320
+ pauseButton.addEventListener('click', () => {
321
+ if ('speechSynthesis' in window) {
322
+ if (isPaused) {
323
+ window.speechSynthesis.resume();
324
+ isPaused = false;
325
+ stopButton.style.display = 'inline-block';
326
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
327
+ } else {
328
+ window.speechSynthesis.pause();
329
+ isPaused = true;
330
+ stopButton.style.display = 'none';
331
+ pauseButton.innerHTML = '<i class="fas fa-play"></i>';
332
+ }
333
+ }
334
+ });
335
+
336
+ stopButton.addEventListener('click', () => {
337
+ if ('speechSynthesis' in window) {
338
+ if (isStoped){
339
+ speechSynthesis.speak(msg);
340
+ isPaused = false;
341
+ isStoped = false;
342
+ pauseButton.innerHTML = '<i class="fas fa-pause"></i>';
343
+ pauseButton.style.display = 'inline-block';
344
+ stopButton.innerHTML = '<i class="fas fa-stop"></i>';
345
+ }else{
346
+ window.speechSynthesis.cancel();
347
+ isPaused = false;
348
+ isStoped = true;
349
+ pauseButton.style.display = 'none';
350
+ stopButton.innerHTML = '<i class="fas fa-play"></i>'; }
351
+ }
352
+ });
353
+
354
+ // Add the buttons to the actions container
355
+ aiResultElement.appendChild(pauseButton);
356
+ aiResultElement.appendChild(stopButton);
357
+
358
+ // Style the AI response text
359
+ aiText.setAttribute('style', 'font-size: 1rem; line-height: 1.5rem;');
360
+ aiResultElement.appendChild(aiText);
361
+ aiResponseContainer.appendChild(aiResultElement);
362
+ }
363
+ }
364
+
365
+ function updateURLWithQuery(query) {
366
+ const newURL = `${window.location.pathname}?query=${encodeURIComponent(query)}`;
367
+ window.history.pushState({
368
+ path: newURL
369
+ }, '', newURL);
370
+ }
371
+
372
+ function removeDuplicateResults(results) {
373
+ const uniqueResults = [];
374
+ const seen = new Set();
375
+ for (const result of results) {
376
+ if (!seen.has(result.href)) {
377
+ seen.add(result.href);
378
+ uniqueResults.push(result);
379
+ }
380
+ }
381
+ return uniqueResults;
382
+ }
383
+
384
+ async function fetchResults(query, resultsPerPage) {
385
+ const response = await fetch(`${BASE_URL}/api/search?q=${encodeURIComponent(query)}&max_results=${resultsPerPage}`);
386
+ if (!response.ok) {
387
+ displayError("🔄️ Пожалуйста, подождите. Словари делают всё для вас...");
388
+ hideLoading();
389
+ return [];
390
+ }
391
+ const searchResults = await response.json();
392
+ return searchResults;
393
+ }
394
+
395
+ const summaryPopup = document.getElementById('summaryPopup');
396
+ const summaryContent = document.getElementById('summaryContent');
397
+ const answerPopup = document.getElementById('answerPopup');
398
+ const answerContent = document.getElementById('answerContent');
399
+ const summaryCache = {};
400
+ const answerCache = {};
401
+
402
+ function displayResults(results, append = false) {
403
+ if (!append) {
404
+ resultsContainer.innerHTML = '';
405
+ }
406
+ const newResults = results.filter(result => !seenUrls.has(result.href));
407
+ newResults.forEach((result, index) => {
408
+ seenUrls.add(result.href);
409
+ const resultElement = document.createElement("div");
410
+ resultElement.classList.add("result");
411
+ const titleElement = document.createElement("h3");
412
+ const titleLink = document.createElement("a");
413
+ titleLink.href = result.href;
414
+ titleLink.textContent = result.title;
415
+ titleLink.target = "_blank";
416
+ titleLink.rel = "noopener noreferrer";
417
+ titleElement.appendChild(titleLink);
418
+ const urlElement = document.createElement("div");
419
+ urlElement.classList.add("url");
420
+ const urlLink = document.createElement("a");
421
+ urlLink.href = result.href;
422
+ urlLink.textContent = result.href;
423
+ urlLink.target = "_blank";
424
+ urlLink.rel = "noopener noreferrer";
425
+ urlElement.appendChild(urlLink);
426
+ const descriptionElement = document.createElement("p");
427
+ descriptionElement.textContent = result.body;
428
+ resultElement.appendChild(titleElement);
429
+ resultElement.appendChild(urlElement);
430
+ resultElement.appendChild(descriptionElement);
431
+ const actionsContainer = document.createElement('div');
432
+ actionsContainer.classList.add('actions');
433
+ const summarizeButton = document.createElement('button');
434
+ summarizeButton.textContent = 'Суммаризируй сайт';
435
+ summarizeButton.addEventListener('click', () => {
436
+ showSummaryPopup(result.href);
437
+ });
438
+ actionsContainer.appendChild(summarizeButton);
439
+ const answerButton = document.createElement('button');
440
+ answerButton.textContent = 'Ответ на этот сайт';
441
+ answerButton.addEventListener('click', () => {
442
+ showAnswerPopup(result.href);
443
+ });
444
+ actionsContainer.appendChild(answerButton);
445
+ resultElement.appendChild(actionsContainer);
446
+ resultsContainer.appendChild(resultElement);
447
+ setTimeout(() => {
448
+ resultElement.classList.add("show");
449
+ }, 1 * index);
450
+ });
451
+ if (resultsContainer.children.length === 0) {
452
+ noResultsMessage.style.display = 'block';
453
+ } else {
454
+ noResultsMessage.style.display = 'none';
455
+ }
456
+ if (!append) {
457
+ const endTime = performance.now();
458
+ const timeTaken = (endTime/2 - startTime/2).toFixed(2);
459
+ document.getElementById('results-info').textContent = `⌛ Ваш запрос обрабатывался ${timeTaken} мс.`;
460
+ }
461
+ }
462
+
463
+ function displayError(message) {
464
+ resultsContainer.innerHTML = '';
465
+ const errorElement = document.createElement("p");
466
+ errorElement.textContent = message;
467
+ errorElement.style.color = "red";
468
+ resultsContainer.appendChild(errorElement);
469
+ }
470
+
471
+ searchQueryInput.addEventListener("input", debounce(async () => {
472
+ selectedSuggestionIndex = -1;
473
+ const searchQuery = searchQueryInput.value;
474
+ if (searchQuery.trim() === "") {
475
+ suggestionsContainer.style.display = "none";
476
+ return;
477
+ }
478
+ const suggestions = await fetchSuggestions(searchQuery);
479
+ displaySuggestions(suggestions);
480
+ }, 500));
481
+
482
+ searchQueryInput.addEventListener("focus", () => {
483
+ if (searchQueryInput.value.trim() !== "") {
484
+ suggestionsContainer.style.display = "block";
485
+ }
486
+ });
487
+
488
+ document.addEventListener("click", (event) => {
489
+ if (!searchForm.contains(event.target)) {
490
+ suggestionsContainer.style.display = "none";
491
+ }
492
+ });
493
+
494
+ searchQueryInput.addEventListener("keydown", async (event) => {
495
+ if (event.key === "ArrowUp" || event.key === "ArrowDown") {
496
+ event.preventDefault();
497
+ const suggestionItems = suggestionsContainer.querySelectorAll("li");
498
+ const numSuggestions = suggestionItems.length;
499
+ if (event.key === "ArrowUp") {
500
+ selectedSuggestionIndex = (selectedSuggestionIndex - 1 + numSuggestions) % numSuggestions;
501
+ } else {
502
+ selectedSuggestionIndex = (selectedSuggestionIndex + 1) % numSuggestions;
503
+ }
504
+ updateSuggestionSelection();
505
+ if (selectedSuggestionIndex !== -1 && suggestionItems[selectedSuggestionIndex]) {
506
+ searchQueryInput.value = suggestionItems[selectedSuggestionIndex].textContent;
507
+ suggestionItems[selectedSuggestionIndex].focus();
508
+ }
509
+ } else if (event.key === "Enter" && selectedSuggestionIndex !== -1) {
510
+ event.preventDefault();
511
+ const selectedSuggestion = suggestionsContainer.querySelectorAll("li")[selectedSuggestionIndex];
512
+ if (selectedSuggestion) {
513
+ searchQueryInput.value = selectedSuggestion.textContent;
514
+ suggestionsContainer.style.display = "none";
515
+ performSearch(searchQueryInput.value);
516
+ }
517
+ }
518
+ });
519
+
520
+ searchForm.addEventListener("submit", async (event) => {
521
+ event.preventDefault();
522
+ selectedSuggestionIndex = -1;
523
+ const searchQuery = searchQueryInput.value;
524
+ performSearch(searchQuery);
525
+ });
526
+
527
+ window.addEventListener('scroll', () => {
528
+ if (allResultsFetched) return;
529
+ const {
530
+ scrollTop,
531
+ scrollHeight,
532
+ clientHeight
533
+ } = document.documentElement;
534
+ const isNearBottom = scrollTop + clientHeight >= scrollHeight - 100;
535
+ if (isNearBottom && allResults.length > seenUrls.size) {
536
+ loadingMoreIndicator.classList.add('active');
537
+ const resultsToDisplay = allResults.slice(seenUrls.size, seenUrls.size + RESULTS_PER_PAGE);
538
+ setTimeout(() => {
539
+ displayResults(resultsToDisplay, true);
540
+ for (const result of resultsToDisplay) {
541
+ seenUrls.add(result.href);
542
+ }
543
+ if (allResults.length === seenUrls.size) {
544
+ allResultsFetched = true;
545
+ loadingMoreIndicator.classList.remove('active');
546
+ }
547
+ }, 300);
548
+ }
549
+ });
550
+
551
+ function getQueryParameter(name) {
552
+ const urlParams = new URLSearchParams(window.location.search);
553
+ return urlParams.get(name);
554
+ }
555
+
556
+ window.addEventListener('load', () => {
557
+ const initialQuery = getQueryParameter('query');
558
+ if (initialQuery) {
559
+ searchQueryInput.value = initialQuery;
560
+ performSearch(initialQuery);
561
+ }
562
+ });
563
+
564
+ function decodeHtml(html) {
565
+ var txt = document.createElement("textarea");
566
+ txt.innerHTML = html;
567
+ return txt.value;
568
+ }
569
+
570
+ function showSummaryPopup(url) {
571
+ summaryContent.innerHTML = '';
572
+ summaryPopup.querySelector('.loading').style.display = 'block';
573
+ summaryPopup.style.display = 'block';
574
+ if (summaryCache[url]) {
575
+ displaySummary(summaryCache[url]);
576
+ } else {
577
+ fetchWebsiteSummary(url).then(summary => {
578
+ summaryCache[url] = summary;
579
+ displaySummary(summary);
580
+ }).catch(error => {
581
+ console.error('Ошибка суммаризации.:', error);
582
+ displaySummary('Ошибка суммаризации.');
583
+ });
584
+ }
585
+ }
586
+
587
+ function displaySummary(summary) {
588
+ summaryPopup.querySelector('.loading').style.display = 'none';
589
+ summaryContent.textContent = summary;
590
+ }
591
+
592
+ async function fetchWebsiteSummary(url) {
593
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=${url}&question=${encodeURIComponent("Summarize this site in Detail, Do not start with any intro, Just start summarizing. Summarize in this manner which is very important to human user.")}&model=mixtral-8x7b`);
594
+ if (!response.ok) {
595
+ throw new Error('Ошибка суммаризации сайта.');
596
+ }
597
+ const data = await response.json();
598
+ return data[0] || 'Суммаризация недоступна. Придётся прочитать полностью...';
599
+ }
600
+
601
+ function showAnswerPopup(url) {
602
+ answerContent.innerHTML = '';
603
+ answerPopup.querySelector('.loading').style.display = 'block';
604
+ answerPopup.style.display = 'block';
605
+ if (answerCache[url]) {
606
+ displayAnswer(answerCache[url]);
607
+ } else {
608
+ const question = searchQueryInput.value;
609
+ fetchWebsiteAnswer(url, question).then(answer => {
610
+ answerCache[url] = answer;
611
+ displayAnswer(answer);
612
+ }).catch(error => {
613
+ console.error('Ошибка загрузки ответа:', error);
614
+ displayAnswer('Ошибка загрузки ответа.');
615
+ });
616
+ }
617
+ }
618
+
619
+ function displayAnswer(answer) {
620
+ answerPopup.querySelector('.loading').style.display = 'none';
621
+ answerContent.textContent = answer;
622
+ }
623
+
624
+ async function fetchWebsiteAnswer(url, question) {
625
+ const response = await fetch(`${BASE_URL}/api/ask_website?url=${url}&question=${encodeURIComponent(question)}(encodeURIComponent("Answer in concise way, First tell answer then Other thing"))&model=llama-3-70b`);
626
+ if (!response.ok) {
627
+ throw new Error('Ошибка загрузки ответа с сайта.');
628
+ }
629
+ const data = await response.json();
630
+ return data[0] || 'Ответ не найден. Слишком сложный вопрос...';
631
+ }
632
+
633
+ summaryPopup.querySelector('.close').addEventListener('click', () => {
634
+ summaryPopup.style.display = 'none';
635
+ });
636
+
637
+ answerPopup.querySelector('.close').addEventListener('click', () => {
638
+ answerPopup.style.display = 'none';
639
+ });
640
+ </script>
641
+ </body>
642
+ </html>