freddyaboulton HF Staff commited on
Commit
4a666b1
·
verified ·
1 Parent(s): 845f627

Upload folder using huggingface_hub

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