Spaces:
Runtime error
Runtime error
add tool function to query price of asset
Browse files
app.py
CHANGED
|
@@ -3,20 +3,86 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -46,7 +112,6 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
|
|
| 46 |
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import json
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
+
import yfinance as yf
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
@tool
|
| 12 |
+
def query_market_asset(symbol: str) -> str:
|
| 13 |
+
"""A universal market data tool to query the current price or level of ANY asset.
|
| 14 |
+
|
| 15 |
+
Supported asset classes include major indices, stocks, ETFs, crypto, and forex.
|
| 16 |
+
|
| 17 |
Args:
|
| 18 |
+
symbol: The specific ticker symbol used by Yahoo Finance. Examples:
|
| 19 |
+
- Indices (requires '^'): '^GSPC' (S&P 500), '^VIX' (Volatility Index), '^DJI' (Dow Jones), '^IXIC' (Nasdaq)
|
| 20 |
+
- Equities / ETFs: 'AAPL' (Apple), 'SPY' (SPDR S&P 500 ETF), 'TLT' (20+ Yr Treasury Bond)
|
| 21 |
+
- Crypto: 'BTC-USD' (Bitcoin), 'ETH-USD' (Ethereum)
|
| 22 |
+
- Forex: 'EURUSD=X' (EUR/USD rate), 'USDCNH=X' (USD/Offshore RMB)
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
A JSON-formatted string containing the current price, high/low, timestamp, and asset info.
|
| 26 |
"""
|
| 27 |
+
symbol = symbol.strip().upper()
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
ticker = yf.Ticker(symbol)
|
| 31 |
+
|
| 32 |
+
data = ticker.history(period="1d", interval="1m")
|
| 33 |
+
|
| 34 |
+
if not data.empty:
|
| 35 |
+
latest_row = data.iloc[-1]
|
| 36 |
+
current_price = float(latest_row['Close'])
|
| 37 |
+
open_price = float(latest_row['Open'])
|
| 38 |
+
high_price = float(latest_row['High'])
|
| 39 |
+
low_price = float(latest_row['Low'])
|
| 40 |
+
volume = int(latest_row['Volume'])
|
| 41 |
+
timestamp = str(data.index[-1])
|
| 42 |
+
|
| 43 |
+
result = {
|
| 44 |
+
"status": "success",
|
| 45 |
+
"symbol": symbol,
|
| 46 |
+
"current_price": round(current_price, 4),
|
| 47 |
+
"open": round(open_price, 4),
|
| 48 |
+
"high": round(high_price, 4),
|
| 49 |
+
"low": round(low_price, 4),
|
| 50 |
+
"volume": volume,
|
| 51 |
+
"timestamp": timestamp,
|
| 52 |
+
"data_type": "intraday_1m"
|
| 53 |
+
}
|
| 54 |
+
else:
|
| 55 |
+
info = ticker.info
|
| 56 |
+
current_price = info.get("regularMarketPrice") or info.get("previousClose") or info.get("ask") or info.get("bid")
|
| 57 |
+
|
| 58 |
+
if current_price:
|
| 59 |
+
result = {
|
| 60 |
+
"status": "success",
|
| 61 |
+
"symbol": symbol,
|
| 62 |
+
"current_price": round(float(current_price), 4),
|
| 63 |
+
"open": info.get("regularMarketOpen") or info.get("open"),
|
| 64 |
+
"high": info.get("regularMarketDayHigh") or info.get("dayHigh"),
|
| 65 |
+
"low": info.get("regularMarketDayLow") or info.get("dayLow"),
|
| 66 |
+
"volume": info.get("regularMarketVolume") or info.get("volume", 0),
|
| 67 |
+
"short_name": info.get("shortName", ""),
|
| 68 |
+
"data_type": "cached_info"
|
| 69 |
+
}
|
| 70 |
+
else:
|
| 71 |
+
result = {
|
| 72 |
+
"status": "error",
|
| 73 |
+
"symbol": symbol,
|
| 74 |
+
"message": "No price data could be resolved for this asset."
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
result = {
|
| 79 |
+
"status": "error",
|
| 80 |
+
"symbol": symbol,
|
| 81 |
+
"message": f"Exception occurred while querying: {str(e)}"
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 85 |
+
|
| 86 |
|
| 87 |
@tool
|
| 88 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 112 |
custom_role_conversions=None,
|
| 113 |
)
|
| 114 |
|
|
|
|
| 115 |
# Import tool from Hub
|
| 116 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 117 |
|