OnurKerimoglu commited on
Commit
b1228ac
·
1 Parent(s): a267ed2

app: check whether ticker exists before calls

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -4,43 +4,57 @@ from src.stock_analysis_agent import StockAnalyst
4
  from src.technical_analysis import TechnicalAnalysis
5
 
6
 
7
- def initialize_agent():
8
  print(f'Initializing Stock-Analysis Agent')
9
- # Initialize RAG instance
10
  try:
11
  stock_analyst = StockAnalyst()
12
- message = "Stock-Analysis Agent initialized successfully."
13
  except Exception as e:
14
  stock_analyst = None
15
- message = f"Error initializing Stock-Analysis Agent:\n{e}"
16
  return message, stock_analyst
17
 
18
  def ask_stock_agent(agent_instance, ticker):
19
  if agent_instance is None:
20
  return 'Stock-Analysis Agent is not initialized. Please initialize first.'
21
- response = agent_instance.get_formatted_stock_summary(ticker)
 
 
 
 
 
 
 
 
22
  return response
23
 
24
- def plot_stock(ticker):
25
- # run the technical analysis
26
- _, fig = TechnicalAnalysis(
27
- ticker=ticker,
28
- fetchperiodinweeks=12,
29
- plot_ta=True,
30
- savefig=False,
31
- debug=False).run()
 
 
 
 
 
 
 
32
  return fig
33
 
34
  with gr.Blocks() as demo:
35
  gr.Markdown("# Stock Analysis Agent")
36
  # State to store the RAG instance
37
- message, stock_analyst = initialize_agent()
38
- agent_instance = gr.State(None)
39
  init_button = gr.Button("Initialize")
40
  init_status = gr.Textbox(label="Initialization Status", interactive=False)
41
  # Event handlers
42
  init_button.click(
43
- initialize_agent,
44
  outputs=[init_status, agent_instance] # Output: status message and the RAG instance
45
  )
46
  # gr.Text(label='Status', value=message)
@@ -51,7 +65,7 @@ with gr.Blocks() as demo:
51
  plot_output = gr.Plot(label=ticker, format="png")
52
  analyze_button.click(
53
  fn=plot_stock,
54
- inputs=[ticker],
55
  outputs=[plot_output],
56
  api_name="plot_stock")
57
  md_output = gr.Markdown(label="Output Box")
 
4
  from src.technical_analysis import TechnicalAnalysis
5
 
6
 
7
+ def initialize():
8
  print(f'Initializing Stock-Analysis Agent')
 
9
  try:
10
  stock_analyst = StockAnalyst()
11
+ message = "Stock-Analysis Agent initialized successfully.\n"
12
  except Exception as e:
13
  stock_analyst = None
14
+ message = f"Error initializing Stock-Analysis Agent:\n{e}\n"
15
  return message, stock_analyst
16
 
17
  def ask_stock_agent(agent_instance, ticker):
18
  if agent_instance is None:
19
  return 'Stock-Analysis Agent is not initialized. Please initialize first.'
20
+ ticker_exists = agent_instance.tickerfinder.does_ticker_exist(ticker)
21
+ if ticker_exists:
22
+ response = agent_instance.get_formatted_stock_summary(ticker)
23
+ else:
24
+ response = f"""
25
+ Ticker symbol {ticker} is not listed by the U.S. Securities and Exchange Commission database,
26
+ see: https://www.sec.gov/files/company_tickers.json.
27
+ If you are not sure about the ticker symbol use the Ticker-Finder tool to find the correct ticker.
28
+ """
29
  return response
30
 
31
+ def plot_stock(agent_instance, ticker):
32
+ if agent_instance is None:
33
+ return 'Stock-Analysis Agent is not initialized. Please initialize first.'
34
+ ticker_exists = agent_instance.tickerfinder.does_ticker_exist(ticker)
35
+ if ticker_exists:
36
+ # run the technical analysis
37
+ _, fig = TechnicalAnalysis(
38
+ ticker=ticker,
39
+ fetchperiodinweeks=12,
40
+ plot_ta=True,
41
+ savefig=False,
42
+ debug=False).run()
43
+ else:
44
+ message = f"""Ticker {ticker} does not exist"""
45
+ fig = TechnicalAnalysis(ticker).get_fetcherror_fig(message)
46
  return fig
47
 
48
  with gr.Blocks() as demo:
49
  gr.Markdown("# Stock Analysis Agent")
50
  # State to store the RAG instance
51
+ message, stock_analyst = initialize()
52
+ agent_instance = gr.State(None)
53
  init_button = gr.Button("Initialize")
54
  init_status = gr.Textbox(label="Initialization Status", interactive=False)
55
  # Event handlers
56
  init_button.click(
57
+ initialize,
58
  outputs=[init_status, agent_instance] # Output: status message and the RAG instance
59
  )
60
  # gr.Text(label='Status', value=message)
 
65
  plot_output = gr.Plot(label=ticker, format="png")
66
  analyze_button.click(
67
  fn=plot_stock,
68
+ inputs=[agent_instance, ticker],
69
  outputs=[plot_output],
70
  api_name="plot_stock")
71
  md_output = gr.Markdown(label="Output Box")