ndurner commited on
Commit
7a53e3e
·
1 Parent(s): a11b656

fix downloads

Browse files
Files changed (1) hide show
  1. app.py +40 -15
app.py CHANGED
@@ -386,29 +386,54 @@ with gr.Blocks(delete_cache=(86400, 86400)) as demo:
386
  dl_button = gr.Button("File download")
387
  dl_button.click(lambda: None, [chatbot], js="""
388
  (chat_history) => {
389
- // Attempt to extract content enclosed in backticks with an optional filename
390
- const contentRegex = /```(\\S*\\.(\\S+))?\\n?([\\s\\S]*?)```/;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
392
- if (match && match[3]) {
393
- // Extract the content and the file extension
394
- const content = match[3];
395
- const fileExtension = match[2] || 'txt'; // Default to .txt if extension is not found
396
- const filename = match[1] || `download.${fileExtension}`;
397
- // Create a Blob from the content
398
- const blob = new Blob([content], {type: `text/${fileExtension}`});
399
- // Create a download link for the Blob
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
  const url = URL.createObjectURL(blob);
401
  const a = document.createElement('a');
402
  a.href = url;
403
- // If the filename from the chat history doesn't have an extension, append the default
404
- a.download = filename.includes('.') ? filename : `${filename}.${fileExtension}`;
405
  document.body.appendChild(a);
406
  a.click();
407
  document.body.removeChild(a);
408
  URL.revokeObjectURL(url);
409
- } else {
410
- // Inform the user if the content is malformed or missing
411
- alert('Sorry, the file content could not be found or is in an unrecognized format.');
412
  }
413
  }
414
  """)
 
386
  dl_button = gr.Button("File download")
387
  dl_button.click(lambda: None, [chatbot], js="""
388
  (chat_history) => {
389
+ const languageToExt = {
390
+ 'python': 'py',
391
+ 'javascript': 'js',
392
+ 'typescript': 'ts',
393
+ 'csharp': 'cs',
394
+ 'ruby': 'rb',
395
+ 'shell': 'sh',
396
+ 'bash': 'sh',
397
+ 'markdown': 'md',
398
+ 'yaml': 'yml',
399
+ 'rust': 'rs',
400
+ 'golang': 'go',
401
+ 'kotlin': 'kt'
402
+ };
403
+
404
+ const contentRegex = /```(?:([^\\n]+)?\\n)?([\\s\\S]*?)```/;
405
  const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
406
+
407
+ if (match && match[2]) {
408
+ const specifier = match[1] ? match[1].trim() : '';
409
+ const content = match[2];
410
+
411
+ let filename = 'download';
412
+ let fileExtension = 'txt'; // default
413
+
414
+ if (specifier) {
415
+ if (specifier.includes('.')) {
416
+ // If specifier contains a dot, treat it as a filename
417
+ const parts = specifier.split('.');
418
+ filename = parts[0];
419
+ fileExtension = parts[1];
420
+ } else {
421
+ // Use mapping if exists, otherwise use specifier itself
422
+ const langLower = specifier.toLowerCase();
423
+ fileExtension = languageToExt[langLower] || langLower;
424
+ filename = 'code';
425
+ }
426
+ }
427
+
428
+ const blob = new Blob([content], {type: 'text/plain'});
429
  const url = URL.createObjectURL(blob);
430
  const a = document.createElement('a');
431
  a.href = url;
432
+ a.download = `${filename}.${fileExtension}`;
 
433
  document.body.appendChild(a);
434
  a.click();
435
  document.body.removeChild(a);
436
  URL.revokeObjectURL(url);
 
 
 
437
  }
438
  }
439
  """)