move tensorflow as a lazyload and wrap in main
This commit is contained in:
@@ -1,59 +1,64 @@
|
|||||||
import tensorflow as tf
|
|
||||||
import keras
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
|
from datapuller import DataPuller
|
||||||
|
|
||||||
# Initilize SqLite
|
def main():
|
||||||
dbconn = sqlite3.connect("./data/appdata.db")
|
# Initilize SqLite
|
||||||
dbcursor = dbconn.cursor()
|
dbconn = sqlite3.connect("./data/appdata.db")
|
||||||
|
dbcursor = dbconn.cursor()
|
||||||
|
|
||||||
# Create keystore table if it doesnt exist already
|
# Create keystore table if it doesnt exist already
|
||||||
dbcursor.execute('''
|
dbcursor.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS keystore (
|
CREATE TABLE IF NOT EXISTS keystore (
|
||||||
key TEXT PRIMARY KEY,
|
key TEXT PRIMARY KEY,
|
||||||
value TEXT
|
value TEXT
|
||||||
)
|
)
|
||||||
''')
|
''')
|
||||||
|
|
||||||
def SetKey(Key, Value):
|
def SetKey(Key, Value):
|
||||||
dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
|
dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
|
||||||
dbconn.commit()
|
dbconn.commit()
|
||||||
|
|
||||||
def GetKey(Key):
|
def GetKey(Key):
|
||||||
dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
|
dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
|
||||||
result = dbcursor.fetchone()
|
result = dbcursor.fetchone()
|
||||||
return result[0] if result else None
|
return result[0] if result else None
|
||||||
|
|
||||||
# Pull the last run data
|
# Pull the last run data
|
||||||
lastrundata = GetKey("LastRun")
|
lastrundata = GetKey("LastRun")
|
||||||
lastrun = None
|
lastrun = None
|
||||||
if lastrundata != None:
|
if lastrundata != None:
|
||||||
lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" )
|
lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" )
|
||||||
else:
|
else:
|
||||||
lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" )
|
lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" )
|
||||||
|
|
||||||
# If the strings dont match becuase were only checking day it has to be a new day so update the data
|
# If the strings dont match becuase were only checking day it has to be a new day so update the data
|
||||||
if lastrun != str(date.today()):
|
if lastrun != str(date.today()):
|
||||||
# This will update the data store in the data folder
|
# This will update the data store in the data folder
|
||||||
import datapuller
|
DataPuller.pull()
|
||||||
# Update the last run to today
|
# Update the last run to today
|
||||||
SetKey("LastRun", str(date.today()))
|
SetKey("LastRun", str(date.today()))
|
||||||
|
|
||||||
# Load in the AI algorithm
|
# Load in the AI algorithm late so yfinance works properly
|
||||||
from keras.layers import Dense, Flatten, Conv2D
|
import tensorflow as tf
|
||||||
from keras import Model
|
import keras
|
||||||
|
from keras.layers import Dense, Flatten, Conv2D
|
||||||
|
from keras import Model
|
||||||
|
|
||||||
mnist = keras.datasets.mnist
|
mnist = keras.datasets.mnist
|
||||||
|
|
||||||
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
||||||
x_train, x_test = x_train / 255.0, x_test / 255.0
|
x_train, x_test = x_train / 255.0, x_test / 255.0
|
||||||
|
|
||||||
# Add a channels dimension
|
# Add a channels dimension
|
||||||
x_train = x_train[..., tf.newaxis].astype("float32")
|
x_train = x_train[..., tf.newaxis].astype("float32")
|
||||||
x_test = x_test[..., tf.newaxis].astype("float32")
|
x_test = x_test[..., tf.newaxis].astype("float32")
|
||||||
|
|
||||||
# batch and shuffle the dataset
|
# batch and shuffle the dataset
|
||||||
train_ds = tf.data.Dataset.from_tensor_slices(
|
train_ds = tf.data.Dataset.from_tensor_slices(
|
||||||
(x_train, y_train)).shuffle(10000).batch(32)
|
(x_train, y_train)).shuffle(10000).batch(32)
|
||||||
|
|
||||||
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
|
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user