Spaces:
Sleeping
Sleeping
Commit
·
cccec5f
1
Parent(s):
093b44b
introduced app.py: a gradio app for interacting with the agent
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from src.stock_analysis_agent import StockAnalyst
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def initialize_agent():
|
| 6 |
+
print(f'Initializing Stock-Analysis Agent')
|
| 7 |
+
# Initialize RAG instance
|
| 8 |
+
try:
|
| 9 |
+
stock_analyst = StockAnalyst()
|
| 10 |
+
message = "Stock-Analysis Agent initialized successfully."
|
| 11 |
+
except Exception as e:
|
| 12 |
+
stock_analyst = None
|
| 13 |
+
message = f"Error initializing Stock-Analysis Agent:\n{e}"
|
| 14 |
+
return message, stock_analyst
|
| 15 |
+
|
| 16 |
+
def interact_with_agent(agent_instance, ticker):
|
| 17 |
+
if agent_instance is None:
|
| 18 |
+
return 'Stock-Analysis Agent is not initialized. Please initialize first.'
|
| 19 |
+
messages = agent_instance.get_stock_suggestion(ticker)
|
| 20 |
+
final_message = messages[3].model_dump()['content']
|
| 21 |
+
return final_message
|
| 22 |
+
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown("# Stock Analysis Agent")
|
| 25 |
+
# State to store the RAG instance
|
| 26 |
+
message, stock_analyst = initialize_agent()
|
| 27 |
+
agent_instance = gr.State(None)
|
| 28 |
+
init_button = gr.Button("Initialize")
|
| 29 |
+
init_status = gr.Textbox(label="Initialization Status", interactive=False)
|
| 30 |
+
# Event handlers
|
| 31 |
+
init_button.click(
|
| 32 |
+
initialize_agent,
|
| 33 |
+
outputs=[init_status, agent_instance] # Output: status message and the RAG instance
|
| 34 |
+
)
|
| 35 |
+
# gr.Text(label='Status', value=message)
|
| 36 |
+
gr.Markdown("Enter a stock symbol ('ticker') to be analyzed (GOOG, MSFT, etc.)")
|
| 37 |
+
ticker = gr.Textbox(label="ticker")
|
| 38 |
+
analyze_button = gr.Button('Analyze Stock')
|
| 39 |
+
output = gr.Textbox(label="Output Box")
|
| 40 |
+
analyze_button.click(
|
| 41 |
+
fn=interact_with_agent,
|
| 42 |
+
inputs=[agent_instance, ticker],
|
| 43 |
+
outputs=output,
|
| 44 |
+
api_name="analyze_stock")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|
| 49 |
+
|