@@ -143,13 +143,29 @@ <h2>Chat Sessions</h2>
143
143
</ div >
144
144
</ div >
145
145
</ 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
+
147
157
< script src ="https://cdn.jsdelivr.net/npm/uuid@8.3.2/dist/umd/uuid.min.js "> </ script >
148
158
< 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 >
149
161
< script >
162
+ let currentHostUrl = localStorage . getItem ( 'hostUrl' ) || document . getElementById ( 'hostUrlInput' ) . value ; // Retrieve from localStorage or use default
163
+
150
164
function updateHostUrl ( ) {
151
165
const newUrl = document . getElementById ( 'hostUrlInput' ) . value ;
152
166
if ( newUrl ) { // Check if the URL is not empty
167
+ currentHostUrl = newUrl ;
168
+ localStorage . setItem ( 'hostUrl' , newUrl ) ; // Save the new URL in localStorage
153
169
const chatComponents = document . querySelectorAll ( 'langflow-chat' ) ;
154
170
chatComponents . forEach ( chat => {
155
171
chat . setAttribute ( 'host_url' , newUrl ) ;
@@ -158,7 +174,7 @@ <h2>Chat Sessions</h2>
158
174
}
159
175
160
176
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 ) ) ;
162
178
163
179
function populateSessionTable ( ) {
164
180
const table = document . getElementById ( 'sessionTable' ) ;
@@ -230,11 +246,72 @@ <h2>Chat Sessions</h2>
230
246
updateSession ( 'langflowBasicMemoryChat' , sessionId ) ;
231
247
updateSession ( 'langflowAstraMemoryChat' , sessionId ) ;
232
248
}
233
-
249
+
234
250
window . onload = function ( ) {
251
+ document . getElementById ( 'hostUrlInput' ) . value = currentHostUrl ;
235
252
populateSessionTable ( ) ;
236
253
updateHostUrl ( ) ;
237
254
} ;
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
+
238
315
</ script >
239
316
</ body >
240
317
</ html >
0 commit comments