# 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);
const urlChatbotImage = await uploadFile(imageFile);
console.log(urlChatbotImage);
let titleTxt = `FROMAGe`;
const shareBtnEl = gradioEl.querySelector('#share-btn');
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
shareBtnEl.style.pointerEvents = 'none';
shareIconEl.style.display = 'none';
loadingIconEl.style.removeProperty('display');
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');
shareIconEl.style.removeProperty('display');
loadingIconEl.style.display = 'none';
}
"""
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 + ".jpg";
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);
}
"""