-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
240 lines (208 loc) · 7.74 KB
/
app.py
File metadata and controls
240 lines (208 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import streamlit as st
import plotly.express as px
import pandas as pd
from stock import (
generate_response,
process_stock,
parallel_process_stocks,
fetch_from_pinecone,
pinecone_index,
vectorstore
)
from datetime import datetime
import requests
import json
# Page configuration
st.set_page_config(
page_title="Stock Market Analysis",
page_icon="📈",
layout="wide"
)
# Custom CSS
st.markdown("""
<style>
.company-card {
background-color: #1E1E1E;
padding: 20px;
border-radius: 10px;
margin: 10px 0;
border: 1px solid #333;
}
.metric-value {
color: #00ff00;
font-size: 20px;
}
.company-name {
color: #ffffff;
font-size: 24px;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
# Function to get company tickers
def get_company_tickers():
url = "https://raw.githubusercontent.com/team-headstart/Financial-Analysis-and-Automation-with-LLMs/main/company_tickers.json"
response = requests.get(url)
if response.status_code == 200:
return json.loads(response.content.decode('utf-8'))
else:
st.error(f"Failed to download file. Status code: {response.status_code}")
return None
# Check if data exists in Pinecone
def check_data_exists():
try:
namespaces = fetch_from_pinecone()
return len(namespaces) > 0
except Exception as e:
st.error(f"Error checking Pinecone index: {e}")
return False
# Initialize session state for data status
if 'data_initialized' not in st.session_state:
st.session_state.data_initialized = check_data_exists()
# Main content
st.title("📊 Stock Market Analysis")
# Show data initialization message if needed
if not st.session_state.data_initialized:
st.warning("⚠️ No stock data found. Please initialize the database first.")
if st.button("Initialize Stock Database"):
with st.spinner("Fetching and storing stock data..."):
company_tickers = get_company_tickers()
if company_tickers:
tickers_to_process = [company_tickers[num]['ticker']
for num in company_tickers.keys()]
# Process all companies
parallel_process_stocks(tickers=tickers_to_process)
st.session_state.data_initialized = True
st.success("✅ Database initialized successfully!")
st.rerun()
else:
st.error("Failed to fetch company tickers")
st.stop()
# Sidebar
with st.sidebar:
st.title("🔍 Search Settings")
sector = st.selectbox(
"Select Sector",
["All", "Technology", "Healthcare", "Financials",
"Consumer Discretionary", "Industrials", "Energy",
"Materials", "Consumer Staples", "Utilities",
"Real Estate", "Communication Services"],
index=0
)
st.subheader("Market Cap Range (Billions $)")
min_cap = st.number_input("Minimum", min_value=0, value=0)
max_cap = st.number_input("Maximum", min_value=0, value=1000)
top_k = st.slider(
"Number of Results",
min_value=1,
max_value=20,
value=10
)
st.subheader("🔄 Data Management")
if st.button("Update Stock Data"):
with st.spinner("Updating stock data..."):
company_tickers = get_company_tickers()
if company_tickers:
tickers_to_process = [company_tickers[num]['ticker']
for num in company_tickers.keys()]
parallel_process_stocks(tickers=tickers_to_process)
st.success("✅ Stock data updated successfully!")
else:
st.error("Failed to fetch company tickers")
st.write("""
Enter your query about stocks and companies. Use natural language to ask about
market caps, sectors, or business descriptions.
""")
# Query section with search button
col1, col2 = st.columns([3, 1])
with col1:
query = st.text_input(
"What would you like to know?",
placeholder="e.g., Show me technology companies with market cap over 100 billion"
)
with col2:
search_button = st.button("🔍 Search", use_container_width=True)
# Process query
if search_button and query:
with st.spinner("Analyzing..."):
filters = {
"sector": sector,
"min_cap": min_cap,
"max_cap": max_cap
}
response = generate_response(query, top_k, filters)
# Parse the response to extract structured data
companies = []
current_company = {}
for line in response.split('\n'):
if '(' in line and ')' in line:
if current_company:
companies.append(current_company)
current_company = {'name': line.strip()}
elif 'Market Cap:' in line:
current_company['market_cap'] = float(line.split(':')[1].strip().replace(',', ''))
elif 'Description:' in line:
current_company['Business Summary'] = line.split(':')[1].strip()
if current_company:
companies.append(current_company)
# Create visualizations
if companies:
# Market Cap Comparison
df = pd.DataFrame(companies)
col1, col2 = st.columns([2, 1])
with col1:
st.subheader("Market Cap Comparison")
fig = px.bar(
df,
x='name',
y='market_cap',
title='Company Market Capitalizations',
labels={'name': 'Company', 'market_cap': 'Market Cap ($B)'},
color='market_cap',
color_continuous_scale='Viridis'
)
fig.update_layout(
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
font_color='white'
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("Market Share")
fig = px.pie(
df,
values='market_cap',
names='name',
title='Market Share Distribution'
)
fig.update_layout(
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
font_color='white'
)
st.plotly_chart(fig, use_container_width=True)
# Detailed Company Cards
st.subheader("Detailed Analysis")
for company in companies:
with st.container():
st.markdown(f"""
<div class="company-card">
<div class="company-name">{company['name']}</div>
<div class="metric-value">Market Cap: ${company['market_cap']:,.2f}B</div>
<p>{company['Business Summary']}</p>
</div>
""", unsafe_allow_html=True)
# Footer with additional metrics
st.markdown("---")
metrics_cols = st.columns(4)
with metrics_cols[0]:
st.metric("Total Companies", len(companies) if 'companies' in locals() else 0)
with metrics_cols[1]:
if 'companies' in locals() and companies:
avg_market_cap = sum(c['market_cap'] for c in companies) / len(companies)
st.metric("Avg Market Cap", f"${avg_market_cap:,.2f}B")
with metrics_cols[2]:
st.markdown(f"**Last Updated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
with metrics_cols[3]:
st.markdown("**Data Source:** Yahoo Finance")
st.markdown("**Coded by :** Sheick with ❤️ ")