Adding the Machine Learning Module or the Deep Learning Module
if you have not created a algorithm to deploy or implement into your flask app you can follow on previous section.
So for this part I have taken some jupyter notebook code or added my trained model, & generalized into function call here Add Machine Learning Module (Monte Carlo simulation).
Create a models folder

Inside of the models folder we will create a models folder where we will create python file that will be our module for calling our machine learning algorithms, tensorflow trained models, or other statistical tools
For example in this code we have preprocessing data step before passing data to our model to ensure it can handle unusually use cases then the tensorflow model is called on the flask app server to run on the data and return the results which we can save to the database.

we will import out libraries
import tensorflow as tf
import pandas as pd
import numpy as np
don’t for get to pip install libraries
pip install tensorflow
pip install pandas
pip install numpy
then pip freeze your library because when you deploy your flask app again at the end you want to make sure your prototype is installing the right libraries on deployment server.
pip freeze > requirements.txt
First we are going create one function that calls with 3 other functions.
As I explained before this call_model function is going just ETL our
Preprocess –> Model –> Load results
def call_model(data):
# Step 1: Preprocess the messy data
data_for_model = load_and_preprocess_data(data)
# Step 2: we load model and predict & rearrange the data
new_results = apply_model(data_for_model)
# Step 3: Save the organized data to data base
output_file_path = r'E:\NittanyAI Projects\Nittany-AI-Rapid-Prototyping-Code\back-end-prototype\results'
save_data(new_results, output_file_path)
Your python file should look like this

This call_model will be called in our flask app route Hello World every time someone hits the endpoint we will call model to run on the google cloud server.
Your app.py file should look like this
import os
from flask import Flask
from dotenv import load_dotenv
from flask_cors import CORS
from models.model_call import call_model
#Load environment variables
load_dotenv()
# Initialize Flask App
app = Flask(__name__)
# then CORS class is used to wrap the app object to add CORS headers to all responses from the app
CORS(app)
app.config['DEBUG'] = os.environ.get('DEBUG')
API_KEY = os.environ.get("API_KEY")
@app.route("/")
def hello():
return render_template('index.html')
@app.route("/call-model", methods=['POST'])
def call_model_route():
form_data = request.form
model_result = call_model(API_KEY, form_data)
return "Test"
if __name__ == '__main__':
app.run(debug=True)
You can see that we are calling the function call_model on our endpoint
Now we need to write the other 3 functions above that will handle our ETL. Entry Transform Load process. Functions
- load_and_preprocess_data
- apply_model
- save_data
Load & Preprocess data function
Then we add our Load_and_preprocess_data(data): function
def load_and_preprocess_data(data):
# Load the data
return data # assuming coords is the preprocessed data
inside our load_and_preprocess_data(data): function we can load data, edit so it fits the models parameters correctly, or make API calls. we going call the Alpha Advantage API and structure our data later on. so we will can back and fill in that later.
You should see the file as this

