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

added ticker finder in the app, match results in html

Browse files
Files changed (2) hide show
  1. app.py +14 -5
  2. src/ticker_finder.py +15 -10
app.py CHANGED
@@ -24,7 +24,7 @@ def ask_stock_agent(agent_instance, ticker):
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
 
@@ -50,17 +50,26 @@ with gr.Blocks() as demo:
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)
61
- gr.Markdown("Enter a stock symbol ('ticker') to be analyzed (GOOG, MSFT, etc.)")
62
- ticker = gr.Textbox(label="ticker")
63
- # plot_button = gr.Button('Plot')
 
 
 
 
 
 
 
 
 
64
  analyze_button = gr.Button('Analyze Stock')
65
  plot_output = gr.Plot(label=ticker, format="png")
66
  analyze_button.click(
 
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 to find the correct ticker.
28
  """
29
  return response
30
 
 
50
  # State to store the RAG instance
51
  message, stock_analyst = initialize()
52
  agent_instance = gr.State(None)
 
53
  init_status = gr.Textbox(label="Initialization Status", interactive=False)
54
+ init_button = gr.Button("Initialize")
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)
61
+ # TickerFinder tool
62
+ 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.")
63
+ tf_output = gr.Markdown(label="Match Results")
64
+ search_button = gr.Button("Search")
65
+ search_button.click(
66
+ fn=stock_analyst.tickerfinder.find_best_matching_ticker_or_title,
67
+ inputs=[ticker_to_find],
68
+ outputs=[tf_output],
69
+ api_name="ticker_finder"
70
+ )
71
+ # Enter stock symbol
72
+ ticker = gr.Textbox(label="Enter a stock ticker symbol to be analyzed (GOOG, MSFT, etc.)")
73
  analyze_button = gr.Button('Analyze Stock')
74
  plot_output = gr.Plot(label=ticker, format="png")
75
  analyze_button.click(
src/ticker_finder.py CHANGED
@@ -98,7 +98,7 @@ class TickerFinder():
98
  def find_best_matching_title(
99
  self,
100
  input_name:str,
101
- top_n=3) -> pd.DataFrame:
102
  """
103
  Find the best matching company title for a given company name.
104
  Args:
@@ -124,7 +124,7 @@ class TickerFinder():
124
  def find_best_matching_ticker(
125
  self,
126
  ticker:str,
127
- top_n:int=3
128
  ) -> pd.DataFrame:
129
  """
130
  Find the best matching company ticker for a given ticker.
@@ -173,12 +173,14 @@ class TickerFinder():
173
  # sort by score:
174
  c_matches_sorted = c_matches_dedup.sort_values(by='Score', ascending=False)
175
  # convert results into a pretty string:
176
- results = self.df_to_pretty_string(c_matches_sorted )
177
  return(results)
178
 
179
  def df_to_pretty_string(
180
  self,
181
- df):
 
 
182
  """
183
  Convert a pd.DataFrame into a pretty string, using the tabulate package.
184
  Args:
@@ -188,13 +190,16 @@ class TickerFinder():
188
  pretty_string : str
189
  A string containing the pretty-formatted dataframe
190
  """
191
- pretty_string = tabulate(df[['Title', 'Ticker']],
192
- # headers='keys',
193
- tablefmt='plain',
 
 
 
194
  showindex=False,
195
  numalign='left',
196
  stralign='left')
197
- return pretty_string
198
 
199
  def does_ticker_exist(
200
  self,
@@ -213,7 +218,7 @@ class TickerFinder():
213
 
214
 
215
  # if __name__ == "__main__":
216
- # results = TickerFinder().find_best_matching_ticker_or_title("microsoft")
217
- # print(results)
218
  # exists = TickerFinder().does_ticker_exist('bbbbb')
219
  # print(f'Ticker exists') if exists else print(f'Ticker does not exist')
 
98
  def find_best_matching_title(
99
  self,
100
  input_name:str,
101
+ top_n=5) -> pd.DataFrame:
102
  """
103
  Find the best matching company title for a given company name.
104
  Args:
 
124
  def find_best_matching_ticker(
125
  self,
126
  ticker:str,
127
+ top_n:int=5
128
  ) -> pd.DataFrame:
129
  """
130
  Find the best matching company ticker for a given ticker.
 
173
  # sort by score:
174
  c_matches_sorted = c_matches_dedup.sort_values(by='Score', ascending=False)
175
  # convert results into a pretty string:
176
+ results = self.df_to_pretty_string(c_matches_sorted)
177
  return(results)
178
 
179
  def df_to_pretty_string(
180
  self,
181
+ df:pd.DataFrame,
182
+ num_rows:int=5
183
+ ) -> str:
184
  """
185
  Convert a pd.DataFrame into a pretty string, using the tabulate package.
186
  Args:
 
190
  pretty_string : str
191
  A string containing the pretty-formatted dataframe
192
  """
193
+ df = df.rename(columns={'Title': 'Company Name', 'Ticker': 'Ticker Symbol'})
194
+ df_subset = df[['Company Name', 'Ticker Symbol']].iloc[0:num_rows]
195
+ pretty_table= tabulate(df_subset,
196
+ headers='keys',
197
+ # tablefmt='plain',
198
+ tablefmt='html',
199
  showindex=False,
200
  numalign='left',
201
  stralign='left')
202
+ return pretty_table
203
 
204
  def does_ticker_exist(
205
  self,
 
218
 
219
 
220
  # if __name__ == "__main__":
221
+ # results = TickerFinder().find_best_matching_ticker_or_title("microsoft")
222
+ # print(results)
223
  # exists = TickerFinder().does_ticker_exist('bbbbb')
224
  # print(f'Ticker exists') if exists else print(f'Ticker does not exist')