utils / share-candle.js
radames's picture
Create share-candle.js
0473e83
async function shareToCommunity(ele, canvasID) {
ele.disabled = true;
const canvas = document.getElementById(canvasID);
const canvasBlob = await new Promise((resolve) =>
canvas.toBlob(resolve, "image/png")
);
const fileName = `share-${Date.now()}.png`;
const canvasFile = new File([canvasBlob], fileName, {
type: "image/png",
});
const imageUrl = await uploadFile(canvasFile);
const title = "Candle + YOLOv8";
const descriptionMd = `<img src="${imageUrl}">
YOLOv8 with [Candle](https://github.com/huggingface/candle)`;
const params = new URLSearchParams({
title: title,
description: descriptionMd,
preview: true,
});
window.open(
`https://huggingface.co/spaces/lmz/candle-yolo/discussions/new?${params}`,
"_blank"
);
ele.disabled = false;
}
async function uploadFile(file) {
const UPLOAD_URL = "https://huggingface.co/uploads";
const response = await fetch(UPLOAD_URL, {
method: "POST",
headers: {
"Content-Type": file.type,
"X-Requested-With": "XMLHttpRequest",
},
body: file,
});
const url = await response.text();
return url;
}
globalThis.shareToCommunity = shareToCommunity;