File size: 4,493 Bytes
67ad93e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
<script lang="ts">
	import { FileData, type Client } from "@gradio/client";
	import { onMount, onDestroy } from "svelte";

	type FileDataWithProgress = FileData & { progress: number };

	let {
		upload_id,
		root,
		files,
		stream_handler,
		ondone
	}: {
		upload_id: string;
		root: string;
		files: FileData[];
		stream_handler: Client["stream"];
		ondone?: () => void;
	} = $props();

	let stream: Awaited<ReturnType<Client["stream"]>>;
	let progress = $state(false);
	let current_file_upload = $state<FileDataWithProgress>();
	let file_to_display = $derived(current_file_upload || files_with_progress[0]);

	let files_with_progress = $state<FileDataWithProgress[]>(
		files.map((file) => {
			return {
				...file,
				progress: 0
			};
		})
	);

	function handleProgress(filename: string, chunk_size: number): void {
		// Find the corresponding file in the array and update its progress
		files_with_progress = files_with_progress.map((file) => {
			if (file.orig_name === filename) {
				file.progress += chunk_size;
			}
			return file;
		});
	}

	function getProgress(file: FileDataWithProgress): number {
		return (file.progress * 100) / (file.size || 0) || 0;
	}

	onMount(async () => {
		stream = await stream_handler(
			new URL(`${root}/gradio_api/upload_progress?upload_id=${upload_id}`)
		);

		if (stream == null) {
			throw new Error("Event source is not defined");
		}
		// Event listener for progress updates
		stream.onmessage = async function (event) {
			const _data = JSON.parse(event.data);
			if (!progress) progress = true;
			if (_data.msg === "done") {
				// the stream will close itself but is here for clarity; remove .close() in 5.0
				stream?.close();
				ondone?.();
			} else {
				current_file_upload = _data;
				handleProgress(_data.orig_name, _data.chunk_size);
			}
		};
	});

	onDestroy(() => {
		// the stream will close itself but is here for clarity; remove .close() in 5.0
		if (stream != null || stream != undefined) stream.close();
	});

	function calculateTotalProgress(files: FileDataWithProgress[]): number {
		let totalProgress = 0;
		files.forEach((file) => {
			totalProgress += getProgress(file);
		});

		document.documentElement.style.setProperty(
			"--upload-progress-width",
			(totalProgress / files.length).toFixed(2) + "%"
		);

		return totalProgress / files.length;
	}

	$effect(() => {
		calculateTotalProgress(files_with_progress);
	});
</script>

<div class="wrap" class:progress>
	<span class="uploading"
		>Uploading {files_with_progress.length}
		{files_with_progress.length > 1 ? "files" : "file"}...</span
	>

	{#if file_to_display}
		<div class="file">
			<span>
				<div class="progress-bar">
					<progress
						style="visibility:hidden;height:0;width:0;"
						value={getProgress(file_to_display)}
						max="100">{getProgress(file_to_display)}</progress
					>
				</div>
			</span>
			<span class="file-name">
				{file_to_display.orig_name}
			</span>
		</div>
	{/if}
</div>

<style>
	.wrap {
		overflow-y: auto;
		transition: opacity 0.5s ease-in-out;
		background: var(--block-background-fill);
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		min-height: var(--size-40);
		width: var(--size-full);
	}

	.wrap::after {
		content: "";
		position: absolute;
		top: 0;
		left: 0;
		width: var(--upload-progress-width);
		height: 100%;
		transition: all 0.5s ease-in-out;
		z-index: 1;
	}

	.uploading {
		font-size: var(--text-lg);
		font-family: var(--font);
		z-index: 2;
	}

	.file-name {
		margin: var(--spacing-md);
		font-size: var(--text-lg);
		color: var(--body-text-color-subdued);
	}

	.file {
		font-size: var(--text-md);
		z-index: 2;
		display: flex;
		align-items: center;
	}

	.file progress {
		display: inline;
		height: var(--size-1);
		width: 100%;
		transition: all 0.5s ease-in-out;
		color: var(--color-accent);
		border: none;
	}

	.file progress[value]::-webkit-progress-value {
		background-color: var(--color-accent);
		border-radius: 20px;
	}

	.file progress[value]::-webkit-progress-bar {
		background-color: var(--border-color-accent);
		border-radius: 20px;
	}

	.progress-bar {
		width: 14px;
		height: 14px;
		border-radius: 50%;
		background:
			radial-gradient(
				closest-side,
				var(--block-background-fill) 64%,
				transparent 53% 100%
			),
			conic-gradient(
				var(--color-accent) var(--upload-progress-width),
				var(--border-color-accent) 0
			);
		transition: all 0.5s ease-in-out;
	}
</style>