move tensorflow as a lazyload and wrap in main

This commit is contained in:
2026-02-11 20:03:25 -08:00
parent 0fcdfc91bf
commit 777483f8bf
+37 -32
View File
@@ -1,59 +1,64 @@
import tensorflow as tf
import keras
import sqlite3
from datetime import date, datetime
from datapuller import DataPuller
# Initilize SqLite
dbconn = sqlite3.connect("./data/appdata.db")
dbcursor = dbconn.cursor()
def main():
# Initilize SqLite
dbconn = sqlite3.connect("./data/appdata.db")
dbcursor = dbconn.cursor()
# Create keystore table if it doesnt exist already
dbcursor.execute('''
CREATE TABLE IF NOT EXISTS keystore (
# Create keystore table if it doesnt exist already
dbcursor.execute('''
CREATE TABLE IF NOT EXISTS keystore (
key TEXT PRIMARY KEY,
value TEXT
)
''')
)
''')
def SetKey(Key, Value):
def SetKey(Key, Value):
dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value))
dbconn.commit()
def GetKey(Key):
def GetKey(Key):
dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, ))
result = dbcursor.fetchone()
return result[0] if result else None
# Pull the last run data
lastrundata = GetKey("LastRun")
lastrun = None
if lastrundata != None:
# Pull the last run data
lastrundata = GetKey("LastRun")
lastrun = None
if lastrundata != None:
lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" )
else:
else:
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 lastrun != str(date.today()):
# 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()):
# This will update the data store in the data folder
import datapuller
DataPuller.pull()
# Update the last run to today
SetKey("LastRun", str(date.today()))
# Load in the AI algorithm
from keras.layers import Dense, Flatten, Conv2D
from keras import Model
# Load in the AI algorithm late so yfinance works properly
import tensorflow as tf
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, x_test = x_train / 255.0, x_test / 255.0
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Add a channels dimension
x_train = x_train[..., tf.newaxis].astype("float32")
x_test = x_test[..., tf.newaxis].astype("float32")
# Add a channels dimension
x_train = x_train[..., tf.newaxis].astype("float32")
x_test = x_test[..., tf.newaxis].astype("float32")
# batch and shuffle the dataset
train_ds = tf.data.Dataset.from_tensor_slices(
# batch and shuffle the dataset
train_ds = tf.data.Dataset.from_tensor_slices(
(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()