Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="jumbotron text-center">
    <h1>Language detection with TensorFlow.js</h1>
    <p>PowerCoders AI workshop - 2019 EPFL EdTech Collider</p> 
</div>

<div class="container">
    <div class="row">
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Languages</h5>
                    <textarea id="languages">["Deutsch", "Fran\u00e7ais", "Itialiano", "Rumantsch", "English"]</textarea>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Vocabulary</h5>
                    <textarea id="vocabulary">[" a ", "s e", " in", "cha", "the", "e. ", "n s", "ers", "e f", "ti ", "st ", "azi", " l\u2019", "s. ", "n i", "erl", "er ", "de ", " re", "ur ", "da ", "es ", "i c", "uns", "lic", "ons", "to ", "ent", "pli", " su", "gen", "con", "for", " al", "e c", "e l", "von", "e p", "ni ", "izr", "el ", "ada", "ch ", "una", "uis", "l 1", " ch", "des", "tsc", " le", "ted", "hwe", "a. ", "ant", "en ", "ono", "s s", " as", "n t", "si ", " tu", "lan", "ed ", "sa ", "di ", "ung", "les", "sta", "nta", "a c", "del", "au ", " mo", "che", "ver", " se", " pr", "ra ", "nge", "ist", "ize", "swi", "cht", "a s", "of ", "tra", " we", "cun", "en.", "her", "ro ", "ss ", " ei", "ral", "e a", "one", "d i", "ns ", " fo", "aus", "sch", "zer", " da", "und", " im", "son", "zze", "a e", "ica", " sw", "dal", " la", "iss", "all", "ate", "nce", "a l", "nd ", "ala", "im ", "ne ", " is", "nen", " ne", "ils", " to", "n d", "zio", "us ", "wit", " so", "is ", "ue ", "svi", "ont", "et ", " th", "eur", "t l", "ta ", ", a", " st", "ach", "te ", " ca", "ll'", "a t", ". l", "ell", "s p", "itz", "oun", "le ", "s o", " un", "nde", " au", "e e", "las", "me ", "ziu", "ts ", ". d", " ce", "dan", "mit", "ter", "a p", "no ", "ind", "s l", "e, ", "den", "par", "re ", " vo", "ina", " be", "ion", "a d", "ura", "he ", "tze", " es", " ar", " et", "nto", " pa", " ge", " po", " qu", "chw", "o a", "tio", "iqu", "la ", "tad", "li ", "on ", " \u00e0 ", " an", " co", " wa", " mi", "tre", " ha", "ls ", "han", "s a", "ich", "our", " of", "n l", "un ", "ren", "a, ", "nel", " te", "sen", "il ", "s c", "ng ", "du ", "iun", " si", "ha ", "ert", "n. ", "ste", "era", "na ", " \u00e8 ", "der", "eme", "men", "lla", "sui", "rla", "a m", "an ", "t d", "s, ", "rei", "r s", "ing", "rde", "ran", "ber", "viz", " zu", ", d", "lle", "ali", "ern", "eiz", "nte", "in ", "and", " l'", "ine", "s t", "s d", "e t", " il", "zra", "se ", "i a", " sc", "ans", " cu", "e d", " fr", "ere", "e o", "th ", "ede", "que", "are", "das", " de", "it ", "hen", "man", " pe", "f t", "r l", "\u00e9ra", "ly ", "oni", "n a", "or ", "ie ", " en", " e ", "o d", "nt ", "iz ", " d'", "ire", " sv", ", l", "ale", "ia ", "ein", "per", "ato", "ten", "i s", "sse", "wei", "ati", "al ", " ve", "die", "e i", "eit", "isc", "i p", "o s", "i d", "d t", "res", "est", " pl", " di", "izz", " du", "as ", "wis", "e s"]</textarea>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Text</h5>
                    <textarea id="text">il n’y a pas le feu au lac</textarea>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-3"></div>

    <div class="row">
        <div class="col-sm-12">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Predictions</h5>
                    <p class="card-text">Neural network output</p>
                    <pre id="prediction"></pre>
                    <button type="button" class="btn btn-primary" onclick="guess()">Guess language</button>
                </div>
            </div>
        </div>
    </div>
</div>
              
            
!

CSS

              
                textarea {
    min-width: 100%;
    height: 120px;
}
              
            
!

JS

              
                async function guess() {

    // Load language, vocabulary and text
    var languages = JSON.parse(document.getElementById("languages").value)
    var vocabulary = JSON.parse(document.getElementById("vocabulary").value)
    var text = document.getElementById("text").value

    // Encode text
    var text_encoded = new Array(vocabulary.length).fill(0);
    for (var i = 0; i < text.length; i++) {
        for (var j = 0; j < vocabulary.length; j++) {
            ngram = vocabulary[j]
            if (text.substr(i, ngram.length) === ngram) {
                text_encoded[j] += 1
            }
        }
    }

    // Make predictions!
    const model = await tf.loadLayersModel('https://raw.githubusercontent.com/epfl-exts/powercoders-ai/master/python-code/tfjs-model/model.json');
    const preds_tensor = model.predict(tf.tensor(text_encoded, shape=[1, vocabulary.length]))

    // Print them
    preds_tensor.data().then(function(probs) {
        output = ''
        for (var i = 0; i < languages.length; i++) {
            var spaces = ' '.repeat(15 - languages[i].length)
            output += languages[i] + spaces + (100*probs[i]).toFixed(2) + '%<br />'
        }
        document.getElementById("prediction").innerHTML = output
    });
}
              
            
!
999px

Console