// These two things need to be updated at each release for the version selector. // Last stable version const stableVersion = "v2.11.0" // Dictionary doc folder to label const versionMapping = { "master": "master", "": "v2.11.0 (stable)", "v2.10.0": "v2.10.0", "v2.9.1": "v2.9.0/v2.9.1", "v2.8.0": "v2.8.0", "v2.7.0": "v2.7.0", "v2.6.0": "v2.6.0", "v2.5.1": "v2.5.0/v2.5.1", "v2.4.0": "v2.4.0/v2.4.1", "v2.3.0": "v2.3.0", "v2.2.0": "v2.2.0/v2.2.1/v2.2.2", "v2.1.1": "v2.1.1", "v2.0.0": "v2.0.0", "v1.2.0": "v1.2.0", "v1.1.0": "v1.1.0", "v1.0.0": "v1.0.0" } function addIcon() { const huggingFaceLogo = "https://huggingface.co/landing/assets/transformers-docs/huggingface_logo.svg"; const image = document.createElement("img"); image.setAttribute("src", huggingFaceLogo); const div = document.createElement("div"); div.appendChild(image); div.style.textAlign = 'center'; div.style.paddingTop = '30px'; div.style.backgroundColor = '#6670FF'; const scrollDiv = document.querySelector(".wy-side-scroll"); scrollDiv.prepend(div); } function addCustomFooter() { const customFooter = document.createElement("div"); const questionOrIssue = document.createElement("div"); questionOrIssue.innerHTML = "Stuck? Read our Blog posts or Create an issue"; customFooter.appendChild(questionOrIssue); customFooter.classList.add("footer"); const social = document.createElement("div"); social.classList.add("footer__Social"); const imageDetails = [ { link: "https://huggingface.co", imageLink: "https://huggingface.co/landing/assets/transformers-docs/website.svg" }, { link: "https://twitter.com/huggingface", imageLink: "https://huggingface.co/landing/assets/transformers-docs/twitter.svg" }, { link: "https://github.com/huggingface", imageLink: "https://huggingface.co/landing/assets/transformers-docs/github.svg" }, { link: "https://www.linkedin.com/company/huggingface/", imageLink: "https://huggingface.co/landing/assets/transformers-docs/linkedin.svg" } ]; imageDetails.forEach(imageLinks => { const link = document.createElement("a"); const image = document.createElement("img"); image.src = imageLinks.imageLink; link.href = imageLinks.link; image.style.width = "30px"; image.classList.add("footer__CustomImage"); link.appendChild(image); social.appendChild(link); }); customFooter.appendChild(social); document.querySelector("footer").appendChild(customFooter); } function addGithubButton() { const div = `
`; document.querySelector(".wy-side-nav-search .icon-home").insertAdjacentHTML('afterend', div); } function addVersionControl() { // To grab the version currently in view, we parse the url const parts = location.toString().split('/'); let versionIndex = parts.length - 2; // Index page may not have a last part with filename.html so we need to go up if (parts[parts.length - 1] != "" && ! parts[parts.length - 1].match(/\.html$/)) { versionIndex = parts.length - 1; } // Main classes and models are nested so we need to go deeper else if (parts[versionIndex] == "main_classes" || parts[versionIndex] == "model_doc") { versionIndex = versionIndex - 1; } const version = parts[versionIndex]; // Menu with all the links, const versionMenu = document.createElement("div"); const htmlLines = []; for (const [key, value] of Object.entries(versionMapping)) { let baseUrlIndex = (version == "transformers") ? versionIndex + 1: versionIndex; var urlParts = parts.slice(0, baseUrlIndex); if (key != "") { urlParts = urlParts.concat([key]); } urlParts = urlParts.concat(parts.slice(versionIndex+1)); htmlLines.push(`${value}`); } versionMenu.classList.add("version-dropdown"); versionMenu.innerHTML = htmlLines.join('\n'); // Button for version selection const versionButton = document.createElement("div"); versionButton.classList.add("version-button"); let label = (version == "transformers") ? stableVersion : version versionButton.innerText = label.concat(" âŒ"); // Toggle the menu when we click on the button versionButton.addEventListener("click", () => { versionMenu.classList.toggle("version-show"); }); // Hide the menu when we click elsewhere window.addEventListener("click", (event) => { if (event.target != versionButton){ versionMenu.classList.remove('version-show'); } }); // Container const div = document.createElement("div"); div.appendChild(versionButton); div.appendChild(versionMenu); div.style.paddingTop = '25px'; div.style.backgroundColor = '#6670FF'; div.style.display = 'block'; div.style.textAlign = 'center'; const scrollDiv = document.querySelector(".wy-side-scroll"); scrollDiv.insertBefore(div, scrollDiv.children[1]); } function addHfMenu() { const div = ` `; document.body.insertAdjacentHTML('afterbegin', div); } function platformToggle() { const codeBlocks = Array.from(document.getElementsByClassName("highlight")); const pytorchIdentifier = "## PYTORCH CODE"; const tensorflowIdentifier = "## TENSORFLOW CODE"; const pytorchSpanIdentifier = `${pytorchIdentifier}`; const tensorflowSpanIdentifier = `${tensorflowIdentifier}`; const getFrameworkSpans = filteredCodeBlock => { const spans = filteredCodeBlock.element.innerHTML; const pytorchSpanPosition = spans.indexOf(pytorchSpanIdentifier); const tensorflowSpanPosition = spans.indexOf(tensorflowSpanIdentifier); let pytorchSpans; let tensorflowSpans; if(pytorchSpanPosition < tensorflowSpanPosition){ pytorchSpans = spans.slice(pytorchSpanPosition + pytorchSpanIdentifier.length + 1, tensorflowSpanPosition); tensorflowSpans = spans.slice(tensorflowSpanPosition + tensorflowSpanIdentifier.length + 1, spans.length); }else{ tensorflowSpans = spans.slice(tensorflowSpanPosition + tensorflowSpanIdentifier.length + 1, pytorchSpanPosition); pytorchSpans = spans.slice(pytorchSpanPosition + pytorchSpanIdentifier.length + 1, spans.length); } return { ...filteredCodeBlock, pytorchSample: pytorchSpans , tensorflowSample: tensorflowSpans } }; const createFrameworkButtons = sample => { const pytorchButton = document.createElement("button"); pytorchButton.innerText = "PyTorch"; const tensorflowButton = document.createElement("button"); tensorflowButton.innerText = "TensorFlow"; const selectorDiv = document.createElement("div"); selectorDiv.classList.add("framework-selector"); selectorDiv.appendChild(pytorchButton); selectorDiv.appendChild(tensorflowButton); sample.element.parentElement.prepend(selectorDiv); // Init on PyTorch sample.element.innerHTML = sample.pytorchSample; pytorchButton.classList.add("selected"); tensorflowButton.classList.remove("selected"); pytorchButton.addEventListener("click", () => { sample.element.innerHTML = sample.pytorchSample; pytorchButton.classList.add("selected"); tensorflowButton.classList.remove("selected"); }); tensorflowButton.addEventListener("click", () => { sample.element.innerHTML = sample.tensorflowSample; tensorflowButton.classList.add("selected"); pytorchButton.classList.remove("selected"); }); }; codeBlocks .map(element => {return {element: element.firstChild, innerText: element.innerText}}) .filter(codeBlock => codeBlock.innerText.includes(pytorchIdentifier) && codeBlock.innerText.includes(tensorflowIdentifier)) .map(getFrameworkSpans) .forEach(createFrameworkButtons); } /*! * github-buttons v2.2.10 * (c) 2019 ăȘă€ă * @license BSD-2-Clause */ /** * modified to run programmatically */ function parseGithubButtons (){"use strict";var e=window.document,t=e.location,o=window.encodeURIComponent,r=window.decodeURIComponent,n=window.Math,a=window.HTMLElement,i=window.XMLHttpRequest,l="https://unpkg.com/github-buttons@2.2.10/dist/buttons.html",c=i&&i.prototype&&"withCredentials"in i.prototype,d=c&&a&&a.prototype.attachShadow&&!a.prototype.attachShadow.prototype,s=function(e,t,o){e.addEventListener?e.addEventListener(t,o):e.attachEvent("on"+t,o)},u=function(e,t,o){e.removeEventListener?e.removeEventListener(t,o):e.detachEvent("on"+t,o)},h=function(e,t,o){var r=function(n){return u(e,t,r),o(n)};s(e,t,r)},f=function(e,t,o){var r=function(n){if(t.test(e.readyState))return u(e,"readystatechange",r),o(n)};s(e,"readystatechange",r)},p=function(e){return function(t,o,r){var n=e.createElement(t);if(o)for(var a in o){var i=o[a];null!=i&&(null!=n[a]?n[a]=i:n.setAttribute(a,i))}if(r)for(var l=0,c=r.length;l