import tensorflow.keras.backend as K from tensorflow.keras.layers import LSTM from pickle import load import numpy as np import tensorflow as tf import gradio as gr model_V2 = 'ByteLevelLM.h5' K.clear_session() tf.keras.backend.clear_session() np.random.seed(42) tf.random.set_seed(42) HeNormal = tf.keras.initializers.he_normal() daily_V2 = tf.keras.models.load_model(model_V2, custom_objects={'HeNormal': HeNormal},compile=False) #Tokenizer def tokenize(): import json with open('Tokenizer.json', encoding='utf-8') as f: data = json.load(f) tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data) with open('index2char.json', encoding='utf-8') as f: index2char = json.load(f) char2index = dict((int(v),int(k)) for k,v in index2char.items()) tokenizer.word_index = char2index return tokenizer def model2_preds(news_headline_input): headline = news_headline_input headline = '' + headline + '<\s' tokenizer = tokenize() sample_2 = headline.encode('utf-8') sample_2 = tokenizer.texts_to_sequences([sample_2]) predict_v2 = daily_V2.predict(sample_2, verbose = 0)[0,0] # app_type = ui_display(title = "Model 2 Predictions (256 Bits Embeddings)") return "Probability of Buy Signal from News Headline/s: %f" % predict_v2 # Create an instance of the Gradio Interface application function with the appropriate parameters. app = gr.Interface(fn=model2_preds, title="Event Driven Trading (Byte Level Language Modelling)", description='News headlines from OverNight concatenated for next day Buy/Sell Probability/Signal', inputs = gr.Textbox(label="News Headline/s", info='Separate several news headlines by a space'), outputs=gr.Textbox(show_label = True,label="Prediction", info='This is the probability to buy at market close today and sell market close tomorrow'), submit_btn = 'Predict') # Launch the app if __name__ == '__main__': app.launch(share=True)