Spaces:
Runtime error
Runtime error
File size: 8,380 Bytes
effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 61996ad effb90e 18cd77b effb90e 61996ad effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b 61996ad 18cd77b effb90e 61996ad 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 61996ad effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e 18cd77b effb90e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
<script lang="ts">
import { PUBLIC_BACKEND_WS_URL } from '$env/static/public';
import { onMount, tick } from 'svelte';
import { nanoid } from 'nanoid';
import { chatsStore, selectedChatId, loadingState, lastBase64Image } from '$lib/store';
import type { Message, Chat, InferenceResponse, ImageFile } from '$lib/types';
import { MessageType, Sender } from '$lib/types';
import { timeFormater, fetchImageBase64 } from '$lib/utils';
import ChatInput from '$lib/ChatInput.svelte';
import ChatMessage from '$lib/ChatMessage.svelte';
import ChatNewBtn from '$lib/ChatNewBtn.svelte';
import IconDelete from './Icons/IconDelete.svelte';
$: isLoading = !($loadingState === '' || $loadingState === 'Complete');
let chatBoxEl: HTMLDivElement;
let chatInputEl: HTMLInputElement;
function clearStateMsg(t = 5000) {
setTimeout(() => {
$loadingState = '';
}, t);
}
onMount(() => {
const observer = new ResizeObserver(() => {
window.scrollTo(0, chatBoxEl.getBoundingClientRect().height);
if ('parentIFrame' in window) {
(window as any).parentIFrame.scrollTo(0, chatBoxEl.getBoundingClientRect().height);
}
});
observer.observe(chatBoxEl);
// generateImage();
});
$: chatData = $chatsStore.find((chat) => chat.id === $selectedChatId);
$: messages = chatData?.messages.sort((a, b) => a.timestamp - b.timestamp) || [];
$: if ($selectedChatId) {
// find last message with image type
$lastBase64Image =
[...messages].reverse().find((message) => message.type === MessageType.IMAGE)?.content ||
null;
}
// this is when page starts or user deletes all chats
function newChat() {
const chatId = nanoid();
const chat: Chat = {
id: chatId,
blurb: `New Chat - ${chatId}`,
messages: [],
timestamp: new Date().getTime()
};
$chatsStore = [chat].concat($chatsStore);
$selectedChatId = chat.id;
}
function deleteChat(id: string) {
$chatsStore = $chatsStore.filter((chat) => chat.id !== id);
}
// this is called when user send a text or image
// if image then update last image store
// if text and has image send to inference
function submitMessage(event: CustomEvent) {
if ($chatsStore.length === 0) {
newChat();
}
const { type, content } = event.detail;
const message: Message = {
sender: Sender.USER,
id: nanoid(),
type: type,
content: content,
timestamp: new Date().getTime()
};
updateChatStore(message);
if (type === MessageType.IMAGE) {
// upate last image to be the last image sent
$lastBase64Image = content;
} else if (type === MessageType.TEXT) {
// if the last message was an image, then we want to run inference
// on the image and the text
if (!$lastBase64Image) {
const message: Message = {
sender: Sender.BOT,
id: nanoid(),
type: MessageType.TEXT,
content: "Sorry, I don't have an image to work with.",
timestamp: new Date().getTime()
};
updateChatStore(message);
return;
}
runInference($lastBase64Image, content);
}
}
// hack to update store
function updateChatStore(message: Message) {
$chatsStore = $chatsStore.map((chat) => {
if (chat.id === $selectedChatId) {
chat.messages.push(message);
}
return chat;
});
}
// run inference via websockets
async function runInference(image: string, prompt: string) {
if (isLoading || image === '' || prompt === '') {
return;
}
$loadingState = 'Pending';
const sessionHash = crypto.randomUUID();
const hashpayload = {
fn_index: 1,
session_hash: sessionHash
};
const datapayload = {
data: [
prompt, // prompt
10.5, // text guidance
1.5, // image guidance
image,
15, // steps
'', // negative promtp,
512, // width
512, // height
0 // seed
]
};
const websocket = new WebSocket(`wss://${PUBLIC_BACKEND_WS_URL}/queue/join`);
// websocket.onopen = async function (event) {
// websocket.send(JSON.stringify({ hash: sessionHash }));
// };
websocket.onclose = (evt) => {
if (!evt.wasClean) {
$loadingState = 'Error';
}
};
websocket.onmessage = async function (event) {
try {
const data = JSON.parse(event.data);
$loadingState = '';
switch (data.msg) {
case 'send_hash':
websocket.send(JSON.stringify(hashpayload));
break;
case 'send_data':
$loadingState = 'Sending Data';
websocket.send(JSON.stringify({ ...hashpayload, ...datapayload }));
break;
case 'queue_full':
$loadingState = 'Queue full';
websocket.close();
return;
case 'estimation':
const { rank, queue_size } = data;
$loadingState = `On queue ${rank}/${queue_size}`;
break;
case 'process_generating':
$loadingState = data.success ? 'Generating' : 'Error';
break;
// here is success
// got an image in response from inference
// then update the chat store
case 'process_completed':
try {
const response = data.output as InferenceResponse;
const imageData = response.data[0] as ImageFile[];
const nsfwData = data.output.data[1] as boolean[] | null;
console.log('imageData', imageData);
console.log('nsfwData', nsfwData);
if (nsfwData && nsfwData[0]) {
const message: Message = {
sender: Sender.BOT,
id: nanoid(),
type: MessageType.TEXT,
content:
'Sorry this prompt possibly generates NSFW content. Please try another prompt.',
timestamp: new Date().getTime()
};
updateChatStore(message);
} else {
const fileName = imageData[0].name;
const imageBase64 = await fetchImageBase64(
`https://${PUBLIC_BACKEND_WS_URL}/file=${fileName}`
);
const message: Message = {
sender: Sender.BOT,
id: nanoid(),
type: MessageType.IMAGE,
content: imageBase64,
timestamp: new Date().getTime()
};
$lastBase64Image = imageBase64;
updateChatStore(message);
}
$loadingState = data.success ? 'Complete' : 'Error';
clearStateMsg();
} catch (err) {
const tError = err as Error;
$loadingState = tError?.message;
clearStateMsg(10000);
}
websocket.close();
return;
case 'process_starts':
$loadingState = 'Processing';
break;
}
} catch (e) {
console.error(e);
$loadingState = 'Error';
}
};
}
</script>
<div>
<h1 class="text-2xl">CHATS</h1>
<div class="grid min-h-[40rem] grid-cols-4">
<div class="col-span-1 flex flex-col border-r p-4 relative">
<div class="sticky top-3">
<ChatNewBtn on:click={newChat} />
<div class="max-h-[40rem] flex flex-col gap-2 overflow-y-scroll">
{#if $chatsStore.length}
{#each $chatsStore as chat}
<div class="flex flex-col relative">
<button
class="disabled:opacity-60 disabled:cursor-progress"
on:click={() => ($selectedChatId = chat.id)}
disabled={isLoading}
>
<div
class=" flex flex-col h-16 items-start justify-center rounded-xl bg-gray-100 px-4 text-gray-900
{chat.id === $selectedChatId ? 'bg-gray-400' : ''}"
>
<h3 class="w-full truncate font-semibold">{chat.blurb}</h3>
<p class="w-full truncate text-sm text-gray-500">
{timeFormater(new Date(chat.timestamp))}
</p>
</div>
</button>
<button
class="text-black absolute right-1 bottom-1 disabled:opacity-60"
on:click={() => deleteChat(chat.id)}
disabled={isLoading}
>
<IconDelete />
</button>
</div>
{/each}
{:else}
<div
class="flex h-16 flex-col items-start justify-center rounded-xl bg-gray-100 px-4 text-gray-900"
>
<h3 class="w-full truncate font-semibold">No chats</h3>
<p class="w-full truncate text-sm text-gray-500">Start a new Chat!</p>
</div>
{/if}
</div>
</div>
</div>
<div class="col-span-3 flex flex-col" bind:this={chatBoxEl}>
{#each messages as message}
<ChatMessage {message} />
{/each}
<ChatInput on:submitMessage={submitMessage} bind:inputEl={chatInputEl} disabled={isLoading} />
</div>
<div class="top-0 right-0 z-10">
Loading: {$loadingState}
</div>
</div>
</div>
<style lang="postcss" scoped>
</style>
|