File size: 3,983 Bytes
b2ecf7d
94753b6
b2ecf7d
 
 
 
94753b6
b2ecf7d
 
 
 
 
 
 
9d298eb
b2ecf7d
3f534ed
94753b6
 
 
 
 
 
 
 
b2ecf7d
 
94753b6
b2ecf7d
 
 
94753b6
 
b2ecf7d
 
94753b6
b2ecf7d
 
 
94753b6
b2ecf7d
 
 
94753b6
 
 
 
 
 
 
 
b2ecf7d
 
94753b6
 
b2ecf7d
 
 
94753b6
b2ecf7d
94753b6
 
 
 
 
 
 
b2ecf7d
 
94753b6
b2ecf7d
 
 
 
94753b6
b2ecf7d
94753b6
 
 
 
 
 
 
b2ecf7d
 
94753b6
b2ecf7d
 
 
 
 
 
 
 
 
94753b6
b2ecf7d
 
 
94753b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type {
	ChatMessage,
	WidgetExampleAssetAndPromptInput,
	WidgetExampleAssetAndTextInput,
	WidgetExampleAssetAndZeroShotInput,
	WidgetExampleAssetInput,
	WidgetExampleChatInput,
	WidgetExampleSentenceSimilarityInput,
	WidgetExampleStructuredDataInput,
	WidgetExampleTableDataInput,
	WidgetExampleTextAndContextInput,
	WidgetExampleTextAndTableInput,
	WidgetExampleTextInput,
	WidgetExampleZeroShotTextInput,
} from "@huggingface/tasks";

export function isObject(arg: unknown): arg is Record<string, unknown> {
	return !!arg && arg?.constructor === Object;
}
function isStrArray(arg: unknown): arg is string[] {
	return Array.isArray(arg) && arg.every((v) => typeof v === "string");
}

export function isTextInput<TOutput>(sample: unknown): sample is WidgetExampleTextInput<TOutput> {
	return isObject(sample) && "text" in sample && typeof sample.text === "string";
}

export function isTextAndContextInput<TOutput>(sample: unknown): sample is WidgetExampleTextAndContextInput<TOutput> {
	return isTextInput(sample) && "context" in sample;
}

export function isAssetInput<TOutput>(sample: unknown): sample is WidgetExampleAssetInput<TOutput> {
	return isObject(sample) && "src" in sample && typeof sample.src === "string";
}

export function isAssetAndPromptInput<TOutput>(sample: unknown): sample is WidgetExampleAssetAndPromptInput<TOutput> {
	return isAssetInput(sample) && "prompt" in sample && typeof sample.prompt === "string";
}

export function isAssetAndTextInput<TOutput>(sample: unknown): sample is WidgetExampleAssetAndTextInput<TOutput> {
	return isAssetInput(sample) && isTextInput(sample);
}

export function isStructuredDataInput<TOutput>(sample: unknown): sample is WidgetExampleStructuredDataInput<TOutput> {
	/// TODO: check the values' type in sample.structured_data
	return (
		isObject(sample) &&
		"structured_data" in sample &&
		isObject(sample.structured_data) &&
		Object.values(sample.structured_data).every((val) => typeof val === "number" || typeof val === "string")
	);
}

export function isTableDataInput<TOutput>(sample: unknown): sample is WidgetExampleTableDataInput<TOutput> {
	return isObject(sample) && "table" in sample;
}

function _isZeroShotTextInput<TOutput>(
	sample: unknown
): sample is Exclude<WidgetExampleZeroShotTextInput<TOutput>, "text"> {
	return (
		isObject(sample) &&
		"candidate_labels" in sample &&
		typeof sample.candidate_labels === "string" &&
		"multi_class" in sample &&
		typeof sample.multi_class === "boolean"
	);
}

export function isZeroShotTextInput<TOutput>(sample: unknown): sample is WidgetExampleZeroShotTextInput<TOutput> {
	return isTextInput(sample) && _isZeroShotTextInput(sample);
}

export function isSentenceSimilarityInput<TOutput>(
	sample: unknown
): sample is WidgetExampleSentenceSimilarityInput<TOutput> {
	return (
		isObject(sample) &&
		"source_sentence" in sample &&
		typeof sample.candidate_labels === "string" &&
		"sentences" in sample &&
		isStrArray(sample.sentences)
	);
}

export function isTextAndTableInput<TOutput>(sample: unknown): sample is WidgetExampleTextAndTableInput<TOutput> {
	return (
		isTextInput(sample) &&
		"table" in sample &&
		Array.isArray(sample.table) &&
		sample.table.every((r) => Array.isArray(r) && r.every((c) => typeof c === "string" || typeof c === "number"))
	);
}

export function isAssetAndZeroShotInput<TOutput>(
	sample: unknown
): sample is WidgetExampleAssetAndZeroShotInput<TOutput> {
	return isAssetInput(sample) && _isZeroShotTextInput(sample);
}

export function isChatInput<TOutput>(sample: unknown): sample is WidgetExampleChatInput<TOutput> {
	return (
		isObject(sample) &&
		"messages" in sample &&
		Array.isArray(sample.messages) &&
		sample.messages.every(
			(message): message is ChatMessage =>
				isObject(message) &&
				"role" in message &&
				"content" in message &&
				typeof message.role === "string" &&
				["user", "system", "assistant"].includes(message.role) &&
				typeof message.content === "string"
		)
	);
}