firobeid commited on
Commit
72c7bb7
·
1 Parent(s): a9f8614
Files changed (2) hide show
  1. .ipynb_checkpoints/app-checkpoint.py +53 -0
  2. app.py +42 -51
.ipynb_checkpoints/app-checkpoint.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow.keras.backend as K
2
+ from tensorflow.keras.layers import LSTM
3
+ from pickle import load
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ import gradio as gr
7
+
8
+ model_V2 = 'ByteLevelLM.h5'
9
+
10
+ K.clear_session()
11
+ tf.keras.backend.clear_session()
12
+ np.random.seed(42)
13
+ tf.random.set_seed(42)
14
+ HeNormal = tf.keras.initializers.he_normal()
15
+ daily_V2 = tf.keras.models.load_model(model_V2,
16
+ custom_objects={'HeNormal': HeNormal},compile=False)
17
+ #Tokenizer
18
+ def tokenize():
19
+ import json
20
+ with open('Tokenizer.json', encoding='utf-8') as f:
21
+ data = json.load(f)
22
+ tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
23
+ with open('index2char.json', encoding='utf-8') as f:
24
+ index2char = json.load(f)
25
+ char2index = dict((int(v),int(k)) for k,v in index2char.items())
26
+ tokenizer.word_index = char2index
27
+ return tokenizer
28
+
29
+ def model2_preds(news_headline_input):
30
+ headline = news_headline_input
31
+ headline = '<s>' + headline + '<\s'
32
+
33
+ tokenizer = tokenize()
34
+
35
+ sample_2 = headline.encode('utf-8')
36
+ sample_2 = tokenizer.texts_to_sequences([sample_2])
37
+
38
+ predict_v2 = daily_V2.predict(sample_2, verbose = 0)[0,0]
39
+ # app_type = ui_display(title = "Model 2 Predictions (256 Bits Embeddings)")
40
+ return "Probability of Buy Signal from News Headline/s: %f" % predict_v2
41
+
42
+ # Create an instance of the Gradio Interface application function with the appropriate parameters.
43
+ app = gr.Interface(fn=model2_preds,
44
+ title="Event Driven Trading (Byte Level Language Modelling)",
45
+ description='News headlines from OverNight concatenated for next day Buy/Sell Probability/Signal',
46
+ inputs = gr.Textbox(label="News Headline/s", info='Separate several news headlines by a space'),
47
+ outputs=gr.Textbox(show_label = True,label="Prediction", info='This is the probability to buy at market close today and sell market close tomorrow'),
48
+ submit_btn = 'Predict')
49
+
50
+ # Launch the app
51
+ if __name__ == '__main__':
52
+ app.launch(share=True)
53
+
app.py CHANGED
@@ -1,62 +1,53 @@
1
- from tensorflow.keras.models import load_model
 
2
  from pickle import load
3
  import numpy as np
4
  import tensorflow as tf
5
  import gradio as gr
6
 
 
7
 
