-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
221 lines (187 loc) · 8.34 KB
/
app.py
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
import streamlit as st
import yfinance as yf
import matplotlib.pyplot as plt
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
import time
# Custom CSS Styling
st.markdown("""
<style>
.chat-agent { font-weight: bold; color: #ffcc00; }
.status-indicator { margin-left: 10px; font-size: 0.8em; }
.completed { color: #00cc00; }
.processing { color: #ffcc00; }
.metric-box { padding: 10px; background: #2e2e2e; border-radius: 5px; margin: 5px 0; }
</style>
""", unsafe_allow_html=True)
st.title("📊 Personal Financial Advisor Agent")
st.caption("🔍 Get stock market trends, company insights, and investment advice!")
# Define AI Agents with dedicated models
AGENTS = {
"market_analyst": {
"system_prompt": "You are MarketAnalyst. Provide key stock market trends.",
"icon": "📈",
"model": "llama3.2" # Default model
},
"company_researcher": {
"system_prompt": "You are CompanyResearcher. Analyze financials & risks.",
"icon": "🏢",
"model": "deepseek-r1:1.5b"
},
"investment_strategist": {
"system_prompt": "You are InvestmentStrategist. Recommend investments.",
"icon": "💰",
"model": "llama3.2"
}
}
# Initialize LLM engines for all agents
LLM_ENGINES = {
agent_name: ChatOllama(
model=config["model"],
base_url="http://localhost:11434",
temperature=0.3
) for agent_name, config in AGENTS.items()
}
# Sidebar Configuration
with st.sidebar:
st.header("⚙️ Advisor Configuration")
# Model Selection for each agent
st.markdown("### Agent Models")
for agent_name, config in AGENTS.items():
new_model = st.selectbox(
label=f"{config['icon']} {agent_name.replace('_', ' ').title()}",
options=["llama3.2", "deepseek-r1:1.5b"],
index=0 if config["model"] == "llama3.2" else 1,
key=f"model_{agent_name}"
)
AGENTS[agent_name]["model"] = new_model
st.divider()
# Performance Metrics
st.markdown("### 🚀 Agent Performance")
if "performance" not in st.session_state:
st.session_state.performance = {
agent: {"count": 0, "total_time": 0, "last_time": 0}
for agent in AGENTS.keys()
}
for agent, metrics in st.session_state.performance.items():
avg_time = metrics["total_time"] / metrics["count"] if metrics["count"] > 0 else 0
st.markdown(f"""
<div class="metric-box">
{AGENTS[agent]['icon']} {agent.replace('_', ' ').title()}<br>
📊 Last: {metrics['last_time']:.2f}s<br>
⚡ Avg: {avg_time:.2f}s<br>
🎯 Calls: {metrics['count']}
</div>
""", unsafe_allow_html=True)
if st.button("🧹 Clear Metrics"):
st.session_state.performance = {
agent: {"count": 0, "total_time": 0, "last_time": 0}
for agent in AGENTS.keys()
}
st.rerun()
# Session State Management
if "message_log" not in st.session_state:
st.session_state.message_log = [{"role": "ai", "content": "Hello! I’m your financial advisor. How can I help you today? 📊"}]
if "stock_plot" not in st.session_state:
st.session_state.stock_plot = None
# Display Chat Messages
chat_container = st.container()
with chat_container:
for message in st.session_state.message_log:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Generate Response from AI Agents
def generate_agent_response(agent_name, user_query, context=""):
start_time = time.time()
# Reinitialize engine with current model selection
llm_engine = ChatOllama(
model=AGENTS[agent_name]["model"],
base_url="http://localhost:11434",
temperature=0.3
)
prompt = ChatPromptTemplate.from_messages([
("system", AGENTS[agent_name]["system_prompt"]),
("human", f"User Query: {user_query}\n\nContext: {context}")
])
chain = prompt | llm_engine | StrOutputParser()
response = chain.invoke({})
# Update performance metrics
duration = time.time() - start_time
st.session_state.performance[agent_name]["count"] += 1
st.session_state.performance[agent_name]["total_time"] += duration
st.session_state.performance[agent_name]["last_time"] = duration
return response
# Fetch Stock Market Data & Generate Plots
def get_stock_info(ticker):
stock = yf.Ticker(ticker)
hist = stock.history(period="6mo")
fig, ax = plt.subplots()
ax.plot(hist.index, hist["Close"], label="Stock Price", color='blue')
ax.plot(hist.index, hist["Close"].rolling(window=50).mean(), label="50-day MA", color='red', linestyle='dashed')
ax.plot(hist.index, hist["Close"].rolling(window=200).mean(), label="200-day MA", color='green', linestyle='dashed')
ax.set_title(f"{ticker} Stock Price & Moving Averages")
ax.set_xlabel("Date")
ax.set_ylabel("Price (Local Currency)")
ax.legend()
st.session_state.stock_plot = fig
info = stock.info
return f"- **{info.get('longName', 'Unknown')} ({ticker})**\n" \
f"- 📊 Price: {info.get('regularMarketPrice', 'N/A')} {info.get('currency', 'N/A')}\n" \
f"- 📉 52W Low: {info.get('fiftyTwoWeekLow', 'N/A')} {info.get('currency', 'N/A')}\n" \
f"- 📈 52W High: {info.get('fiftyTwoWeekHigh', 'N/A')} {info.get('currency', 'N/A')}\n" \
f"- 💰 Market Cap: {info.get('marketCap', 'N/A')} {info.get('currency', 'N/A')}\n" \
f"- 📅 Earnings: {info.get('earningsDate', 'N/A')}"
# Process User Query
user_query = st.chat_input("Ask your financial query here...")
if user_query:
st.session_state.message_log.append({"role": "user", "content": user_query})
ticker = None
words = user_query.split()
for word in words:
if len(word) >= 2 and (word.isupper() or "." in word): # Support for international tickers
ticker = word
break
with st.spinner("📊 Analyzing market trends and investment opportunities..."):
# Initialize execution tracking
st.session_state.completed_agents = []
steps = 3 if ticker else 2
current_step = 1
# Market Analyst
st.session_state.current_agent = "MarketAnalyst"
st.session_state.status_message = "Analyzing market trends..."
st.session_state.progress = (current_step/steps)*100
market_trends = generate_agent_response("market_analyst", user_query)
st.session_state.completed_agents.append("MarketAnalyst")
current_step += 1
# Investment Strategist
st.session_state.current_agent = "InvestmentStrategist"
st.session_state.status_message = "Generating investment advice..."
st.session_state.progress = (current_step/steps)*100
investment_advice = generate_agent_response("investment_strategist", user_query, market_trends)
st.session_state.completed_agents.append("InvestmentStrategist")
current_step += 1
# Company Researcher (if applicable)
if ticker:
st.session_state.current_agent = "CompanyResearcher"
st.session_state.status_message = "Analyzing company data..."
st.session_state.progress = (current_step/steps)*100
stock_info = get_stock_info(ticker)
company_analysis = generate_agent_response("company_researcher", f"Analyze {ticker}", stock_info)
st.session_state.completed_agents.append("CompanyResearcher")
else:
stock_info = ""
company_analysis = ""
# Finalize processing
st.session_state.current_agent = None
st.session_state.status_message = "Analysis complete!"
st.session_state.progress = 100
# Compile Final Response
final_response = f"""
- {AGENTS['market_analyst']['icon']} **Market Trends**\n - {market_trends}\n
- {AGENTS['investment_strategist']['icon']} **Investment Advice**\n - {investment_advice}\n
- {AGENTS['company_researcher']['icon']} **Company Analysis**\n {stock_info}\n {company_analysis}\n"""
st.session_state.message_log.append({"role": "ai", "content": final_response})
if st.session_state.stock_plot:
st.pyplot(st.session_state.stock_plot)
st.rerun()