Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
#from requests import Request, Session
|
| 8 |
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
|
| 9 |
import json
|
|
|
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
@@ -42,31 +43,44 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's important to specify the ret
|
|
| 42 |
return "What magic will you build ?"
|
| 43 |
|
| 44 |
@tool
|
| 45 |
-
def fetch_active_crypto()->
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
| 50 |
parameters = {
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
}
|
| 55 |
headers = {
|
| 56 |
-
|
| 57 |
-
|
| 58 |
}
|
| 59 |
-
|
| 60 |
session = requests.Session()
|
| 61 |
session.headers.update(headers)
|
| 62 |
-
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
@tool
|
|
|
|
| 7 |
#from requests import Request, Session
|
| 8 |
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
|
| 9 |
import json
|
| 10 |
+
from typing import Dict, Any, Optional
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
|
|
|
| 43 |
return "What magic will you build ?"
|
| 44 |
|
| 45 |
@tool
|
| 46 |
+
def fetch_active_crypto() -> Optional[Dict[str, Any]]:
|
| 47 |
+
"""A tool that fetches all active crypto by market cap in USD.
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
Optional[Dict[str, Any]]: A dictionary containing the top 10 cryptocurrencies by market cap,
|
| 51 |
+
or None if an error occurs.
|
| 52 |
"""
|
| 53 |
url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
| 54 |
parameters = {
|
| 55 |
+
'start': '1',
|
| 56 |
+
'limit': '5000',
|
| 57 |
+
'convert': 'USD'
|
| 58 |
}
|
| 59 |
headers = {
|
| 60 |
+
'Accepts': 'application/json',
|
| 61 |
+
'X-CMC_PRO_API_KEY': 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c',
|
| 62 |
}
|
| 63 |
+
|
| 64 |
session = requests.Session()
|
| 65 |
session.headers.update(headers)
|
| 66 |
+
|
| 67 |
try:
|
| 68 |
+
response = session.get(url, params=parameters)
|
| 69 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 70 |
+
data = json.loads(response.text)
|
| 71 |
+
|
| 72 |
+
# Extract the top 10 cryptocurrencies by market cap
|
| 73 |
+
if 'data' in data:
|
| 74 |
+
sorted_crypto = sorted(data['data'], key=lambda x: x['quote']['USD']['market_cap'], reverse=True)
|
| 75 |
+
top_10 = sorted_crypto[:10]
|
| 76 |
+
return {crypto['name']: crypto['quote']['USD'] for crypto in top_10}
|
| 77 |
+
else:
|
| 78 |
+
print("No data found in the response.")
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
except (ConnectionError, Timeout, TooManyRedirects, requests.exceptions.HTTPError) as e:
|
| 82 |
+
print(f"An error occurred: {e}")
|
| 83 |
+
return None
|
| 84 |
|
| 85 |
|
| 86 |
@tool
|