mishig HF staff commited on
Commit
ea99635
1 Parent(s): 06d9337

refactor abortController

Browse files
src/lib/components/InferencePlayground/InferencePlayground.svelte CHANGED
@@ -40,15 +40,13 @@
40
  let showModelPickerModal = false;
41
  let loading = false;
42
  let latency = 0;
43
- let abortControllers: AbortController[] = [];
44
  let waitForNonStreaming = true;
45
 
46
  $: systemPromptSupported = isSystemPromptSupported(conversation.model);
47
 
48
  onDestroy(() => {
49
- for (const abortController of abortControllers) {
50
- abortController.abort();
51
- }
52
  });
53
 
54
  function addMessage() {
@@ -84,53 +82,12 @@
84
  }
85
 
86
  function abort() {
87
- if (abortControllers.length) {
88
- for (const abortController of abortControllers) {
89
- abortController.abort();
90
- }
91
- abortControllers = [];
92
- }
93
  loading = false;
94
  waitForNonStreaming = false;
95
  }
96
 
97
- async function runInference(conversation: Conversation) {
98
- const startTime = performance.now();
99
- const hf = createHfInference(hfToken);
100
-
101
- if (conversation.streaming) {
102
- const streamingMessage = { role: 'assistant', content: '' };
103
- conversation.messages = [...conversation.messages, streamingMessage];
104
- const abortController = new AbortController();
105
- abortControllers.push(abortController);
106
-
107
- await handleStreamingResponse(
108
- hf,
109
- conversation,
110
- (content) => {
111
- if (streamingMessage) {
112
- streamingMessage.content = content;
113
- conversation.messages = [...conversation.messages];
114
- conversations = conversations;
115
- }
116
- },
117
- abortController,
118
- systemMessage
119
- );
120
- } else {
121
- waitForNonStreaming = true;
122
- const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage);
123
- // check if the user did not abort the request
124
- if (waitForNonStreaming) {
125
- conversation.messages = [...conversation.messages, newMessage];
126
- conversations = conversations;
127
- }
128
- }
129
-
130
- const endTime = performance.now();
131
- latency = Math.round(endTime - startTime);
132
- }
133
-
134
  async function submit() {
135
  // // last message has to be from user
136
  // if (currentConversation.messages?.at(-1)?.role !== 'user') {
@@ -145,8 +102,40 @@
145
  loading = true;
146
 
147
  try {
148
- const promises = conversations.map((conversation) => runInference(conversation));
149
- await Promise.all(promises);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  addMessage();
151
  } catch (error) {
152
  if (error.name !== 'AbortError') {
 
40
  let showModelPickerModal = false;
41
  let loading = false;
42
  let latency = 0;
43
+ let abortController: AbortController | undefined = undefined;
44
  let waitForNonStreaming = true;
45
 
46
  $: systemPromptSupported = isSystemPromptSupported(conversation.model);
47
 
48
  onDestroy(() => {
49
+ abortController?.abort();
 
 
50
  });
51
 
52
  function addMessage() {
 
82
  }
83
 
84
  function abort() {
85
+ abortController?.abort();
86
+ abortController = undefined;
 
 
 
 
87
  loading = false;
88
  waitForNonStreaming = false;
89
  }
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  async function submit() {
92
  // // last message has to be from user
93
  // if (currentConversation.messages?.at(-1)?.role !== 'user') {
 
102
  loading = true;
103
 
104
  try {
105
+ const startTime = performance.now();
106
+ const hf = createHfInference(hfToken);
107
+
108
+ if (conversation.streaming) {
109
+ const streamingMessage = { role: 'assistant', content: '' };
110
+ conversation.messages = [...conversation.messages, streamingMessage];
111
+ abortController = new AbortController();
112
+
113
+ await handleStreamingResponse(
114
+ hf,
115
+ conversation,
116
+ (content) => {
117
+ if (streamingMessage) {
118
+ streamingMessage.content = content;
119
+ conversation.messages = [...conversation.messages];
120
+ conversations = conversations;
121
+ }
122
+ },
123
+ abortController,
124
+ systemMessage
125
+ );
126
+ } else {
127
+ waitForNonStreaming = true;
128
+ const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage);
129
+ // check if the user did not abort the request
130
+ if (waitForNonStreaming) {
131
+ conversation.messages = [...conversation.messages, newMessage];
132
+ conversations = conversations;
133
+ }
134
+ }
135
+
136
+ const endTime = performance.now();
137
+ latency = Math.round(endTime - startTime);
138
+
139
  addMessage();
140
  } catch (error) {
141
  if (error.name !== 'AbortError') {