8
- # Set the model to the saved trained 300 epoch model.
9
- model = load_model('four_chapters_moby_dick_model_300_FIRAS.keras')
10
- # Set the tokenizer to the trained tokenizer from the model.
11
- tokenizer = load(open('four_chapters_moby_dick_tokenizer_300_FIRAS', 'rb'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- def preprocess(texts):
14
- X = np.array(tokenizer.texts_to_sequences([texts])) -1
15
- return X
16
-
17
- def next_word(text, num_gen_words=0,
18
- randome_sampling = False,
19
- temperature=1):
20
- '''
21
- Author : Firas Obeid
22
- Randome_Sampling : Using a categorical distribution to predict the character returned by the model
23
- Low temperatures results in more predictable text.
24
- Higher temperatures results in more surprising text.
25
- Experiment to find the best setting.
26
- '''
27
- input_text = text
28
- output_text = []
29
-
30
- for i in range(num_gen_words):
31
- X_new = preprocess(input_text)
32
-
33
- if randome_sampling:
34
- y_proba = model.predict(X_new, verbose = 0)[0, -1:, :]#first sentence, last token
35
- rescaled_logits = tf.math.log(y_proba) / temperature
36
- pred_word_ind = tf.random.categorical(rescaled_logits, num_samples=1) + 1
37
- pred_word = tokenizer.sequences_to_texts(pred_word_ind.numpy())[0]
38
- else:
39
- y_proba = model.predict(X_new, verbose=0)[0] #first sentence
40
- pred_word_ind = np.argmax(y_proba, axis = -1) +1
41
- pred_word = tokenizer.index_word[pred_word_ind[-1]]
42
-
43
-
44
- input_text += ' ' + pred_word
45
- output_text.append(pred_word)
46
-
47
- return ' '.join(output_text)
48
-
49
- def generate_text(text, num_gen_words=25, temperature=1, randome_sampling=False):
50
- return next_word(text, num_gen_words, randome_sampling, temperature)
51
 
52
  # Create an instance of the Gradio Interface application function with the appropriate parameters.
53
- max_output = gr.Number(value=150)
54
- app = gr.Interface(fn=generate_text,
55
- inputs=["text",
56
- gr.Slider(1, 50, value=1, step=1, label="Minimum number of Shakespearean words to generate", info="Choose between 1 and 50"),
57
- gr.Slider(0.1, 5, value=0.1, step=0.1, label="Temprature", info="Choose between 0.1 and 5"),
58
- "checkbox"],
59
- outputs="text")
60
  # Launch the app
61
  if __name__ == '__main__':
62
- app.launch(share=True)
 
 
1
+ import tensorflow.keras.backend as K
2
+ from tensorflow.keras.layers import LSTM
3
  from pickle import load
4
  import numpy as np
5
  import tensorflow as tf
6
  import gradio as gr
7
 
8
+ model_V2 = 'ByteLevelLM.h5'
9
 
10
+ K.clear_session()
11
+ tf.keras.backend.clear_session()
12
+ np.random.seed(42)
13
+ tf.random.set_seed(42)
14
+ HeNormal = tf.keras.initializers.he_normal()
15
+ daily_V2 = tf.keras.models.load_model(model_V2,
16
+ custom_objects={'HeNormal': HeNormal},compile=False)
17
+ #Tokenizer
18
+ def tokenize():
19
+ import json
20
+ with open('Tokenizer.json', encoding='utf-8') as f:
21
+ data = json.load(f)
22
+ tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
23
+ with open('index2char.json', encoding='utf-8') as f:
24
+ index2char = json.load(f)
25
+ char2index = dict((int(v),int(k)) for k,v in index2char.items())
26
+ tokenizer.word_index = char2index
27
+ return tokenizer
28
+
29
+ def model2_preds(news_headline_input):
30
+ headline = news_headline_input
31
+ headline = '<s>' + headline + '<\s'
32
 
33
+ tokenizer = tokenize()
34
+
35
+ sample_2 = headline.encode('utf-8')
36
+ sample_2 = tokenizer.texts_to_sequences([sample_2])
37
+
38
+ predict_v2 = daily_V2.predict(sample_2, verbose = 0)[0,0]
39
+ # app_type = ui_display(title = "Model 2 Predictions (256 Bits Embeddings)")
40
+ return "Probability of Buy Signal from News Headline/s: %f" % predict_v2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Create an instance of the Gradio Interface application function with the appropriate parameters.
43
+ app = gr.Interface(fn=model2_preds,
44
+ title="Event Driven Trading (Byte Level Language Modelling)",
45
+ description='News headlines from OverNight concatenated for next day Buy/Sell Probability/Signal',
46
+ inputs = gr.Textbox(label="News Headline/s", info='Separate several news headlines by a space'),
47
+ outputs=gr.Textbox(show_label = True,label="Prediction", info='This is the probability to buy at market close today and sell market close tomorrow'),
48
+ submit_btn = 'Predict')
49
+
50
  # Launch the app
51
  if __name__ == '__main__':
52
+ app.launch(share=True)
53
+