Update share_btn.py
Browse files- share_btn.py +4 -75
share_btn.py
CHANGED
|
@@ -1,75 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin" style="color: #ffffff;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
| 7 |
-
|
| 8 |
-
share_js = """async () => {
|
| 9 |
-
async function uploadFile(file){
|
| 10 |
-
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
| 11 |
-
const response = await fetch(UPLOAD_URL, {
|
| 12 |
-
method: 'POST',
|
| 13 |
-
headers: {
|
| 14 |
-
'Content-Type': file.type,
|
| 15 |
-
'X-Requested-With': 'XMLHttpRequest',
|
| 16 |
-
},
|
| 17 |
-
body: file, /// <- File inherits from Blob
|
| 18 |
-
});
|
| 19 |
-
const url = await response.text();
|
| 20 |
-
return url;
|
| 21 |
-
}
|
| 22 |
-
async function getInputAudioFile(audioEl){
|
| 23 |
-
const res = await fetch(audioEl.src);
|
| 24 |
-
const blob = await res.blob();
|
| 25 |
-
const audioId = Date.now();
|
| 26 |
-
const fileName = `bark-audio-${{audioId}}.wav`;
|
| 27 |
-
return new File([blob], fileName, { type: 'audio/wav' });
|
| 28 |
-
}
|
| 29 |
-
async function audioToBase64(audioFile) {
|
| 30 |
-
return new Promise((resolve, reject) => {
|
| 31 |
-
let reader = new FileReader();
|
| 32 |
-
reader.readAsDataURL(audioFile);
|
| 33 |
-
reader.onload = () => resolve(reader.result);
|
| 34 |
-
reader.onerror = error => reject(error);
|
| 35 |
-
|
| 36 |
-
});
|
| 37 |
-
}
|
| 38 |
-
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
|
| 39 |
-
const inputText = gradioEl.querySelector('#input_text textarea').value;
|
| 40 |
-
const speakerOption = gradioEl.querySelector('#speaker_option .single-select').innerText;
|
| 41 |
-
const audioElement = gradioEl.querySelector('#audio_out audio');
|
| 42 |
-
|
| 43 |
-
const titleTxt = `Bark: ${inputText.slice(0, 50)}...`;
|
| 44 |
-
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
| 45 |
-
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
| 46 |
-
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
| 47 |
-
if(!audioElement){
|
| 48 |
-
return;
|
| 49 |
-
};
|
| 50 |
-
shareBtnEl.style.pointerEvents = 'none';
|
| 51 |
-
shareIconEl.style.display = 'none';
|
| 52 |
-
loadingIconEl.style.removeProperty('display');
|
| 53 |
-
const outputAudioFile = await getInputAudioFile(audioElement);
|
| 54 |
-
const urlOutputAudio = await uploadFile(outputAudioFile);
|
| 55 |
-
|
| 56 |
-
const descriptionMd = `
|
| 57 |
-
### Text
|
| 58 |
-
${inputText}
|
| 59 |
-
|
| 60 |
-
### Acoustic Prompt
|
| 61 |
-
${speakerOption}
|
| 62 |
-
|
| 63 |
-
### Audio
|
| 64 |
-
<audio controls><source src="${urlOutputAudio}" type="audio/wav"></audio>
|
| 65 |
-
`;
|
| 66 |
-
const params = new URLSearchParams({
|
| 67 |
-
title: titleTxt,
|
| 68 |
-
description: descriptionMd,
|
| 69 |
-
});
|
| 70 |
-
const paramsStr = params.toString();
|
| 71 |
-
window.open(`https://huggingface.co/spaces/suno/bark/discussions/new?${paramsStr}`, '_blank');
|
| 72 |
-
shareBtnEl.style.removeProperty('pointer-events');
|
| 73 |
-
shareIconEl.style.removeProperty('display');
|
| 74 |
-
loadingIconEl.style.display = 'none';
|
| 75 |
-
}"""
|
|
|
|
| 1 |
+
# Placeholder for share button components
|
| 2 |
+
community_icon_html = ""
|
| 3 |
+
loading_icon_html = ""
|
| 4 |
+
share_js = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|