Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
def get_price(coin_id): | |
""" | |
Fetch the current USD price of a cryptocurrency by its CoinGecko ID. | |
Args: | |
coin_id (str): The CoinGecko identifier of the cryptocurrency (e.g., 'bitcoin'). | |
Returns: | |
str: A formatted string with the coin name and its current price in USD, | |
or an error message if the price could not be fetched. | |
""" | |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd" | |
headers = {'User-Agent': 'Mozilla/5.0'} | |
response = requests.get(url, headers=headers, timeout=5) | |
data = response.json() | |
if coin_id in data: | |
return f"{coin_id.capitalize()} price: ${data[coin_id]['usd']}" | |
return "Error fetching price." | |
coin_options = ["bitcoin", "ethereum", "solana", "dogecoin"] | |
with gr.Blocks() as app: | |
gr.Markdown("## Coin Price Checker (CoinGecko)") | |
coin = gr.Dropdown(choices=coin_options, label="Select a coin") | |
output = gr.Textbox(label="Price (USD)") | |
coin.change(fn=get_price, inputs=coin, outputs=output) | |
app.launch(mcp_server=True) | |