@gradio/image 0.23.2-dev.0 → 0.24.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,47 @@
1
- <script>import { createEventDispatcher, onMount } from "svelte";
2
- import { uploadToHuggingFace } from "@gradio/utils";
3
- import {
4
- BlockLabel,
5
- Empty,
6
- IconButton,
7
- ShareButton,
8
- IconButtonWrapper,
9
- FullscreenButton,
10
- DownloadLink
11
- } from "@gradio/atoms";
12
- import { Download, Image as ImageIcon } from "@gradio/icons";
13
- import { get_coordinates_of_clicked_image } from "./utils";
14
- import Image from "./Image.svelte";
15
- export let value;
16
- export let label = void 0;
17
- export let show_label;
18
- export let buttons = null;
19
- export let selectable = false;
20
- export let i18n;
21
- export let display_icon_button_wrapper_top_corner = false;
22
- export let fullscreen = false;
23
- const dispatch = createEventDispatcher();
24
- const handle_click = (evt) => {
25
- let coordinates = get_coordinates_of_clicked_image(evt);
26
- if (coordinates) {
27
- dispatch("select", { index: coordinates, value: null });
28
- }
29
- };
30
- let image_container;
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;
31
45
  </script>
32
46
 
33
47
  <BlockLabel
@@ -35,12 +49,13 @@ let image_container;
35
49
  Icon={ImageIcon}
36
50
  label={!show_label ? "" : label || i18n("image.image")}
37
51
  />
