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 = `sd-perception-${{imgId}}.png`;
            return new File([blob], fileName, { type: 'image/png' });
        }else{
            const fileName = `sd-perception-${{imgId}}.jpg`;
            return new File([blob], fileName, { type: 'image/jpeg' });
        }
	}
	
    async function getVideoBlobFile(videoEL){
        const res = await fetch(videoEL.src);
        const blob = await res.blob();
        const videoId = Date.now() % 200;
        const fileName = `ms-image2video-${{videoId}}.mp4`;
        const videoBlob = new File([blob], fileName, { type: 'video/mp4' });
        console.log(videoBlob);
        return videoBlob;
	}
    
    const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
    const inputImgEl = gradioEl.querySelector('#image-in img');
    const outputVideo = gradioEl.querySelector('#video-out video');
    
    const shareBtnEl = gradioEl.querySelector('#share-btn');
    const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
    const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
    if(!outputVideo){
        return;
    };
    shareBtnEl.style.pointerEvents = 'none';
    shareIconEl.style.display = 'none';
    loadingIconEl.style.removeProperty('display');
    
    const inputFile = await getInputImgFile(inputImgEl);
    const urlInputImg = await uploadFile(inputFile);
    const videoOutFile = await getVideoBlobFile(outputVideo);
	const dataOutputVid = await uploadFile(videoOutFile);
    
    const descriptionMd = `
#### Image init:
 #### MS Image2Video result:
${dataOutputVid}
`;
    const params = new URLSearchParams({
        title: "Please provide a title :)",
        description: descriptionMd,
    });
	const paramsStr = params.toString();
	window.open(`https://huggingface.co/spaces/fffiloni/MS-Image2Video/discussions/new?${paramsStr}`, '_blank');
    shareBtnEl.style.removeProperty('pointer-events');
    shareIconEl.style.removeProperty('display');
    loadingIconEl.style.display = 'none';
}"""
#### MS Image2Video result:
${dataOutputVid}
`;
    const params = new URLSearchParams({
        title: "Please provide a title :)",
        description: descriptionMd,
    });
	const paramsStr = params.toString();
	window.open(`https://huggingface.co/spaces/fffiloni/MS-Image2Video/discussions/new?${paramsStr}`, '_blank');
    shareBtnEl.style.removeProperty('pointer-events');
    shareIconEl.style.removeProperty('display');
    loadingIconEl.style.display = 'none';
}"""