File size: 1,538 Bytes
5a216a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
import gradio as gr
import requests

# Function to fetch the current price of a cryptocurrency from CoinGecko
def get_crypto_price(coin_id):
    # Define the endpoint and query parameters
    endpoint = f"https://api.coingecko.com/api/v3/simple/price"
    params = {
        "ids": coin_id,  # ID of the cryptocurrency
        "vs_currencies": "usd",  # Currency to compare with (USD)
    }
    # Make the GET request to CoinGecko API
    response = requests.get(endpoint, params=params)
    
    if response.status_code == 200:
        # Get the JSON data from the response
        data = response.json()
        # Extract the price for the specified coin
        price = data.get(coin_id, {}).get("usd", "N/A")
        if price == "N/A":
            return f"Couldn't find data for '{coin_id}'. Please check the ID and try again."
        else:
            return f"The current price of {coin_id} is ${price:.2f} USD."
    else:
        return f"Error fetching data from CoinGecko. Status code: {response.status_code}"

# Gradio interface function
def crypto_price_gradio(coin_id):
    return get_crypto_price(coin_id)

# Create the Gradio interface
iface = gr.Interface(
    fn=crypto_price_gradio,
    inputs=gr.Textbox(label="Enter Cryptocurrency ID (e.g., bitcoin, ethereum)"),
    outputs=gr.Textbox(label="Price in USD"),
    title="Cryptocurrency Price Checker",
    description="Enter a cryptocurrency ID to get its current price in USD."
)

# Launch the Gradio interface
iface.launch()