Nush88 commited on
Commit
2cb0e1b
·
verified ·
1 Parent(s): b619997

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
+ import requests
4
+ import daal4py as d4p # Intel DAAL
5
+ import numpy as np
6
+ import dpctl
7
+ from sklearnex import patch_sklearn, config_context
8
+ patch_sklearn()
9
+
10
+ # Alpha Vantage API Setup (replace with your API key)
11
+ ALPHA_VANTAGE_API_KEY = "your_alpha_vantage_api_key"
12
+
13
+ # Initialize Hugging Face's sentiment analysis pipeline
14
+ @st.cache_resource
15
+ def load_sentiment_model():
16
+ return pipeline("sentiment-analysis", model="huggingface/llama-3b-instruct")
17
+
18
+ # Load LLaMA model for custom recommendations or Q&A
19
+ @st.cache_resource
20
+ def load_llama_model():
21
+ model_name = "meta-llama/Llama-2-7b-chat-hf" # Adjust this to your preferred model
22
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
23
+ model = AutoModelForCausalLM.from_pretrained(model_name)
24
+ return tokenizer, model
25
+
26
+ # Fetch stock data using Alpha Vantage
27
+ def fetch_stock_data(symbol):
28
+ url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={ALPHA_VANTAGE_API_KEY}"
29
+ response = requests.get(url)
30
+ return response.json().get("Time Series (Daily)", {})
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)
39
+ algorithm = d4p.low_order_moments()
40
+
41
+ # Apply rolling window and calculate moving averages
42
+ moving_averages = []
43
+ for i in range(len(price_array) - window + 1):
44
+ window_data = price_array[i:i + window]
45
+ result = algorithm.compute(window_data)
46
+ moving_averages.append(result.mean[0])
47
+
48
+ return moving_averages
49
+
50
+ # Perform technical analysis using Alpha Vantage and oneDAL
51
+ def technical_analysis(symbol):
52
+ data = fetch_stock_data(symbol)
53
+
54
+ if data:
55
+ # Extract closing prices from the time series data
56
+ closing_prices = [float(v['4. close']) for v in data.values()]
57
+ dates = list(data.keys())
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[0]
64
+ latest_price = closing_prices[0]
65
+ latest_moving_average = moving_averages[0] if moving_averages else "N/A"
66
+
67
+ return {
68
+ "Date": latest_date,
69
+ "Closing Price": latest_price,
70
+ "5-Day Moving Average": latest_moving_average
71
+ }
72
+ return {}
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
+ """)
84
+
85
+ # Input: Stock symbol of a public listed company
86
+ company_symbol = st.text_input("Enter the stock symbol (e.g., AAPL, TSLA, GOOGL):")
87
+
88
+ if company_symbol:
89
+ try:
90
+ # Fetch stock data from Alpha Vantage API
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.json(stock_data)
97
+
98
+ # Split the sections into different boxes using Streamlit's expander
99
+ with st.expander("Technical Analysis (Intel oneDAL)"):
100
+ st.subheader("Technical Analysis")
101
+ tech_analysis = technical_analysis(company_symbol)
102
+ st.write(tech_analysis)
103
+
104
+ with st.expander("Sentiment Analysis"):
105
+ st.subheader("Sentiment Analysis")
106
+ sentiment_model = load_sentiment_model()
107
+ sentiment = sentiment_analysis(company_symbol, sentiment_model)
108
+ st.write(sentiment)
109
+
110
+ with st.expander("Recommendation"):
111
+ st.subheader("Recommendation")
112
+ tokenizer, llama_model = load_llama_model()
113
+ stock_recommendation = recommendation(company_symbol, tokenizer, llama_model)
114
+ st.write(stock_recommendation)
115
+ else:
116
+ st.error(f"No data available for the symbol entered.")
117
+
118
+ except Exception as e:
119
+ st.error(f"An error occurred: {e}")
120
+
121
+ if __name__ == "__main__":
122
+ main()