# Modified from https://huggingface.co/spaces/haoheliu/audioldm-text-to-audio-generation/blob/79681cd8cb235160a27cdd100673346eb1784e53/share_btn.py
community_icon_html = """"""
loading_icon_html = """"""
share_js = """
async () => {
const html2canvas = (await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')).default;
async function uploadFile(file) {
console.log(file.type)
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, /// <- File inherits from Blob
});
const url = await response.text();
return url;
}
async function getImageFile(div) {
return new Promise((resolve, reject) =>
html2canvas(div)
.then((canvas) => {
const imageBlob = canvas.toBlob((blob) => {
const imageId = Date.now();
const fileName = "FROMAGe-" + imageId + ".jpg";
resolve(new File([blob], fileName, { type: 'image/jpeg' }));
}, 'image/jpeg', 0.95);
})
)
}
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
const chatbotEl = gradioEl.querySelector('#chatbot')
const imageFile = await getImageFile(chatbotEl);
console.log(imageFile);
const urlChatbotImage = await uploadFile(imageFile);
console.log(urlChatbotImage);
let titleTxt = `FROMAGe Example`;
//const shareBtnEl = gradioEl.querySelector('#share-btn');
//shareBtnEl.style.pointerEvents = 'none';
const descriptionMd = `
`;
const params = new URLSearchParams({
title: titleTxt,
description: descriptionMd,
});
const paramsStr = params.toString();
window.open(`https://huggingface.co/spaces/jykoh/fromage/discussions/new?${paramsStr}`, '_blank');
//shareBtnEl.style.removeProperty('pointer-events');
}
"""
save_js = """
async () => {
const html2canvas = (await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')).default;
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
async function getImageFile(div) {
return new Promise((resolve, reject) =>
html2canvas(div)
.then((canvas) => {
const imageId = Date.now();
const fileName = "FROMAGe-" + imageId + ".png";
saveAs(canvas.toDataURL(), fileName);
})
)
}
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
const chatbotEl = gradioEl.querySelector('#chatbot')
const imageFile = await getImageFile(chatbotEl);
console.log(imageFile);
}
"""