| import gradio as gr
|
| import requests
|
|
|
|
|
| def get_crypto_price(coin_id):
|
|
|
| endpoint = f"https://api.coingecko.com/api/v3/simple/price"
|
| params = {
|
| "ids": coin_id,
|
| "vs_currencies": "usd",
|
| }
|
|
|
| response = requests.get(endpoint, params=params)
|
|
|
| if response.status_code == 200:
|
|
|
| data = response.json()
|
|
|
| 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}"
|
|
|
|
|
| def crypto_price_gradio(coin_id):
|
| return get_crypto_price(coin_id)
|
|
|
|
|
| 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."
|
| )
|
|
|
|
|
| iface.launch()
|
|
|