freddyaboulton HF Staff commited on
Commit
5d54b49
·
verified ·
1 Parent(s): 9d391d0

Upload folder using huggingface_hub

Browse files
6.0.0/annotatedimage/Index.svelte ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import {
3
+ Block,
4
+ BlockLabel,
5
+ Empty,
6
+ IconButtonWrapper,
7
+ FullscreenButton
8
+ } from "@gradio/atoms";
9
+ import { Image } from "@gradio/icons";
10
+ import { StatusTracker } from "@gradio/statustracker";
11
+ import { Gradio } from "@gradio/utils";
12
+ import type { AnnotatedImageProps, AnnotatedImageEvents } from "./types";
13
+
14
+ const props = $props();
15
+ const gradio = new Gradio<AnnotatedImageEvents, AnnotatedImageProps>(props);
16
+
17
+ let old_value = $state(gradio.props.value);
18
+ let active: string | null = $state(null);
19
+ let image_container: HTMLElement;
20
+ let fullscreen = $state(false);
21
+ let label = $derived(
22
+ gradio.shared.label || gradio.i18n("annotated_image.annotated_image")
23
+ );
24
+
25
+ $effect(() => {
26
+ if (old_value != gradio.props.value) {
27
+ old_value = gradio.props.value;
28
+ gradio.dispatch("change");
29
+ }
30
+ });
31
+
32
+ function handle_mouseover(_label: string): void {
33
+ active = _label;
34
+ }
35
+
36
+ function handle_mouseout(): void {
37
+ active = null;
38
+ }
39
+
40
+ function handle_click(i: number, value: string): void {
41
+ gradio.dispatch("select", {
42
+ value: value,
43
+ index: i
44
+ });
45
+ }
46
+ </script>
47
+
48
+ <Block
49
+ visible={gradio.shared.visible}
50
+ elem_id={gradio.shared.elem_id}
51
+ elem_classes={gradio.shared.elem_classes}
52
+ padding={false}
53
+ height={gradio.props.height}
54
+ width={gradio.props.width}
55
+ allow_overflow={false}
56
+ container={gradio.shared.container}
57
+ scale={gradio.shared.scale}
58
+ min_width={gradio.shared.min_width}
59
+ bind:fullscreen
60
+ >
61
+ <StatusTracker
62
+ autoscroll={gradio.shared.autoscroll}
63
+ i18n={gradio.i18n}
64
+ {...gradio.shared.loading_status}
65
+ />
66
+ <BlockLabel show_label={gradio.shared.show_label} Icon={Image} {label} />
67
+
68
+ <div class="container">
69
+ {#if gradio.props.value == null}
70
+ <Empty size="large" unpadded_box={true}><Image /></Empty>
71
+ {:else}
72
+ <div class="image-container" bind:this={image_container}>
73
+ <IconButtonWrapper>
74
+ {#if gradio.props.buttons.includes("fullscreen") ?? true}
75
+ <FullscreenButton
76
+ {fullscreen}
77
+ on:fullscreen={({ detail }) => {
78
+ fullscreen = detail;
79
+ }}
80
+ />
81
+ {/if}
82
+ </IconButtonWrapper>
83
+
84
+ <img
85
+ class="base-image"
86
+ class:fit-height={gradio.props.height && !fullscreen}
87
+ src={gradio.props.value ? gradio.props.value.image.url : null}
88
+ alt="the base file that is annotated"
89
+ />
90
+ {#each gradio.props.value ? gradio.props.value.annotations : [] as ann, i}
91
+ <img
92
+ alt="segmentation mask identifying {gradio.shared
93
+ .label} within the uploaded file"
94
+ class="mask fit-height"
95
+ class:fit-height={!fullscreen}
96
+ class:active={active == ann.label}
97
+ class:inactive={active != ann.label && active != null}
98
+ src={ann.image.url}
99
+ style={gradio.props.color_map && ann.label in gradio.props.color_map
100
+ ? null
101
+ : `filter: hue-rotate(${Math.round(
102
+ (i * 360) / (gradio.props.value?.annotations.length ?? 1)
103
+ )}deg);`}
104
+ />
105
+ {/each}
106
+ </div>
107
+ {#if gradio.props.show_legend && gradio.props.value}
108
+ <div class="legend">
109
+ {#each gradio.props.value.annotations as ann, i}
110
+ <button
111
+ class="legend-item"
112
+ style="background-color: {gradio.props.color_map &&
113
+ ann.label in gradio.props.color_map
114
+ ? gradio.props.color_map[ann.label] + '88'
115
+ : `hsla(${Math.round(
116
+ (i * 360) / gradio.props.value.annotations.length
117
+ )}, 100%, 50%, 0.3)`}"
118
+ on:mouseover={() => handle_mouseover(ann.label)}
119
+ on:focus={() => handle_mouseover(ann.label)}
120
+ on:mouseout={() => handle_mouseout()}
121
+ on:blur={() => handle_mouseout()}
122
+ on:click={() => handle_click(i, ann.label)}
123
+ >
124
+ {ann.label}
125
+ </button>
126
+ {/each}
127
+ </div>
128
+ {/if}
129
+ {/if}
130
+ </div>
131
+ </Block>
132
+
133
+ <style>
134
+ .base-image {
135
+ display: block;
136
+ width: 100%;
137
+ height: auto;
138
+ }
139
+ .container {
140
+ display: flex;
141
+ position: relative;
142
+ flex-direction: column;
143
+ justify-content: center;
144
+ align-items: center;
145
+ width: var(--size-full);
146
+ height: var(--size-full);
147
+ }
148
+ .image-container {
149
+ position: relative;
150
+ top: 0;
151
+ left: 0;
152
+ flex-grow: 1;
153
+ width: 100%;
154
+ overflow: hidden;
155
+ }
156
+ .fit-height {
157
+ top: 0;
158
+ left: 0;
159
+ width: 100%;
160
+ height: 100%;
161
+ object-fit: contain;
162
+ }
163
+ .mask {
164
+ opacity: 0.85;
165
+ transition: all 0.2s ease-in-out;
166
+ position: absolute;
167
+ }
168
+ .image-container:hover .mask {
169
+ opacity: 0.3;
170
+ }
171
+ .mask.active {
172
+ opacity: 1;
173
+ }
174
+ .mask.inactive {
175
+ opacity: 0;
176
+ }
177
+ .legend {
178
+ display: flex;
179
+ flex-direction: row;
180
+ flex-wrap: wrap;
181
+ align-content: center;
182
+ justify-content: center;
183
+ align-items: center;
184
+ gap: var(--spacing-sm);
185
+ padding: var(--spacing-sm);
186
+ }
187
+ .legend-item {
188
+ display: flex;
189
+ flex-direction: row;
190
+ align-items: center;
191
+ cursor: pointer;
192
+ border-radius: var(--radius-sm);
193
+ padding: var(--spacing-sm);
194
+ }
195
+ </style>
6.0.0/annotatedimage/package.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/annotatedimage",
3
+ "version": "0.10.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "devDependencies": {
19
+ "@gradio/preview": "workspace:^"
20
+ },
21
+ "peerDependencies": {
22
+ "svelte": "^5.43.4"
23
+ },
24
+ "dependencies": {
25
+ "@gradio/atoms": "workspace:^",
26
+ "@gradio/icons": "workspace:^",
27
+ "@gradio/statustracker": "workspace:^",
28
+ "@gradio/upload": "workspace:^",
29
+ "@gradio/utils": "workspace:^",
30
+ "@gradio/client": "workspace:^"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/gradio-app/gradio.git",
35
+ "directory": "js/annotatedimage"
36
+ }
37
+ }
6.0.0/annotatedimage/types.ts ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+
3
+ export interface Annotation {
4
+ image: FileData;
5
+ label: string;
6
+ }
7
+
8
+ export interface AnnotatedImageValue {
9
+ image: FileData;
10
+ annotations: Annotation[];
11
+ }
12
+
13
+ export interface AnnotatedImageProps {
14
+ value: AnnotatedImageValue | null;
15
+ show_legend: boolean;
16
+ height: number | undefined;
17
+ width: number | undefined;
18
+ color_map: Record<string, string>;
19
+ buttons: string[];
20
+ }
21
+
22
+ export interface AnnotatedImageEvents {
23
+ change: never;
24
+ select: { index: number; value: string };
25
+ }