Keras
nicholasKluge's picture
Update README.md
2301695 verified
metadata
license: apache-2.0
library_name: keras

BiLSTM Sentiment Classifier (Teeny-Tiny Castle)

This model is part of a tutorial tied to the Teeny-Tiny Castle, an open-source repository containing educational tools for AI Ethics and Safety research.

How to Use

from huggingface_hub import hf_hub_download

# Download the model
hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier",
                filename="BiLSTM-sentiment-classifier.h5",
                local_dir="./",
                repo_type="model"
 )

# Download the tokenizer file
hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier",
                filename="tokenizer-BiLSTM-sentiment-classifier.json",
                local_dir="./",
                repo_type="model"
 )

import json
import torch
import numpy as np
import pandas as pd
import tensorflow as tf

model = tf.keras.models.load_model('./BiLSTM-sentiment-classifier.h5')

with open('./tokenizer-BiLSTM-sentiment-classifier.json') as fp:
 data = json.load(fp)
 tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
 fp.close()

strings = [
    'this explanation is really bad',
    'i did not like this tutorial 2/10',
    'this tutorial is garbage i wont my money back',
    'is nice to see philosophers doing machine learning',
    'this is a great and wonderful example of nlp',
    'this tutorial is great one of the best tutorials ever made'
]

preds = model.predict(
 tf.keras.preprocessing.sequence.pad_sequences(
 tokenizer.texts_to_sequences(strings),
        maxlen=250,
        truncating='post'
 ), verbose=0)

for i, string in enumerate(strings):
    print(f'Review: "{string}"\n(Negative ๐Ÿ˜Š {round((preds[i][0]) * 100)}% | Positive ๐Ÿ˜” {round(preds[i][1] * 100)}%)\n')