Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import
|
| 4 |
import daal4py as d4p # Intel DAAL
|
| 5 |
import numpy as np
|
| 6 |
-
import
|
| 7 |
-
from sklearnex import patch_sklearn, config_context
|
| 8 |
-
patch_sklearn()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
# Initialize Hugging Face's sentiment analysis pipeline
|
| 14 |
@st.cache_resource
|
|
@@ -23,16 +21,15 @@ def load_llama_model():
|
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 24 |
return tokenizer, model
|
| 25 |
|
| 26 |
-
# Fetch stock data using
|
| 27 |
def fetch_stock_data(symbol):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
|
| 32 |
# Compute Moving Average using Intel oneDAL
|
| 33 |
def compute_moving_average(prices, window=5):
|
| 34 |
# Convert prices to a NumPy array and reshape it for DAAL
|
| 35 |
-
import numpy as np
|
| 36 |
price_array = np.array(prices, dtype=np.float64).reshape(-1, 1)
|
| 37 |
|
| 38 |
# Initialize Intel DAAL low-order moments algorithm (for moving average)
|
|
@@ -47,22 +44,22 @@ def compute_moving_average(prices, window=5):
|
|
| 47 |
|
| 48 |
return moving_averages
|
| 49 |
|
| 50 |
-
# Perform technical analysis using
|
| 51 |
def technical_analysis(symbol):
|
| 52 |
data = fetch_stock_data(symbol)
|
| 53 |
|
| 54 |
-
if data:
|
| 55 |
-
# Extract closing prices from the
|
| 56 |
-
closing_prices = [
|
| 57 |
-
dates =
|
| 58 |
|
| 59 |
# Compute 5-day moving average using oneDAL
|
| 60 |
moving_averages = compute_moving_average(closing_prices)
|
| 61 |
|
| 62 |
# Display latest date's price and moving average
|
| 63 |
-
latest_date = dates[
|
| 64 |
-
latest_price = closing_prices[
|
| 65 |
-
latest_moving_average = moving_averages[
|
| 66 |
|
| 67 |
return {
|
| 68 |
"Date": latest_date,
|
|
@@ -73,11 +70,10 @@ def technical_analysis(symbol):
|
|
| 73 |
|
| 74 |
# Streamlit Web App
|
| 75 |
def main():
|
| 76 |
-
st.title("Stock Analysis App with Intel oneDAL")
|
| 77 |
st.write("""
|
| 78 |
This app provides a comprehensive stock analysis including:
|
| 79 |
- Sentiment Analysis of recent news
|
| 80 |
-
- Fundamental Analysis (Market Cap, PE Ratio, EPS)
|
| 81 |
- Technical Analysis (Prices, Moving Average using Intel oneDAL)
|
| 82 |
- Buy/Sell/Hold Recommendations
|
| 83 |
""")
|
|
@@ -87,13 +83,13 @@ def main():
|
|
| 87 |
|
| 88 |
if company_symbol:
|
| 89 |
try:
|
| 90 |
-
# Fetch stock data from
|
| 91 |
stock_data = fetch_stock_data(company_symbol)
|
| 92 |
|
| 93 |
-
if stock_data:
|
| 94 |
# Display the fetched stock overview
|
| 95 |
st.subheader("Asset Overview")
|
| 96 |
-
st.
|
| 97 |
|
| 98 |
# Split the sections into different boxes using Streamlit's expander
|
| 99 |
with st.expander("Technical Analysis (Intel oneDAL)"):
|
|
@@ -104,7 +100,7 @@ def main():
|
|
| 104 |
with st.expander("Sentiment Analysis"):
|
| 105 |
st.subheader("Sentiment Analysis")
|
| 106 |
sentiment_model = load_sentiment_model()
|
| 107 |
-
sentiment =
|
| 108 |
st.write(sentiment)
|
| 109 |
|
| 110 |
with st.expander("Recommendation"):
|
|
@@ -119,4 +115,4 @@ def main():
|
|
| 119 |
st.error(f"An error occurred: {e}")
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
| 122 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import yfinance as yf
|
| 4 |
import daal4py as d4p # Intel DAAL
|
| 5 |
import numpy as np
|
| 6 |
+
from sklearnex import patch_sklearn
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Apply scikit-learn optimizations
|
| 9 |
+
patch_sklearn()
|
| 10 |
|
| 11 |
# Initialize Hugging Face's sentiment analysis pipeline
|
| 12 |
@st.cache_resource
|
|
|
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 22 |
return tokenizer, model
|
| 23 |
|
| 24 |
+
# Fetch stock data using Yahoo Finance
|
| 25 |
def fetch_stock_data(symbol):
|
| 26 |
+
ticker = yf.Ticker(symbol)
|
| 27 |
+
data = ticker.history(period="1mo") # Fetch 1 month of historical data
|
| 28 |
+
return data
|
| 29 |
|
| 30 |
# Compute Moving Average using Intel oneDAL
|
| 31 |
def compute_moving_average(prices, window=5):
|
| 32 |
# Convert prices to a NumPy array and reshape it for DAAL
|
|
|
|
| 33 |
price_array = np.array(prices, dtype=np.float64).reshape(-1, 1)
|
| 34 |
|
| 35 |
# Initialize Intel DAAL low-order moments algorithm (for moving average)
|
|
|
|
| 44 |
|
| 45 |
return moving_averages
|
| 46 |
|
| 47 |
+
# Perform technical analysis using Yahoo Finance and oneDAL
|
| 48 |
def technical_analysis(symbol):
|
| 49 |
data = fetch_stock_data(symbol)
|
| 50 |
|
| 51 |
+
if not data.empty:
|
| 52 |
+
# Extract closing prices from the fetched data
|
| 53 |
+
closing_prices = data['Close'].tolist()
|
| 54 |
+
dates = data.index.strftime('%Y-%m-%d').tolist()
|
| 55 |
|
| 56 |
# Compute 5-day moving average using oneDAL
|
| 57 |
moving_averages = compute_moving_average(closing_prices)
|
| 58 |
|
| 59 |
# Display latest date's price and moving average
|
| 60 |
+
latest_date = dates[-1]
|
| 61 |
+
latest_price = closing_prices[-1]
|
| 62 |
+
latest_moving_average = moving_averages[-1] if moving_averages else "N/A"
|
| 63 |
|
| 64 |
return {
|
| 65 |
"Date": latest_date,
|
|
|
|
| 70 |
|
| 71 |
# Streamlit Web App
|
| 72 |
def main():
|
| 73 |
+
st.title("Stock Analysis App with Yahoo Finance & Intel oneDAL")
|
| 74 |
st.write("""
|
| 75 |
This app provides a comprehensive stock analysis including:
|
| 76 |
- Sentiment Analysis of recent news
|
|
|
|
| 77 |
- Technical Analysis (Prices, Moving Average using Intel oneDAL)
|
| 78 |
- Buy/Sell/Hold Recommendations
|
| 79 |
""")
|
|
|
|
| 83 |
|
| 84 |
if company_symbol:
|
| 85 |
try:
|
| 86 |
+
# Fetch stock data from Yahoo Finance API
|
| 87 |
stock_data = fetch_stock_data(company_symbol)
|
| 88 |
|
| 89 |
+
if not stock_data.empty:
|
| 90 |
# Display the fetched stock overview
|
| 91 |
st.subheader("Asset Overview")
|
| 92 |
+
st.dataframe(stock_data.tail()) # Show the last few rows of the data
|
| 93 |
|
| 94 |
# Split the sections into different boxes using Streamlit's expander
|
| 95 |
with st.expander("Technical Analysis (Intel oneDAL)"):
|
|
|
|
| 100 |
with st.expander("Sentiment Analysis"):
|
| 101 |
st.subheader("Sentiment Analysis")
|
| 102 |
sentiment_model = load_sentiment_model()
|
| 103 |
+
sentiment = sentiment_model(company_symbol) # Sentiment analysis on the stock name
|
| 104 |
st.write(sentiment)
|
| 105 |
|
| 106 |
with st.expander("Recommendation"):
|
|
|
|
| 115 |
st.error(f"An error occurred: {e}")
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
main()
|