|
|
import streamlit as st |
|
|
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer |
|
|
import yfinance as yf |
|
|
import daal4py as d4p |
|
|
import numpy as np |
|
|
from sklearnex import patch_sklearn |
|
|
|
|
|
|
|
|
patch_sklearn() |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
def load_sentiment_model(): |
|
|
return pipeline("sentiment-analysis", model="huggingface/llama-3b-instruct") |
|
|
|
|
|
|
|
|
@st.cache_resource |
|
|
def load_llama_model(): |
|
|
model_name = "meta-llama/Llama-2-7b-chat-hf" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
return tokenizer, model |
|
|
|
|
|
|
|
|
def fetch_stock_data(symbol): |
|
|
ticker = yf.Ticker(symbol) |
|
|
data = ticker.history(period="1mo") |
|
|
return data |
|
|
|
|
|
|
|
|
def compute_moving_average(prices, window=5): |
|
|
|
|
|
price_array = np.array(prices, dtype=np.float64).reshape(-1, 1) |
|
|
|
|
|
|
|
|
algorithm = d4p.low_order_moments() |
|
|
|
|
|
|
|
|
moving_averages = [] |
|
|
for i in range(len(price_array) - window + 1): |
|
|
window_data = price_array[i:i + window] |
|
|
result = algorithm.compute(window_data) |
|
|
moving_averages.append(result.mean[0]) |
|
|
|
|
|
return moving_averages |
|
|
|
|
|
|
|
|
def technical_analysis(symbol): |
|
|
data = fetch_stock_data(symbol) |
|
|
|
|
|
if not data.empty: |
|
|
|
|
|
closing_prices = data['Close'].tolist() |
|
|
dates = data.index.strftime('%Y-%m-%d').tolist() |
|
|
|
|
|
|
|
|
moving_averages = compute_moving_average(closing_prices) |
|
|
|
|
|
|
|
|
latest_date = dates[-1] |
|
|
latest_price = closing_prices[-1] |
|
|
latest_moving_average = moving_averages[-1] if moving_averages else "N/A" |
|
|
|
|
|
return { |
|
|
"Date": latest_date, |
|
|
"Closing Price": latest_price, |
|
|
"5-Day Moving Average": latest_moving_average |
|
|
} |
|
|
return {} |
|
|
|
|
|
|
|
|
def main(): |
|
|
st.title("Stock Analysis App with Yahoo Finance & Intel oneDAL") |
|
|
st.write(""" |
|
|
This app provides a comprehensive stock analysis including: |
|
|
- Sentiment Analysis of recent news |
|
|
- Technical Analysis (Prices, Moving Average using Intel oneDAL) |
|
|
- Buy/Sell/Hold Recommendations |
|
|
""") |
|
|
|
|
|
|
|
|
company_symbol = st.text_input("Enter the stock symbol (e.g., AAPL, TSLA, GOOGL):") |
|
|
|
|
|
if company_symbol: |
|
|
try: |
|
|
|
|
|
stock_data = fetch_stock_data(company_symbol) |
|
|
|
|
|
if not stock_data.empty: |
|
|
|
|
|
st.subheader("Asset Overview") |
|
|
st.dataframe(stock_data.tail()) |
|
|
|
|
|
|
|
|
with st.expander("Technical Analysis (Intel oneDAL)"): |
|
|
st.subheader("Technical Analysis") |
|
|
tech_analysis = technical_analysis(company_symbol) |
|
|
st.write(tech_analysis) |
|
|
|
|
|
with st.expander("Sentiment Analysis"): |
|
|
st.subheader("Sentiment Analysis") |
|
|
sentiment_model = load_sentiment_model() |
|
|
sentiment = sentiment_model(company_symbol) |
|
|
st.write(sentiment) |
|
|
|
|
|
with st.expander("Recommendation"): |
|
|
st.subheader("Recommendation") |
|
|
tokenizer, llama_model = load_llama_model() |
|
|
stock_recommendation = recommendation(company_symbol, tokenizer, llama_model) |
|
|
st.write(stock_recommendation) |
|
|
else: |
|
|
st.error(f"No data available for the symbol entered.") |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"An error occurred: {e}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|