Spaces:
Sleeping
Sleeping
Update app.py
Browse filestool StockPriceTool added
app.py
CHANGED
|
@@ -39,49 +39,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 41 |
|
| 42 |
-
@tool
|
| 43 |
-
def get_current_stock_price(ticker_symbol: str) -> Optional[dict]:
|
| 44 |
-
"""
|
| 45 |
-
Fetches the latest stock price for the given ticker symbol and calculates
|
| 46 |
-
the price change over the last 5 days.
|
| 47 |
-
|
| 48 |
-
Args:
|
| 49 |
-
ticker_symbol: The stock ticker symbol to fetch the price for.
|
| 50 |
-
|
| 51 |
-
Returns:
|
| 52 |
-
dict: A dictionary containing the current stock price, the price change
|
| 53 |
-
over the last 5 days, and the percentage change.
|
| 54 |
-
Returns None if there's an error or no data is available.
|
| 55 |
-
"""
|
| 56 |
-
try:
|
| 57 |
-
# Fetch stock data
|
| 58 |
-
stock = yf.Ticker(ticker_symbol)
|
| 59 |
-
stock_data = stock.history(period="5d") # Fetch data for the last 5 days
|
| 60 |
-
|
| 61 |
-
if stock_data.empty:
|
| 62 |
-
return {"error": f"No historical data available for {ticker_symbol}"}
|
| 63 |
-
|
| 64 |
-
# Get the current price (last close price)
|
| 65 |
-
current_price = stock_data['Close'].iloc[-1]
|
| 66 |
-
|
| 67 |
-
# Calculate the price change over the last 5 days
|
| 68 |
-
price_change = current_price - stock_data['Close'].iloc[0]
|
| 69 |
-
price_change_percent = (price_change / stock_data['Close'].iloc[0]) * 100
|
| 70 |
-
|
| 71 |
-
# Round all values to 2 decimal places
|
| 72 |
-
return {
|
| 73 |
-
"price": round(current_price, 2),
|
| 74 |
-
"price_change": round(price_change, 2),
|
| 75 |
-
"price_change_percent": round(price_change_percent, 2)
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
except yf.exceptions.YFinanceException as yf_err:
|
| 79 |
-
logger.error(f"YFinance error for {ticker_symbol}: {str(yf_err)}")
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.error(f"Unexpected error fetching price for {ticker_symbol}: {str(e)}")
|
| 82 |
-
return None
|
| 83 |
-
|
| 84 |
final_answer = FinalAnswerTool()
|
|
|
|
| 85 |
|
| 86 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 87 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
@@ -102,7 +61,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 102 |
|
| 103 |
agent = CodeAgent(
|
| 104 |
model=model,
|
| 105 |
-
tools=[
|
| 106 |
max_steps=6,
|
| 107 |
verbosity_level=1,
|
| 108 |
grammar=None,
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
final_answer = FinalAnswerTool()
|
| 43 |
+
stock_price_tool = StockPriceTool()
|
| 44 |
|
| 45 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 46 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 61 |
|
| 62 |
agent = CodeAgent(
|
| 63 |
model=model,
|
| 64 |
+
tools=[stock_price_tool], ## add your tools here (don't remove final answer)
|
| 65 |
max_steps=6,
|
| 66 |
verbosity_level=1,
|
| 67 |
grammar=None,
|