community_icon_html = """"""
loading_icon_html = """"""
share_js = """async () => {
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, /// <- File inherits from Blob
});
const url = await response.text();
return url;
}
async function getInputImgFile(imgEl){
const res = await fetch(imgEl.src);
const blob = await res.blob();
const imgId = Date.now() % 200;
const isPng = imgEl.src.startsWith(`data:image/png`);
if(isPng){
const fileName = `img-to-sfx-${{imgId}}.png`;
return new File([blob], fileName, { type: 'image/png' });
}else{
const fileName = `img-to-sfx-${{imgId}}.jpg`;
return new File([blob], fileName, { type: 'image/jpeg' });
}
}
async function getOutputSoundFile(audioEL){
const res = await fetch(audioEL.src);
const blob = await res.blob();
const audioId = Date.now() % 200;
const fileName = `img-to-sfx-${{audioId}}.wav`;
const musicBlob = new File([blob], fileName, { type: 'audio/wav' });
console.log(musicBlob);
return musicBlob;
}
async function audioToBase64(audioFile) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.readAsDataURL(audioFile);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
const captionTxt = gradioEl.querySelector('#text-caption textarea').value;
const inputImgEl = gradioEl.querySelector('#input-img img');
const outputSound = gradioEl.querySelector('#sound-output audio');
const outputSound_src = gradioEl.querySelector('#sound-output audio').src;
const outputSound_name = outputSound_src.split('/').pop();
const shareBtnEl = gradioEl.querySelector('#share-btn');
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
if(!outputSound){
return;
};
shareBtnEl.style.pointerEvents = 'none';
shareIconEl.style.display = 'none';
loadingIconEl.style.removeProperty('display');
const inputFile = await getInputImgFile(inputImgEl);
const urlInputImg = await uploadFile(inputFile);
const soundFile = await getOutputSoundFile(outputSound);
const dataOutputSound = await uploadFile(soundFile);
const descriptionMd = `
#### Image input:
${urlInputImg}
#### Sound Effect:
${dataOutputSound}
`;
const paramsStr = params.toString();
window.open(`https://huggingface.co/spaces/fffiloni/image-to-sound-fx/discussions/new?${paramsStr}`, '_blank');
shareBtnEl.style.removeProperty('pointer-events');
shareIconEl.style.removeProperty('display');
loadingIconEl.style.display = 'none';
}"""