Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Thomas G. Lopes
commited on
Commit
·
899d9c6
1
Parent(s):
058d10c
img txt 2 txt fully working
Browse files
src/lib/components/inference-playground/conversation.svelte
CHANGED
|
@@ -58,8 +58,8 @@
|
|
| 58 |
{#if !viewCode}
|
| 59 |
{#each conversation.messages as _msg, idx}
|
| 60 |
<Message
|
| 61 |
-
bind:
|
| 62 |
-
|
| 63 |
autofocus={idx === conversation.messages.length - 1}
|
| 64 |
{loading}
|
| 65 |
onDelete={() => deleteMessage(idx)}
|
|
|
|
| 58 |
{#if !viewCode}
|
| 59 |
{#each conversation.messages as _msg, idx}
|
| 60 |
<Message
|
| 61 |
+
bind:message={conversation.messages[idx]!}
|
| 62 |
+
{conversation}
|
| 63 |
autofocus={idx === conversation.messages.length - 1}
|
| 64 |
{loading}
|
| 65 |
onDelete={() => deleteMessage(idx)}
|
src/lib/components/inference-playground/message.svelte
CHANGED
|
@@ -1,83 +1,140 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
-
import { TextareaAutosize } from "$lib/spells/textarea-autosize.svelte.js";
|
| 3 |
-
import type { ConversationMessage } from "$lib/types.js";
|
| 4 |
import Tooltip from "$lib/components/tooltip.svelte";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import IconImage from "~icons/carbon/image-reference";
|
| 6 |
|
| 7 |
type Props = {
|
| 8 |
-
|
| 9 |
-
|
| 10 |
loading?: boolean;
|
| 11 |
autofocus?: boolean;
|
| 12 |
onDelete?: () => void;
|
| 13 |
};
|
| 14 |
|
| 15 |
-
let {
|
| 16 |
|
| 17 |
let element = $state<HTMLTextAreaElement>();
|
| 18 |
new TextareaAutosize({
|
| 19 |
element: () => element,
|
| 20 |
-
input: () => content,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
});
|
| 22 |
</script>
|
| 23 |
|
| 24 |
<div
|
| 25 |
-
class="group/message group flex flex-col items-start gap-x-4 gap-y-2 border-b px-3.5 pt-4 pb-6 hover:bg-gray-100/70
|
| 26 |
-
|
| 27 |
class:pointer-events-none={loading}
|
|
|
|
|
|
|
| 28 |
>
|
| 29 |
-
<div class="
|
| 30 |
-
{
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
group-focus-within/message:visible group-hover/message:visible hover:bg-gray-100
|
| 53 |
hover:text-blue-700 focus:z-10 focus:ring-4
|
| 54 |
focus:ring-gray-100 focus:outline-hidden sm:invisible dark:border-gray-600 dark:bg-gray-800
|
| 55 |
dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
group-focus-within/message:visible group-hover/message:visible hover:bg-gray-100
|
| 72 |
hover:text-blue-700 focus:z-10 focus:ring-4
|
| 73 |
focus:ring-gray-100 focus:outline-hidden sm:invisible dark:border-gray-600 dark:bg-gray-800
|
| 74 |
dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
</div>
|
|
|
|
| 1 |
<script lang="ts">
|
|
|
|
|
|
|
| 2 |
import Tooltip from "$lib/components/tooltip.svelte";
|
| 3 |
+
import { TextareaAutosize } from "$lib/spells/textarea-autosize.svelte.js";
|
| 4 |
+
import { PipelineTag, type Conversation, type ConversationMessage } from "$lib/types.js";
|
| 5 |
+
import { fileToDataURL } from "$lib/utils/file.js";
|
| 6 |
+
import { FileUpload } from "melt/builders";
|
| 7 |
+
import { fade } from "svelte/transition";
|
| 8 |
import IconImage from "~icons/carbon/image-reference";
|
| 9 |
|
| 10 |
type Props = {
|
| 11 |
+
conversation: Conversation;
|
| 12 |
+
message: ConversationMessage;
|
| 13 |
loading?: boolean;
|
| 14 |
autofocus?: boolean;
|
| 15 |
onDelete?: () => void;
|
| 16 |
};
|
| 17 |
|
| 18 |
+
let { message = $bindable(), conversation, loading, autofocus, onDelete }: Props = $props();
|
| 19 |
|
| 20 |
let element = $state<HTMLTextAreaElement>();
|
| 21 |
new TextareaAutosize({
|
| 22 |
element: () => element,
|
| 23 |
+
input: () => message.content ?? "",
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
const canUploadImgs = $derived(
|
| 27 |
+
message.role === "user" && conversation.model.pipeline_tag === PipelineTag.ImageTextToText
|
| 28 |
+
);
|
| 29 |
+
const fileUpload = new FileUpload({
|
| 30 |
+
accept: "image/*",
|
| 31 |
+
async onAccept(file) {
|
| 32 |
+
if (!message.images) message.images = [];
|
| 33 |
+
|
| 34 |
+
const dataUrl = await fileToDataURL(file);
|
| 35 |
+
if (message.images.includes(dataUrl)) return;
|
| 36 |
+
|
| 37 |
+
message.images.push(await fileToDataURL(file));
|
| 38 |
+
// We're dealing with files ourselves, so we don't want fileUpload to have any internal state,
|
| 39 |
+
// to avoid conflicts
|
| 40 |
+
fileUpload.clear();
|
| 41 |
+
},
|
| 42 |
+
disabled: () => !canUploadImgs,
|
| 43 |
});
|
| 44 |
</script>
|
| 45 |
|
| 46 |
<div
|
| 47 |
+
class="group/message group relative flex flex-col items-start gap-x-4 gap-y-2 border-b px-3.5 pt-4 pb-6 hover:bg-gray-100/70
|
| 48 |
+
@2xl:px-6 dark:border-gray-800 dark:hover:bg-gray-800/30"
|
| 49 |
class:pointer-events-none={loading}
|
| 50 |
+
{...fileUpload.dropzone}
|
| 51 |
+
onclick={undefined}
|
| 52 |
>
|
| 53 |
+
<div class=" flex w-full flex-col items-start gap-x-4 gap-y-2 @2xl:flex-row">
|
| 54 |
+
{#if fileUpload.isDragging}
|
| 55 |
+
<div
|
| 56 |
+
class="absolute inset-2 z-10 flex flex-col items-center justify-center rounded-xl bg-gray-800/50 backdrop-blur-md"
|
| 57 |
+
transition:fade={{ duration: 100 }}
|
| 58 |
+
>
|
| 59 |
+
<IconImage />
|
| 60 |
+
<p>Drop the image here to upload</p>
|
| 61 |
+
</div>
|
| 62 |
+
{/if}
|
| 63 |
+
|
| 64 |
+
<div class="pt-3 text-sm font-semibold uppercase @2xl:basis-[130px]">
|
| 65 |
+
{message.role}
|
| 66 |
+
</div>
|
| 67 |
+
<div class="flex w-full gap-4">
|
| 68 |
+
<!-- svelte-ignore a11y_autofocus -->
|
| 69 |
+
<!-- svelte-ignore a11y_positive_tabindex -->
|
| 70 |
+
<textarea
|
| 71 |
+
bind:this={element}
|
| 72 |
+
{autofocus}
|
| 73 |
+
bind:value={message.content}
|
| 74 |
+
placeholder="Enter {message.role} message"
|
| 75 |
+
class="grow resize-none overflow-hidden rounded-lg bg-transparent px-2 py-2.5 ring-gray-100 outline-none group-hover/message:ring-3 hover:bg-white focus:bg-white focus:ring-3 @2xl:px-3 dark:ring-gray-600 dark:hover:bg-gray-900 dark:focus:bg-gray-900"
|
| 76 |
+
rows="1"
|
| 77 |
+
tabindex="2"
|
| 78 |
+
></textarea>
|
| 79 |
|
| 80 |
+
{#if canUploadImgs}
|
| 81 |
+
<Tooltip openDelay={250}>
|
| 82 |
+
{#snippet trigger(tooltip)}
|
| 83 |
+
<button
|
| 84 |
+
tabindex="0"
|
| 85 |
+
type="button"
|
| 86 |
+
class="mt-1.5 grid size-8 place-items-center rounded-lg border border-gray-200 bg-white text-xs font-medium text-gray-900
|
| 87 |
group-focus-within/message:visible group-hover/message:visible hover:bg-gray-100
|
| 88 |
hover:text-blue-700 focus:z-10 focus:ring-4
|
| 89 |
focus:ring-gray-100 focus:outline-hidden sm:invisible dark:border-gray-600 dark:bg-gray-800
|
| 90 |
dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
|
| 91 |
+
{...tooltip.trigger}
|
| 92 |
+
{...fileUpload.trigger}
|
| 93 |
+
>
|
| 94 |
+
<IconImage />
|
| 95 |
+
</button>
|
| 96 |
+
<input {...fileUpload.input} />
|
| 97 |
+
{/snippet}
|
| 98 |
+
Add image
|
| 99 |
+
</Tooltip>
|
| 100 |
+
{/if}
|
| 101 |
|
| 102 |
+
<Tooltip>
|
| 103 |
+
{#snippet trigger(tooltip)}
|
| 104 |
+
<button
|
| 105 |
+
tabindex="0"
|
| 106 |
+
onclick={onDelete}
|
| 107 |
+
type="button"
|
| 108 |
+
class="mt-1.5 size-8 rounded-lg border border-gray-200 bg-white text-xs font-medium text-gray-900
|
| 109 |
group-focus-within/message:visible group-hover/message:visible hover:bg-gray-100
|
| 110 |
hover:text-blue-700 focus:z-10 focus:ring-4
|
| 111 |
focus:ring-gray-100 focus:outline-hidden sm:invisible dark:border-gray-600 dark:bg-gray-800
|
| 112 |
dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
|
| 113 |
+
{...tooltip.trigger}
|
| 114 |
+
>
|
| 115 |
+
✕
|
| 116 |
+
</button>
|
| 117 |
+
{/snippet}
|
| 118 |
+
Delete
|
| 119 |
+
</Tooltip>
|
| 120 |
+
</div>
|
| 121 |
</div>
|
| 122 |
+
{#if message.images?.length}
|
| 123 |
+
<div class="mt-2">
|
| 124 |
+
<div class="flex items-center gap-2">
|
| 125 |
+
{#each message.images as img (img)}
|
| 126 |
+
<div class="group/img relative">
|
| 127 |
+
<img src={img} alt="uploaded" class="size-12 rounded-lg object-cover" />
|
| 128 |
+
<button
|
| 129 |
+
type="button"
|
| 130 |
+
onclick={() => (message.images = message.images?.filter(i => i !== img))}
|
| 131 |
+
class="invisible absolute -top-1 -right-1 grid size-5 place-items-center rounded-full bg-gray-800 text-xs text-white group-hover/img:visible hover:bg-gray-700"
|
| 132 |
+
>
|
| 133 |
+
✕
|
| 134 |
+
</button>
|
| 135 |
+
</div>
|
| 136 |
+
{/each}
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
{/if}
|
| 140 |
</div>
|
src/lib/components/inference-playground/utils.ts
CHANGED
|
@@ -1,8 +1,29 @@
|
|
| 1 |
-
import type { Conversation, ModelWithTokenizer } from "$lib/types.js";
|
| 2 |
-
import type { InferenceSnippet } from "@huggingface/tasks";
|
| 3 |
import { type ChatCompletionOutputMessage } from "@huggingface/tasks";
|
| 4 |
|
| 5 |
import { HfInference, snippets, type InferenceProvider } from "@huggingface/inference";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
export async function handleStreamingResponse(
|
| 8 |
hf: HfInference,
|
|
@@ -19,7 +40,7 @@ export async function handleStreamingResponse(
|
|
| 19 |
for await (const chunk of hf.chatCompletionStream(
|
| 20 |
{
|
| 21 |
model: model.id,
|
| 22 |
-
messages,
|
| 23 |
provider: conversation.provider,
|
| 24 |
...conversation.config,
|
| 25 |
},
|
|
@@ -44,7 +65,7 @@ export async function handleNonStreamingResponse(
|
|
| 44 |
|
| 45 |
const response = await hf.chatCompletion({
|
| 46 |
model: model.id,
|
| 47 |
-
messages,
|
| 48 |
provider: conversation.provider,
|
| 49 |
...conversation.config,
|
| 50 |
});
|
|
|
|
| 1 |
+
import type { Conversation, ConversationMessage, ModelWithTokenizer } from "$lib/types.js";
|
| 2 |
+
import type { ChatCompletionInputMessage, InferenceSnippet } from "@huggingface/tasks";
|
| 3 |
import { type ChatCompletionOutputMessage } from "@huggingface/tasks";
|
| 4 |
|
| 5 |
import { HfInference, snippets, type InferenceProvider } from "@huggingface/inference";
|
| 6 |
+
type ChatCompletionInputMessageChunk =
|
| 7 |
+
NonNullable<ChatCompletionInputMessage["content"]> extends string | (infer U)[] ? U : never;
|
| 8 |
+
|
| 9 |
+
function parseMessage(message: ConversationMessage): ChatCompletionInputMessage {
|
| 10 |
+
if (!message.images) return message;
|
| 11 |
+
return {
|
| 12 |
+
...message,
|
| 13 |
+
content: [
|
| 14 |
+
{
|
| 15 |
+
type: "text",
|
| 16 |
+
text: message.content ?? "",
|
| 17 |
+
},
|
| 18 |
+
...message.images.map(img => {
|
| 19 |
+
return {
|
| 20 |
+
type: "image_url",
|
| 21 |
+
image_url: { url: img },
|
| 22 |
+
} satisfies ChatCompletionInputMessageChunk;
|
| 23 |
+
}),
|
| 24 |
+
],
|
| 25 |
+
};
|
| 26 |
+
}
|
| 27 |
|
| 28 |
export async function handleStreamingResponse(
|
| 29 |
hf: HfInference,
|
|
|
|
| 40 |
for await (const chunk of hf.chatCompletionStream(
|
| 41 |
{
|
| 42 |
model: model.id,
|
| 43 |
+
messages: messages.map(parseMessage),
|
| 44 |
provider: conversation.provider,
|
| 45 |
...conversation.config,
|
| 46 |
},
|
|
|
|
| 65 |
|
| 66 |
const response = await hf.chatCompletion({
|
| 67 |
model: model.id,
|
| 68 |
+
messages: messages.map(parseMessage),
|
| 69 |
provider: conversation.provider,
|
| 70 |
...conversation.config,
|
| 71 |
});
|
src/lib/types.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { ChatCompletionInputMessage } from "@huggingface/tasks";
|
|
| 3 |
|
| 4 |
export type ConversationMessage = Pick<ChatCompletionInputMessage, "name" | "role" | "tool_calls"> & {
|
| 5 |
content?: string;
|
|
|
|
| 6 |
};
|
| 7 |
|
| 8 |
export type Conversation = {
|
|
|
|
| 3 |
|
| 4 |
export type ConversationMessage = Pick<ChatCompletionInputMessage, "name" | "role" | "tool_calls"> & {
|
| 5 |
content?: string;
|
| 6 |
+
images?: string[];
|
| 7 |
};
|
| 8 |
|
| 9 |
export type Conversation = {
|
src/lib/utils/file.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function fileToDataURL(file: File): Promise<string> {
|
| 2 |
+
return new Promise((resolve, reject) => {
|
| 3 |
+
const reader = new FileReader();
|
| 4 |
+
|
| 5 |
+
reader.onload = function (event) {
|
| 6 |
+
resolve(event.target?.result as string);
|
| 7 |
+
};
|
| 8 |
+
|
| 9 |
+
reader.onerror = function (error) {
|
| 10 |
+
reject(error);
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
reader.readAsDataURL(file);
|
| 14 |
+
});
|
| 15 |
+
}
|