Code viewer for World: New World
// Function to display text to Ancient Brain World
function displayText(text) {
    world.println(text); // This is the correct way to print text in Ancient Brain
}

// Function to summarize text using Hugging Face API
async function summarizeText(text) {
    const url = "https://api-inference.huggingface.co/models/DISLab/SummLlama3.2-3B";
    const apiKey = "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"; // Replace this with your actual Hugging Face API key
    
    const headers = {
        "Authorization": `Bearer ${apiKey}`,
        "Content-Type": "application/json"
    };

    const body = {
        inputs: text
    };

    try {
        const response = await fetch(url, {
            method: "POST",
            headers: headers,
            body: JSON.stringify(body)
        });

        // Check if the response is successful
        if (response.ok) {
            const result = await response.json();
            const summary = result[0]?.summary_text || "No summary found.";
            displayText("Summary: " + summary); // Output the summary
        } else {
            const error = await response.json();
            displayText("Error: " + error.error); // Output any errors from the API
        }
    } catch (error) {
        displayText("An error occurred: " + error.message); // Display any fetch errors
    }
}

// Create an input field for the user to type their text
const inputField = world.createTextField("inputText", "Enter text to summarize here...");
const submitButton = world.createButton("summarizeButton", "Summarize Text");

// When the button is clicked, summarize the entered text
submitButton.onclick = function() {
    const inputText = inputField.getText();
    if (inputText) {
        summarizeText(inputText);
    } else {
        displayText("Please enter some text to summarize.");
    }
};

// Display an initial prompt to the user
displayText("Please enter text to summarize and click the 'Summarize Text' button.");