Views
No views yet
1from huggingface_hub import snapshot_download
2import tensorflow as tf
3import json
4import pickle
5import numpy as np
6
7# Download model files
8model_path = snapshot_download(repo_id="firobeid/L4_LSTM_financial_News_Headlines_generator")
9
10# Load the LSTM model
11model = tf.keras.models.load_model(f"{model_path}/lstm_model")
12
13# Load tokenizer
14try:
15 # Try JSON format first
16 with open(f"{model_path}/tokenizer.json", 'r', encoding='utf-8') as f:
17 tokenizer_json = f.read()
18 tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(tokenizer_json)
19except FileNotFoundError:
20 # Fallback to pickle format
21 with open(f"{model_path}/tokenizer.pkl", 'rb') as f:
22 tokenizer = pickle.load(f)
23
24# Text generation function
25import numpy as np
26from tensorflow.keras.preprocessing.sequence import pad_sequences
27
28def preprocess(texts, max_sequence_length = 71):
29 texts = '<s> {}'.format(texts.lower())
30 X = np.array(tokenizer.texts_to_sequences([texts])) # REMOVE -1
31 pad_encoded = pad_sequences(X,
32 maxlen= max_sequence_length,
33 padding='pre')
34 return pad_encoded
35
36def next_word(model, tokenizer,
37 text, num_gen_words=1,
38 randome_sampling = False,
39 temperature=1):
40 '''
41 Randome_Sampling : Using a categorical distribution to predict the character returned by the model
42 Low temperatures results in more predictable text.
43 Higher temperatures results in more surprising text.
44 Experiment to find the best setting.
45 '''
46 input_text = text
47 output_text = [input_text]
48
49 for i in range(num_gen_words):
50 X_new = preprocess(input_text)
51
52 if randome_sampling:
53 y_proba = model.predict(X_new, verbose = 0)[0, -1:, :]#first sentence, last token
54 rescaled_logits = tf.math.log(y_proba) / temperature
55 pred_word_ind = tf.random.categorical(rescaled_logits, num_samples=1) #REMOVE THIS + 1
56 pred_word = tokenizer.sequences_to_texts(pred_word_ind.numpy())[0]
57 else:
58 y_proba = model.predict(X_new, verbose=0)[0] #first sentence
59 pred_word_ind = np.argmax(y_proba, axis = -1) #REMOVE THIS + 1
60 pred_word = tokenizer.index_word[pred_word_ind[-1]]
61
62
63 input_text += ' ' + pred_word
64 output_text.append(pred_word)
65
66 if pred_word == '</s>':
67 return ' '.join(output_text)
68
69 return ' '.join(output_text)
70
71def generate_text(model, tokenizer, text, num_gen_words=25, temperature=1, random_sampling=False):
72 return next_word(model, tokenizer, text, num_gen_words, random_sampling, temperature)
73
74# Example usage
75# Start with these tag: <s>, while keeping words in lower case
76generate_text(model,
77 tokenizer,
78 "Apple",
79 num_gen_words = 10,
80 random_sampling = True,
81 temperature= 10)