Skip to content

Commit 1fc35e2

Browse files
committed
Add upload button and support text files
Previously we could drag and drop image files, as well as paste them, in the new llamafiler web ui. Now there's an upload paperclip icon, to make this feature more obvious. Text files are now supported too, in addition to images, which can be helpful for summarization.
1 parent 508ea3a commit 1fc35e2

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

llamafile/server/www/chatbot.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,20 @@ ul li:first-child {
482482
background: #0b5ed7;
483483
}
484484

485+
.upload-button {
486+
padding-left: .5rem;
487+
background: transparent;
488+
border: none;
489+
border-radius: 6px;
490+
cursor: pointer;
491+
font-size: 1rem;
492+
transition: color 0.2s;
493+
}
494+
495+
.upload-button:hover {
496+
color: #0d6efd;
497+
}
498+
485499
.mode-dropdown {
486500
display: none;
487501
}

llamafile/server/www/chatbot.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const completionsInput = document.getElementById("completions-input");
4747
const completeButton = document.getElementById("complete-button");
4848
const completionsSettingsButton = document.getElementById("completions-settings-button");
4949
const completionsStopButton = document.getElementById("completions-stop-button");
50+
const uploadButton = document.getElementById("upload-button");
51+
const fileUpload = document.getElementById("file-upload");
5052

5153
let abortController = null;
5254
let disableAutoScroll = false;
@@ -308,19 +310,26 @@ async function fixImageDataUri(dataUri, maxLength = 1024 * 1024) {
308310
}
309311

310312
async function onFile(file) {
311-
if (!file.type.toLowerCase().startsWith('image/')) {
312-
console.warn('Only image files are supported');
313+
const reader = new FileReader();
314+
if (file.type.toLowerCase().startsWith('image/')) {
315+
reader.onloadend = async function() {
316+
const description = file.name;
317+
const realDataUri = await fixImageDataUri(reader.result);
318+
const fakeDataUri = 'data:,placeholder/' + generateId();
319+
uploadedFiles.push([fakeDataUri, realDataUri]);
320+
insertText(chatInput, `![${description}](${fakeDataUri})`);
321+
};
322+
reader.readAsDataURL(file);
323+
} else if (file.type.toLowerCase().startsWith('text/')) {
324+
reader.onloadend = function() {
325+
const content = reader.result;
326+
insertText(chatInput, `\`\`\`\n${content}\n\`\`\``);
327+
};
328+
reader.readAsText(file);
329+
} else {
330+
console.warn('Only image and text files are supported');
313331
return;
314332
}
315-
const reader = new FileReader();
316-
reader.onloadend = async function() {
317-
const description = file.name;
318-
const realDataUri = await fixImageDataUri(reader.result);
319-
const fakeDataUri = 'data:,placeholder/' + generateId();
320-
uploadedFiles.push([fakeDataUri, realDataUri]);
321-
insertText(chatInput, `![${description}](${fakeDataUri})`);
322-
};
323-
reader.readAsDataURL(file);
324333
}
325334

326335
function insertText(elem, text) {
@@ -676,6 +685,17 @@ async function chatbot() {
676685
document.addEventListener("drop", onDragEnd);
677686
document.addEventListener("drop", onDrop);
678687
document.addEventListener("paste", onPaste);
688+
689+
uploadButton.addEventListener("click", () => {
690+
fileUpload.click();
691+
});
692+
693+
fileUpload.addEventListener("change", (e) => {
694+
if (e.target.files[0]) {
695+
onFile(e.target.files[0]);
696+
e.target.value = '';
697+
}
698+
});
679699
}
680700

681701
chatbot();

llamafile/server/www/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ <h1>
3232
<button class="send-button" id="send-button">Send</button>
3333
<button class="stop-button" id="stop-button" style="display:none">Stop</button>
3434
<button class="settings-button" id="settings-button" title="Settings">⚙️</button>
35+
<button class="upload-button" id="upload-button">📎</button>
3536
<button class="redo-button" id="redo-button" title="Redo last message"></button>
37+
<input type="file" id="file-upload" accept="image/*,text/*" style="display: none">
3638
</div>
3739
</div>
3840

0 commit comments

Comments
 (0)