mujtabarizvi commited on
Commit
bdaa6f0
·
verified ·
1 Parent(s): 1bc6ae7

Replaced Binance API with CoinGecko

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -10,32 +10,28 @@ from Gradio_UI import GradioUI
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def get_crypto_price(symbol: str) -> str:
13
- """A tool that fetches the current price of a cryptocurrency in USD using Binance API.
14
- Args:
15
- symbol: The cryptocurrency symbol (e.g., 'BTC', 'ETH', 'XRP').
16
- """
17
  try:
18
  symbol = symbol.upper()
19
- url = f"https://api.binance.com/api/v3/ticker/price?symbol={symbol}USDT"
20
- response = requests.get(url)
21
- response.raise_for_status()
 
 
22
  data = response.json()
23
 
24
- if 'price' in data:
25
- price = "{:,.2f}".format(float(data['price']))
26
- return f"📈 {symbol} Current Price: ${price}"
27
- else:
28
- return f"❌ Error: {data.get('msg', 'Unknown error')}"
29
 
30
- except requests.exceptions.HTTPError as e:
31
- if response.status_code == 400:
32
- error_msg = data.get('msg', 'Invalid cryptocurrency symbol')
33
- return f"❌ {error_msg} - Try symbols like BTC, ETH, or SOL"
34
- return f"❌ HTTP Error: {str(e)}"
35
- except requests.exceptions.RequestException as e:
36
- return f"🌐 Network Error: {str(e)}"
37
  except Exception as e:
38
- return f"⚠️ Unexpected Error: {str(e)}"
 
 
 
 
39
 
40
  @tool
41
  def get_current_time_in_timezone(timezone: str) -> str:
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def get_crypto_price(symbol: str) -> str:
13
+ """Get cryptocurrency price with basic error handling"""
 
 
 
14
  try:
15
  symbol = symbol.upper()
16
+ # Using CoinGecko's simple API (no key needed)
17
+ response = requests.get(
18
+ f"https://api.coingecko.com/api/v3/simple/price?ids={symbol.lower()}&vs_currencies=usd",
19
+ timeout=3
20
+ )
21
  data = response.json()
22
 
23
+ if symbol.lower() in data and 'usd' in data[symbol.lower()]:
24
+ price = "{:,.2f}".format(data[symbol.lower()]['usd'])
25
+ return f"Current {symbol} price: ${price}"
 
 
26
 
27
+ return f"Could not find price for {symbol} (try BTC/ETH/SOL)"
28
+
 
 
 
 
 
29
  except Exception as e:
30
+ # Fallback to hardcoded prices if API fails
31
+ default_prices = {'BTC': 63000, 'ETH': 3500, 'SOL': 150}
32
+ if symbol in default_prices:
33
+ return f"{symbol} price unavailable - Last known: ${default_prices[symbol]:,}"
34
+ return f"Price check failed for {symbol}"
35
 
36
  @tool
37
  def get_current_time_in_timezone(timezone: str) -> str: