initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Function to fetch the current price of a cryptocurrency from CoinGecko
|
5 |
+
def get_crypto_price(coin_id):
|
6 |
+
# Define the endpoint and query parameters
|
7 |
+
endpoint = f"https://api.coingecko.com/api/v3/simple/price"
|
8 |
+
params = {
|
9 |
+
"ids": coin_id, # ID of the cryptocurrency
|
10 |
+
"vs_currencies": "usd", # Currency to compare with (USD)
|
11 |
+
}
|
12 |
+
# Make the GET request to CoinGecko API
|
13 |
+
response = requests.get(endpoint, params=params)
|
14 |
+
|
15 |
+
if response.status_code == 200:
|
16 |
+
# Get the JSON data from the response
|
17 |
+
data = response.json()
|
18 |
+
# Extract the price for the specified coin
|
19 |
+
price = data.get(coin_id, {}).get("usd", "N/A")
|
20 |
+
if price == "N/A":
|
21 |
+
return f"Couldn't find data for '{coin_id}'. Please check the ID and try again."
|
22 |
+
else:
|
23 |
+
return f"The current price of {coin_id} is ${price:.2f} USD."
|
24 |
+
else:
|
25 |
+
return f"Error fetching data from CoinGecko. Status code: {response.status_code}"
|
26 |
+
|
27 |
+
# Gradio interface function
|
28 |
+
def crypto_price_gradio(coin_id):
|
29 |
+
return get_crypto_price(coin_id)
|
30 |
+
|
31 |
+
# Create the Gradio interface
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=crypto_price_gradio,
|
34 |
+
inputs=gr.Textbox(label="Enter Cryptocurrency ID (e.g., bitcoin, ethereum)"),
|
35 |
+
outputs=gr.Textbox(label="Price in USD"),
|
36 |
+
title="Cryptocurrency Price Checker",
|
37 |
+
description="Enter a cryptocurrency ID to get its current price in USD."
|
38 |
+
)
|
39 |
+
|
40 |
+
# Launch the Gradio interface
|
41 |
+
iface.launch()
|