Spaces:
Sleeping
Sleeping
fix file download naming
Browse files
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 |
-
//
|
| 248 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
const match = contentRegex.exec(chat_history[chat_history.length - 1][1]);
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
const
|
| 253 |
-
const
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
const url = URL.createObjectURL(blob);
|
| 259 |
const a = document.createElement('a');
|
| 260 |
a.href = url;
|
| 261 |
-
|
| 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 |
""")
|