File size: 2,476 Bytes
1c5f6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200187d
1c5f6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200187d
1c5f6ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 getBase64(file) {
       var reader = new FileReader();
       reader.readAsDataURL(file);
       reader.onload = function () {
           console.log(reader.result);
       };
       reader.onerror = function (error) {
           console.log('Error: ', error);
       };
    }
    const toDataURL = url => fetch(url)
        .then(response => response.blob())
        .then(blob => new Promise((resolve, reject) => {
            const reader = new FileReader()
            reader.onloadend = () => resolve(reader.result)
            reader.onerror = reject
            reader.readAsDataURL(blob)
        }));
    async function dataURLtoFile(dataurl, filename) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, {type:mime});
    };

    const gradioEl = document.querySelector('body > gradio-app');
    const imgEls = gradioEl.querySelectorAll('#output-screen img');
    if(!imgEls.length){
        return;
    };

    const urls = await Promise.all([...imgEls].map((imgEl) => {
        const origURL = imgEl.src;
        const imgId = Date.now() % 200;
        const fileName = 'semantic-palette-' + imgId + '.png';
        return toDataURL(origURL)
            .then(dataUrl => {
                return dataURLtoFile(dataUrl, fileName);
            })
        })).then(fileData => {return Promise.all([...fileData].map((file) => {
            return uploadFile(file);
        }))});

	const htmlImgs = urls.map(url => `<img src='${url}' width='2560' height='1024'>`);
	const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
${htmlImgs.join(`\n`)}
</div>`;
    const params = new URLSearchParams({
        title: `My creation`,
        description: descriptionMd,
    });
	const paramsStr = params.toString();
	window.open(`https://huggingface.co/spaces/ironjr/SemanticPalette/discussions/new?${paramsStr}`, '_blank');
}"""