smol_agent / app.py
Akshayram1's picture
Update app.py
7bf11dd verified
raw
history blame
3.66 kB
import streamlit as st
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
import yfinance as yf
# Define agents for both functionalities
search_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
blog_agent = CodeAgent(tools=[], model=HfApiModel()) # Replace with blog-specific tools/models if needed
# Function to log agent actions
def log_agent_action(prompt, result, agent_name):
st.write(f"### Agent Activity ({agent_name}):")
st.write("**Prompt Sent to Agent:**")
st.code(prompt, language="text")
st.write("**Agent Output:**")
st.code(result, language="text")
# Function to retrieve stock data using yfinance
def get_stock_info(symbol):
try:
stock = yf.Ticker(symbol)
info = stock.info
return {
"Symbol": info.get("symbol", "N/A"),
"Name": info.get("shortName", "N/A"),
"Current Price": info.get("regularMarketPrice", "N/A"),
"Currency": info.get("currency", "N/A"),
"Previous Close": info.get("regularMarketPreviousClose", "N/A"),
"Market Cap": info.get("marketCap", "N/A"),
}
except Exception as e:
return {"Error": f"Could not retrieve stock information: {e}"}
# Streamlit app title
st.title("AI Agent Hub: Blog Writing & Stock Data Retrieval")
# Main navigation options
st.sidebar.header("Select a Feature")
selected_feature = st.sidebar.radio(
"What would you like to do?",
("Blog Writing Agent", "Stock Data Helper")
)
# Prompt input and execution logic
if selected_feature == "Blog Writing Agent":
st.header("Blog Writing Agent")
blog_prompt = st.text_area("Enter your blog topic or prompt:")
if st.button("Generate Blog Content"):
if blog_prompt:
with st.spinner("Generating blog content..."):
try:
# Run the blog agent
blog_result = blog_agent.run(blog_prompt)
# Display result and log backend activity
st.write("Generated Blog Content:")
st.write(blog_result)
log_agent_action(blog_prompt, blog_result, "Blog Writing Agent")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter a blog prompt to continue.")
elif selected_feature == "Stock Data Helper":
st.header("Stock Data Helper")
stock_prompt = st.text_area("Enter your query (e.g., company name, stock symbol):")
use_yfinance = st.checkbox("Fetch additional data using yfinance (e.g., AAPL for Apple)")
if st.button("Retrieve Stock Data"):
if stock_prompt:
with st.spinner("Retrieving stock data..."):
try:
# Run the search agent for stock data
stock_result = search_agent.run(stock_prompt)
st.write("Stock Data Result from Agent:")
st.write(stock_result)
log_agent_action(stock_prompt, stock_result, "Stock Data Helper")
# Fetch additional data using yfinance
if use_yfinance:
stock_info = get_stock_info(stock_prompt)
st.write("Additional Stock Information (yfinance):")
st.json(stock_info)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter a stock-related query to continue.")
# Footer
st.markdown("---")
st.caption("Powered by SmolAgents, Streamlit, and yfinance")