38
- {#if value === null || !value.url}
52
+ {#if value == null || !value?.url}
39
53
  <Empty unpadded_box={true} size="large"><ImageIcon /></Empty>
40
54
  {:else}
41
55
  <div class="image-container" bind:this={image_container}>
42
56
  <IconButtonWrapper
43
57
  display_top_corner={display_icon_button_wrapper_top_corner}
58
+ show_background={show_button_background}
44
59
  >
45
60
  {#if buttons === null ? true : buttons.includes("fullscreen")}
46
61
  <FullscreenButton {fullscreen} on:fullscreen />
@@ -67,7 +82,11 @@ let image_container;
67
82
  </IconButtonWrapper>
68
83
  <button on:click={handle_click}>
69
84
  <div class:selectable class="image-frame">
70
- <Image src={value.url} alt="" loading="lazy" on:load />
85
+ <Image
86
+ src={value.url}
87
+ restProps={{ loading: "lazy", alt: "" }}
88
+ on:load
89
+ />
71
90
  </div>
72
91
  </button>
73
92
  </div>
@@ -1,35 +1,38 @@
1
- import { SvelteComponent } from "svelte";
2
1
  import type { SelectData } from "@gradio/utils";
3
2
  import type { I18nFormatter } from "@gradio/utils";
4
3
  import type { FileData } from "@gradio/client";
5
- declare const __propDef: {
6
- props: {
7
- value: null | FileData;
8
- label?: string | undefined;
9
- show_label: boolean;
10
- buttons?: string[] | null;
11
- selectable?: boolean;
12
- i18n: I18nFormatter;
13
- display_icon_button_wrapper_top_corner?: boolean;
14
- fullscreen?: boolean;
4
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
5
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
6
+ $$bindings?: Bindings;
7
+ } & Exports;
8
+ (internal: unknown, props: Props & {
9
+ $$events?: Events;
10
+ $$slots?: Slots;
11
+ }): Exports & {
12
+ $set?: any;
13
+ $on?: any;
15
14
  };
16
- events: {
17
- fullscreen: CustomEvent<any>;
18
- share: CustomEvent<import("@gradio/utils").ShareData>;
19
- error: CustomEvent<string>;
20
- load: Event;
21
- change: CustomEvent<string>;
22
- select: CustomEvent<SelectData>;
23
- } & {
24
- [evt: string]: CustomEvent<any>;
25
- };
26
- slots: {};
27
- exports?: {} | undefined;
28
- bindings?: string | undefined;
29
- };
30
- export type ImagePreviewProps = typeof __propDef.props;
31
- export type ImagePreviewEvents = typeof __propDef.events;
32
- export type ImagePreviewSlots = typeof __propDef.slots;
33
- export default class ImagePreview extends SvelteComponent<ImagePreviewProps, ImagePreviewEvents, ImagePreviewSlots> {
15
+ z_$$bindings?: Bindings;
34
16
  }
35
- export {};
17
+ declare const ImagePreview: $$__sveltets_2_IsomorphicComponent<{
18
+ value: null | FileData;
19
+ label?: string | undefined;
20
+ show_label: boolean;
21
+ buttons?: string[] | null;
22
+ selectable?: boolean;
23
+ i18n: I18nFormatter;
24
+ display_icon_button_wrapper_top_corner?: boolean;
25
+ fullscreen?: boolean;
26
+ show_button_background?: boolean;
27
+ }, {
28
+ fullscreen: CustomEvent<any>;
29
+ share: CustomEvent<import("@gradio/utils").ShareData>;
30
+ error: CustomEvent<string>;
31
+ load: Event;
32
+ change: CustomEvent<string>;
33
+ select: CustomEvent<SelectData>;
34
+ } & {
35
+ [evt: string]: CustomEvent<any>;
36
+ }, {}, {}, string>;
37
+ type ImagePreview = InstanceType<typeof ImagePreview>;
38
+ export default ImagePreview;
@@ -1,138 +1,184 @@
1
- <script>import { createEventDispatcher, tick } from "svelte";
2
- import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
3
- import { Clear, Image as ImageIcon } from "@gradio/icons";
4
- import { FullscreenButton } from "@gradio/atoms";
5
- import {
6
- } from "@gradio/utils";
7
- import { get_coordinates_of_clicked_image } from "./utils";
8
- import Webcam from "./Webcam.svelte";
9
- import { Upload, UploadProgress } from "@gradio/upload";
10
- import { FileData } from "@gradio/client";
11
- import { SelectSource } from "@gradio/atoms";
12
- import Image from "./Image.svelte";
13
- export let value = null;
14
- export let label = void 0;
15
- export let show_label;
16
- export let sources = ["upload", "clipboard", "webcam"];
17
- export let streaming = false;
18
- export let pending = false;
19
- export let webcam_options;
20
- export let selectable = false;
21
- export let root;
22
- export let i18n;
23
- export let max_file_size = null;
24
- export let upload;
25
- export let stream_handler;
26
- export let stream_every;
27
- export let modify_stream;
28
- export let set_time_limit;
29
- export let show_fullscreen_button = true;
30
- let upload_input;
31
- export let uploading = false;
32
- export let active_source = null;
33
- export let fullscreen = false;
34
- let files = [];
35
- let upload_id;
36
- async function handle_upload({
37
- detail
38
- }) {
39
- if (!streaming) {
40
- if (detail.path?.toLowerCase().endsWith(".svg") && detail.url) {
41
- const response = await fetch(detail.url);
42
- const svgContent = await response.text();
43
- value = {
44
- ...detail,
45
- url: `data:image/svg+xml,${encodeURIComponent(svgContent)}`
46
- };
47
- } else {
48
- value = detail;
49
- }
50
- await tick();
51
- dispatch("upload");
52
- }
53
- }
54
- function handle_clear() {
55
- value = null;
56
- dispatch("clear");
57
- dispatch("change", null);
58
- }
59
- function handle_remove_image_click(event) {
60
- handle_clear();
61
- event.stopPropagation();
62
- }
63
- async function handle_save(img_blob, event) {
64
- if (event === "stream") {
65
- dispatch("stream", {
66
- value: { url: img_blob },
67
- is_value_data: true
68
- });
69
- return;
70
- }
71
- upload_id = Math.random().toString(36).substring(2, 15);
72
- const f_ = new File([img_blob], `image.${streaming ? "jpeg" : "png"}`);
73
- files = [
74
- new FileData({
75
- path: f_.name,
76
- orig_name: f_.name,
77
- blob: f_,
78
- size: f_.size,
79
- mime_type: f_.type,
80
- is_stream: false
81
- })
82
- ];
83
- pending = true;
84
- const f = await upload_input.load_files([f_], upload_id);
85
- if (event === "change" || event === "upload") {
86
- value = f?.[0] || null;
87
- await tick();
88
- dispatch("change");
89
- }
90
- pending = false;
91
- }
92
- $: active_streaming = streaming && active_source === "webcam";
93
- $: if (uploading && !active_streaming) value = null;
94
- const dispatch = createEventDispatcher();
95
- export let dragging = false;
96
- $: dispatch("drag", dragging);
97
- function handle_click(evt) {
98
- let coordinates = get_coordinates_of_clicked_image(evt);
99
- if (coordinates) {
100
- dispatch("select", { index: coordinates, value: null });
101
- }
102
- }
103
- $: if (!active_source && sources) {
104
- active_source = sources[0];
105
- }
106
- async function handle_select_source(source) {
107
- switch (source) {
108
- case "clipboard":
109
- upload_input.paste_clipboard();
110
- break;
111
- default:
112
- break;
113
- }
114
- }
115
- let image_container;
116
- function on_drag_over(evt) {
117
- evt.preventDefault();
118
- evt.stopPropagation();
119
- if (evt.dataTransfer) {
120
- evt.dataTransfer.dropEffect = "copy";
121
- }
122
- dragging = true;
123
- }
124
- async function on_drop(evt) {
125
- evt.preventDefault();
126
- evt.stopPropagation();
127
- dragging = false;
128
- if (value) {
129
- handle_clear();
130
- await tick();
131
- }
132
- active_source = "upload";
133
- await tick();
134
- upload_input.load_files_from_drop(evt);
135
- }
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
+ }
136
182
  </script>
137
183
 
138
184
  <BlockLabel {show_label} Icon={ImageIcon} label={label || "Image"} />
@@ -159,6 +205,7 @@ async function on_drop(evt) {
159
205
  on:drop={on_drop}
160
206
  >
161
207
  <Upload
208
+ bind:upload_promise
162
209
  hidden={value !== null || active_source === "webcam"}
163
210
  bind:this={upload_input}
164
211
  bind:uploading
@@ -189,6 +236,7 @@ async function on_drop(evt) {
189
236
  on:drag
190
237
  on:upload={(e) => handle_save(e.detail, "upload")}
191
238
  on:close_stream
239
+ {stream_state}
192
240
  mirror_webcam={webcam_options.mirror}
193
241
  {stream_every}
194
242
  {streaming}
@@ -196,15 +244,14 @@ async function on_drop(evt) {
196
244
  include_audio={false}
197
245
  {i18n}
198
246
  {upload}
199
- bind:modify_stream
200
- bind:set_time_limit
247
+ {time_limit}
201
248
  webcam_constraints={webcam_options.constraints}
202
249
  />
203
250
  {:else if value !== null && !streaming}
204
251
  <!-- svelte-ignore a11y-click-events-have-key-events-->
205
252
  <!-- svelte-ignore a11y-no-static-element-interactions-->
206
253
  <div class:selectable class="image-frame" on:click={handle_click}>
207
- <Image src={value.url} alt={value.alt_text} />
254
+ <Image src={value.url} restProps={{ alt: value.alt_text }} />
208
255
  </div>
209
256
  {/if}
210
257
  </div>
@@ -1,54 +1,64 @@
1
- import { SvelteComponent } from "svelte";
2
1
  import { type SelectData, type I18nFormatter, type ValueData } from "@gradio/utils";
3
2
  import { FileData, type Client } from "@gradio/client";
4
3
  import type { Base64File, WebcamOptions } from "./types";
5
- declare const __propDef: {
6
- props: {
7
- value?: null | FileData | Base64File;
8
- label?: string | undefined;
9
- show_label: boolean;
10
- sources?: ("clipboard" | "upload" | "microphone" | "webcam" | null)[];
11
- streaming?: boolean;
12
- pending?: boolean;
13
- webcam_options: WebcamOptions;
14
- selectable?: boolean;
15
- root: string;
16
- i18n: I18nFormatter;
17
- max_file_size?: number | null;
18
- upload: Client["upload"];
19
- stream_handler: Client["stream"];
20
- stream_every: number;
21
- modify_stream: (state: "open" | "closed" | "waiting") => void;
22
- set_time_limit: (arg0: number) => void;
23
- show_fullscreen_button?: boolean;
24
- uploading?: boolean;
25
- active_source?: "clipboard" | "upload" | "microphone" | "webcam" | null;
26
- fullscreen?: boolean;
27
- dragging?: boolean;
4
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
5
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
6
+ $$bindings?: Bindings;
7
+ } & Exports;
8
+ (internal: unknown, props: Props & {
9
+ $$events?: Events;
10
+ $$slots?: Slots;
11
+ }): Exports & {
12
+ $set?: any;
13
+ $on?: any;
28
14
  };
29
- events: {
30
- fullscreen: CustomEvent<boolean>;
31
- error: CustomEvent<any> | CustomEvent<string>;
32
- drag: CustomEvent<any>;
33
- close_stream: CustomEvent<undefined>;
34
- change?: CustomEvent<undefined> | undefined;
35
- stream: CustomEvent<ValueData>;
36
- clear?: CustomEvent<undefined> | undefined;
37
- upload?: CustomEvent<undefined> | undefined;
38
- select: CustomEvent<SelectData>;
39
- end_stream: CustomEvent<never>;
40
- } & {
41
- [evt: string]: CustomEvent<any>;
42
- };
43
- slots: {
44
- default: {};
45
- };
46
- exports?: {} | undefined;
47
- bindings?: string | undefined;
48
- };
49
- export type ImageUploaderProps = typeof __propDef.props;
50
- export type ImageUploaderEvents = typeof __propDef.events;
51
- export type ImageUploaderSlots = typeof __propDef.slots;
52
- export default class ImageUploader extends SvelteComponent<ImageUploaderProps, ImageUploaderEvents, ImageUploaderSlots> {
15
+ z_$$bindings?: Bindings;
53
16
  }
54
- export {};
17
+ type $$__sveltets_2_PropsWithChildren<Props, Slots> = Props & (Slots extends {
18
+ default: any;
19
+ } ? Props extends Record<string, never> ? any : {
20
+ children?: any;
21
+ } : {});
22
+ declare const ImageUploader: $$__sveltets_2_IsomorphicComponent<$$__sveltets_2_PropsWithChildren<{
23
+ value?: null | FileData | Base64File;
24
+ label?: string | undefined;
25
+ show_label: boolean;
26
+ sources?: ("upload" | "clipboard" | "microphone" | "webcam" | null)[];
27
+ streaming?: boolean;
28
+ pending?: boolean;
29
+ webcam_options: WebcamOptions;
30
+ selectable?: boolean;
31
+ root: string;
32
+ i18n: I18nFormatter;
33
+ max_file_size?: number | null;
34
+ upload: Client["upload"];
35
+ stream_handler: Client["stream"];
36
+ stream_every: number;
37
+ time_limit: number;
38
+ show_fullscreen_button?: boolean;
39
+ stream_state?: "open" | "waiting" | "closed";
40
+ upload_promise?: Promise<any> | null;
41
+ uploading?: boolean;
42
+ active_source?: "upload" | "clipboard" | "microphone" | "webcam" | null;
43
+ fullscreen?: boolean;
44
+ dragging?: boolean;
45
+ }, {
46
+ default: {};
47
+ }>, {
48
+ fullscreen: CustomEvent<boolean>;
49
+ error: CustomEvent<any> | CustomEvent<string>;
50
+ drag: CustomEvent<any>;
51
+ close_stream: CustomEvent<undefined>;
52
+ change?: CustomEvent<undefined> | undefined;
53
+ stream: CustomEvent<ValueData>;
54
+ clear?: CustomEvent<undefined> | undefined;
55
+ upload?: CustomEvent<undefined> | undefined;
56
+ select: CustomEvent<SelectData>;
57
+ end_stream: CustomEvent<never>;
58
+ } & {
59
+ [evt: string]: CustomEvent<any>;
60
+ }, {
61
+ default: {};
62
+ }, {}, string>;
63
+ type ImageUploader = InstanceType<typeof ImageUploader>;
64
+ export default ImageUploader;