Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,48 @@ import requests
|
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
| 5 |
|
| 6 |
-
# Function to fetch
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
response.raise_for_status() # Check for HTTP errors
|
| 12 |
data = response.json()
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Function to provide TradingView widget HTML
|
| 18 |
def tradingview_widget():
|
|
@@ -32,19 +64,44 @@ def tradingview_widget():
|
|
| 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
|
| 38 |
return "Try asking about the BTC price or TradingView chart."
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
| 5 |
|
| 6 |
+
# Function to fetch historical price data from CoinGecko
|
| 7 |
+
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 8 |
+
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
if response.status_code == 200:
|
|
|
|
| 11 |
data = response.json()
|
| 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[coin_id]['usd']
|
| 24 |
+
else:
|
| 25 |
+
return f"Error fetching current price for {coin_id}"
|
| 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 tradingview_widget():
|
|
|
|
| 64 |
# Function to analyze prompt and provide information
|
| 65 |
def analyze_btc(prompt):
|
| 66 |
if "price" in prompt.lower():
|
| 67 |
+
return fetch_current_price("bitcoin")
|
| 68 |
elif "chart" in prompt.lower():
|
| 69 |
+
return tradingview_widget() # Use HTML component for TradingView chart
|
| 70 |
return "Try asking about the BTC price or TradingView chart."
|
| 71 |
|
| 72 |
+
# Gradio Interface Setup
|
| 73 |
+
|
| 74 |
+
# Historical Price Chart Interface
|
| 75 |
+
crypto_price_chart_interface = gr.Interface(
|
| 76 |
+
fn=plot_historical_prices,
|
| 77 |
+
inputs=[
|
| 78 |
+
gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 79 |
+
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
| 80 |
+
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
| 81 |
+
],
|
| 82 |
+
outputs=gr.Plot(label="Cryptocurrency Price Chart"),
|
| 83 |
+
title="Cryptocurrency Price Chart (2024-2025)"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Real-Time Trade Data Interface
|
| 87 |
+
real_time_trade_interface = gr.Interface(
|
| 88 |
+
fn=fetch_current_price,
|
| 89 |
+
inputs=gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 90 |
+
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 91 |
+
title="Real-Time Cryptocurrency Trade Data"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# AI Bitcoin Analyzer Interface
|
| 95 |
+
ai_btc_analyzer_interface = gr.Interface(
|
| 96 |
fn=analyze_btc,
|
| 97 |
inputs="text",
|
| 98 |
outputs="html", # Ensure output is HTML
|
| 99 |
+
title="AI Bitcoin Analyzer",
|
| 100 |
description="Ask about the current Bitcoin price or TradingView chart."
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Combine all interfaces into a single tabbed app
|
| 104 |
+
gr.TabbedInterface(
|
| 105 |
+
[crypto_price_chart_interface, real_time_trade_interface, ai_btc_analyzer_interface],
|
| 106 |
+
tab_names=["Price Chart", "Real-Time Trade Data", "AI Bitcoin Analyzer"]
|
| 107 |
+
).launch()
|