muskangoyal06 commited on
Commit
4c527e5
·
verified ·
1 Parent(s): b7c802f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
+ import requests
4
+ import daal4py as d4p # Intel DAAL
5
+
6
+ # Alpha Vantage API Setup (replace with your API key)
7
+ ALPHA_VANTAGE_API_KEY = "your_alpha_vantage_api_key"
8
+
9
+ # Initialize Hugging Face's sentiment analysis pipeline
10
+ @st.cache_resource
11
+ def load_sentiment_model():
12
+ return pipeline("sentiment-analysis", model="huggingface/llama-3b-instruct")
13
+
14
+ # Load LLaMA model for custom recommendations or Q&A
15
+ @st.cache_resource
16
+ def load_llama_model():
17
+ model_name = "meta-llama/Llama-2-7b-chat-hf" # Adjust this to your preferred model
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
19
+ model = AutoModelForCausalLM.from_pretrained(model_name)
20
+ return tokenizer, model
21
+
22
+ # Fetch stock data using Alpha Vantage
23
+ def fetch_stock_data(symbol):
24
+ url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={ALPHA_VANTAGE_API_KEY}"
25
+ response = requests.get(url)
26
+ return response.json().get("Time Series (Daily)", {})
27
+
28
+ # Compute Moving Average using Intel oneDAL
29
+ def compute_moving_average(prices, window=5):
30
+ # Convert prices to a NumPy array and reshape it for DAAL
31
+ import numpy as np
32
+ price_array = np.array(prices, dtype=np.float64).reshape(-1, 1)
33
+
34
+ # Initialize Intel DAAL low-order moments algorithm (for moving average)
35
+ algorithm = d4p.low_order_moments()
36
+
37
+ # Apply rolling window and calculate moving averages
38
+ moving_averages = []
39
+ for i in range(len(price_array) - window + 1):
40
+ window_data = price_array[i:i + window]
41
+ result = algorithm.compute(window_data)
42
+ moving_averages.append(result.mean[0])
43
+
44
+ return moving_averages
45
+
46
+ # Perform technical analysis using Alpha Vantage and oneDAL
47
+ def technical_analysis(symbol):
48
+ data = fetch_stock_data(symbol)
49
+
50
+ if data:
51
+ # Extract closing prices from the time series data
52
+ closing_prices = [float(v['4. close']) for v in data.values()]
53
+ dates = list(data.keys())
54
+
55
+ # Compute 5-day moving average using oneDAL
56
+ moving_averages = compute_moving_average(closing_prices)
57
+
58
+ # Display latest date's price and moving average
59
+ latest_date = dates[0]
60
+ latest_price = closing_prices[0]
61
+ latest_moving_average = moving_averages[0] if moving_averages else "N/A"
62
+
63
+ return {
64
+ "Date": latest_date,
65
+ "Closing Price": latest_price,
66
+ "5-Day Moving Average": latest_moving_average
67
+ }
68
+ return {}
69
+
70
+ # Streamlit Web App
71
+ def main():
72
+ st.title("Stock Analysis App with Intel oneDAL")
73
+ st.write("""
74
+ This app provides a comprehensive stock analysis including:
75
+ - Sentiment Analysis of recent news
76
+ - Fundamental Analysis (Market Cap, PE Ratio, EPS)
77
+ - Technical Analysis (Prices, Moving Average using Intel oneDAL)
78
+ - Buy/Sell/Hold Recommendations
79
+ """)
80
+
81
+ # Input: Stock symbol of a public listed company
82
+ company_symbol = st.text_input("Enter the stock symbol (e.g., AAPL, TSLA, GOOGL):")
83
+
84
+ if company_symbol:
85
+ try:
86
+ # Fetch stock data from Alpha Vantage API
87
+ stock_data = fetch_stock_data(company_symbol)
88
+
89
+ if stock_data:
90
+ # Display the fetched stock overview
91
+ st.subheader("Asset Overview")
92
+ st.json(stock_data)
93
+
94
+ # Split the sections into different boxes using Streamlit's `expander`
95
+ with st.expander("Technical Analysis (Intel oneDAL)"):
96
+ st.subheader("Technical Analysis")
97
+ tech_analysis = technical_analysis(company_symbol)
98
+ st.write(tech_analysis)
99
+
100
+ with st.expander("Sentiment Analysis"):
101
+ st.subheader("Sentiment Analysis")
102
+ sentiment_model = load_sentiment_model()
103
+ sentiment = sentiment_analysis(company_symbol, sentiment_model)
104
+ st.write(sentiment)
105
+
106
+ with st.expander("Recommendation"):
107
+ st.subheader("Recommendation")
108
+ tokenizer, llama_model = load_llama_model()
109
+ stock_recommendation = recommendation(company_symbol, tokenizer, llama_model)
110
+ st.write(stock_recommendation)
111
+ else:
112
+ st.error(f"No data available for the symbol entered.")
113
+
114
+ except Exception as e:
115
+ st.error(f"An error occurred: {e}")
116
+
117
+ if __name__ == "__main__":
118
+ main()