Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -2,52 +2,52 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
-
import time
|
| 6 |
-
|
| 7 |
-
# Function to fetch data with retry mechanism
|
| 8 |
-
def fetch_data_with_retries(url, max_retries=3):
|
| 9 |
-
for attempt in range(max_retries):
|
| 10 |
-
try:
|
| 11 |
-
response = requests.get(url)
|
| 12 |
-
response.raise_for_status()
|
| 13 |
-
return response.json()
|
| 14 |
-
except requests.RequestException as e:
|
| 15 |
-
if attempt < max_retries - 1:
|
| 16 |
-
time.sleep(2) # Wait before retrying
|
| 17 |
-
else:
|
| 18 |
-
return f"Error fetching data: {e}"
|
| 19 |
-
|
| 20 |
-
# Function to fetch historical price data from CoinGecko
|
| 21 |
-
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 22 |
-
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
| 23 |
-
data = fetch_data_with_retries(url)
|
| 24 |
-
if isinstance(data, str):
|
| 25 |
-
return data
|
| 26 |
-
prices = data.get('prices', [])
|
| 27 |
-
return prices
|
| 28 |
|
| 29 |
-
# Function to fetch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def fetch_current_price(coin_id):
|
| 31 |
-
url = f"https://api.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Function to plot historical prices using Plotly
|
| 38 |
def plot_historical_prices(coin_name, from_date, to_date):
|
| 39 |
from_timestamp = date_to_timestamp(from_date)
|
| 40 |
to_timestamp = date_to_timestamp(to_date)
|
| 41 |
|
| 42 |
-
|
| 43 |
|
| 44 |
-
if isinstance(
|
| 45 |
-
|
| 46 |
-
fig.update_layout(title=prices)
|
| 47 |
-
return fig
|
| 48 |
|
| 49 |
-
df = pd.DataFrame(
|
| 50 |
-
df['date'] = pd.to_datetime(df['timestamp'], unit='
|
| 51 |
|
| 52 |
fig = go.Figure()
|
| 53 |
fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
|
|
@@ -69,14 +69,19 @@ def get_tradingview_chart():
|
|
| 69 |
"""
|
| 70 |
return widget_html
|
| 71 |
|
| 72 |
-
# Function to fetch real-time trade data from
|
| 73 |
def get_real_time_trade_data():
|
| 74 |
-
url = "https://api.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Function to analyze Bitcoin based on the prompt
|
| 82 |
def analyze_btc(prompt):
|
|
@@ -86,37 +91,17 @@ def analyze_btc(prompt):
|
|
| 86 |
elif "trade data" in prompt.lower():
|
| 87 |
return get_real_time_trade_data()
|
| 88 |
elif "chart" in prompt.lower():
|
| 89 |
-
return get_tradingview_chart() # Use HTML component for TradingView chart
|
| 90 |
return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
|
| 91 |
|
| 92 |
# Gradio Interface Setup
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
fn=plot_historical_prices,
|
| 97 |
-
inputs=[
|
| 98 |
-
gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 99 |
-
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
| 100 |
-
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
| 101 |
-
],
|
| 102 |
-
outputs=gr.Plot(label="Cryptocurrency Price Chart"),
|
| 103 |
-
title="Cryptocurrency Price Chart (2024-2025)"
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
# Real-Time Trade Data Interface
|
| 107 |
-
real_time_trade_interface = gr.Interface(
|
| 108 |
-
fn=get_real_time_trade_data,
|
| 109 |
-
inputs=[],
|
| 110 |
-
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 111 |
-
title="Real-Time Cryptocurrency Trade Data"
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# AI Bitcoin Analyzer Interface
|
| 115 |
-
ai_btc_analyzer_interface = gr.Interface(
|
| 116 |
fn=analyze_btc,
|
| 117 |
inputs="text",
|
| 118 |
outputs="html", # Ensure output is HTML
|
| 119 |
-
title="
|
| 120 |
description="Ask about the current Bitcoin price, real-time trade data, or TradingView chart."
|
| 121 |
)
|
| 122 |
|
|
@@ -128,8 +113,36 @@ tradingview_interface = gr.Interface(
|
|
| 128 |
title="Live TradingView Chart"
|
| 129 |
)
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# Combining all interfaces into a single tabbed app
|
| 132 |
gr.TabbedInterface(
|
| 133 |
-
[crypto_price_chart_interface, real_time_trade_interface,
|
| 134 |
-
tab_names=["Price Chart", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
|
| 135 |
).launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Function to fetch historical price data from CryptoCompare
|
| 7 |
+
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 8 |
+
url = f"https://min-api.cryptocompare.com/data/v2/pricehistorical"
|
| 9 |
+
params = {
|
| 10 |
+
'fsym': coin_id.upper(),
|
| 11 |
+
'tsyms': 'USD',
|
| 12 |
+
'ts': to_timestamp
|
| 13 |
+
}
|
| 14 |
+
response = requests.get(url, params=params)
|
| 15 |
+
if response.status_code == 200:
|
| 16 |
+
data = response.json()
|
| 17 |
+
return data['Data']
|
| 18 |
+
else:
|
| 19 |
+
return f"Error fetching historical data for {coin_id}"
|
| 20 |
+
|
| 21 |
+
# Function to fetch current prices from CryptoCompare
|
| 22 |
def fetch_current_price(coin_id):
|
| 23 |
+
url = f"https://min-api.cryptocompare.com/data/price"
|
| 24 |
+
params = {
|
| 25 |
+
'fsym': coin_id.upper(),
|
| 26 |
+
'tsyms': 'USD'
|
| 27 |
+
}
|
| 28 |
+
response = requests.get(url, params=params)
|
| 29 |
+
if response.status_code == 200:
|
| 30 |
+
data = response.json()
|
| 31 |
+
return data['USD']
|
| 32 |
+
else:
|
| 33 |
+
return f"Error fetching current price for {coin_id}"
|
| 34 |
+
|
| 35 |
+
# Function to convert dates to timestamps
|
| 36 |
+
def date_to_timestamp(date_str):
|
| 37 |
+
return int(pd.Timestamp(date_str).timestamp())
|
| 38 |
|
| 39 |
# Function to plot historical prices using Plotly
|
| 40 |
def plot_historical_prices(coin_name, from_date, to_date):
|
| 41 |
from_timestamp = date_to_timestamp(from_date)
|
| 42 |
to_timestamp = date_to_timestamp(to_date)
|
| 43 |
|
| 44 |
+
data = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 45 |
|
| 46 |
+
if isinstance(data, str): # In case of error
|
| 47 |
+
return data
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
df = pd.DataFrame(list(data.items()), columns=['timestamp', 'price'])
|
| 50 |
+
df['date'] = pd.to_datetime(df['timestamp'], unit='s')
|
| 51 |
|
| 52 |
fig = go.Figure()
|
| 53 |
fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
|
|
|
|
| 69 |
"""
|
| 70 |
return widget_html
|
| 71 |
|
| 72 |
+
# Function to fetch real-time trade data from CryptoCompare
|
| 73 |
def get_real_time_trade_data():
|
| 74 |
+
url = f"https://min-api.cryptocompare.com/data/price"
|
| 75 |
+
params = {
|
| 76 |
+
'fsym': 'BTC',
|
| 77 |
+
'tsyms': 'USD'
|
| 78 |
+
}
|
| 79 |
+
response = requests.get(url, params=params)
|
| 80 |
+
if response.status_code == 200:
|
| 81 |
+
data = response.json()
|
| 82 |
+
return f"Latest trading data for Bitcoin is: ${data['USD']}"
|
| 83 |
+
else:
|
| 84 |
+
return "Error fetching real-time trade data for Bitcoin."
|
| 85 |
|
| 86 |
# Function to analyze Bitcoin based on the prompt
|
| 87 |
def analyze_btc(prompt):
|
|
|
|
| 91 |
elif "trade data" in prompt.lower():
|
| 92 |
return get_real_time_trade_data()
|
| 93 |
elif "chart" in prompt.lower():
|
| 94 |
+
return gr.HTML(get_tradingview_chart()) # Use HTML component for TradingView chart
|
| 95 |
return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
|
| 96 |
|
| 97 |
# Gradio Interface Setup
|
| 98 |
|
| 99 |
+
# AI BTC Analysis Interface
|
| 100 |
+
btc_analysis_interface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
fn=analyze_btc,
|
| 102 |
inputs="text",
|
| 103 |
outputs="html", # Ensure output is HTML
|
| 104 |
+
title="Bitcoin Analyzer",
|
| 105 |
description="Ask about the current Bitcoin price, real-time trade data, or TradingView chart."
|
| 106 |
)
|
| 107 |
|
|
|
|
| 113 |
title="Live TradingView Chart"
|
| 114 |
)
|
| 115 |
|
| 116 |
+
# Real-time Trade Data Interface
|
| 117 |
+
real_time_trade_interface = gr.Interface(
|
| 118 |
+
fn=get_real_time_trade_data,
|
| 119 |
+
inputs=[],
|
| 120 |
+
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 121 |
+
title="Real-Time Cryptocurrency Trade Data"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Price Chart Interface
|
| 125 |
+
crypto_price_chart_interface = gr.Interface(
|
| 126 |
+
fn=plot_historical_prices,
|
| 127 |
+
inputs=[
|
| 128 |
+
gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 129 |
+
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
| 130 |
+
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
| 131 |
+
],
|
| 132 |
+
outputs=gr.Plot(label="Cryptocurrency Price Chart"),
|
| 133 |
+
title="Cryptocurrency Price Chart (2024-2025)"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Current Price Interface
|
| 137 |
+
current_price_interface = gr.Interface(
|
| 138 |
+
fn=fetch_current_price,
|
| 139 |
+
inputs=gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
|
| 140 |
+
outputs=gr.Textbox(label="Current Price"),
|
| 141 |
+
title="Current Cryptocurrency Price"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
# Combining all interfaces into a single tabbed app
|
| 145 |
gr.TabbedInterface(
|
| 146 |
+
[crypto_price_chart_interface, current_price_interface, real_time_trade_interface, btc_analysis_interface, tradingview_interface],
|
| 147 |
+
tab_names=["Price Chart", "Current Price", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
|
| 148 |
).launch()
|