Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
-
import plotly.
|
| 5 |
-
|
| 6 |
-
# Function to fetch
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
prices = data['prices']
|
| 13 |
-
return prices
|
| 14 |
-
else:
|
| 15 |
-
return f"Error fetching historical data for {coin_id}"
|
| 16 |
-
|
| 17 |
-
# Function to fetch current prices from CoinGecko
|
| 18 |
-
def fetch_current_price(coin_id):
|
| 19 |
-
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
| 20 |
-
response = requests.get(url)
|
| 21 |
-
if response.status_code == 200:
|
| 22 |
data = response.json()
|
| 23 |
-
return data[
|
| 24 |
-
|
| 25 |
-
return f"Error fetching
|
| 26 |
-
|
| 27 |
-
# Function to convert dates to timestamps
|
| 28 |
-
def date_to_timestamp(date_str):
|
| 29 |
-
return int(pd.Timestamp(date_str).timestamp())
|
| 30 |
-
|
| 31 |
-
# Function to plot historical prices using Plotly
|
| 32 |
-
def plot_historical_prices(coin_name, from_date, to_date):
|
| 33 |
-
from_timestamp = date_to_timestamp(from_date)
|
| 34 |
-
to_timestamp = date_to_timestamp(to_date)
|
| 35 |
-
|
| 36 |
-
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 37 |
-
|
| 38 |
-
if isinstance(prices, str): # In case of error
|
| 39 |
-
return prices
|
| 40 |
-
|
| 41 |
-
df = pd.DataFrame(prices, columns=['timestamp', 'price'])
|
| 42 |
-
df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
|
| 43 |
-
|
| 44 |
-
fig = go.Figure()
|
| 45 |
-
fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
|
| 46 |
-
fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
|
| 47 |
-
return fig
|
| 48 |
|
| 49 |
# Function to provide TradingView widget HTML
|
| 50 |
-
def
|
| 51 |
widget_html = """
|
| 52 |
<iframe
|
| 53 |
src="https://www.tradingview.com/widgetembed/?frameElementId=tradingview_ea33b&symbol=BINANCE:BTCUSDT&interval=60"
|
|
@@ -61,76 +29,22 @@ def get_tradingview_chart():
|
|
| 61 |
"""
|
| 62 |
return widget_html
|
| 63 |
|
| 64 |
-
# Function to
|
| 65 |
-
def get_real_time_trade_data():
|
| 66 |
-
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1"
|
| 67 |
-
response = requests.get(url)
|
| 68 |
-
if response.status_code == 200:
|
| 69 |
-
data = response.json()
|
| 70 |
-
return f"Latest trading data for Bitcoin is: ${data['prices'][-1][1]}"
|
| 71 |
-
else:
|
| 72 |
-
return "Error fetching real-time trade data for Bitcoin."
|
| 73 |
-
|
| 74 |
-
# Function to analyze Bitcoin based on the prompt
|
| 75 |
def analyze_btc(prompt):
|
| 76 |
-
"""Analyze the prompt and provide information."""
|
| 77 |
if "price" in prompt.lower():
|
| 78 |
-
return
|
| 79 |
-
elif "trade data" in prompt.lower():
|
| 80 |
-
return get_real_time_trade_data()
|
| 81 |
elif "chart" in prompt.lower():
|
| 82 |
-
return gr.HTML(
|
| 83 |
-
return "Try asking about the
|
| 84 |
-
|
| 85 |
-
# Gradio Interface Setup
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
fn=analyze_btc,
|
| 90 |
inputs="text",
|
| 91 |
outputs="html", # Ensure output is HTML
|
| 92 |
title="Bitcoin Analyzer",
|
| 93 |
-
description="Ask about the current Bitcoin price
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
# TradingView Interface for live charts
|
| 97 |
-
tradingview_interface = gr.Interface(
|
| 98 |
-
fn=get_tradingview_chart,
|
| 99 |
-
inputs=[],
|
| 100 |
-
outputs=gr.HTML(label="TradingView Chart"),
|
| 101 |
-
title="Live TradingView Chart"
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
# Real-time Trade Data Interface
|
| 105 |
-
real_time_trade_interface = gr.Interface(
|
| 106 |
-
fn=get_real_time_trade_data,
|
| 107 |
-
inputs=[],
|
| 108 |
-
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 109 |
-
title="Real-Time Cryptocurrency Trade Data"
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
# Price Chart Interface
|
| 113 |
-
crypto_price_chart_interface = gr.Interface(
|
| 114 |
-
fn=plot_historical_prices,
|
| 115 |
-
inputs=[
|
| 116 |
-
gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 117 |
-
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
| 118 |
-
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
| 119 |
-
],
|
| 120 |
-
outputs=gr.Plot(label="Cryptocurrency Price Chart"),
|
| 121 |
-
title="Cryptocurrency Price Chart (2024-2025)"
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
# Current Price Interface
|
| 125 |
-
current_price_interface = gr.Interface(
|
| 126 |
-
fn=fetch_current_price,
|
| 127 |
-
inputs=gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 128 |
-
outputs=gr.Textbox(label="Current Price"),
|
| 129 |
-
title="Current Cryptocurrency Price"
|
| 130 |
)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
[crypto_price_chart_interface, current_price_interface, real_time_trade_interface, btc_analysis_interface, tradingview_interface],
|
| 135 |
-
tab_names=["Price Chart", "Current Price", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
|
| 136 |
-
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
+
import plotly.graph_objs as go
|
| 5 |
+
|
| 6 |
+
# Function to fetch current price from CoinGecko
|
| 7 |
+
def fetch_current_price():
|
| 8 |
+
try:
|
| 9 |
+
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
response.raise_for_status() # Check for HTTP errors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
data = response.json()
|
| 13 |
+
return f"Current Bitcoin price: ${data['bitcoin']['usd']}"
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error fetching price: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Function to provide TradingView widget HTML
|
| 18 |
+
def tradingview_widget():
|
| 19 |
widget_html = """
|
| 20 |
<iframe
|
| 21 |
src="https://www.tradingview.com/widgetembed/?frameElementId=tradingview_ea33b&symbol=BINANCE:BTCUSDT&interval=60"
|
|
|
|
| 29 |
"""
|
| 30 |
return widget_html
|
| 31 |
|
| 32 |
+
# Function to analyze prompt and provide information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def analyze_btc(prompt):
|
|
|
|
| 34 |
if "price" in prompt.lower():
|
| 35 |
+
return fetch_current_price()
|
|
|
|
|
|
|
| 36 |
elif "chart" in prompt.lower():
|
| 37 |
+
return gr.HTML(tradingview_widget()) # Use HTML component for TradingView chart
|
| 38 |
+
return "Try asking about the BTC price or TradingView chart."
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Set up the Gradio interface
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
fn=analyze_btc,
|
| 43 |
inputs="text",
|
| 44 |
outputs="html", # Ensure output is HTML
|
| 45 |
title="Bitcoin Analyzer",
|
| 46 |
+
description="Ask about the current Bitcoin price or TradingView chart."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
interface.launch()
|
|
|
|
|
|
|
|
|