candenizkocak commited on
Commit
cb2c9c5
·
1 Parent(s): adb1948

Initial commit

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +69 -0
  3. requirements.txt +3 -0
  4. tools.py +127 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: YFinanceAgents
3
- emoji: 🐠
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: gradio
 
1
  ---
2
  title: YFinanceAgents
3
+ emoji: 📈
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+
5
+ genai.configure(api_key=os.environ['GEMINI_API_KEY'])
6
+
7
+ from tools import analyze_financials, analyze_stock, analyze_analysts_estimatations, analyze_news
8
+
9
+ functions = {
10
+ "analyze_financials": analyze_financials,
11
+ "analyze_stock": analyze_stock,
12
+ "analyze_analysts_estimatations": analyze_analysts_estimatations,
13
+ "analyze_news": analyze_news,
14
+ }
15
+
16
+ model = genai.GenerativeModel(
17
+ model_name="gemini-1.5-pro",
18
+ tools=functions.values(),
19
+ system_instruction="You are an expert in financial analysis. Given a set of financial statements of a company, I ask you to analyze the company. If analyze_stock function is called determine the period from one of the items in the list depending on the request ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']."
20
+ )
21
+
22
+ chat_session = model.start_chat(history=[])
23
+
24
+ def call_function(function_call, functions):
25
+ function_name = function_call.name
26
+ function_args = function_call.args
27
+ return functions[function_name](**function_args)
28
+
29
+ def chat_with_bot(user_input, history=[]):
30
+ # Send user message to model
31
+ response = chat_session.send_message(user_input)
32
+ part = response.candidates[0].content.parts[0]
33
+
34
+ function_result = ""
35
+ if part.function_call:
36
+ function_result = call_function(part.function_call, functions)
37
+ function_name = part.function_call.name
38
+ bot_response = f"Function `{function_name}` was called. Result:\n\n{function_result}"
39
+ else:
40
+ bot_response = part.text
41
+
42
+ history.append((user_input, bot_response))
43
+ return history, history
44
+
45
+ examples = [
46
+ "Summarize analysts views on GOOG.",
47
+ "Analyze last month's stock data of NVDA.",
48
+ "Provide a brief overview of MSFT news.",
49
+ "Analyze financials of META in detail."
50
+ ]
51
+
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("### Financial Analysis Chatbot")
54
+
55
+ chatbot = gr.Chatbot()
56
+ state = gr.State([])
57
+
58
+ with gr.Row():
59
+ with gr.Column(scale=4):
60
+ msg_input = gr.Textbox(label="Your message", placeholder="Ask something...", lines=1)
61
+ with gr.Column(scale=1):
62
+ send_button = gr.Button("Send")
63
+
64
+ gr.Examples(examples=examples, inputs=msg_input, fn=chat_with_bot, outputs=[chatbot, state], cache_examples=False)
65
+
66
+ send_button.click(chat_with_bot, inputs=[msg_input, state], outputs=[chatbot, state])
67
+ msg_input.submit(chat_with_bot, inputs=[msg_input, state], outputs=[chatbot, state])
68
+
69
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ google-generativeai==0.8.3
2
+ yfinance==0.2.49
3
+ gradio==5.5.0
tools.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ import yfinance as yf
3
+
4
+ def analyze_financials(company: str, user_input: str):
5
+ ticker = yf.Ticker(company)
6
+ info = ticker.info
7
+ calendar = ticker.calendar
8
+ sec_filings = ticker.sec_filings
9
+ income_stmt = ticker.income_stmt
10
+ quarterly_income_stmt = ticker.quarterly_income_stmt
11
+ balance_sheet = ticker.balance_sheet
12
+ quarterly_balance_sheet = ticker.quarterly_balance_sheet
13
+ cashflow = ticker.cashflow
14
+ quarterly_cashflow = ticker.quarterly_cashflow
15
+
16
+ analyst_generation_config = {
17
+ "temperature": 0.1,
18
+ "top_p": 0.95,
19
+ "top_k": 40,
20
+ "max_output_tokens": 8192,
21
+ "response_mime_type": "text/plain",
22
+ }
23
+
24
+ analyst_model = genai.GenerativeModel(
25
+ model_name="gemini-1.5-pro",
26
+ generation_config=analyst_generation_config,
27
+ system_instruction="You are an expert financial analyst. Given a set of financial statements of a company, I ask you to analyze the company. You will have access to ticker name, calendar, sec filings, income statement, quarterly income statement, balance sheet, quarterly balance sheet, cashflow, quarterly cashflow."
28
+ )
29
+
30
+ analyst_chat_session = analyst_model.start_chat(
31
+ history=[
32
+ ]
33
+ )
34
+
35
+ analyst_response = analyst_chat_session.send_message(f"Given the following data answer the question {user_input}\n, {ticker}, {info}, {calendar}, {sec_filings}, {income_stmt}, {quarterly_income_stmt}, {balance_sheet}, {quarterly_balance_sheet}, {cashflow}, {quarterly_cashflow}")
36
+
37
+ return analyst_response.text
38
+
39
+ def analyze_stock(company: str, period: str, user_input: str):
40
+ ticker = yf.Ticker(company)
41
+ info = ticker.info
42
+ hist = ticker.history(period=period)
43
+ hist_metadata = ticker.history_metadata
44
+
45
+ stock_generation_config = {
46
+ "temperature": 0.1,
47
+ "top_p": 0.95,
48
+ "top_k": 40,
49
+ "max_output_tokens": 8192,
50
+ "response_mime_type": "text/plain",
51
+ }
52
+
53
+ stock_model = genai.GenerativeModel(
54
+ model_name="gemini-1.5-pro",
55
+ generation_config=stock_generation_config,
56
+ system_instruction="You are an expert financial analyst. Given a set of financial statements of a company, I ask you to analyze the company. You will have access to ticker name, company info, historical data."
57
+ )
58
+
59
+ stock_chat_session = stock_model.start_chat(
60
+ history=[
61
+ ]
62
+ )
63
+
64
+ stock_response = stock_chat_session.send_message(f"Given the following data answer the question {user_input}\n, {info}, {hist}, {hist_metadata}")
65
+
66
+ return stock_response.text
67
+
68
+ def analyze_analysts_estimatations(company: str, user_input: str):
69
+ ticker = yf.Ticker(company)
70
+ info = ticker.info
71
+ analyst_price_targets = ticker.analyst_price_targets
72
+ earnings_estimate = ticker.earnings_estimate
73
+ revenue_estimate = ticker.revenue_estimate
74
+ earnings_history = ticker.earnings_history
75
+ eps_trend = ticker.eps_trend
76
+ eps_revisions = ticker.eps_revisions
77
+ growth_estimates = ticker.growth_estimates
78
+
79
+ analysts_generation_config = {
80
+ "temperature": 0.1,
81
+ "top_p": 0.95,
82
+ "top_k": 40,
83
+ "max_output_tokens": 8192,
84
+ "response_mime_type": "text/plain",
85
+ }
86
+
87
+ analysts_model = genai.GenerativeModel(
88
+ model_name="gemini-1.5-pro",
89
+ generation_config=analysts_generation_config,
90
+ system_instruction="You are an expert financial analyst. Given a set of financial statements of a company, I ask you to analyze the company. You will have access to ticker name, company info, analyst price targets, earnings estimate, revenue estimate, earnings history, eps trend, eps revisions, growth estimates."
91
+ )
92
+
93
+ analysts_chat_session = analysts_model.start_chat(
94
+ history=[
95
+ ]
96
+ )
97
+
98
+ analysts_response = analysts_chat_session.send_message(f"Given the following data answer the question {user_input}\n, {info}, {analyst_price_targets}, {earnings_estimate}, {revenue_estimate}, {earnings_history}, {eps_trend}, {eps_revisions}, {growth_estimates}")
99
+
100
+ return analysts_response.text
101
+
102
+ def analyze_news(company: str, user_input: str):
103
+ ticker = yf.Ticker(company)
104
+ news = ticker.news
105
+
106
+ news_generation_config = {
107
+ "temperature": 0.1,
108
+ "top_p": 0.95,
109
+ "top_k": 40,
110
+ "max_output_tokens": 8192,
111
+ "response_mime_type": "text/plain",
112
+ }
113
+
114
+ news_model = genai.GenerativeModel(
115
+ model_name="gemini-1.5-pro",
116
+ generation_config=news_generation_config,
117
+ system_instruction="You are an expert financial analyst. Given a set of financial news on company, I ask you to analyze the company. You will have access to ticker name, news on yahoo finance."
118
+ )
119
+
120
+ news_chat_session = news_model.start_chat(
121
+ history=[
122
+ ]
123
+ )
124
+
125
+ news_response = news_chat_session.send_message(f"Given the following data answer the question {user_input}\n, {news}")
126
+
127
+ return news_response.text