Skip to content

Commit

Permalink
compile template onMount
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin committed Feb 14, 2024
1 parent 05b333a commit abf7f8a
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import type { WidgetProps, ExampleRunOpts, InferenceRunOpts } from "../../shared/types.js";
import { Template } from "@huggingface/jinja";
import type { SpecialTokensMap, TokenizerConfig, WidgetExampleTextInput } from "@huggingface/tasks";
Expand Down Expand Up @@ -38,15 +39,10 @@
let text = "";
let compiledTemplate: Template;
let tokenizerConfig: TokenizerConfig;
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
const trimmedText = text.trim();
if (!trimmedText) {
return;
}
// Error checking
// Check config and compile template
onMount(() => {
const config = model.config;
if (config === undefined) {
outputJson = "";
Expand All @@ -55,23 +51,32 @@
return;
}
const tokenizerConfig = config.tokenizer as TokenizerConfig | undefined;
if (tokenizerConfig === undefined) {
if (config.tokenizer === undefined) {
outputJson = "";
messages = [];

This comment has been minimized.

Copy link
@SBrandeis

SBrandeis Feb 14, 2024

Contributor

I think those two assignments can be removed from the onMount function

This comment has been minimized.

Copy link
@Wauplin

Wauplin Feb 14, 2024

Author Contributor

You're right. Removed in 25c77c8. Thanks!

error = "Tokenizer config not found";
return;
}
tokenizerConfig = config.tokenizer;
const chatTemplate = tokenizerConfig.chat_template;
if (chatTemplate === undefined) {
outputJson = "";
messages = [];
error = "No chat template found in tokenizer config";
return;
}
compiledTemplate = new Template(chatTemplate);
});
async function getOutput({ withModelLoading = false, isOnLoadCall = false }: InferenceRunOpts = {}) {
if (!compiledTemplate) {
const chatTemplate = tokenizerConfig.chat_template;
if (chatTemplate === undefined) {
outputJson = "";
messages = [];
error = "No chat template found in tokenizer config";
return;
}
compiledTemplate = new Template(chatTemplate);
return;
}
const trimmedText = text.trim();
if (!trimmedText) {
return;
}
if (shouldUpdateUrl && !messages.length) {
Expand Down

0 comments on commit abf7f8a

Please sign in to comment.