Spaces:
Sleeping
Sleeping
Commit
·
80d9402
1
Parent(s):
7935906
app: auto initialization
Browse files
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
|
| 20 |
-
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
print(f"Stock-Analysis Agent initialized successfully.")
|
| 24 |
except Exception as e:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return
|
| 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 |
-
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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=
|
| 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=[
|
| 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=[
|
| 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 |
|