User:Polygnotus/Scripts/Apertus.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/Apertus.
// <nowiki>
const script = document.createElement('script');
script.type = 'module';
script.textContent = `
import { InferenceClient } from "https://cdn.jsdelivr.net/npm/@huggingface/inference/+esm";
const STORAGE_KEY = 'apertus_hf_token';
function getStoredToken() {
return localStorage.getItem(STORAGE_KEY);
}
function setStoredToken(token) {
localStorage.setItem(STORAGE_KEY, token);
}
async function callApertus(message, token) {
const client = new InferenceClient(token);
const response = await client.chatCompletion({
model: "swiss-ai/Apertus-8B-Instruct-2509",
messages: [{ role: "user", content: message }],
max_tokens: 500
});
const content = response.choices[0].message.content;
console.log(content);
return content;
}
async function handleAskApertus() {
let token = getStoredToken();
if (!token) {
token = prompt("Enter your HuggingFace API token:");
if (!token) return;
setStoredToken(token);
}
const question = prompt("Ask Apertus:");
if (!question) return;
try {
const response = await callApertus(question, token);
alert(response);
} catch (error) {
console.error("Error calling Apertus:", error);
alert("Error: " + error.message);
}
}
// Add link to Tools menu
function addToolsLink() {
const toolsMenu = document.querySelector('#p-tb ul');
if (toolsMenu) {
const li = document.createElement('li');
li.className = 'mw-list-item';
const link = document.createElement('a');
link.href = '#';
link.textContent = 'Ask Apertus';
link.addEventListener('click', (e) => {
e.preventDefault();
handleAskApertus();
});
li.appendChild(link);
toolsMenu.appendChild(li);
console.log("✅ Apertus Tools link added");
}
}
// Wait for page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addToolsLink);
} else {
addToolsLink();
}
`;
document.head.appendChild(script);
// </nowiki>