Spaces:
Runtime error
Runtime error
refactor abortController
Browse files39 unmodified lines404142434344454647484950495150515229 unmodified lines8283848788899091928586878889909798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321339192938 unmodified lines10210310414814910510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014139 unmodified lines let showModelPickerModal = false; let loading = false; let latency = 0; let abortControllers: AbortController[] = []; let abortController: AbortController | undefined = undefined; let waitForNonStreaming = true;
$: systemPromptSupported = isSystemPromptSupported(conversation.model);
onDestroy(() => { for (const abortController of abortControllers) { abortController.abort(); abortController?.abort(); } });
function addMessage() {29 unmodified lines }
function abort() { if (abortControllers.length) { for (const abortController of abortControllers) { abortController.abort(); } abortControllers = []; } abortController?.abort(); abortController = undefined; loading = false; waitForNonStreaming = false; }
async function runInference(conversation: Conversation) { const startTime = performance.now(); const hf = createHfInference(hfToken);
if (conversation.streaming) { const streamingMessage = { role: 'assistant', content: '' }; conversation.messages = [...conversation.messages, streamingMessage]; const abortController = new AbortController(); abortControllers.push(abortController);
await handleStreamingResponse( hf, conversation, (content) => { if (streamingMessage) { streamingMessage.content = content; conversation.messages = [...conversation.messages]; conversations = conversations; } }, abortController, systemMessage ); } else { waitForNonStreaming = true; const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage); // check if the user did not abort the request if (waitForNonStreaming) { conversation.messages = [...conversation.messages, newMessage]; conversations = conversations; } }
const endTime = performance.now(); latency = Math.round(endTime - startTime); }
async function submit() { // // last message has to be from user // if (currentConversation.messages?.at(-1)?.role !== 'user') {8 unmodified lines loading = true;
try { const promises = conversations.map((conversation) => runInference(conversation)); await Promise.all(promises); const startTime = performance.now(); const hf = createHfInference(hfToken);
if (conversation.streaming) { const streamingMessage = { role: 'assistant', content: '' }; conversation.messages = [...conversation.messages, streamingMessage]; abortController = new AbortController();
await handleStreamingResponse( hf, conversation, (content) => { if (streamingMessage) { streamingMessage.content = content; conversation.messages = [...conversation.messages]; conversations = conversations; } }, abortController, systemMessage ); } else { waitForNonStreaming = true; const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage); // check if the user did not abort the request if (waitForNonStreaming) { conversation.messages = [...conversation.messages, newMessage]; conversations = conversations; } }
const endTime = performance.now(); latency = Math.round(endTime - startTime);
addMessage(); } catch (error) { if (error.name !== 'AbortError') {src/lib/components/InferencePlayground/InferencePlayground.svelte+38-49