File size: 4,188 Bytes
d8d37b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Translation (Tokenizer & Model) - Hugging Face Transformers.js</title>

    <script type="module">
        // Import the library
        import { AutoTokenizer, AutoModelForSeq2SeqLM } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.5.4';

        // Make them available globally
        window.AutoTokenizer = AutoTokenizer;
        window.AutoModelForSeq2SeqLM = AutoModelForSeq2SeqLM;
    </script>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <link rel="stylesheet" href="css/styles.css">
</head>

<body>
    <div class="container-main">
        <!-- Page Header -->
        <div class="header">
            <div class="header-logo">
                <img src="images/logo.png" alt="logo">
            </div>
            <div class="header-main-text">
                <h1>Hugging Face Transformers.js</h1>
            </div>
            <div class="header-sub-text">
                <h3>Free AI Models for JavaScript Web Development</h3>
            </div>
        </div>
        <hr> <!-- Separator -->

        <!-- Back to Home button -->
        <div class="row mt-5">
            <div class="col-md-12 text-center">
                <a href="index.html" class="btn btn-outline-secondary"
                    style="color: #3c650b; border-color: #3c650b;">Back to Main Page</a>
            </div>
        </div>

        <!-- Content -->
        <div class="container mt-5">
            <!-- Centered Titles -->
            <div class="text-center">
                <h2>Natural Language Processing</h2>
                <h4>Translation (Using Tokenizers and Models))</h4>
            </div>

            <!-- Actual Content of this page -->
            <div id="translation-container" class="container mt-4">
                <h5>Load and run an AutoModelForSeq2SeqLM Model and an AutoTokenizer:</h5>
                <div class="d-flex align-items-center">
                    <label for="translationText" class="mb-0 text-nowrap" style="margin-right: 15px;">Enter Text to
                        Translate:</label>
                    <input type="text" class="form-control flex-grow-1" id="translationText"
                        value="I love transformers!" placeholder="Enter text"
                        style="margin-right: 15px; margin-left: 15px;">
                    <button id="translateButton" class="btn btn-primary" onclick="translateText()">Translate</button>
                </div>
                <div class="mt-4">
                    <h4>Output:</h4>
                    <pre id="outputArea"></pre>
                </div>
            </div>

            <!-- Back to Home button -->
            <div class="row mt-5">
                <div class="col-md-12 text-center">
                    <a href="index.html" class="btn btn-outline-secondary"
                        style="color: #3c650b; border-color: #3c650b;">Back to Main Page</a>
                </div>
            </div>
        </div>
    </div>

    <script>

        let tokenizer;
        let model;

        // Initialize the sentiment analysis model
        async function initializeModel() {
            tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small');
            model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small');

        }

        async function translateText() {

            const textFieldValue = document.getElementById("translationText").value.trim();
            let { input_ids } = await tokenizer(textFieldValue);

            console.log("input_ids: " + input_ids);

            let outputs = await model.generate(input_ids);

            let decoded = tokenizer.decode(outputs[0], { skip_special_tokens: true });

            console.log("outputs: " + outputs);
            console.log("decoded: " + decoded);

            document.getElementById("outputArea").innerText = JSON.stringify(decoded, null, 2);
        }

        // Initialize the model after the DOM is completely loaded
        window.addEventListener("DOMContentLoaded", initializeModel);
    </script>
</body>

</html>