trading_model / app.py
john3huggingface's picture
feat: add app.py
5cb5b93 verified
import gradio as gr
import requests
# Function to fetch comprehensive Bitcoin information from CoinGecko
def fetch_bitcoin_info():
# API endpoint for detailed Bitcoin information
endpoint = "https://api.coingecko.com/api/v3/coins/bitcoin"
# Send a GET request to the endpoint
response = requests.get(endpoint)
if response.status_code == 200:
data = response.json()
# Extract detailed information for Bitcoin
current_price = data["market_data"]["current_price"]["usd"]
market_cap = data["market_data"]["market_cap"]["usd"]
trading_volume_24h = data["market_data"]["total_volume"]["usd"]
rank = data["market_cap_rank"]
change_rate_24h = data["market_data"]["price_change_percentage_24h"]
all_time_high = data["market_data"]["ath"]["usd"]
number_of_markets = len(data["tickers"]) # Number of markets in which Bitcoin is listed
number_of_exchanges = len(set([ticker["market"]["name"] for ticker in data["tickers"]])) # Unique exchanges
total_supply = data["market_data"]["total_supply"]
circulating_supply = data["market_data"]["circulating_supply"]
# Create a formatted output
output = (
f"Bitcoin Price Information:\n"
f"Current Price: ${current_price:.2f}\n"
f"Market Cap: ${market_cap:.2f}\n"
f"24-Hour Trading Volume: ${trading_volume_24h:.2f}\n"
f"24-Hour Change Rate: {change_rate_24h:.2f}%\n"
f"All-Time High (ATH): ${all_time_high:.2f}\n"
f"Market Cap Rank: {rank}\n"
f"Number of Markets: {number_of_markets}\n"
f"Number of Exchanges: {number_of_exchanges}\n"
f"Total Supply: {total_supply:.2f}\n"
f"Circulating Supply: {circulating_supply:.2f}"
)
return output
else:
return "Failed to fetch Bitcoin information."
# Create the Gradio interface
with gr.Blocks() as demo:
# Create a textbox to display Bitcoin information
bitcoin_info_text = gr.Textbox(label="Bitcoin Information", interactive=False)
# Button to fetch Bitcoin information
fetch_button = gr.Button("Fetch Bitcoin Information")
# Connect the button click event to the function to fetch and display Bitcoin information
fetch_button.click(fetch_bitcoin_info, outputs=bitcoin_info_text)
# Launch the Gradio app
demo.launch(share=True)