machineuser commited on
Commit
5a13705
1 Parent(s): 7a24123

Sync widgets demo

Browse files
packages/widgets/src/lib/components/InferenceWidget/widgets/ConversationalWidget/ConversationalWidget.svelte CHANGED
@@ -1,6 +1,7 @@
1
  <script lang="ts">
2
  import { onMount, tick } from "svelte";
3
  import type { WidgetProps, ExampleRunOpts, InferenceRunOpts } from "../../shared/types.js";
 
4
  import { Template } from "@huggingface/jinja";
5
  import type {
6
  SpecialTokensMap,
@@ -22,7 +23,7 @@
22
  import { addInferenceParameters, updateUrl } from "../../shared/helpers.js";
23
  import { widgetStates, getTgiSupportedModels } from "../../stores.js";
24
  import type { Writable } from "svelte/store";
25
- import { isChatInput, isObject, isTextInput } from "../../shared/inputValidation.js";
26
  import { isValidOutputText } from "../../shared/outputValidation.js";
27
 
28
  export let apiToken: WidgetProps["apiToken"];
@@ -41,7 +42,7 @@
41
 
42
  let messages: ChatMessage[] = [];
43
  let error: string = "";
44
- let isLoading = false;
45
  let outputJson: string;
46
  let text = "";
47
 
@@ -52,9 +53,6 @@
52
 
53
  // Check config and compile template
54
  onMount(() => {
55
- (async () => {
56
- tgiSupportedModels = await getTgiSupportedModels(apiUrl);
57
- })();
58
  const config = model.config;
59
  if (config === undefined) {
60
  error = "Model config not found";
@@ -79,7 +77,7 @@
79
  return;
80
  }
81
 
82
- inferenceClient = new HfInference();
83
  });
84
 
85
  async function handleNewMessage(): Promise<void> {
@@ -108,6 +106,8 @@
108
 
109
  async function getOutput({
110
  withModelLoading = false,
 
 
111
  exampleOutput = undefined,
112
  }: InferenceRunOpts<WidgetExampleOutputText> = {}) {
113
  if (exampleOutput) {
@@ -151,6 +151,15 @@
151
  text = "";
152
  error = "";
153
  try {
 
 
 
 
 
 
 
 
 
154
  if ($tgiSupportedModels?.has(model.id)) {
155
  console.debug("Starting text generation using the TGI streaming API");
156
  let newMessage = {
@@ -164,7 +173,7 @@
164
  model: model.id,
165
  accessToken: apiToken,
166
  },
167
- { signal: abort?.signal }
168
  );
169
  for await (const newToken of tokenStream) {
170
  if (newToken.token.special) continue;
@@ -175,10 +184,7 @@
175
  } else {
176
  console.debug("Starting text generation using the synchronous API");
177
  input.parameters.max_new_tokens = 100;
178
- const output = await inferenceClient.textGeneration(
179
- { ...input, model: model.id, accessToken: apiToken },
180
- { includeCredentials, dont_load_model: !withModelLoading, signal: abort?.signal }
181
- );
182
  messages = [...messages, { role: "assistant", content: output.generated_text }];
183
  await tick();
184
  }
@@ -222,7 +228,7 @@
222
  return;
223
  }
224
  const exampleOutput = example.output;
225
- getOutput({ ...opts.inferenceOpts, exampleOutput });
226
  } finally {
227
  isLoading = false;
228
  }
 
1
  <script lang="ts">
2
  import { onMount, tick } from "svelte";
3
  import type { WidgetProps, ExampleRunOpts, InferenceRunOpts } from "../../shared/types.js";
4
+ import type { Options } from "@huggingface/inference";
5
  import { Template } from "@huggingface/jinja";
6
  import type {
7
  SpecialTokensMap,
 
23
  import { addInferenceParameters, updateUrl } from "../../shared/helpers.js";
24
  import { widgetStates, getTgiSupportedModels } from "../../stores.js";
25
  import type { Writable } from "svelte/store";
26
+ import { isChatInput, isTextInput } from "../../shared/inputValidation.js";
27
  import { isValidOutputText } from "../../shared/outputValidation.js";
28
 
29
  export let apiToken: WidgetProps["apiToken"];
 
42
 
43
  let messages: ChatMessage[] = [];
44
  let error: string = "";
45
+ let isLoading: boolean = false;
46
  let outputJson: string;
47
  let text = "";
48
 
 
53
 
54
  // Check config and compile template
55
  onMount(() => {
 
 
 
56
  const config = model.config;
57
  if (config === undefined) {
58
  error = "Model config not found";
 
77
  return;
78
  }
79
 
80
+ inferenceClient = new HfInference(apiToken);
81
  });
82
 
83
  async function handleNewMessage(): Promise<void> {
 
106
 
107
  async function getOutput({
108
  withModelLoading = false,
109
+ isOnLoadCall = false,
110
+ useCache = true,
111
  exampleOutput = undefined,
112
  }: InferenceRunOpts<WidgetExampleOutputText> = {}) {
113
  if (exampleOutput) {
 
151
  text = "";
152
  error = "";
153
  try {
154
+ const opts = {
155
+ dont_load_model: isOnLoadCall,
156
+ includeCredentials,
157
+ signal: abort?.signal,
158
+ use_cache: useCache,
159
+ wait_for_model: withModelLoading,
160
+ } satisfies Options;
161
+
162
+ tgiSupportedModels = await getTgiSupportedModels(apiUrl);
163
  if ($tgiSupportedModels?.has(model.id)) {
164
  console.debug("Starting text generation using the TGI streaming API");
165
  let newMessage = {
 
173
  model: model.id,
174
  accessToken: apiToken,
175
  },
176
+ opts
177
  );
178
  for await (const newToken of tokenStream) {
179
  if (newToken.token.special) continue;
 
184
  } else {
185
  console.debug("Starting text generation using the synchronous API");
186
  input.parameters.max_new_tokens = 100;
187
+ const output = await inferenceClient.textGeneration({ ...input, model: model.id, accessToken: apiToken }, opts);
 
 
 
188
  messages = [...messages, { role: "assistant", content: output.generated_text }];
189
  await tick();
190
  }
 
228
  return;
229
  }
230
  const exampleOutput = example.output;
231
+ await getOutput({ ...opts.inferenceOpts, exampleOutput });
232
  } finally {
233
  isLoading = false;
234
  }
packages/widgets/src/routes/+page.svelte CHANGED
@@ -45,29 +45,17 @@
45
  bos_token: "<s>",
46
  eos_token: "</s>",
47
  unk_token: "<unk>",
48
- pad_token: undefined,
49
  },
50
  },
51
  widgetData: [
52
  { text: "This is a text-only example", example_title: "Text only" },
53
  {
54
- messages: [
55
- {
56
- content: "You are a helpful assistant replying in very brief and straight-to-the-point answers.",
57
- role: "system",
58
- },
59
- { content: "Please exlain QCD in very few words", role: "user" },
60
- ],
61
  example_title: "Chat messages",
62
  },
63
  {
64
- messages: [
65
- {
66
- content: "You are a helpful assistant replying in very brief and straight-to-the-point answers.",
67
- role: "system",
68
- },
69
- { content: "Please exlain QCD in very few words", role: "user" },
70
- ],
71
  output: {
72
  text: "QCD is the physics of strong force and small particles.",
73
  },
@@ -80,6 +68,13 @@
80
  },
81
  example_title: "Text only with Output",
82
  },
 
 
 
 
 
 
 
83
  ],
84
  },
85
  {
 
45
  bos_token: "<s>",
46
  eos_token: "</s>",
47
  unk_token: "<unk>",
48
+ pad_token: null,
49
  },
50
  },
51
  widgetData: [
52
  { text: "This is a text-only example", example_title: "Text only" },
53
  {
54
+ messages: [{ content: "Please exlain QCD in very few words", role: "user" }],
 
 
 
 
 
 
55
  example_title: "Chat messages",
56
  },
57
  {
58
+ messages: [{ content: "Please exlain QCD in very few words", role: "user" }],
 
 
 
 
 
 
59
  output: {
60
  text: "QCD is the physics of strong force and small particles.",
61
  },
 
68
  },
69
  example_title: "Text only with Output",
70
  },
71
+ {
72
+ example_title: "Invalid example - unsupported role",
73
+ messages: [
74
+ { role: "system", content: "This will fail because of the chat template" },
75
+ { role: "user", content: "What's your favorite condiment?" },
76
+ ],
77
+ },
78
  ],
79
  },
80
  {