Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,30 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
import os
|
| 6 |
-
import
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 10 |
st.set_page_config(
|
| 11 |
page_title="Financial Analysis AI Agent",
|
| 12 |
page_icon="πΉ",
|
| 13 |
-
layout="
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Custom CSS for center alignment
|
| 17 |
-
st.markdown("""
|
| 18 |
-
<style>
|
| 19 |
-
.reportview-container .main .block-container {
|
| 20 |
-
text-align: center;
|
| 21 |
-
}
|
| 22 |
-
.stTextInput > div > div > input {
|
| 23 |
-
text-align: center;
|
| 24 |
-
}
|
| 25 |
-
.stSelectbox > div > div > div {
|
| 26 |
-
text-align: center;
|
| 27 |
-
}
|
| 28 |
-
</style>
|
| 29 |
-
""", unsafe_allow_html=True)
|
| 30 |
-
|
| 31 |
# Initialize Groq Client
|
| 32 |
def get_groq_client():
|
| 33 |
try:
|
| 34 |
-
# Try to get API key from
|
| 35 |
-
groq_api_key =
|
| 36 |
|
| 37 |
if not groq_api_key:
|
| 38 |
-
st.error("Groq API Key is missing. Please set
|
| 39 |
return None
|
| 40 |
|
| 41 |
return Groq(api_key=groq_api_key)
|
|
@@ -69,6 +58,74 @@ def get_stock_info(symbol):
|
|
| 69 |
st.error(f"Error fetching stock information: {e}")
|
| 70 |
return None
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Fetch News Using DuckDuckGo
|
| 73 |
def get_duckduckgo_news(symbol, limit=5):
|
| 74 |
try:
|
|
@@ -138,68 +195,74 @@ def main():
|
|
| 138 |
st.title("π Advanced Financial Insight AI")
|
| 139 |
st.markdown("Comprehensive stock analysis with DuckDuckGo news search")
|
| 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 |
-
# Display
|
| 179 |
-
st.subheader("
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
st.
|
| 191 |
-
st.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Disclaimer
|
| 197 |
-
st.markdown("
|
| 198 |
-
st.warning(
|
| 199 |
"π¨ Disclaimer: This is an AI-generated analysis. "
|
| 200 |
"Always consult with a financial advisor before making investment decisions."
|
| 201 |
)
|
| 202 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 203 |
|
| 204 |
# Run the Streamlit app
|
| 205 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
+
import plotly.graph_objs as plt
|
| 5 |
from groq import Groq
|
| 6 |
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Streamlit App Configuration
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="Financial Analysis AI Agent",
|
| 16 |
page_icon="πΉ",
|
| 17 |
+
layout="wide"
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Initialize Groq Client
|
| 21 |
def get_groq_client():
|
| 22 |
try:
|
| 23 |
+
# Try to get API key from environment variables
|
| 24 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 25 |
|
| 26 |
if not groq_api_key:
|
| 27 |
+
st.error("Groq API Key is missing. Please set GROQ_API_KEY in your .env file.")
|
| 28 |
return None
|
| 29 |
|
| 30 |
return Groq(api_key=groq_api_key)
|
|
|
|
| 58 |
st.error(f"Error fetching stock information: {e}")
|
| 59 |
return None
|
| 60 |
|
| 61 |
+
# Fetch Historical Stock Data and Calculate Volatility
|
| 62 |
+
def get_stock_volatility(symbol, period='1y'):
|
| 63 |
+
try:
|
| 64 |
+
# Download historical stock data
|
| 65 |
+
stock_data = yf.download(symbol, period=period)
|
| 66 |
+
|
| 67 |
+
if stock_data.empty:
|
| 68 |
+
st.error(f"No historical data found for {symbol}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
# Calculate daily returns
|
| 72 |
+
stock_data['Daily Returns'] = stock_data['Close'].pct_change()
|
| 73 |
+
|
| 74 |
+
# Calculate rolling volatility (30-day standard deviation of returns)
|
| 75 |
+
stock_data['Rolling Volatility'] = stock_data['Daily Returns'].rolling(window=30).std() * (252 ** 0.5) # Annualized
|
| 76 |
+
|
| 77 |
+
return stock_data
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error fetching historical stock data: {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
# Create Volatility Visualization
|
| 83 |
+
def create_volatility_chart(stock_data):
|
| 84 |
+
try:
|
| 85 |
+
# Create figure with two y-axes
|
| 86 |
+
fig = plt.Figure()
|
| 87 |
+
|
| 88 |
+
# Price line
|
| 89 |
+
fig.add_trace(
|
| 90 |
+
plt.Scatter(
|
| 91 |
+
x=stock_data.index,
|
| 92 |
+
y=stock_data['Close'],
|
| 93 |
+
name='Stock Price',
|
| 94 |
+
line=dict(color='blue'),
|
| 95 |
+
yaxis='y1'
|
| 96 |
+
)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Volatility line
|
| 100 |
+
fig.add_trace(
|
| 101 |
+
plt.Scatter(
|
| 102 |
+
x=stock_data.index,
|
| 103 |
+
y=stock_data['Rolling Volatility'],
|
| 104 |
+
name='30-Day Volatility',
|
| 105 |
+
line=dict(color='red'),
|
| 106 |
+
yaxis='y2'
|
| 107 |
+
)
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Layout configuration
|
| 111 |
+
fig.update_layout(
|
| 112 |
+
title=f'Stock Price and Volatility',
|
| 113 |
+
xaxis_title='Date',
|
| 114 |
+
yaxis_title='Stock Price',
|
| 115 |
+
yaxis2=dict(
|
| 116 |
+
title='Annualized Volatility',
|
| 117 |
+
overlaying='y',
|
| 118 |
+
side='right'
|
| 119 |
+
),
|
| 120 |
+
height=600,
|
| 121 |
+
legend=dict(x=0, y=1.1, orientation='h')
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
return fig
|
| 125 |
+
except Exception as e:
|
| 126 |
+
st.error(f"Error creating volatility chart: {e}")
|
| 127 |
+
return None
|
| 128 |
+
|
| 129 |
# Fetch News Using DuckDuckGo
|
| 130 |
def get_duckduckgo_news(symbol, limit=5):
|
| 131 |
try:
|
|
|
|
| 195 |
st.title("π Advanced Financial Insight AI")
|
| 196 |
st.markdown("Comprehensive stock analysis with DuckDuckGo news search")
|
| 197 |
|
| 198 |
+
# Sidebar Configuration
|
| 199 |
+
st.sidebar.header("π Stock Analysis")
|
| 200 |
|
| 201 |
+
# Stock Symbol Input
|
| 202 |
+
stock_symbol = st.sidebar.text_input(
|
| 203 |
+
"Enter Stock Symbol",
|
| 204 |
+
value="NVDA",
|
| 205 |
+
help="Enter a valid stock ticker (e.g., AAPL, GOOGL)"
|
| 206 |
+
)
|
|
|
|
| 207 |
|
| 208 |
+
# Analysis Type Selection
|
| 209 |
+
query_type = st.sidebar.selectbox(
|
| 210 |
+
"Select Analysis Type",
|
| 211 |
+
[
|
| 212 |
+
"Comprehensive Analysis",
|
| 213 |
+
"Analyst Recommendations",
|
| 214 |
+
"Latest News Analysis"
|
| 215 |
+
]
|
| 216 |
+
)
|
| 217 |
|
| 218 |
+
# Generate Analysis Button
|
| 219 |
+
if st.sidebar.button("Generate Analysis"):
|
| 220 |
+
with st.spinner("Fetching and analyzing stock data..."):
|
| 221 |
+
try:
|
| 222 |
+
# Fetch Stock Information
|
| 223 |
+
stock_info = get_stock_info(stock_symbol)
|
| 224 |
+
|
| 225 |
+
if stock_info:
|
| 226 |
+
# Display Stock Information
|
| 227 |
+
st.subheader(f"Financial Snapshot: {stock_symbol}")
|
| 228 |
+
info_df = pd.DataFrame.from_dict(stock_info, orient='index', columns=['Value'])
|
| 229 |
+
st.table(info_df)
|
| 230 |
+
|
| 231 |
+
# Fetch and Display Volatility Chart
|
| 232 |
+
volatility_data = get_stock_volatility(stock_symbol)
|
| 233 |
+
if volatility_data is not None:
|
| 234 |
+
# Create and Display Volatility Chart
|
| 235 |
+
st.subheader("π Stock Price and Volatility")
|
| 236 |
+
volatility_chart = create_volatility_chart(volatility_data)
|
| 237 |
+
st.plotly_chart(volatility_chart, use_container_width=True)
|
| 238 |
+
|
| 239 |
+
# Fetch News via DuckDuckGo
|
| 240 |
+
real_time_news = get_duckduckgo_news(stock_symbol)
|
| 241 |
+
|
| 242 |
+
# Display News
|
| 243 |
+
st.subheader("π° Latest News")
|
| 244 |
+
for news in real_time_news:
|
| 245 |
+
st.markdown(f"**{news['title']}**")
|
| 246 |
+
st.markdown(f"*Source: {news['publisher']}*")
|
| 247 |
+
st.markdown(f"[Read more]({news['link']})")
|
| 248 |
+
st.markdown("---")
|
| 249 |
+
|
| 250 |
+
# Generate AI Analysis
|
| 251 |
+
ai_analysis = generate_ai_analysis(stock_info, real_time_news, query_type)
|
| 252 |
+
|
| 253 |
+
# Display AI Analysis
|
| 254 |
+
st.subheader("π€ AI-Powered Insights")
|
| 255 |
+
st.write(ai_analysis)
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
st.error(f"An error occurred: {e}")
|
| 259 |
|
| 260 |
# Disclaimer
|
| 261 |
+
st.sidebar.markdown("---")
|
| 262 |
+
st.sidebar.warning(
|
| 263 |
"π¨ Disclaimer: This is an AI-generated analysis. "
|
| 264 |
"Always consult with a financial advisor before making investment decisions."
|
| 265 |
)
|
|
|
|
| 266 |
|
| 267 |
# Run the Streamlit app
|
| 268 |
if __name__ == "__main__":
|