| 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() | |