Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,9 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
|
|
5 |
|
6 |
-
# Fallback data in case
|
7 |
fallback_historical_data = {
|
8 |
'bitcoin': [
|
9 |
[1633046400000, 42000], [1633132800000, 42250], [1633219200000, 41500],
|
@@ -17,29 +18,43 @@ fallback_current_prices = {
|
|
17 |
'binancecoin': 380
|
18 |
}
|
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 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
def fetch_current_price(coin_id):
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
-
response = requests.get(
|
38 |
response.raise_for_status()
|
39 |
data = response.json()
|
40 |
-
return data[
|
41 |
except requests.exceptions.RequestException:
|
42 |
-
# Return fallback data if
|
43 |
return fallback_current_prices.get(coin_id, f"Error fetching current price for {coin_id}")
|
44 |
|
45 |
# Function to convert dates to timestamps
|
@@ -115,24 +130,35 @@ def btc_analysis():
|
|
115 |
{news}
|
116 |
"""
|
117 |
|
118 |
-
# Function to fetch real-time trade data from CoinGecko (with
|
119 |
def fetch_real_time_trade_data(coin_id):
|
120 |
-
# Fallback data if API is not accessible
|
121 |
fallback_data = {
|
122 |
'bitcoin': [1633039200000, 42000],
|
123 |
'ethereum': [1633039200000, 3100],
|
124 |
'binancecoin': [1633039200000, 380],
|
125 |
}
|
126 |
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
try:
|
130 |
-
response = requests.get(
|
131 |
response.raise_for_status()
|
132 |
data = response.json()
|
133 |
-
return data['
|
134 |
except requests.exceptions.RequestException:
|
135 |
-
# If
|
136 |
return fallback_data.get(coin_id, f"Error fetching real-time trade data for {coin_id}")
|
137 |
|
138 |
# Function to display real-time trade data
|
@@ -145,7 +171,6 @@ def real_time_trade_dashboard(coin):
|
|
145 |
# Top 100 Cryptocurrencies (by CoinGecko IDs)
|
146 |
top_100_cryptos = [
|
147 |
'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron',
|
148 |
-
# Add more top coins as necessary
|
149 |
]
|
150 |
|
151 |
# Gradio Interface Setup
|
@@ -194,11 +219,4 @@ current_price_interface = gr.Interface(
|
|
194 |
title="Current Cryptocurrency Price"
|
195 |
)
|
196 |
|
197 |
-
# Combining all interfaces into a single
|
198 |
-
block = gr.TabbedInterface(
|
199 |
-
[crypto_price_chart_interface, current_price_interface, real_time_trade_interface, btc_analysis_interface, tradingview_interface],
|
200 |
-
tab_names=["Price Chart", "Current Price", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
|
201 |
-
)
|
202 |
-
|
203 |
-
if __name__ == "__main__":
|
204 |
-
block.launch(server_name="0.0.0.0")
|
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
5 |
+
import time
|
6 |
|
7 |
+
# Fallback data in case both APIs fail
|
8 |
fallback_historical_data = {
|
9 |
'bitcoin': [
|
10 |
[1633046400000, 42000], [1633132800000, 42250], [1633219200000, 41500],
|
|
|
18 |
'binancecoin': 380
|
19 |
}
|
20 |
|
21 |
+
# Function to fetch historical price data from CoinGecko with retry logic
|
22 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
23 |
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
24 |
+
for _ in range(3): # Retry 3 times before failing
|
25 |
+
try:
|
26 |
+
response = requests.get(url, timeout=10)
|
27 |
+
response.raise_for_status()
|
28 |
+
data = response.json()
|
29 |
+
prices = data['prices']
|
30 |
+
return prices
|
31 |
+
except requests.exceptions.RequestException:
|
32 |
+
time.sleep(1) # Wait 1 second before retrying
|
33 |
+
return fallback_historical_data.get(coin_id, f"Error fetching historical data for {coin_id}")
|
34 |
+
|
35 |
+
# Function to fetch current prices from CoinGecko and fallback to CryptoCompare
|
36 |
def fetch_current_price(coin_id):
|
37 |
+
gecko_url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
38 |
+
compare_url = f"https://min-api.cryptocompare.com/data/price?fsym={coin_id.upper()}&tsyms=USD"
|
39 |
+
|
40 |
+
# First, try CoinGecko API
|
41 |
+
for _ in range(3): # Retry 3 times before failing
|
42 |
+
try:
|
43 |
+
response = requests.get(gecko_url, timeout=10)
|
44 |
+
response.raise_for_status()
|
45 |
+
data = response.json()
|
46 |
+
return data[coin_id]['usd']
|
47 |
+
except requests.exceptions.RequestException:
|
48 |
+
time.sleep(1)
|
49 |
+
|
50 |
+
# If CoinGecko fails, try CryptoCompare API
|
51 |
try:
|
52 |
+
response = requests.get(compare_url, timeout=10)
|
53 |
response.raise_for_status()
|
54 |
data = response.json()
|
55 |
+
return data['USD']
|
56 |
except requests.exceptions.RequestException:
|
57 |
+
# Return fallback data if both APIs fail
|
58 |
return fallback_current_prices.get(coin_id, f"Error fetching current price for {coin_id}")
|
59 |
|
60 |
# Function to convert dates to timestamps
|
|
|
130 |
{news}
|
131 |
"""
|
132 |
|
133 |
+
# Function to fetch real-time trade data from CoinGecko (with retry and backup logic)
|
134 |
def fetch_real_time_trade_data(coin_id):
|
|
|
135 |
fallback_data = {
|
136 |
'bitcoin': [1633039200000, 42000],
|
137 |
'ethereum': [1633039200000, 3100],
|
138 |
'binancecoin': [1633039200000, 380],
|
139 |
}
|
140 |
|
141 |
+
gecko_url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart?vs_currency=usd&days=1"
|
142 |
+
compare_url = f"https://min-api.cryptocompare.com/data/price?fsym={coin_id.upper()}&tsyms=USD"
|
143 |
+
|
144 |
+
# First, try CoinGecko API
|
145 |
+
for _ in range(3): # Retry 3 times before failing
|
146 |
+
try:
|
147 |
+
response = requests.get(gecko_url, timeout=10)
|
148 |
+
response.raise_for_status()
|
149 |
+
data = response.json()
|
150 |
+
return data['prices'][-1] # Latest price data
|
151 |
+
except requests.exceptions.RequestException:
|
152 |
+
time.sleep(1)
|
153 |
+
|
154 |
+
# If CoinGecko fails, try CryptoCompare API
|
155 |
try:
|
156 |
+
response = requests.get(compare_url, timeout=10)
|
157 |
response.raise_for_status()
|
158 |
data = response.json()
|
159 |
+
return [None, data['USD']]
|
160 |
except requests.exceptions.RequestException:
|
161 |
+
# If both APIs fail, return fallback data
|
162 |
return fallback_data.get(coin_id, f"Error fetching real-time trade data for {coin_id}")
|
163 |
|
164 |
# Function to display real-time trade data
|
|
|
171 |
# Top 100 Cryptocurrencies (by CoinGecko IDs)
|
172 |
top_100_cryptos = [
|
173 |
'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron',
|
|
|
174 |
]
|
175 |
|
176 |
# Gradio Interface Setup
|
|
|
219 |
title="Current Cryptocurrency Price"
|
220 |
)
|
221 |
|
222 |
+
# Combining all interfaces into a single
|
|
|
|
|
|
|
|
|
|
|
|
|
|