Spaces:
Sleeping
Sleeping
fix downloads
Browse files
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 |
-
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
const
|
| 395 |
-
const
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
const url = URL.createObjectURL(blob);
|
| 401 |
const a = document.createElement('a');
|
| 402 |
a.href = url;
|
| 403 |
-
|
| 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 |
""")
|