File size: 1,471 Bytes
efa0b5c
86c4ad7
 
b2ecf7d
86c4ad7
 
 
 
76d4920
 
86c4ad7
 
efa0b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c4ad7
 
 
 
 
 
 
 
 
 
 
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
import { get, writable } from "svelte/store";
import type { ModelData } from "@huggingface/tasks";
import type { ModelLoadInfo, WidgetState } from "./shared/types.js";

export const modelLoadStates = writable<Record<ModelData["id"], ModelLoadInfo>>({});

export const widgetNoInference = writable<Record<ModelData["id"], boolean>>({});

export const isLoggedIn = writable<boolean>(false);

export const widgetStates = writable<Record<ModelData["id"], WidgetState>>({});

const tgiSupportedModels = writable<Set<string> | undefined>(undefined);

export async function getTgiSupportedModels(url: string): Promise<typeof tgiSupportedModels> {
	if (!get(tgiSupportedModels)) {
		const response = await fetch(`${url}/framework/text-generation-inference`);
		const output = await response.json();
		if (response.ok) {
			tgiSupportedModels.set(
				new Set(
					(output as { model_id: string; task: string }[])
						.filter(({ task }) => task === "text-generation")
						.map(({ model_id }) => model_id)
				)
			);
		} else {
			console.warn(response.status, output.error);
		}
	}
	return tgiSupportedModels;
}

export function updateWidgetState(modelId: ModelData["id"], key: keyof WidgetState, val: boolean): void {
	widgetStates.update((states) => {
		// Check if the modelId exists, if not initialize it
		if (!states[modelId]) {
			states[modelId] = {};
		}
		// Update the specific property for the given modelId
		states[modelId][key] = val;
		return states;
	});
}