nicholasKluge
commited on
Commit
•
2301695
1
Parent(s):
17de898
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,59 @@
|
|
1 |
---
|
2 |
license: apache-2.0
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: apache-2.0
|
3 |
+
library_name: keras
|
4 |
---
|
5 |
+
# BiLSTM Sentiment Classifier (Teeny-Tiny Castle)
|
6 |
+
|
7 |
+
This model is part of a tutorial tied to the [Teeny-Tiny Castle](https://github.com/Nkluge-correa/TeenyTinyCastle), an open-source repository containing educational tools for AI Ethics and Safety research.
|
8 |
+
|
9 |
+
## How to Use
|
10 |
+
|
11 |
+
```python
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
# Download the model
|
15 |
+
hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier",
|
16 |
+
filename="BiLSTM-sentiment-classifier.h5",
|
17 |
+
local_dir="./",
|
18 |
+
repo_type="model"
|
19 |
+
)
|
20 |
+
|
21 |
+
# Download the tokenizer file
|
22 |
+
hf_hub_download(repo_id="AiresPucrs/BiLSTM-sentiment-classifier",
|
23 |
+
filename="tokenizer-BiLSTM-sentiment-classifier.json",
|
24 |
+
local_dir="./",
|
25 |
+
repo_type="model"
|
26 |
+
)
|
27 |
+
|
28 |
+
import json
|
29 |
+
import torch
|
30 |
+
import numpy as np
|
31 |
+
import pandas as pd
|
32 |
+
import tensorflow as tf
|
33 |
+
|
34 |
+
model = tf.keras.models.load_model('./BiLSTM-sentiment-classifier.h5')
|
35 |
+
|
36 |
+
with open('./tokenizer-BiLSTM-sentiment-classifier.json') as fp:
|
37 |
+
data = json.load(fp)
|
38 |
+
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
|
39 |
+
fp.close()
|
40 |
+
|
41 |
+
strings = [
|
42 |
+
'this explanation is really bad',
|
43 |
+
'i did not like this tutorial 2/10',
|
44 |
+
'this tutorial is garbage i wont my money back',
|
45 |
+
'is nice to see philosophers doing machine learning',
|
46 |
+
'this is a great and wonderful example of nlp',
|
47 |
+
'this tutorial is great one of the best tutorials ever made'
|
48 |
+
]
|
49 |
+
|
50 |
+
preds = model.predict(
|
51 |
+
tf.keras.preprocessing.sequence.pad_sequences(
|
52 |
+
tokenizer.texts_to_sequences(strings),
|
53 |
+
maxlen=250,
|
54 |
+
truncating='post'
|
55 |
+
), verbose=0)
|
56 |
+
|
57 |
+
for i, string in enumerate(strings):
|
58 |
+
print(f'Review: "{string}"\n(Negative 😊 {round((preds[i][0]) * 100)}% | Positive 😔 {round(preds[i][1] * 100)}%)\n')
|
59 |
+
```
|