Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,34 +52,37 @@ import re
|
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def stock_price(symbol: str) -> str:
|
| 55 |
-
"""A tool that retrieves the current stock price for a given stock symbol
|
| 56 |
Args:
|
| 57 |
symbol: The ticker symbol of the stock (e.g., AAPL for Apple Inc.).
|
| 58 |
Returns:
|
| 59 |
A summary indicating the current price of the stock or an error message if unavailable.
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"An error occurred while retrieving the stock price for {symbol}: {e}"
|
| 81 |
|
| 82 |
|
|
|
|
| 83 |
@tool
|
| 84 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 85 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def stock_price(symbol: str) -> str:
|
| 55 |
+
"""A tool that retrieves the current stock price for a given stock symbol.
|
| 56 |
Args:
|
| 57 |
symbol: The ticker symbol of the stock (e.g., AAPL for Apple Inc.).
|
| 58 |
Returns:
|
| 59 |
A summary indicating the current price of the stock or an error message if unavailable.
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
+
import yfinance as yf
|
| 63 |
+
|
| 64 |
+
# Initialize the ticker for the given symbol
|
| 65 |
+
ticker = yf.Ticker(symbol)
|
| 66 |
|
| 67 |
+
# Try to get the current price from the ticker info
|
| 68 |
+
price = ticker.info.get("currentPrice")
|
| 69 |
|
| 70 |
+
# Fallback: if 'currentPrice' is not available, get the last closing price
|
| 71 |
+
if price is None:
|
| 72 |
+
history = ticker.history(period="1d")
|
| 73 |
+
if not history.empty:
|
| 74 |
+
price = history["Close"].iloc[-1]
|
| 75 |
+
else:
|
| 76 |
+
return f"Could not retrieve the current price for {symbol}. Please verify the ticker symbol."
|
| 77 |
+
|
| 78 |
+
# Return the current price in a formatted string
|
| 79 |
+
return f"The current price for {symbol} is ${price:.2f}."
|
| 80 |
+
|
| 81 |
except Exception as e:
|
| 82 |
return f"An error occurred while retrieving the stock price for {symbol}: {e}"
|
| 83 |
|
| 84 |
|
| 85 |
+
|
| 86 |
@tool
|
| 87 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 88 |
"""A tool that fetches the current local time in a specified timezone.
|