gradio-pr-bot commited on
Commit
2d37aa2
·
verified ·
1 Parent(s): 8218b9c

Upload folder using huggingface_hub

Browse files
5.49.1/image/Example.svelte ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Image from "./shared/Image.svelte";
3
+ import type { FileData } from "@gradio/client";
4
+
5
+ export let value: null | FileData;
6
+ export let type: "gallery" | "table";
7
+ export let selected = false;
8
+ </script>
9
+
10
+ <div
11
+ class="container"
12
+ class:table={type === "table"}
13
+ class:gallery={type === "gallery"}
14
+ class:selected
15
+ class:border={value}
16
+ >
17
+ {#if value}
18
+ <Image src={value.url} alt="" />
19
+ {/if}
20
+ </div>
21
+
22
+ <style>
23
+ .container :global(img) {
24
+ width: 100%;
25
+ height: 100%;
26
+ }
27
+
28
+ .container.selected {
29
+ border-color: var(--border-color-accent);
30
+ }
31
+ .border.table {
32
+ border: 2px solid var(--border-color-primary);
33
+ }
34
+
35
+ .container.table {
36
+ margin: 0 auto;
37
+ border-radius: var(--radius-lg);
38
+ overflow: hidden;
39
+ width: var(--size-20);
40
+ height: var(--size-20);
41
+ object-fit: cover;
42
+ }
43
+
44
+ .container.gallery {
45
+ width: var(--size-20);
46
+ max-width: var(--size-20);
47
+ object-fit: cover;
48
+ }
49
+ </style>
5.49.1/image/Index.svelte ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script context="module" lang="ts">
4
+ export { default as Webcam } from "./shared/Webcam.svelte";
5
+ export { default as BaseImageUploader } from "./shared/ImageUploader.svelte";
6
+ export { default as BaseStaticImage } from "./shared/ImagePreview.svelte";
7
+ export { default as BaseExample } from "./Example.svelte";
8
+ export { default as BaseImage } from "./shared/Image.svelte";
9
+ </script>
10
+
11
+ <script lang="ts">
12
+ import type { Gradio, SelectData, ValueData } from "@gradio/utils";
13
+ import StaticImage from "./shared/ImagePreview.svelte";
14
+ import ImageUploader from "./shared/ImageUploader.svelte";
15
+ import { afterUpdate } from "svelte";
16
+ import type { WebcamOptions } from "./shared/types";
17
+
18
+ import { Block, Empty, UploadText } from "@gradio/atoms";
19
+ import { Image } from "@gradio/icons";
20
+ import { StatusTracker } from "@gradio/statustracker";
21
+ import { upload, type FileData } from "@gradio/client";
22
+ import type { LoadingStatus } from "@gradio/statustracker";
23
+
24
+ type sources = "upload" | "webcam" | "clipboard" | null;
25
+
26
+ let stream_state = "closed";
27
+ let _modify_stream: (state: "open" | "closed" | "waiting") => void = () => {};
28
+ export function modify_stream_state(
29
+ state: "open" | "closed" | "waiting"
30
+ ): void {
31
+ stream_state = state;
32
+ _modify_stream(state);
33
+ }
34
+ export const get_stream_state: () => void = () => stream_state;
35
+ export let set_time_limit: (arg0: number) => void;
36
+ export let value_is_output = false;
37
+ export let elem_id = "";
38
+ export let elem_classes: string[] = [];
39
+ export let visible: boolean | "hidden" = true;
40
+ export let value: null | FileData = null;
41
+ let old_value: null | FileData = null;
42
+ export let label: string;
43
+ export let show_label: boolean;
44
+ export let show_download_button: boolean;
45
+ export let root: string;
46
+
47
+ export let height: number | undefined;
48
+ export let width: number | undefined;
49
+ export let stream_every: number;
50
+
51
+ export let _selectable = false;
52
+ export let container = true;
53
+ export let scale: number | null = null;
54
+ export let min_width: number | undefined = undefined;
55
+ export let loading_status: LoadingStatus;
56
+ export let show_share_button = false;
57
+ export let sources: ("clipboard" | "webcam" | "upload")[] = [
58
+ "upload",
59
+ "clipboard",
60
+ "webcam"
61
+ ];
62
+ export let interactive: boolean;
63
+ export let streaming: boolean;
64
+ export let pending: boolean;
65
+ export let placeholder: string | undefined = undefined;
66
+ export let show_fullscreen_button: boolean;
67
+ export let input_ready: boolean;
68
+ export let webcam_options: WebcamOptions;
69
+ let fullscreen = false;
70
+
71
+ let uploading = false;
72
+ $: input_ready = !uploading;
73
+ export let gradio: Gradio<{
74
+ input: never;
75
+ change: never;
76
+ error: string;
77
+ edit: never;
78
+ stream: ValueData;
79
+ drag: never;
80
+ upload: never;
81
+ clear: never;
82
+ select: SelectData;
83
+ share: ShareData;
84
+ clear_status: LoadingStatus;
85
+ close_stream: string;
86
+ }>;
87
+
88
+ $: {
89
+ if (JSON.stringify(value) !== JSON.stringify(old_value)) {
90
+ old_value = value;
91
+ gradio.dispatch("change");
92
+ if (!value_is_output) {
93
+ gradio.dispatch("input");
94
+ }
95
+ }
96
+ }
97
+
98
+ afterUpdate(() => {
99
+ value_is_output = false;
100
+ });
101
+
102
+ let dragging: boolean;
103
+ let active_source: sources = null;
104
+ let upload_component: ImageUploader;
105
+ const handle_drag_event = (event: Event): void => {
106
+ const drag_event = event as DragEvent;
107
+ drag_event.preventDefault();
108
+ drag_event.stopPropagation();
109
+ if (drag_event.type === "dragenter" || drag_event.type === "dragover") {
110
+ dragging = true;
111
+ } else if (drag_event.type === "dragleave") {
112
+ dragging = false;
113
+ }
114
+ };
115
+
116
+ const handle_drop = (event: Event): void => {
117
+ if (interactive) {
118
+ const drop_event = event as DragEvent;
119
+ drop_event.preventDefault();
120
+ drop_event.stopPropagation();
121
+ dragging = false;
122
+
123
+ if (upload_component) {
124
+ upload_component.loadFilesFromDrop(drop_event);
125
+ }
126
+ }
127
+ };
128
+ </script>
129
+
130
+ {#if !interactive}
131
+ <Block
132
+ {visible}
133
+ variant={"solid"}
134
+ border_mode={dragging ? "focus" : "base"}
135
+ padding={false}
136
+ {elem_id}
137
+ {elem_classes}
138
+ height={height || undefined}
139
+ {width}
140
+ allow_overflow={false}
141
+ {container}
142
+ {scale}
143
+ {min_width}
144
+ bind:fullscreen
145
+ >
146
+ <StatusTracker
147
+ autoscroll={gradio.autoscroll}
148
+ i18n={gradio.i18n}
149
+ {...loading_status}
150
+ />
151
+ <StaticImage
152
+ on:select={({ detail }) => gradio.dispatch("select", detail)}
153
+ on:share={({ detail }) => gradio.dispatch("share", detail)}
154
+ on:error={({ detail }) => gradio.dispatch("error", detail)}
155
+ on:fullscreen={({ detail }) => {
156
+ fullscreen = detail;
157
+ }}
158
+ {fullscreen}
159
+ {value}
160
+ {label}
161
+ {show_label}
162
+ {show_download_button}
163
+ selectable={_selectable}
164
+ {show_share_button}
165
+ i18n={gradio.i18n}
166
+ {show_fullscreen_button}
167
+ />
168
+ </Block>
169
+ {:else}
170
+ <Block
171
+ {visible}
172
+ variant={value === null ? "dashed" : "solid"}
173
+ border_mode={dragging ? "focus" : "base"}
174
+ padding={false}
175
+ {elem_id}
176
+ {elem_classes}
177
+ height={height || undefined}
178
+ {width}
179
+ allow_overflow={false}
180
+ {container}
181
+ {scale}
182
+ {min_width}
183
+ bind:fullscreen
184
+ on:dragenter={handle_drag_event}
185
+ on:dragleave={handle_drag_event}
186
+ on:dragover={handle_drag_event}
187
+ on:drop={handle_drop}
188
+ >
189
+ <StatusTracker
190
+ autoscroll={gradio.autoscroll}
191
+ i18n={gradio.i18n}
192
+ {...loading_status}
193
+ on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
194
+ />
195
+
196
+ <ImageUploader
197
+ bind:this={upload_component}
198
+ bind:uploading
199
+ bind:active_source
200
+ bind:value
201
+ bind:dragging
202
+ selectable={_selectable}
203
+ {root}
204
+ {sources}
205
+ {fullscreen}
206
+ on:edit={() => gradio.dispatch("edit")}
207
+ on:clear={() => {
208
+ fullscreen = false;
209
+ gradio.dispatch("clear");
210
+ }}
211
+ on:stream={({ detail }) => gradio.dispatch("stream", detail)}
212
+ on:drag={({ detail }) => (dragging = detail)}
213
+ on:upload={() => gradio.dispatch("upload")}
214
+ on:select={({ detail }) => gradio.dispatch("select", detail)}
215
+ on:share={({ detail }) => gradio.dispatch("share", detail)}
216
+ on:error={({ detail }) => {
217
+ loading_status = loading_status || {};
218
+ loading_status.status = "error";
219
+ gradio.dispatch("error", detail);
220
+ }}
221
+ on:close_stream={() => {
222
+ gradio.dispatch("close_stream", "stream");
223
+ }}
224
+ on:fullscreen={({ detail }) => {
225
+ fullscreen = detail;
226
+ }}
227
+ {label}
228
+ {show_label}
229
+ {pending}
230
+ {streaming}
231
+ {webcam_options}
232
+ {stream_every}
233
+ bind:modify_stream={_modify_stream}
234
+ bind:set_time_limit
235
+ max_file_size={gradio.max_file_size}
236
+ i18n={gradio.i18n}
237
+ upload={(...args) => gradio.client.upload(...args)}
238
+ stream_handler={gradio.client?.stream}
239
+ >
240
+ {#if active_source === "upload" || !active_source}
241
+ <UploadText i18n={gradio.i18n} type="image" {placeholder} />
242
+ {:else if active_source === "clipboard"}
243
+ <UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
244
+ {:else}
245
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
246
+ {/if}
247
+ </ImageUploader>
248
+ </Block>
249
+ {/if}
5.49.1/image/package.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/image",
3
+ "version": "0.23.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/icons": "workspace:^",
13
+ "@gradio/statustracker": "workspace:^",
14
+ "@gradio/upload": "workspace:^",
15
+ "@gradio/utils": "workspace:^",
16
+ "cropperjs": "^1.5.12",
17
+ "lazy-brush": "^1.0.1",
18
+ "resize-observer-polyfill": "^1.5.1"
19
+ },
20
+ "devDependencies": {
21
+ "@gradio/preview": "workspace:^"
22
+ },
23
+ "main_changeset": true,
24
+ "main": "./Index.svelte",
25
+ "exports": {
26
+ "./package.json": "./package.json",
27
+ ".": {
28
+ "gradio": "./Index.svelte",
29
+ "svelte": "./dist/Index.svelte",
30
+ "types": "./dist/Index.svelte.d.ts"
31
+ },
32
+ "./example": {
33
+ "gradio": "./Example.svelte",
34
+ "svelte": "./dist/Example.svelte",
35
+ "types": "./dist/Example.svelte.d.ts"
36
+ },
37
+ "./base": {
38
+ "gradio": "./shared/ImagePreview.svelte",
39
+ "svelte": "./dist/shared/ImagePreview.svelte",
40
+ "types": "./dist/shared/ImagePreview.svelte.d.ts"
41
+ },
42
+ "./shared": {
43
+ "gradio": "./shared/index.ts",
44
+ "svelte": "./dist/shared/index.js",
45
+ "types": "./dist/shared/index.d.ts"
46
+ }
47
+ },
48
+ "peerDependencies": {
49
+ "svelte": "^4.0.0"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/gradio-app/gradio.git",
54
+ "directory": "js/image"
55
+ }
56
+ }
5.49.1/image/shared/Image.svelte ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { HTMLImgAttributes } from "svelte/elements";
3
+
4
+ interface Props extends HTMLImgAttributes {
5
+ "data-testid"?: string;
6
+ }
7
+ type $$Props = Props;
8
+
9
+ export let src: HTMLImgAttributes["src"] = undefined;
10
+ </script>
11
+
12
+ <!-- svelte-ignore a11y-missing-attribute -->
13
+ <img {src} {...$$restProps} on:load />
14
+
15
+ <style>
16
+ img {
17
+ object-fit: cover;
18
+ }
19
+ </style>
5.49.1/image/shared/ImagePreview.svelte ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, onMount } from "svelte";
3
+ import type { SelectData } from "@gradio/utils";
4
+ import { uploadToHuggingFace } from "@gradio/utils";
5
+ import {
6
+ BlockLabel,
7
+ Empty,
8
+ IconButton,
9
+ ShareButton,
10
+ IconButtonWrapper,
11
+ FullscreenButton,
12
+ DownloadLink
13
+ } from "@gradio/atoms";
14
+ import { Download, Image as ImageIcon } from "@gradio/icons";
15
+ import { get_coordinates_of_clicked_image } from "./utils";
16
+ import Image from "./Image.svelte";
17
+
18
+ import type { I18nFormatter } from "@gradio/utils";
19
+ import type { FileData } from "@gradio/client";
20
+
21
+ export let value: null | FileData;
22
+ export let label: string | undefined = undefined;
23
+ export let show_label: boolean;
24
+ export let show_download_button = true;
25
+ export let selectable = false;
26
+ export let show_share_button = false;
27
+ export let i18n: I18nFormatter;
28
+ export let show_fullscreen_button = true;
29
+ export let display_icon_button_wrapper_top_corner = false;
30
+ export let fullscreen = false;
31
+
32
+ const dispatch = createEventDispatcher<{
33
+ change: string;
34
+ select: SelectData;
35
+ fullscreen: boolean;
36
+ }>();
37
+
38
+ const handle_click = (evt: MouseEvent): void => {
39
+ let coordinates = get_coordinates_of_clicked_image(evt);
40
+ if (coordinates) {
41
+ dispatch("select", { index: coordinates, value: null });
42
+ }
43
+ };
44
+
45
+ let image_container: HTMLElement;
46
+ </script>
47
+
48
+ <BlockLabel
49
+ {show_label}
50
+ Icon={ImageIcon}
51
+ label={!show_label ? "" : label || i18n("image.image")}
52
+ />
53
+ {#if value === null || !value.url}
54
+ <Empty unpadded_box={true} size="large"><ImageIcon /></Empty>
55
+ {:else}
56
+ <div class="image-container" bind:this={image_container}>
57
+ <IconButtonWrapper
58
+ display_top_corner={display_icon_button_wrapper_top_corner}
59
+ >
60
+ {#if show_fullscreen_button}
61
+ <FullscreenButton {fullscreen} on:fullscreen />
62
+ {/if}
63
+
64
+ {#if show_download_button}
65
+ <DownloadLink href={value.url} download={value.orig_name || "image"}>
66
+ <IconButton Icon={Download} label={i18n("common.download")} />
67
+ </DownloadLink>
68
+ {/if}
69
+ {#if show_share_button}
70
+ <ShareButton
71
+ {i18n}
72
+ on:share
73
+ on:error
74
+ formatter={async (value) => {
75
+ if (!value) return "";
76
+ let url = await uploadToHuggingFace(value, "url");
77
+ return `<img src="${url}" />`;
78
+ }}
79
+ {value}
80
+ />
81
+ {/if}
82
+ </IconButtonWrapper>
83
+ <button on:click={handle_click}>
84
+ <div class:selectable class="image-frame">
85
+ <Image src={value.url} alt="" loading="lazy" on:load />
86
+ </div>
87
+ </button>
88
+ </div>
89
+ {/if}
90
+
91
+ <style>
92
+ .image-container {
93
+ height: 100%;
94
+ position: relative;
95
+ min-width: var(--size-20);
96
+ }
97
+
98
+ .image-container button {
99
+ width: var(--size-full);
100
+ height: var(--size-full);
101
+ border-radius: var(--radius-lg);
102
+
103
+ display: flex;
104
+ align-items: center;
105
+ justify-content: center;
106
+ }
107
+
108
+ .image-frame :global(img) {
109
+ width: var(--size-full);
110
+ height: var(--size-full);
111
+ object-fit: scale-down;
112
+ }
113
+
114
+ .selectable {
115
+ cursor: crosshair;
116
+ }
117
+
118
+ :global(.fullscreen-controls svg) {
119
+ position: relative;
120
+ top: 0px;
121
+ }
122
+
123
+ :global(.image-container:fullscreen) {
124
+ background-color: black;
125
+ display: flex;
126
+ justify-content: center;
127
+ align-items: center;
128
+ }
129
+
130
+ :global(.image-container:fullscreen img) {
131
+ max-width: 90vw;
132
+ max-height: 90vh;
133
+ object-fit: scale-down;
134
+ }
135
+
136
+ .image-frame {
137
+ width: auto;
138
+ height: 100%;
139
+ display: flex;
140
+ align-items: center;
141
+ justify-content: center;
142
+ }
143
+ </style>
5.49.1/image/shared/ImageUploader.svelte ADDED
@@ -0,0 +1,304 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, tick } from "svelte";
3
+ import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
4
+ import { Clear, Image as ImageIcon } from "@gradio/icons";
5
+ import { FullscreenButton } from "@gradio/atoms";
6
+ import {
7
+ type SelectData,
8
+ type I18nFormatter,
9
+ type ValueData
10
+ } from "@gradio/utils";
11
+ import { get_coordinates_of_clicked_image } from "./utils";
12
+ import Webcam from "./Webcam.svelte";
13
+
14
+ import { Upload, UploadProgress } from "@gradio/upload";
15
+ import { FileData, type Client } from "@gradio/client";
16
+ import { SelectSource } from "@gradio/atoms";
17
+ import Image from "./Image.svelte";
18
+ import type { Base64File, WebcamOptions } from "./types";
19
+
20
+ export let value: null | FileData | Base64File = null;
21
+ export let label: string | undefined = undefined;
22
+ export let show_label: boolean;
23
+
24
+ type source_type = "upload" | "webcam" | "clipboard" | "microphone" | null;
25
+
26
+ export let sources: source_type[] = ["upload", "clipboard", "webcam"];
27
+ export let streaming = false;
28
+ export let pending = false;
29
+ export let webcam_options: WebcamOptions;
30
+ export let selectable = false;
31
+ export let root: string;
32
+ export let i18n: I18nFormatter;
33
+ export let max_file_size: number | null = null;
34
+ export let upload: Client["upload"];
35
+ export let stream_handler: Client["stream"];
36
+ export let stream_every: number;
37
+
38
+ export let modify_stream: (state: "open" | "closed" | "waiting") => void;
39
+ export let set_time_limit: (arg0: number) => void;
40
+ export let show_fullscreen_button = true;
41
+
42
+ let upload_input: Upload;
43
+ export let uploading = false;
44
+ export let active_source: source_type = null;
45
+ export let fullscreen = false;
46
+
47
+ let files: FileData[] = [];
48
+ let upload_id: string;
49
+
50
+ async function handle_upload({
51
+ detail
52
+ }: CustomEvent<FileData>): Promise<void> {
53
+ if (!streaming) {
54
+ if (detail.path?.toLowerCase().endsWith(".svg") && detail.url) {
55
+ const response = await fetch(detail.url);
56
+ const svgContent = await response.text();
57
+ value = {
58
+ ...detail,
59
+ url: `data:image/svg+xml,${encodeURIComponent(svgContent)}`
60
+ };
61
+ } else {
62
+ value = detail;
63
+ }
64
+
65
+ await tick();
66
+ dispatch("upload");
67
+ }
68
+ }
69
+
70
+ function handle_clear(): void {
71
+ value = null;
72
+ dispatch("clear");
73
+ dispatch("change", null);
74
+ }
75
+
76
+ function handle_remove_image_click(event: MouseEvent): void {
77
+ handle_clear();
78
+ event.stopPropagation();
79
+ }
80
+
81
+ async function handle_save(
82
+ img_blob: Blob | any,
83
+ event: "change" | "stream" | "upload"
84
+ ): Promise<void> {
85
+ if (event === "stream") {
86
+ dispatch("stream", {
87
+ value: { url: img_blob } as Base64File,
88
+ is_value_data: true
89
+ });
90
+ return;
91
+ }
92
+ upload_id = Math.random().toString(36).substring(2, 15);
93
+ const f_ = new File([img_blob], `image.${streaming ? "jpeg" : "png"}`);
94
+ files = [
95
+ new FileData({
96
+ path: f_.name,
97
+ orig_name: f_.name,
98
+ blob: f_,
99
+ size: f_.size,
100
+ mime_type: f_.type,
101
+ is_stream: false
102
+ })
103
+ ];
104
+ pending = true;
105
+ const f = await upload_input.load_files([f_], upload_id);
106
+ if (event === "change" || event === "upload") {
107
+ value = f?.[0] || null;
108
+ await tick();
109
+ dispatch("change");
110
+ }
111
+ pending = false;
112
+ }
113
+
114
+ $: active_streaming = streaming && active_source === "webcam";
115
+ $: if (uploading && !active_streaming) value = null;
116
+
117
+ const dispatch = createEventDispatcher<{
118
+ change?: never;
119
+ stream: ValueData;
120
+ clear?: never;
121
+ drag: boolean;
122
+ upload?: never;
123
+ select: SelectData;
124
+ end_stream: never;
125
+ }>();
126
+
127
+ export let dragging = false;
128
+
129
+ $: dispatch("drag", dragging);
130
+
131
+ function handle_click(evt: MouseEvent): void {
132
+ let coordinates = get_coordinates_of_clicked_image(evt);
133
+ if (coordinates) {
134
+ dispatch("select", { index: coordinates, value: null });
135
+ }
136
+ }
137
+
138
+ $: if (!active_source && sources) {
139
+ active_source = sources[0];
140
+ }
141
+
142
+ async function handle_select_source(
143
+ source: (typeof sources)[number]
144
+ ): Promise<void> {
145
+ switch (source) {
146
+ case "clipboard":
147
+ upload_input.paste_clipboard();
148
+ break;
149
+ default:
150
+ break;
151
+ }
152
+ }
153
+
154
+ let image_container: HTMLElement;
155
+
156
+ function on_drag_over(evt: DragEvent): void {
157
+ evt.preventDefault();
158
+ evt.stopPropagation();
159
+ if (evt.dataTransfer) {
160
+ evt.dataTransfer.dropEffect = "copy";
161
+ }
162
+
163
+ dragging = true;
164
+ }
165
+
166
+ async function on_drop(evt: DragEvent): Promise<void> {
167
+ evt.preventDefault();
168
+ evt.stopPropagation();
169
+ dragging = false;
170
+
171
+ if (value) {
172
+ handle_clear();
173
+ await tick();
174
+ }
175
+
176
+ active_source = "upload";
177
+ await tick();
178
+ upload_input.load_files_from_drop(evt);
179
+ }
180
+ </script>
181
+
182
+ <BlockLabel {show_label} Icon={ImageIcon} label={label || "Image"} />
183
+
184
+ <div data-testid="image" class="image-container" bind:this={image_container}>
185
+ <IconButtonWrapper>
186
+ {#if value?.url && !active_streaming}
187
+ {#if show_fullscreen_button}
188
+ <FullscreenButton {fullscreen} on:fullscreen />
189
+ {/if}
190
+ <IconButton
191
+ Icon={Clear}
192
+ label="Remove Image"
193
+ on:click={handle_remove_image_click}
194
+ />
195
+ {/if}
196
+ </IconButtonWrapper>
197
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
198
+ <div
199
+ class="upload-container"
200
+ class:reduced-height={sources.length > 1}
201
+ style:width={value ? "auto" : "100%"}
202
+ on:dragover={on_drag_over}
203
+ on:drop={on_drop}
204
+ >
205
+ <Upload
206
+ hidden={value !== null || active_source === "webcam"}
207
+ bind:this={upload_input}
208
+ bind:uploading
209
+ bind:dragging
210
+ filetype={active_source === "clipboard" ? "clipboard" : "image/*"}
211
+ on:load={handle_upload}
212
+ on:error
213
+ {root}
214
+ {max_file_size}
215
+ disable_click={!sources.includes("upload") || value !== null}
216
+ {upload}
217
+ {stream_handler}
218
+ aria_label={i18n("image.drop_to_upload")}
219
+ >
220
+ {#if value === null}
221
+ <slot />
222
+ {/if}
223
+ </Upload>
224
+ {#if active_source === "webcam" && !streaming && pending}
225
+ <UploadProgress {root} {upload_id} {stream_handler} {files} />
226
+ {:else if active_source === "webcam" && (streaming || (!streaming && !value))}
227
+ <Webcam
228
+ {root}
229
+ {value}
230
+ on:capture={(e) => handle_save(e.detail, "change")}
231
+ on:stream={(e) => handle_save(e.detail, "stream")}
232
+ on:error
233
+ on:drag
234
+ on:upload={(e) => handle_save(e.detail, "upload")}
235
+ on:close_stream
236
+ mirror_webcam={webcam_options.mirror}
237
+ {stream_every}
238
+ {streaming}
239
+ mode="image"
240
+ include_audio={false}
241
+ {i18n}
242
+ {upload}
243
+ bind:modify_stream
244
+ bind:set_time_limit
245
+ webcam_constraints={webcam_options.constraints}
246
+ />
247
+ {:else if value !== null && !streaming}
248
+ <!-- svelte-ignore a11y-click-events-have-key-events-->
249
+ <!-- svelte-ignore a11y-no-static-element-interactions-->
250
+ <div class:selectable class="image-frame" on:click={handle_click}>
251
+ <Image src={value.url} alt={value.alt_text} />
252
+ </div>
253
+ {/if}
254
+ </div>
255
+ {#if sources.length > 1 || sources.includes("clipboard")}
256
+ <SelectSource
257
+ {sources}
258
+ bind:active_source
259
+ {handle_clear}
260
+ handle_select={handle_select_source}
261
+ />
262
+ {/if}
263
+ </div>
264
+
265
+ <style>
266
+ .image-frame :global(img) {
267
+ width: var(--size-full);
268
+ height: var(--size-full);
269
+ object-fit: scale-down;
270
+ }
271
+
272
+ .upload-container {
273
+ display: flex;
274
+ align-items: center;
275
+ justify-content: center;
276
+
277
+ height: 100%;
278
+ flex-shrink: 1;
279
+ max-height: 100%;
280
+ }
281
+
282
+ .reduced-height {
283
+ height: calc(100% - var(--size-10));
284
+ }
285
+
286
+ .image-container {
287
+ display: flex;
288
+ height: 100%;
289
+ flex-direction: column;
290
+ justify-content: center;
291
+ align-items: center;
292
+ max-height: 100%;
293
+ }
294
+
295
+ .selectable {
296
+ cursor: crosshair;
297
+ }
298
+
299
+ .image-frame {
300
+ object-fit: cover;
301
+ width: 100%;
302
+ height: 100%;
303
+ }
304
+ </style>
5.49.1/image/shared/Webcam.svelte ADDED
@@ -0,0 +1,511 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, onDestroy, onMount } from "svelte";
3
+ import {
4
+ Camera,
5
+ Circle,
6
+ Square,
7
+ DropdownArrow,
8
+ Spinner
9
+ } from "@gradio/icons";
10
+ import type { I18nFormatter } from "@gradio/utils";
11
+ import { StreamingBar } from "@gradio/statustracker";
12
+ import { type FileData, type Client, prepare_files } from "@gradio/client";
13
+ import WebcamPermissions from "./WebcamPermissions.svelte";
14
+ import { fade } from "svelte/transition";
15
+ import {
16
+ get_devices,
17
+ get_video_stream,
18
+ set_available_devices
19
+ } from "./stream_utils";
20
+ import type { Base64File } from "./types";
21
+
22
+ let video_source: HTMLVideoElement;
23
+ let available_video_devices: MediaDeviceInfo[] = [];
24
+ let selected_device: MediaDeviceInfo | null = null;
25
+ let time_limit: number | null = null;
26
+ let stream_state: "open" | "waiting" | "closed" = "closed";
27
+
28
+ export const modify_stream: (state: "open" | "closed" | "waiting") => void = (
29
+ state: "open" | "closed" | "waiting"
30
+ ) => {
31
+ if (state === "closed") {
32
+ time_limit = null;
33
+ stream_state = "closed";
34
+ value = null;
35
+ } else if (state === "waiting") {
36
+ stream_state = "waiting";
37
+ } else {
38
+ stream_state = "open";
39
+ }
40
+ };
41
+
42
+ export const set_time_limit = (time: number): void => {
43
+ if (recording) time_limit = time;
44
+ };
45
+
46
+ let canvas: HTMLCanvasElement;
47
+ export let streaming = false;
48
+ export let pending = false;
49
+ export let root = "";
50
+ export let stream_every = 1;
51
+
52
+ export let mode: "image" | "video" = "image";
53
+ export let mirror_webcam: boolean;
54
+ export let include_audio: boolean;
55
+ export let webcam_constraints: { [key: string]: any } | null = null;
56
+ export let i18n: I18nFormatter;
57
+ export let upload: Client["upload"];
58
+ export let value: FileData | null | Base64File = null;
59
+
60
+ const dispatch = createEventDispatcher<{
61
+ stream: Blob | string;
62
+ capture: FileData | Blob | null;
63
+ error: string;
64
+ start_recording: undefined;
65
+ stop_recording: undefined;
66
+ close_stream: undefined;
67
+ }>();
68
+
69
+ onMount(() => {
70
+ canvas = document.createElement("canvas");
71
+ if (streaming && mode === "image") {
72
+ window.setInterval(() => {
73
+ if (video_source && !pending) {
74
+ take_picture();
75
+ }
76
+ }, stream_every * 1000);
77
+ }
78
+ });
79
+
80
+ const handle_device_change = async (event: InputEvent): Promise<void> => {
81
+ const target = event.target as HTMLInputElement;
82
+ const device_id = target.value;
83
+
84
+ await get_video_stream(
85
+ include_audio,
86
+ video_source,
87
+ webcam_constraints,
88
+ device_id
89
+ ).then(async (local_stream) => {
90
+ stream = local_stream;
91
+ selected_device =
92
+ available_video_devices.find(
93
+ (device) => device.deviceId === device_id
94
+ ) || null;
95
+ options_open = false;
96
+ });
97
+ };
98
+
99
+ async function access_webcam(): Promise<void> {
100
+ try {
101
+ get_video_stream(include_audio, video_source, webcam_constraints)
102
+ .then(async (local_stream) => {
103
+ webcam_accessed = true;
104
+ available_video_devices = await get_devices();
105
+ stream = local_stream;
106
+ })
107
+ .then(() => set_available_devices(available_video_devices))
108
+ .then((devices) => {
109
+ available_video_devices = devices;
110
+
111
+ const used_devices = stream
112
+ .getTracks()
113
+ .map((track) => track.getSettings()?.deviceId)[0];
114
+
115
+ selected_device = used_devices
116
+ ? devices.find((device) => device.deviceId === used_devices) ||
117
+ available_video_devices[0]
118
+ : available_video_devices[0];
119
+ });
120
+
121
+ if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
122
+ dispatch("error", i18n("image.no_webcam_support"));
123
+ }
124
+ } catch (err) {
125
+ if (err instanceof DOMException && err.name == "NotAllowedError") {
126
+ dispatch("error", i18n("image.allow_webcam_access"));
127
+ } else {
128
+ throw err;
129
+ }
130
+ }
131
+ }
132
+
133
+ function take_picture(): void {
134
+ var context = canvas.getContext("2d")!;
135
+ if (
136
+ (!streaming || (streaming && recording)) &&
137
+ video_source.videoWidth &&
138
+ video_source.videoHeight
139
+ ) {
140
+ canvas.width = video_source.videoWidth;
141
+ canvas.height = video_source.videoHeight;
142
+ context.drawImage(
143
+ video_source,
144
+ 0,
145
+ 0,
146
+ video_source.videoWidth,
147
+ video_source.videoHeight
148
+ );
149
+
150
+ if (mirror_webcam) {
151
+ context.scale(-1, 1);
152
+ context.drawImage(video_source, -video_source.videoWidth, 0);
153
+ }
154
+
155
+ if (streaming && (!recording || stream_state === "waiting")) {
156
+ return;
157
+ }
158
+ if (streaming) {
159
+ const image_data = canvas.toDataURL("image/jpeg");
160
+ dispatch("stream", image_data);
161
+ return;
162
+ }
163
+
164
+ canvas.toBlob(
165
+ (blob) => {
166
+ dispatch(streaming ? "stream" : "capture", blob);
167
+ },
168
+ `image/${streaming ? "jpeg" : "png"}`,
169
+ 0.8
170
+ );
171
+ }
172
+ }
173
+
174
+ let recording = false;
175
+ let recorded_blobs: BlobPart[] = [];
176
+ let stream: MediaStream;
177
+ let mimeType: string;
178
+ let media_recorder: MediaRecorder;
179
+
180
+ function take_recording(): void {
181
+ if (recording) {
182
+ media_recorder.stop();
183
+ let video_blob = new Blob(recorded_blobs, { type: mimeType });
184
+ let ReaderObj = new FileReader();
185
+ ReaderObj.onload = async function (e): Promise<void> {
186
+ if (e.target) {
187
+ let _video_blob = new File(
188
+ [video_blob],
189
+ "sample." + mimeType.substring(6)
190
+ );
191
+ const val = await prepare_files([_video_blob]);
192
+ let val_ = (
193
+ (await upload(val, root))?.filter(Boolean) as FileData[]
194
+ )[0];
195
+ dispatch("capture", val_);
196
+ dispatch("stop_recording");
197
+ }
198
+ };
199
+ ReaderObj.readAsDataURL(video_blob);
200
+ } else if (typeof MediaRecorder !== "undefined") {
201
+ dispatch("start_recording");
202
+ recorded_blobs = [];
203
+ let validMimeTypes = ["video/webm", "video/mp4"];
204
+ for (let validMimeType of validMimeTypes) {
205
+ if (MediaRecorder.isTypeSupported(validMimeType)) {
206
+ mimeType = validMimeType;
207
+ break;
208
+ }
209
+ }
210
+ if (mimeType === null) {
211
+ console.error("No supported MediaRecorder mimeType");
212
+ return;
213
+ }
214
+ media_recorder = new MediaRecorder(stream, {
215
+ mimeType: mimeType
216
+ });
217
+ media_recorder.addEventListener("dataavailable", function (e) {
218
+ recorded_blobs.push(e.data);
219
+ });
220
+ media_recorder.start(200);
221
+ }
222
+ recording = !recording;
223
+ }
224
+
225
+ let webcam_accessed = false;
226
+
227
+ function record_video_or_photo({
228
+ destroy
229
+ }: { destroy?: boolean } = {}): void {
230
+ if (mode === "image" && streaming) {
231
+ recording = !recording;
232
+ }
233
+
234
+ if (!destroy) {
235
+ if (mode === "image") {
236
+ take_picture();
237
+ } else {
238
+ take_recording();
239
+ }
240
+ }
241
+
242
+ if (!recording && stream) {
243
+ dispatch("close_stream");
244
+ stream.getTracks().forEach((track) => track.stop());
245
+ video_source.srcObject = null;
246
+ webcam_accessed = false;
247
+ window.setTimeout(() => {
248
+ value = null;
249
+ }, 500);
250
+ value = null;
251
+ }
252
+ }
253
+
254
+ let options_open = false;
255
+
256
+ export function click_outside(node: Node, cb: any): any {
257
+ const handle_click = (event: MouseEvent): void => {
258
+ if (
259
+ node &&
260
+ !node.contains(event.target as Node) &&
261
+ !event.defaultPrevented
262
+ ) {
263
+ cb(event);
264
+ }
265
+ };
266
+
267
+ document.addEventListener("click", handle_click, true);
268
+
269
+ return {
270
+ destroy() {
271
+ document.removeEventListener("click", handle_click, true);
272
+ }
273
+ };
274
+ }
275
+
276
+ function handle_click_outside(event: MouseEvent): void {
277
+ event.preventDefault();
278
+ event.stopPropagation();
279
+ options_open = false;
280
+ }
281
+
282
+ onDestroy(() => {
283
+ if (typeof window === "undefined") return;
284
+ record_video_or_photo({ destroy: true });
285
+ stream?.getTracks().forEach((track) => track.stop());
286
+ });
287
+ </script>
288
+
289
+ <div class="wrap">
290
+ <StreamingBar {time_limit} />
291
+ <!-- svelte-ignore a11y-media-has-caption -->
292
+ <!-- need to suppress for video streaming https://github.com/sveltejs/svelte/issues/5967 -->
293
+ <video
294
+ bind:this={video_source}
295
+ class:flip={mirror_webcam}
296
+ class:hide={!webcam_accessed || (webcam_accessed && !!value)}
297
+ />
298
+ <!-- svelte-ignore a11y-missing-attribute -->
299
+ <img
300
+ src={value?.url}
301
+ class:hide={!webcam_accessed || (webcam_accessed && !value)}
302
+ />
303
+ {#if !webcam_accessed}
304
+ <div
305
+ in:fade={{ delay: 100, duration: 200 }}
306
+ title="grant webcam access"
307
+ style="height: 100%"
308
+ >
309
+ <WebcamPermissions on:click={async () => access_webcam()} />
310
+ </div>
311
+ {:else}
312
+ <div class="button-wrap">
313
+ <button
314
+ on:click={() => record_video_or_photo()}
315
+ aria-label={mode === "image" ? "capture photo" : "start recording"}
316
+ >
317
+ {#if mode === "video" || streaming}
318
+ {#if streaming && stream_state === "waiting"}
319
+ <div class="icon-with-text" style="width:var(--size-24);">
320
+ <div class="icon color-primary" title="spinner">
321
+ <Spinner />
322
+ </div>
323
+ {i18n("audio.waiting")}
324
+ </div>
325
+ {:else if (streaming && stream_state === "open") || (!streaming && recording)}
326
+ <div class="icon-with-text">
327
+ <div class="icon color-primary" title="stop recording">
328
+ <Square />
329
+ </div>
330
+ {i18n("audio.stop")}
331
+ </div>
332
+ {:else}
333
+ <div class="icon-with-text">
334
+ <div class="icon color-primary" title="start recording">
335
+ <Circle />
336
+ </div>
337
+ {i18n("audio.record")}
338
+ </div>
339
+ {/if}
340
+ {:else}
341
+ <div class="icon" title="capture photo">
342
+ <Camera />
343
+ </div>
344
+ {/if}
345
+ </button>
346
+ {#if !recording}
347
+ <button
348
+ class="icon"
349
+ on:click={() => (options_open = true)}
350
+ aria-label="select input source"
351
+ >
352
+ <DropdownArrow />
353
+ </button>
354
+ {/if}
355
+ </div>
356
+ {#if options_open && selected_device}
357
+ <select
358
+ class="select-wrap"
359
+ aria-label="select source"
360
+ use:click_outside={handle_click_outside}
361
+ on:change={handle_device_change}
362
+ >
363
+ <button
364
+ class="inset-icon"
365
+ on:click|stopPropagation={() => (options_open = false)}
366
+ >
367
+ <DropdownArrow />
368
+ </button>
369
+ {#if available_video_devices.length === 0}
370
+ <option value="">{i18n("common.no_devices")}</option>
371
+ {:else}
372
+ {#each available_video_devices as device}
373
+ <option
374
+ value={device.deviceId}
375
+ selected={selected_device.deviceId === device.deviceId}
376
+ >
377
+ {device.label}
378
+ </option>
379
+ {/each}
380
+ {/if}
381
+ </select>
382
+ {/if}
383
+ {/if}
384
+ </div>
385
+
386
+ <style>
387
+ .wrap {
388
+ position: relative;
389
+ width: var(--size-full);
390
+ height: var(--size-full);
391
+ }
392
+
393
+ .hide {
394
+ display: none;
395
+ }
396
+
397
+ video {
398
+ width: var(--size-full);
399
+ height: var(--size-full);
400
+ object-fit: contain;
401
+ }
402
+
403
+ .button-wrap {
404
+ position: absolute;
405
+ background-color: var(--block-background-fill);
406
+ border: 1px solid var(--border-color-primary);
407
+ border-radius: var(--radius-xl);
408
+ padding: var(--size-1-5);
409
+ display: flex;
410
+ bottom: var(--size-2);
411
+ left: 50%;
412
+ transform: translate(-50%, 0);
413
+ box-shadow: var(--shadow-drop-lg);
414
+ border-radius: var(--radius-xl);
415
+ line-height: var(--size-3);
416
+ color: var(--button-secondary-text-color);
417
+ }
418
+
419
+ .icon-with-text {
420
+ width: var(--size-20);
421
+ align-items: center;
422
+ margin: 0 var(--spacing-xl);
423
+ display: flex;
424
+ justify-content: space-evenly;
425
+ }
426
+
427
+ @media (--screen-md) {
428
+ button {
429
+ bottom: var(--size-4);
430
+ }
431
+ }
432
+
433
+ @media (--screen-xl) {
434
+ button {
435
+ bottom: var(--size-8);
436
+ }
437
+ }
438
+
439
+ .icon {
440
+ width: 18px;
441
+ height: 18px;
442
+ display: flex;
443
+ justify-content: space-between;
444
+ align-items: center;
445
+ }
446
+
447
+ .color-primary {
448
+ fill: var(--primary-600);
449
+ stroke: var(--primary-600);
450
+ color: var(--primary-600);
451
+ }
452
+
453
+ .flip {
454
+ transform: scaleX(-1);
455
+ }
456
+
457
+ .select-wrap {
458
+ -webkit-appearance: none;
459
+ -moz-appearance: none;
460
+ appearance: none;
461
+ color: var(--button-secondary-text-color);
462
+ background-color: transparent;
463
+ width: 95%;
464
+ font-size: var(--text-md);
465
+ position: absolute;
466
+ bottom: var(--size-2);
467
+ background-color: var(--block-background-fill);
468
+ box-shadow: var(--shadow-drop-lg);
469
+ border-radius: var(--radius-xl);
470
+ z-index: var(--layer-top);
471
+ border: 1px solid var(--border-color-primary);
472
+ text-align: left;
473
+ line-height: var(--size-4);
474
+ white-space: nowrap;
475
+ text-overflow: ellipsis;
476
+ left: 50%;
477
+ transform: translate(-50%, 0);
478
+ max-width: var(--size-52);
479
+ }
480
+
481
+ .select-wrap > option {
482
+ padding: 0.25rem 0.5rem;
483
+ border-bottom: 1px solid var(--border-color-accent);
484
+ padding-right: var(--size-8);
485
+ text-overflow: ellipsis;
486
+ overflow: hidden;
487
+ }
488
+
489
+ .select-wrap > option:hover {
490
+ background-color: var(--color-accent);
491
+ }
492
+
493
+ .select-wrap > option:last-child {
494
+ border: none;
495
+ }
496
+
497
+ .inset-icon {
498
+ position: absolute;
499
+ top: 5px;
500
+ right: -6.5px;
501
+ width: var(--size-10);
502
+ height: var(--size-5);
503
+ opacity: 0.8;
504
+ }
505
+
506
+ @media (--screen-md) {
507
+ .wrap {
508
+ font-size: var(--text-lg);
509
+ }
510
+ }
511
+ </style>
5.49.1/image/shared/WebcamPermissions.svelte ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Webcam } from "@gradio/icons";
3
+ import { createEventDispatcher } from "svelte";
4
+
5
+ const dispatch = createEventDispatcher<{
6
+ click: undefined;
7
+ }>();
8
+ </script>
9
+
10
+ <button style:height="100%" on:click={() => dispatch("click")}>
11
+ <div class="wrap">
12
+ <span class="icon-wrap">
13
+ <Webcam />
14
+ </span>
15
+ {"Click to Access Webcam"}
16
+ </div>
17
+ </button>
18
+
19
+ <style>
20
+ button {
21
+ cursor: pointer;
22
+ width: var(--size-full);
23
+ }
24
+
25
+ .wrap {
26
+ display: flex;
27
+ flex-direction: column;
28
+ justify-content: center;
29
+ align-items: center;
30
+ min-height: var(--size-60);
31
+ color: var(--block-label-text-color);
32
+ height: 100%;
33
+ padding-top: var(--size-3);
34
+ }
35
+
36
+ .icon-wrap {
37
+ width: 30px;
38
+ margin-bottom: var(--spacing-lg);
39
+ }
40
+
41
+ @media (--screen-md) {
42
+ .wrap {
43
+ font-size: var(--text-lg);
44
+ }
45
+ }
46
+ </style>
5.49.1/image/shared/index.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export { default as Image } from "./Image.svelte";
2
+ export { default as StaticImage } from "./ImagePreview.svelte";
5.49.1/image/shared/stream_utils.ts ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export function get_devices(): Promise<MediaDeviceInfo[]> {
2
+ return navigator.mediaDevices.enumerateDevices();
3
+ }
4
+
5
+ export function handle_error(error: string): void {
6
+ throw new Error(error);
7
+ }
8
+
9
+ export function set_local_stream(
10
+ local_stream: MediaStream | null,
11
+ video_source: HTMLVideoElement
12
+ ): void {
13
+ video_source.srcObject = local_stream;
14
+ video_source.muted = true;
15
+ video_source.play();
16
+ }
17
+
18
+ export async function get_video_stream(
19
+ include_audio: boolean,
20
+ video_source: HTMLVideoElement,
21
+ webcam_constraints: { [key: string]: any } | null,
22
+ device_id?: string
23
+ ): Promise<MediaStream> {
24
+ const constraints: MediaStreamConstraints = {
25
+ video: device_id
26
+ ? { deviceId: { exact: device_id }, ...webcam_constraints?.video }
27
+ : webcam_constraints?.video || {
28
+ width: { ideal: 1920 },
29
+ height: { ideal: 1440 }
30
+ },
31
+ audio: include_audio && (webcam_constraints?.audio ?? true) // Defaults to true if not specified
32
+ };
33
+
34
+ return navigator.mediaDevices
35
+ .getUserMedia(constraints)
36
+ .then((local_stream: MediaStream) => {
37
+ set_local_stream(local_stream, video_source);
38
+ return local_stream;
39
+ });
40
+ }
41
+
42
+ export function set_available_devices(
43
+ devices: MediaDeviceInfo[]
44
+ ): MediaDeviceInfo[] {
45
+ const cameras = devices.filter(
46
+ (device: MediaDeviceInfo) => device.kind === "videoinput"
47
+ );
48
+
49
+ return cameras;
50
+ }
5.49.1/image/shared/types.ts ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ export interface Base64File {
2
+ url: string;
3
+ alt_text: string;
4
+ }
5
+
6
+ export interface WebcamOptions {
7
+ mirror: boolean;
8
+ constraints: MediaStreamConstraints;
9
+ }
5.49.1/image/shared/utils.ts ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const get_coordinates_of_clicked_image = (
2
+ evt: MouseEvent
3
+ ): [number, number] | null => {
4
+ let image;
5
+ if (evt.currentTarget instanceof Element) {
6
+ image = evt.currentTarget.querySelector("img") as HTMLImageElement;
7
+ } else {
8
+ return [NaN, NaN];
9
+ }
10
+
11
+ const imageRect = image.getBoundingClientRect();
12
+ const xScale = image.naturalWidth / imageRect.width;
13
+ const yScale = image.naturalHeight / imageRect.height;
14
+ if (xScale > yScale) {
15
+ const displayed_height = image.naturalHeight / xScale;
16
+ const y_offset = (imageRect.height - displayed_height) / 2;
17
+ var x = Math.round((evt.clientX - imageRect.left) * xScale);
18
+ var y = Math.round((evt.clientY - imageRect.top - y_offset) * xScale);
19
+ } else {
20
+ const displayed_width = image.naturalWidth / yScale;
21
+ const x_offset = (imageRect.width - displayed_width) / 2;
22
+ var x = Math.round((evt.clientX - imageRect.left - x_offset) * yScale);
23
+ var y = Math.round((evt.clientY - imageRect.top) * yScale);
24
+ }
25
+ if (x < 0 || x >= image.naturalWidth || y < 0 || y >= image.naturalHeight) {
26
+ return null;
27
+ }
28
+ return [x, y];
29
+ };