Da-Rabbit-Hole's picture
Update app.py
db27452 verified
raw
history blame
1.68 kB
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)