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