OnurKerimoglu commited on
Commit
80d9402
·
1 Parent(s): 7935906

app: auto initialization

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -16,15 +16,15 @@ def initialize_manual():
16
  message = f"Error initializing Stock-Analysis Agent:\n{e}\n"
17
  return message, stock_analyst
18
 
19
- def initialize_auto():
20
- print(f'Initializing Stock-Analysis Agent')
 
21
  try:
22
- stock_analyst = StockAnalyst()
23
- print(f"Stock-Analysis Agent initialized successfully.")
24
  except Exception as e:
25
- stock_analyst = None
26
- print(f"Error initializing Stock-Analysis Agent:\n{e}")
27
- return stock_analyst
28
 
29
  def ask_stock_agent(agent_instance, ticker):
30
  if agent_instance is None:
@@ -66,27 +66,31 @@ def plot_stock(agent_instance, ticker):
66
  with gr.Blocks() as demo:
67
  gr.Markdown("# Stock Analysis Agent")
68
  # State to store the agent
69
- # auto-initialize (not working)
70
- # stock_analyst = initialize_auto()
71
- # agent_instance = gr.State(stock_analyst)
72
- # manual-initialize with button
73
- message, stock_analyst = initialize_manual() # is this needed?
74
- agent_instance = gr.State(None)
75
- init_status = gr.Textbox(label="Initialization Status", interactive=False)
76
- init_button = gr.Button("Initialize")
77
- # Event handlers
78
- init_button.click(
79
- initialize_manual,
80
- outputs=[init_status, agent_instance] # Output: status message and the agent
81
  )
 
 
 
 
 
 
 
 
82
  print('Agent initialized successfully')
83
  # TickerFinder tool
84
  ticker_to_find = gr.Textbox(label="Ticker Finder: not sure what's the stock ticker symbol? Enter the company name you want to analyze, and I will try to help.")
85
  tf_output = gr.Markdown(label="Match Results")
86
  search_button = gr.Button("Search")
87
  search_button.click(
88
- fn=stock_analyst.tickerfinder.find_best_matching_ticker_or_title,
89
- inputs=[ticker_to_find],
90
  outputs=[tf_output],
91
  api_name="ticker_finder"
92
  )
@@ -96,13 +100,13 @@ with gr.Blocks() as demo:
96
  md_output = gr.Markdown(label="Output Box")
97
  analyze_button.click(
98
  fn=ask_stock_agent,
99
- inputs=[agent_instance, ticker],
100
  outputs=md_output,
101
  api_name="ask_stock_agent")
102
  plot_output = gr.Plot(label=ticker, format="png")
103
  analyze_button.click(
104
  fn=plot_stock,
105
- inputs=[agent_instance, ticker],
106
  outputs=[plot_output],
107
  api_name="plot_stock")
108
 
 
16
  message = f"Error initializing Stock-Analysis Agent:\n{e}\n"
17
  return message, stock_analyst
18
 
19
+ def find_matching_tickers(agent_instance, ticker):
20
+ if agent_instance is None:
21
+ return 'Stock-Analysis Agent is not initialized. Please initialize first.'
22
  try:
23
+ df = agent_instance.tickerfinder.find_best_matching_ticker_or_title(ticker)
 
24
  except Exception as e:
25
+ print(f'Error finding matching tickers for {ticker}: :\n{e}')
26
+ df = None
27
+ return df
28
 
29
  def ask_stock_agent(agent_instance, ticker):
30
  if agent_instance is None:
 
66
  with gr.Blocks() as demo:
67
  gr.Markdown("# Stock Analysis Agent")
68
  # State to store the agent
69
+ stock_analyst_state = gr.State()
70
+ # Auto-initialize
71
+ init_status = gr.Textbox(label="Init Status", interactive=False, value="Initializing…")
72
+ # Initialize automatically when the page loads
73
+ demo.load(
74
+ fn=initialize_manual,
75
+ inputs=None,
76
+ outputs=[init_status, stock_analyst_state]
 
 
 
 
77
  )
78
+ # manual-initialize with button
79
+ # message, stock_analyst = initialize_manual() # is this needed?
80
+ # init_status = gr.Textbox(label="Initialization Status", interactive=False)
81
+ # init_button = gr.Button("Initialize")
82
+ # init_button.click(
83
+ # fn=initialize_manual,
84
+ # outputs=[init_status, stock_analyst_state] # Output: status message and the agent
85
+ # )
86
  print('Agent initialized successfully')
87
  # TickerFinder tool
88
  ticker_to_find = gr.Textbox(label="Ticker Finder: not sure what's the stock ticker symbol? Enter the company name you want to analyze, and I will try to help.")
89
  tf_output = gr.Markdown(label="Match Results")
90
  search_button = gr.Button("Search")
91
  search_button.click(
92
+ fn=find_matching_tickers,
93
+ inputs=[stock_analyst_state, ticker_to_find],
94
  outputs=[tf_output],
95
  api_name="ticker_finder"
96
  )
 
100
  md_output = gr.Markdown(label="Output Box")
101
  analyze_button.click(
102
  fn=ask_stock_agent,
103
+ inputs=[stock_analyst_state, ticker],
104
  outputs=md_output,
105
  api_name="ask_stock_agent")
106
  plot_output = gr.Plot(label=ticker, format="png")
107
  analyze_button.click(
108
  fn=plot_stock,
109
+ inputs=[stock_analyst_state, ticker],
110
  outputs=[plot_output],
111
  api_name="plot_stock")
112