ndurner commited on
Commit
3033ee9
·
1 Parent(s): f76ec8b

fix file download naming

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -244,29 +244,55 @@ with gr.Blocks(delete_cache=(86400, 86400)) as demo:
244
  dl_button = gr.Button("File download")
245
  dl_button.click(lambda: None, [chatbot], js="""
246
  (chat_history) => {
247
- // Attempt to extract content enclosed in backticks with an optional filename
248
- const contentRegex = /```(\\S*\\.(\\S+))?\\n?([\\s\\S]*?)```/;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
250
- if (match && match[3]) {
251
- // Extract the content and the file extension
252
- const content = match[3];
253
- const fileExtension = match[2] || 'txt'; // Default to .txt if extension is not found
254
- const filename = match[1] || `download.${fileExtension}`;
255
- // Create a Blob from the content
256
- const blob = new Blob([content], {type: `text/${fileExtension}`});
257
- // Create a download link for the Blob
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  const url = URL.createObjectURL(blob);
259
  const a = document.createElement('a');
260
  a.href = url;
261
- // If the filename from the chat history doesn't have an extension, append the default
262
- a.download = filename.includes('.') ? filename : `${filename}.${fileExtension}`;
263
  document.body.appendChild(a);
264
  a.click();
265
  document.body.removeChild(a);
266
  URL.revokeObjectURL(url);
267
- } else {
268
- // Inform the user if the content is malformed or missing
269
- alert('Sorry, the file content could not be found or is in an unrecognized format.');
270
  }
271
  }
272
  """)
 
244
  dl_button = gr.Button("File download")
245
  dl_button.click(lambda: None, [chatbot], js="""
246
  (chat_history) => {
247
+ // Only define exception mappings
248
+ const languageToExt = {
249
+ 'python': 'py',
250
+ 'javascript': 'js',
251
+ 'typescript': 'ts',
252
+ 'csharp': 'cs',
253
+ 'ruby': 'rb',
254
+ 'shell': 'sh',
255
+ 'bash': 'sh',
256
+ 'markdown': 'md',
257
+ 'yaml': 'yml',
258
+ 'rust': 'rs',
259
+ 'golang': 'go',
260
+ 'kotlin': 'kt'
261
+ };
262
+
263
+ const contentRegex = /```(?:([^\\n]+)?\\n)?([\\s\\S]*?)```/;
264
  const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
265
+
266
+ if (match && match[2]) {
267
+ const specifier = match[1] ? match[1].trim() : '';
268
+ const content = match[2];
269
+
270
+ let filename = 'download';
271
+ let fileExtension = 'txt'; // default
272
+
273
+ if (specifier) {
274
+ if (specifier.includes('.')) {
275
+ // If specifier contains a dot, treat it as a filename
276
+ const parts = specifier.split('.');
277
+ filename = parts[0];
278
+ fileExtension = parts[1];
279
+ } else {
280
+ // Use mapping if exists, otherwise use specifier itself
281
+ const langLower = specifier.toLowerCase();
282
+ fileExtension = languageToExt[langLower] || langLower;
283
+ filename = 'code';
284
+ }
285
+ }
286
+
287
+ const blob = new Blob([content], {type: 'text/plain'});
288
  const url = URL.createObjectURL(blob);
289
  const a = document.createElement('a');
290
  a.href = url;
291
+ a.download = `${filename}.${fileExtension}`;
 
292
  document.body.appendChild(a);
293
  a.click();
294
  document.body.removeChild(a);
295
  URL.revokeObjectURL(url);
 
 
 
296
  }
297
  }
298
  """)