File size: 1,113 Bytes
be1fe39
 
 
 
 
d0946a9
 
 
 
 
 
 
 
 
 
be1fe39
0b2b4c2
 
be1fe39
 
 
 
 
 
0b2b4c2
be1fe39
 
 
d0946a9
be1fe39
 
 
 
 
5ca97ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)