gradio-pr-bot commited on
Commit
6675656
·
verified ·
1 Parent(s): 939d07b

Upload folder using huggingface_hub

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