dibend's picture
Update app.py
0c9d63c
# Import libraries
import yfinance as yf
import tensorflow as tf
import numpy as np
import gradio as gr
from IPython.display import display
# Set up TensorBoard log directory
log_dir = 'tmp'
# Define the function to predict asset prices
def predict_asset(ticker):
# Download a year's worth of data for the asset the user entered
data = yf.download(ticker, period='5y')
# Normalize the data using TensorFlow's Keras utility
matrix = tf.keras.utils.normalize(data.values)
# Create a sequential machine learning model using TensorFlow's Keras library
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(128, activation='relu', input_shape=(matrix.shape[1],)))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
# Create TensorBoard callback
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir)
# Train the model using the normalized data and the asset prices
model.fit(matrix, data['Close'], epochs=100, callbacks=[tensorboard_callback])
# Use the trained model to predict prices for the asset
predicted_prices = model.predict(matrix)
predicted_prices = np.array(predicted_prices).flatten()
# Calculate the average predicted price
mean_predicted_price = np.mean(predicted_prices)
current_price = data['Close'][-1]
percent_change = (mean_predicted_price - current_price) / current_price * 100
# Set variables to control the color and wording of the output
color = 'green' if percent_change >= 0 else 'red'
increase_decrease = 'increase' if percent_change >= 0 else 'decrease'
# Return the formatted output as a string
return '<h1>' + ticker + ' Predicted Prices 5 years in the future:</h1><br />' + str(predicted_prices) + \
'<br /><br /><h1>' + ticker + ' Average Predicted Price 5 years in the Future:<br />&dollar;' + str(mean_predicted_price) + \
'<br /><br />' + ticker + ' Current Price:<br />&dollar;' + str(current_price) + \
'<br /><br />' + ticker + ' Percent Change from Current Price to Average Predicted Price 5 Years in the Future:<br /><span style="color: ' + color + '">' + \
str(percent_change) + '% ' + increase_decrease + '</span></h1>'
# Create the Gradio interface
interface = gr.Interface(
fn=predict_asset,
inputs="text",
outputs="html",
title="Asset Price Prediction",
description="""This program uses Deep Learning to predict the price of any asset on Yahoo Finance 5 years in the future if 5 years of historical data are available, or for the length of the price data history in the future.
Disclosures
1. Purpose
This code is designed to estimate the price of a user-inputted asset in 5 years. It is publicly available and intended for informational and educational purposes only.
2. Data Sources
The code utilizes the yfinance library to access data from Yahoo Finance. The user acknowledges that the use of Yahoo Finance data is subject to Yahoo's terms of service, and the author of this code does not have any specific agreement with Yahoo Finance.
3. User Input
Users are responsible for providing valid input in the form of a ticker symbol. The code does not validate the input, and incorrect or invalid input may lead to errors or inaccurate predictions.
4. Predictive Model Disclaimer
The predictions generated by this code are based on limited historical data and a mathematical model. They are provided for informational purposes only and should not be construed as financial or investment advice. Users should consult with a qualified financial professional before making any investment decisions.
5. Licensing
This code is publicly available under the GNU General Public License v3.0 (GPL-3.0). Users must comply with the terms of this license when using, modifying, or distributing the code.
6. Risk Factors
Users are cautioned that the predictions generated by this code are speculative and may not accurately reflect future asset prices. Reliance on these predictions for investment decisions may result in financial loss. The author of this code is not responsible for any investment decisions or financial losses incurred by users.
7. Privacy
No personal or sensitive information is collected, stored, or processed by this code.
8. Limitation of Liability
The author of this code makes no warranties or representations regarding the accuracy, reliability, or completeness of the predictions or any other information provided by this code. Users use this code at their own risk, and the author shall not be liable for any direct, indirect, incidental, or consequential damages arising from its use.""",
)
# Launch the interface
interface.launch(share=False, debug=True)