Views
No views yet
1import json
2import torch
3import numpy as np
4import pandas as pd
5import tensorflow as tf
6from huggingface_hub import hf_hub_download
7
8# Download the model
9hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier-adversarial",
10 filename="BiLSTM-sentiment-classifier-adversarial.h5",
11 local_dir="./",
12 repo_type="model"
13 )
14
15# Download the tokenizer file
16hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier-adversarial",
17 filename="tokenizer-BiLSTM-sentiment-classifier-adversarial.json",
18 local_dir="./",
19 repo_type="model"
20 )
21model = tf.keras.models.load_model('./BiLSTM-sentiment-classifier.h5')
22
23with open('./tokenizer-BiLSTM-sentiment-classifier.json') as fp:
24 data = json.load(fp)
25 tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
26 fp.close()
27
28strings = [
29 'this explanation is really bad',
30 'i did not like this tutorial 2/10',
31 'this tutorial is garbage i wont my money back',
32 'is nice to see philosophers doing machine learning',
33 'this is a great and wonderful example of nlp',
34 'this tutorial is great one of the best tutorials ever made'
35]
36
37preds = model.predict(
38 tf.keras.preprocessing.sequence.pad_sequences(
39 tokenizer.texts_to_sequences(strings),
40 maxlen=250,
41 truncating='post'
42 ), verbose=0)
43
44for i, string in enumerate(strings):
45 print(f'Review: "{string}"\n(Negative 😊 {preds[i][0] * 100:.2f}% | Positive 😔 {preds[i][1] * 100:.2f}%)\n')