Skip to content

Commit ef19e5a

Browse files
author
Mishig Davaadorj
authored
Sync with hf internal (#228)
1 parent f054b46 commit ef19e5a

File tree

5 files changed

+102
-54
lines changed

5 files changed

+102
-54
lines changed

widgets/src/lib/InferenceWidget/shared/WidgetDropzone/WidgetDropzone.svelte

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
export let isLoading = false;
66
export let label =
77
"Drag image file here or click to browse from your computer";
8-
export let onSelectFile: (file: File) => void;
8+
export let onSelectFile: (file: File | Blob) => void;
9+
export let onError: (e: string) => void;
910
1011
let fileInput: HTMLInputElement;
1112
let isDragging = false;
@@ -18,6 +19,46 @@
1819
onSelectFile(file);
1920
}
2021
}
22+
23+
async function onDrop(e: DragEvent) {
24+
isDragging = false;
25+
const itemList = e.dataTransfer?.items;
26+
if (!itemList) {
27+
return;
28+
}
29+
const items: DataTransferItem[] = [];
30+
for (let i = 0; i < itemList.length; i++) {
31+
items.push(itemList[i]);
32+
}
33+
const uriItem = items.find(
34+
(x) => x.kind === "string" && x.type === "text/uri-list"
35+
);
36+
const fileItem = items.find((x) => x.kind === "file");
37+
if (uriItem) {
38+
const url = await new Promise<string>((resolve) =>
39+
uriItem.getAsString((s) => resolve(s))
40+
);
41+
/// Run through our own proxy to bypass CORS:
42+
const proxiedUrl =
43+
url.startsWith(`http://localhost`) ||
44+
new URL(url).host === window.location.host
45+
? url
46+
: `https://widgets-cors-proxy.huggingface.co/proxy?url=${url}`;
47+
const res = await fetch(proxiedUrl);
48+
const file = await res.blob();
49+
50+
imgSrc = URL.createObjectURL(file);
51+
onSelectFile(file);
52+
} else if (fileItem) {
53+
const file = fileItem.getAsFile();
54+
if (file) {
55+
imgSrc = URL.createObjectURL(file);
56+
onSelectFile(file);
57+
}
58+
} else {
59+
onError(`Unrecognized dragged and dropped file or element.`);
60+
}
61+
}
2162
</script>
2263
2364
<input
@@ -41,11 +82,7 @@
4182
isDragging = false;
4283
}}
4384
on:dragover|preventDefault
44-
on:drop|preventDefault={(e) => {
45-
isDragging = false;
46-
fileInput.files = e.dataTransfer?.files ?? null;
47-
onChange();
48-
}}
85+
on:drop|preventDefault={onDrop}
4986
>
5087
{#if !imgSrc}
5188
<span class="pointer-events-none text-sm">{label}</span>

widgets/src/lib/InferenceWidget/widgets/AutomaticSpeechRecognitionWidget/AutomaticSpeechRecognitionWidget.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@
7070
return;
7171
}
7272
73-
const requestBody = file ? { file } : { url: selectedSampleUrl };
73+
if (!file && selectedSampleUrl) {
74+
/// Run through our own proxy to bypass CORS:
75+
const proxiedUrl =
76+
selectedSampleUrl.startsWith(`http://localhost`) ||
77+
new URL(selectedSampleUrl).host === window.location.host
78+
? selectedSampleUrl
79+
: `https://widgets-cors-proxy.huggingface.co/proxy?url=${selectedSampleUrl}`;
80+
const res = await fetch(proxiedUrl);
81+
file = await res.blob();
82+
}
83+
84+
const requestBody = { file };
7485
7586
isLoading = true;
7687
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export class DataTable {
2+
max: number;
3+
min: number;
4+
std: number;
5+
6+
constructor(public body: number[] | number[][]) {
7+
const all = this.body.flat();
8+
this.max = Math.max(...all);
9+
this.min = Math.min(...all);
10+
this.std = this.max - this.min;
11+
}
12+
13+
get isArrLevel0(): boolean {
14+
return isArrLevel0(this.body);
15+
}
16+
17+
get oneDim(): number[] {
18+
return this.body as number[];
19+
}
20+
get twoDim(): number[][] {
21+
return this.body as number[][];
22+
}
23+
24+
bg(value: number): string {
25+
if (value > this.min + this.std * 0.7) {
26+
return "bg-green-100 dark:bg-green-800";
27+
}
28+
if (value > this.min + this.std * 0.6) {
29+
return "bg-green-50 dark:bg-green-900";
30+
}
31+
if (value < this.min + this.std * 0.3) {
32+
return "bg-red-100 dark:bg-red-800";
33+
}
34+
if (value < this.min + this.std * 0.4) {
35+
return "bg-red-50 dark:bg-red-900";
36+
}
37+
return "";
38+
}
39+
}
40+
41+
function isArrLevel0(x: number[] | number[][]): x is number[] {
42+
return typeof x[0] === "number";
43+
}

widgets/src/lib/InferenceWidget/widgets/FeatureExtractionWidget/FeatureExtractionWidget.svelte

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,7 @@
1010
getSearchParams,
1111
updateUrl,
1212
} from "../../shared/helpers";
13-
14-
const isArrLevel0 = (x: number[] | number[][]): x is number[] => {
15-
return typeof x[0] === "number";
16-
};
17-
18-
class DataTable {
19-
max: number;
20-
min: number;
21-
std: number;
22-
23-
constructor(public body: number[] | number[][]) {
24-
const all = this.body.flat();
25-
this.max = Math.max(...all);
26-
this.min = Math.min(...all);
27-
this.std = this.max - this.min;
28-
}
29-
30-
get isArrLevel0() {
31-
return isArrLevel0(this.body);
32-
}
33-
34-
get oneDim(): number[] {
35-
return this.body as number[];
36-
}
37-
get twoDim(): number[][] {
38-
return this.body as number[][];
39-
}
40-
41-
bg(value: number): string {
42-
if (value > this.min + this.std * 0.7) {
43-
return "bg-green-100 dark:bg-green-800";
44-
}
45-
if (value > this.min + this.std * 0.6) {
46-
return "bg-green-50 dark:bg-green-900";
47-
}
48-
if (value < this.min + this.std * 0.3) {
49-
return "bg-red-100 dark:bg-red-800";
50-
}
51-
if (value < this.min + this.std * 0.4) {
52-
return "bg-red-50 dark:bg-red-900";
53-
}
54-
return "";
55-
}
56-
}
13+
import { DataTable } from "./DataTable";
5714
5815
export let apiToken: WidgetProps["apiToken"];
5916
export let apiUrl: WidgetProps["apiUrl"];

widgets/src/lib/InferenceWidget/widgets/ImageClassificationWidget/ImageClassificationWidget.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
let output: Array<{ label: string; score: number }> = [];
2222
let outputJson: string;
2323
24-
function onSelectFile(file: File) {
24+
function onSelectFile(file: File | Blob) {
2525
getOutput(file);
2626
}
2727
28-
async function getOutput(file: File, withModelLoading = false) {
28+
async function getOutput(file: File | Blob, withModelLoading = false) {
2929
if (!file) {
3030
return;
3131
}
@@ -91,7 +91,7 @@
9191
>
9292
<svelte:fragment slot="top">
9393
<form>
94-
<WidgetDropzone {isLoading} {onSelectFile} />
94+
<WidgetDropzone {isLoading} {onSelectFile} onError={(e) => (error = e)} />
9595
</form>
9696
</svelte:fragment>
9797
<svelte:fragment slot="bottom">

0 commit comments

Comments
 (0)