File size: 5,630 Bytes
0ad7748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a60d004
 
0ad7748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<svelte:options accessors={true} />

<script context="module" lang="ts">
	export { default as BaseMultimodalTextbox } from "./shared/MultimodalTextbox.svelte";
	export { default as BaseExample } from "./Example.svelte";
</script>

<script lang="ts">
	import { Gradio, type SelectData } from "@gradio/utils";
	import MultimodalTextbox from "./shared/MultimodalTextbox.svelte";
	import { Block } from "@gradio/atoms";
	import { StatusTracker } from "@gradio/statustracker";
	import { onMount, tick } from "svelte";
	import type { WaveformOptions } from "../audio/shared/types";
	import type {
		MultimodalTextboxProps,
		MultimodalTextboxEvents
	} from "./types";

	let upload_promise = $state<Promise<any>>();

	class MultimodalTextboxGradio extends Gradio<
		MultimodalTextboxEvents,
		MultimodalTextboxProps
	> {
		async get_data() {
			if (upload_promise) {
				await upload_promise;
				await tick();
			}
			const data = await super.get_data();

			return data;
		}
	}

	let props = $props();
	const gradio = new MultimodalTextboxGradio(props);

	const value = $derived(gradio.props.value || { text: "", files: [] });

	let dragging = $state<boolean>(false);
	let active_source = $state<"microphone" | null>(null);

	let color_accent = "darkorange";

	const waveform_settings = {
		height: 50,
		barWidth: 2,
		barGap: 3,
		cursorWidth: 2,
		cursorColor: "#ddd5e9",
		autoplay: false,
		barRadius: 10,
		dragToSeek: true,
		normalize: true,
		minPxPerSec: 20,
		waveColor: "",
		progressColor: "",
		mediaControls: false as boolean | undefined,
		sampleRate: 44100
	};

	onMount(() => {
		color_accent = getComputedStyle(document?.documentElement).getPropertyValue(
			"--color-accent"
		);
		set_trim_region_colour();
		waveform_settings.waveColor =
			gradio.props?.waveform_options?.waveform_color || "#9ca3af";
		waveform_settings.progressColor =
			gradio.props?.waveform_options?.waveform_progress_color || color_accent;
		waveform_settings.mediaControls =
			gradio.props?.waveform_options?.show_controls;
		waveform_settings.sampleRate =
			gradio.props?.waveform_options?.sample_rate || 44100;
	});

	const trim_region_settings = {
		color: gradio.props?.waveform_options?.trim_region_color,
		drag: true,
		resize: true
	};

	function set_trim_region_colour(): void {
		document.documentElement.style.setProperty(
			"--trim-region-color",
			trim_region_settings.color || color_accent
		);
	}

	// Create const references to the callbacks so that afterUpdate in child is not called on every prop change
	// in the DOM. See https://github.com/gradio-app/gradio/issues/11933
	// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
	const upload_fn = (...args: Parameters<typeof gradio.shared.client.upload>) =>
		gradio.shared.client.upload(...args);
	const i18n = (s: string | null | undefined): string => gradio.i18n(s);
	const stream_handler_fn = (
		...args: Parameters<typeof gradio.shared.client.stream>
	): EventSource => gradio.shared.client.stream(...args);

	let sources_string = $derived(
		gradio.props.sources.join(",") as
			| "upload"
			| "upload,microphone"
			| "microphone"
			| "microphone,upload"
	);

	let file_types_string = $derived.by(
		() => (gradio.props.file_types || []).join(",") || null
	);
</script>

<Block
	visible={gradio.shared.visible}
	elem_id={gradio.shared.elem_id}
	elem_classes={[...(gradio.shared.elem_classes || []), "multimodal-textbox"]}
	scale={gradio.shared.scale}
	min_width={gradio.shared.min_width}
	allow_overflow={false}
	padding={false}
	border_mode={dragging ? "focus" : "base"}
>
	{#if gradio.shared.loading_status}
		<StatusTracker
			autoscroll={gradio.shared.autoscroll}
			i18n={gradio.i18n}
			{...gradio.shared.loading_status}
			on:clear_status={() =>
				gradio.dispatch("clear_status", gradio.shared.loading_status)}
		/>
	{/if}

	<MultimodalTextbox
		bind:upload_promise
		{value}
		value_is_output
		bind:dragging
		bind:active_source
		{file_types_string}
		root={gradio.shared.root}
		label={gradio.shared.label || "MultimodalTextbox"}
		info={gradio.props.info}
		show_label={gradio.shared.show_label}
		lines={gradio.props.lines}
		rtl={gradio.props.rtl}
		text_align={gradio.props.text_align}
		waveform_settings={waveform_settings as WaveformOptions}
		{i18n}
		max_lines={!gradio.props.max_lines
			? gradio.props.lines + 1
			: gradio.props.max_lines}
		placeholder={gradio.props.placeholder}
		submit_btn={gradio.props.submit_btn}
		stop_btn={gradio.props.stop_btn}
		autofocus={gradio.props.autofocus}
		autoscroll={gradio.shared.autoscroll}
		file_count={gradio.props.file_count}
		{sources_string}
		max_file_size={gradio.shared.max_file_size}
		on:change={(e) => (
			(gradio.props.value = e.detail),
			gradio.dispatch("change", gradio.props.value)
		)}
		on:input={() => gradio.dispatch("input")}
		on:submit={() => gradio.dispatch("submit")}
		on:stop={() => gradio.dispatch("stop")}
		on:blur={() => gradio.dispatch("blur")}
		on:select={(e) => gradio.dispatch("select", e.detail)}
		on:focus={() => gradio.dispatch("focus")}
		on:error={({ detail }) => {
			gradio.dispatch("error", detail);
		}}
		on:start_recording={() => gradio.dispatch("start_recording")}
		on:pause_recording={() => gradio.dispatch("pause_recording")}
		on:stop_recording={() => gradio.dispatch("stop_recording")}
		on:upload={(e) => gradio.dispatch("upload", e.detail)}
		on:clear={() => gradio.dispatch("clear")}
		disabled={!gradio.shared.interactive}
		upload={upload_fn}
		stream_handler={stream_handler_fn}
		max_plain_text_length={gradio.props.max_plain_text_length}
		html_attributes={gradio.props.html_attributes}
	/>
</Block>