Create share-candle.js
Browse files- share-candle.js +45 -0
share-candle.js
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
async function shareToCommunity(ele, canvasID) {
|
2 |
+
ele.disabled = true;
|
3 |
+
|
4 |
+
const canvas = document.getElementById(canvasID);
|
5 |
+
const canvasBlob = await new Promise((resolve) =>
|
6 |
+
canvas.toBlob(resolve, "image/png")
|
7 |
+
);
|
8 |
+
const fileName = `share-${Date.now()}.png`;
|
9 |
+
const canvasFile = new File([canvasBlob], fileName, {
|
10 |
+
type: "image/png",
|
11 |
+
});
|
12 |
+
const imageUrl = await uploadFile(canvasFile);
|
13 |
+
|
14 |
+
const title = "Candle + YOLOv8";
|
15 |
+
const descriptionMd = `<img src="${imageUrl}">
|
16 |
+
|
17 |
+
YOLOv8 with [Candle](https://github.com/huggingface/candle)`;
|
18 |
+
|
19 |
+
const params = new URLSearchParams({
|
20 |
+
title: title,
|
21 |
+
description: descriptionMd,
|
22 |
+
preview: true,
|
23 |
+
});
|
24 |
+
|
25 |
+
window.open(
|
26 |
+
`https://huggingface.co/spaces/lmz/candle-yolo/discussions/new?${params}`,
|
27 |
+
"_blank"
|
28 |
+
);
|
29 |
+
ele.disabled = false;
|
30 |
+
}
|
31 |
+
async function uploadFile(file) {
|
32 |
+
const UPLOAD_URL = "https://huggingface.co/uploads";
|
33 |
+
const response = await fetch(UPLOAD_URL, {
|
34 |
+
method: "POST",
|
35 |
+
headers: {
|
36 |
+
"Content-Type": file.type,
|
37 |
+
"X-Requested-With": "XMLHttpRequest",
|
38 |
+
},
|
39 |
+
body: file,
|
40 |
+
});
|
41 |
+
const url = await response.text();
|
42 |
+
return url;
|
43 |
+
}
|
44 |
+
|
45 |
+
globalThis.shareToCommunity = shareToCommunity;
|