Skip to content

Commit a14d11c

Browse files
committed
initial research digest with hard-coded tweaks
1 parent cb8bc5c commit a14d11c

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

example.html

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,29 @@ <h2>Chat Sessions</h2>
143143
</div>
144144
</div>
145145
</div>
146-
146+
147+
<div class="container">
148+
<h2>Document Summary</h2>
149+
<input type="file" id="fileInput" accept="application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document, text/plain, application/rtf, text/markdown, text/html" />
150+
<select id="summaryEndpoint">
151+
<option value="research-paper-digest">Research Paper Digest</option>
152+
</select>
153+
<button onclick="loadAndDigestFile()">Summarize Document</button>
154+
<div id="summaryOutput"></div>
155+
</div>
156+
147157
<script src="https://cdn.jsdelivr.net/npm/uuid@8.3.2/dist/umd/uuid.min.js"></script>
148158
<script src="https://cdn.jsdelivr.net/gh/logspace-ai/langflow-embedded-chat@v1.0.5/dist/build/static/js/bundle.min.js"></script>
159+
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
160+
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>
149161
<script>
162+
let currentHostUrl = localStorage.getItem('hostUrl') || document.getElementById('hostUrlInput').value; // Retrieve from localStorage or use default
163+
150164
function updateHostUrl() {
151165
const newUrl = document.getElementById('hostUrlInput').value;
152166
if (newUrl) { // Check if the URL is not empty
167+
currentHostUrl = newUrl;
168+
localStorage.setItem('hostUrl', newUrl); // Save the new URL in localStorage
153169
const chatComponents = document.querySelectorAll('langflow-chat');
154170
chatComponents.forEach(chat => {
155171
chat.setAttribute('host_url', newUrl);
@@ -158,7 +174,7 @@ <h2>Chat Sessions</h2>
158174
}
159175

160176
let sessions = JSON.parse(localStorage.getItem('sessions')) || [{'label': 'initial', 'id': uuid.v4()}];
161-
localStorage.setItem('sessions', JSON.stringify(sessions)); // Initialize or re-save
177+
localStorage.setItem('sessions', JSON.stringify(sessions));
162178

163179
function populateSessionTable() {
164180
const table = document.getElementById('sessionTable');
@@ -230,11 +246,72 @@ <h2>Chat Sessions</h2>
230246
updateSession('langflowBasicMemoryChat', sessionId);
231247
updateSession('langflowAstraMemoryChat', sessionId);
232248
}
233-
249+
234250
window.onload = function() {
251+
document.getElementById('hostUrlInput').value = currentHostUrl;
235252
populateSessionTable();
236253
updateHostUrl();
237254
};
255+
256+
function loadAndDigestFile() {
257+
const file = document.getElementById('fileInput').files[0];
258+
const endpoint = document.getElementById('summaryEndpoint').value;
259+
260+
// Clear previous content and show loading message immediately
261+
const summaryOutputDiv = document.getElementById('summaryOutput');
262+
summaryOutputDiv.innerHTML = '<div style="margin-top: 20px;">Processing...</div>';
263+
264+
if (file) {
265+
const reader = new FileReader();
266+
reader.readAsDataURL(file);
267+
reader.onload = function () {
268+
const base64Content = reader.result.split(',')[1];
269+
const filename = file.name;
270+
sendFileToAPI(endpoint, base64Content, filename);
271+
};
272+
reader.onerror = function (error) {
273+
console.error('Error reading file: ', error);
274+
summaryOutputDiv.innerHTML = `<p>Error reading file: ${error.message}</p>`; // Display error message in the summary output
275+
};
276+
} else {
277+
alert('Please select a file before clicking "Summarize Document".');
278+
}
279+
}
280+
281+
function sendFileToAPI(endpoint, base64Content, filename) {
282+
const apiURL = `${currentHostUrl}/api/v1/run/${endpoint}?stream=false`;
283+
fetch(apiURL, {
284+
method: "POST",
285+
headers: {
286+
'Content-Type': 'application/json'
287+
},
288+
body: JSON.stringify({
289+
output_type: "chat",
290+
input_type: "chat",
291+
"tweaks": {
292+
"TextInput-eYtbq": {"input_value": base64Content },
293+
"TextInput-AlrlI": {"input_value": filename }
294+
}
295+
})
296+
})
297+
.then(response => {
298+
if (!response.ok) throw new Error('Network response was not ok');
299+
return response.json();
300+
})
301+
.then(data => {
302+
// console.log("API Data:", data);
303+
// Extract the markdown text from the deeply nested structure
304+
var markdownText = data.outputs[0].outputs[0].results.text.text;
305+
const renderedHTML = marked.parse(markdownText || '');
306+
document.getElementById('summaryOutput').innerHTML = DOMPurify.sanitize(renderedHTML);
307+
})
308+
.catch(error => {
309+
console.error('Error:', error);
310+
document.getElementById('summaryOutput').innerHTML = `<p>An error occurred while processing your document: ${error}</p>`;
311+
document.getElementById('loadingIndicator').style.display = 'none'; // Hide loading indicator on error
312+
});
313+
}
314+
238315
</script>
239316
</body>
240317
</html>

0 commit comments

Comments
 (0)