Spaces:
Runtime error
Runtime error
File size: 1,680 Bytes
1112a98 db27452 1112a98 5b9e3fb d3d8f1a 1112a98 2312015 1112a98 124fc6d 1112a98 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
from smolagents import tool
import requests
import pandas as pd
import Gradio_UI
# @tool
# def my_custom_tool(arg1: str, arg2: int) -> str:
# """A placeholder tool that simply echoes its inputs.
# Args:
# arg1: The first argument.
# arg2: The second argument.
# Returns:
# A short status string.
# """
# return f"arg1={arg1}, arg2={arg2}"
@tool
def get_crypto_prices(crypto_ids: list[str], currency: str = "usd") -> dict:
"""Get current crypto prices using CoinGecko Simple Price.
Args:
crypto_ids: List of CoinGecko coin IDs, e.g. ["bitcoin","ethereum","ripple"].
currency: Fiat currency code, e.g. "usd".
Returns:
Dict mapping coin_id -> price in the requested currency.
"""
if not crypto_ids:
return {"error": "crypto_ids must be a non-empty list"}
url = "https://api.coingecko.com/api/v3/simple/price"
params = {"ids": ",".join(crypto_ids), "vs_currencies": currency.lower()}
try:
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
except Exception as e:
return {"error": f"request failed: {e}"}
data = r.json() # shape: {"bitcoin":{"usd":12345.6}, ...}
# flatten to { "bitcoin": 12345.6, ... }
return {coin: payload.get(currency.lower()) for coin, payload in data.items()}
# Example usage outside the agent:
cryptos = ["bitcoin", "ethereum", "ripple", "tether", "bnb", "solana", "cardano", "tron", "chainlink"]
prices = get_crypto_prices(cryptos, currency="usd")
print(prices)
# Optional: nice tabular view for humans
df = pd.DataFrame.from_dict(prices, orient="index", columns=["usd"])
print(df)
|