Apply Model Function
Use the Monte Carlo simulation for the tutorial other code snippets are just examples
In this function apply_model we can load a already trained tensorflow model for example below this what that looks like. We call tensorflow library and its class tf to load the model.h5. Model Format: The .h5 extension indicates that the file is stored in the HDF5 (Hierarchical Data Format version 5) format. HDF5 is a data model, library, and file format for storing and managing data, and it’s widely used for handling large amounts of data and complex data objects.
def apply_model(data_for_model, model_path='my_model.h5'):
# Load the model
model = tf.keras.models.load_model(model_path)
# Get the new coordinates from the model
new_results = model.predict(data_for_model)
return new_results
Or call machine learning algorithms like K-means learns. to get access to latest python machine learning algorithms I would check sci-kitlearn library
def apply_model(data, num_clusters):
"""
Applies k-means clustering to the given data using the specified number of clusters.
Parameters:
data (array-like): The data to be clustered.
num_clusters (int): The number of clusters to form.
Returns:
array: The cluster labels for each data point.
"""
kmeans = KMeans(n_clusters=num_clusters, random_state=0)
kmeans.fit(data)
return kmeans.labels_
For the step by step were going to use Monte Carlo simulation to predict Dollar Cost Averaging into specific stocks. Monte Carlo Simulation can be apply to many problems is a computational technique that uses random sampling to approximate complex mathematical or physical systems.
Here model scenarios of stock price movement with significant uncertainty and predict outcomes by simulating many different possible situations.
Here’s a metaphor to explain it easier:
Imagine a vast forest with countless paths, each leading to different destinations. A Monte Carlo simulation is like sending out a swarm of birds to explore these paths, each bird choosing a route at random. By observing where most birds gather, we can predict the most likely destination, despite the complexity and multitude of possible paths.
So we will need to have two functions every time Monte Carlo Simulation ireates in the loop we have to calculate dca return as a “possibility”.
def apply_model(stock_data, num_iter, dollars, num_months):
# dca_simulation SPY 12-month simulation
stock_data = stock_data
num_iter = int(num_iter)
dollars = float(dollars)
num_months = int(num_months)
ticker_sim_dat = []
for k in range(num_iter):
# caluations for simulation
x = calculate_dca_return(stock_data, dollars, num_months)
ticker_sim_dat.append(100*x)
# print("iterating", k)
return ticker_sim_dat
So everytime the Monte Carlo Simulation iterates it has to calculate a possible return for DCA.
def calculate_dca_return(stock_data, monthly_investment, months_to_invest):
"""
Dollar Cost Averaging (DCA) Monte Carlo Simulation Implementation
Invest $1000 per month in ETF. Once every month, randomly choose one day to purchase the maximum number of shares allowed with available buying power
Calculate the return of dollar-cost averaging on a stock.
Parameters:
stock_data (DataFrame): Historical stock data.
monthly_investment (float): Amount invested each month.
months_to_invest (int): Number of months over which investments are made.
Returns:
float: ROI (Return on Investment)
"""
# Ensure 'Date' column is in datetime format
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
# Generate a range of dates for analysis
start_date = stock_data['Date'].min()
end_date = stock_data['Date'].max() - pd.DateOffset(months=months_to_invest)
date_range = pd.date_range(start=start_date, end=end_date, freq='M')
# Randomly select a start month for investing
invest_start_date = np.random.choice(date_range)
# Initialize investment tracking variables
total_investment = 0
total_value = 0
shares_owned = 0
# Loop over each month to calculate investment progress
for month_count in range(months_to_invest):
current_date = invest_start_date + pd.DateOffset(months=month_count)
current_stock_data = stock_data[stock_data['Date'].dt.to_period('M') == current_date.to_period('M')]
if not current_stock_data.empty:
# Randomly select a trading day in the month
random_trading_day = current_stock_data.sample()
# Get closing price and calculate number of shares bought
closing_price = float(random_trading_day['close'].iloc[0])
shares_bought = monthly_investment / closing_price
# Update investment totals
total_investment += monthly_investment
shares_owned += shares_bought
total_value = shares_owned * closing_price
# Calculate ROI
roi = (total_value - total_investment) / total_investment
return roi
Save data
Now we have function after model runs to structure our data past back to the flask app which will pass it to the front-end
def save_data(organized_data, output_file_path):
with pd.ExcelWriter(output_file_path) as writer:
organized_data.to_excel(writer)
print("successfully saved")
So your code model_call.py should like this what’s in this paste bin
You app.py should look like this right now were going past the API_KEY to model_call.py functions.
API_KEY = os.environ.get('API_KEY')
@app.route('/')
def hello(data):
# here we can past inputs from the frontend to our model
model_result = call_model(API_KEY, data)
print(model_result)
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
As you notice that load_and_preprocess function has new parameters API_KEY. That means we will need to update the call_model functions by adding new parameter API_KEY. call model function call should l ike this below, where were passing a API_KEY
def call_model(API_KEY, data):
# Step 1: Preprocess the messy data
data_for_model = load_and_preprocess_data(API_KEY, data)
# Step 2: we load model and predict & rearrange the data
new_results = apply_model(data_for_model)
# Step 3: Save the organized data to data base
output_file_path = r'E:\NittanyAI Projects\Nittany-AI-Rapid-Prototyping-Code\back-end-prototype\results'
save_data(new_results, output_file_path)
back in the load_and_process_data function under the python script called model_call.py we place our test code
def load_and_preprocess_data(data):
# Load the data
return data # assuming coords is the preprocessed data
Should now be by calling the time_series_fun_monthly
from services.api_calls import time_series_fun_monthly
def load_and_preprocess_data(API_KEY, data):
# Load the data
ticker = data['ticker']
data = time_series_fun_monthly(API_KEY, ticker)
return data # assuming coords is the preprocessed data
Now finally that we implemented the Monte Carlo algorithm into the flask app we are going to need UI inputs to test. This is not the official UI its just to test the logic and functions making sure we are getting returns from each step so go to next section.

[…] that we have Monte Carlo algorithm implemented, and if you missed that you can go to previous section to do […]