Upload LSTM model and tokenizer
Browse files- .gitattributes +2 -0
- README.md +116 -0
- config.json +7 -0
- desktop.ini +2 -0
- lstm_model/fingerprint.pb +3 -0
- lstm_model/keras_metadata.pb +3 -0
- lstm_model/saved_model.pb +3 -0
- lstm_model/variables/variables.data-00000-of-00001 +3 -0
- lstm_model/variables/variables.index +0 -0
- tokenizer.json +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
lstm_model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- text-generation
|
4 |
+
- lstm
|
5 |
+
- tensorflow
|
6 |
+
library_name: tensorflow
|
7 |
+
pipeline_tag: text-generation
|
8 |
+
---
|
9 |
+
|
10 |
+
# LSTM Text Generation Model
|
11 |
+
|
12 |
+
This model was trained using TensorFlow/Keras for financial article generation tasks.
|
13 |
+
|
14 |
+
## Model Details
|
15 |
+
|
16 |
+
- **Model Type**: LSTM
|
17 |
+
- **Framework**: TensorFlow/Keras
|
18 |
+
- **Task**: Text Generation
|
19 |
+
- **Vocabulary Size**: 30000
|
20 |
+
- **Architecture**: Bi-directional Long Short-Term Memory (LSTM)
|
21 |
+
|
22 |
+
## Usage
|
23 |
+
|
24 |
+
```python
|
25 |
+
from huggingface_hub import snapshot_download
|
26 |
+
import tensorflow as tf
|
27 |
+
import json
|
28 |
+
import pickle
|
29 |
+
import numpy as np
|
30 |
+
|
31 |
+
# Download model files
|
32 |
+
model_path = snapshot_download(repo_id="firobeid/L4_LSTM_financial_News_Headlines_generator")
|
33 |
+
|
34 |
+
# Load the LSTM model
|
35 |
+
model = tf.keras.models.load_model(f"{model_path}/lstm_model")
|
36 |
+
|
37 |
+
# Load tokenizer
|
38 |
+
try:
|
39 |
+
# Try JSON format first
|
40 |
+
with open(f"{model_path}/tokenizer.json", 'r', encoding='utf-8') as f:
|
41 |
+
tokenizer_json = f.read()
|
42 |
+
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(tokenizer_json)
|
43 |
+
except FileNotFoundError:
|
44 |
+
# Fallback to pickle format
|
45 |
+
with open(f"{model_path}/tokenizer.pkl", 'rb') as f:
|
46 |
+
tokenizer = pickle.load(f)
|
47 |
+
|
48 |
+
# Text generation function
|
49 |
+
import numpy as np
|
50 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
51 |
+
|
52 |
+
def preprocess(texts, max_sequence_length = 71):
|
53 |
+
texts = '<s> %s'.format(texts.lower())
|
54 |
+
X = np.array(tokenizer.texts_to_sequences([texts])) # REMOVE -1
|
55 |
+
pad_encoded = pad_sequences(X,
|
56 |
+
maxlen= max_sequence_length,
|
57 |
+
padding='pre')
|
58 |
+
return pad_encoded
|
59 |
+
|
60 |
+
def next_word(model, tokenizer,
|
61 |
+
text, num_gen_words=1,
|
62 |
+
randome_sampling = False,
|
63 |
+
temperature=1):
|
64 |
+
'''
|
65 |
+
Randome_Sampling : Using a categorical distribution to predict the character returned by the model
|
66 |
+
Low temperatures results in more predictable text.
|
67 |
+
Higher temperatures results in more surprising text.
|
68 |
+
Experiment to find the best setting.
|
69 |
+
'''
|
70 |
+
input_text = text
|
71 |
+
output_text = [input_text]
|
72 |
+
|
73 |
+
for i in range(num_gen_words):
|
74 |
+
X_new = preprocess(input_text)
|
75 |
+
|
76 |
+
if randome_sampling:
|
77 |
+
y_proba = model.predict(X_new, verbose = 0)[0, -1:, :]#first sentence, last token
|
78 |
+
rescaled_logits = tf.math.log(y_proba) / temperature
|
79 |
+
pred_word_ind = tf.random.categorical(rescaled_logits, num_samples=1) #REMOVE THIS + 1
|
80 |
+
pred_word = tokenizer.sequences_to_texts(pred_word_ind.numpy())[0]
|
81 |
+
else:
|
82 |
+
y_proba = model.predict(X_new, verbose=0)[0] #first sentence
|
83 |
+
pred_word_ind = np.argmax(y_proba, axis = -1) #REMOVE THIS + 1
|
84 |
+
pred_word = tokenizer.index_word[pred_word_ind[-1]]
|
85 |
+
|
86 |
+
|
87 |
+
input_text += ' ' + pred_word
|
88 |
+
output_text.append(pred_word)
|
89 |
+
|
90 |
+
if pred_word == '</s>':
|
91 |
+
return ' '.join(output_text)
|
92 |
+
|
93 |
+
return ' '.join(output_text)
|
94 |
+
|
95 |
+
def generate_text(model, tokenizer, text, num_gen_words=25, temperature=1, random_sampling=False):
|
96 |
+
return next_word(model, tokenizer, text, num_gen_words, random_sampling, temperature)
|
97 |
+
|
98 |
+
# Example usage
|
99 |
+
# Start with these tag: <s>, while keeping words in lower case
|
100 |
+
generate_text(model,
|
101 |
+
tokenizer,
|
102 |
+
"Apple",
|
103 |
+
num_gen_words = 10,
|
104 |
+
random_sampling = True,
|
105 |
+
temperature= 10)
|
106 |
+
```
|
107 |
+
|
108 |
+
## Training
|
109 |
+
|
110 |
+
This model was trained on text data using LSTM architecture for next-word prediction.
|
111 |
+
|
112 |
+
## Limitations
|
113 |
+
|
114 |
+
- Model performance depends on training data quality and size
|
115 |
+
- Generated text may not always be coherent for longer sequences
|
116 |
+
- Model architecture is optimized for the specific vocabulary it was trained on
|
config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"model_type": "lstm",
|
3 |
+
"framework": "tensorflow",
|
4 |
+
"task": "text-generation",
|
5 |
+
"vocab_size": 30000,
|
6 |
+
"max_sequence_length": 71
|
7 |
+
}
|
desktop.ini
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[.ShellClassInfo]
|
2 |
+
IconResource=C:\Program Files\Google\Drive File Stream\108.0.1.0\GoogleDriveFS.exe,26
|
lstm_model/fingerprint.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:438c6b8d68eefefcd45426c3ac4f4ae5fd3cd2dd2181ac88fa3b5007f62f4587
|
3 |
+
size 55
|
lstm_model/keras_metadata.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4ccd43f3683507d23c448f49a4d7d1b9d57f32f6379d9ff3499e8f615f111eef
|
3 |
+
size 30349
|
lstm_model/saved_model.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d0ccb06635318ba4810da725dea3a917867106cbcb0ab2f9c5494d5bcc043776
|
3 |
+
size 12469352
|
lstm_model/variables/variables.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d212bec25afb2a4d0a2e53776debb686f758a2e08769a088bafe6a27fbd00407
|
3 |
+
size 84689920
|
lstm_model/variables/variables.index
ADDED
Binary file (1.55 kB). View file
|
|
tokenizer.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fba7c276e7f9a0e8a02881b676f0f6e7e9f984221508cc289a8e6a9c8f675842
|
3 |
+
size 18883354
|