Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -2,33 +2,37 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objects 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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return prices
|
| 15 |
-
except requests.RequestException as e:
|
| 16 |
-
return f"Error fetching historical data: {e}"
|
| 17 |
|
| 18 |
# Function to fetch current prices from CoinGecko
|
| 19 |
def fetch_current_price(coin_id):
|
| 20 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return data[coin_id]['usd']
|
| 26 |
-
except requests.RequestException as e:
|
| 27 |
-
return f"Error fetching current price: {e}"
|
| 28 |
-
|
| 29 |
-
# Function to convert dates to timestamps
|
| 30 |
-
def date_to_timestamp(date_str):
|
| 31 |
-
return int(pd.Timestamp(date_str).timestamp())
|
| 32 |
|
| 33 |
# Function to plot historical prices using Plotly
|
| 34 |
def plot_historical_prices(coin_name, from_date, to_date):
|
|
@@ -37,7 +41,7 @@ def plot_historical_prices(coin_name, from_date, to_date):
|
|
| 37 |
|
| 38 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 39 |
|
| 40 |
-
if isinstance(prices, str):
|
| 41 |
fig = go.Figure()
|
| 42 |
fig.update_layout(title=prices)
|
| 43 |
return fig
|
|
@@ -68,14 +72,11 @@ def get_tradingview_chart():
|
|
| 68 |
# Function to fetch real-time trade data from CoinGecko
|
| 69 |
def get_real_time_trade_data():
|
| 70 |
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1"
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
return f"Latest trading data for Bitcoin is: ${latest_price}"
|
| 77 |
-
except requests.RequestException as e:
|
| 78 |
-
return f"Error fetching real-time trade data: {e}"
|
| 79 |
|
| 80 |
# Function to analyze Bitcoin based on the prompt
|
| 81 |
def analyze_btc(prompt):
|
|
|
|
| 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 current prices from CoinGecko
|
| 30 |
def fetch_current_price(coin_id):
|
| 31 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
| 32 |
+
data = fetch_data_with_retries(url)
|
| 33 |
+
if isinstance(data, str):
|
| 34 |
+
return data
|
| 35 |
+
return data[coin_id]['usd']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Function to plot historical prices using Plotly
|
| 38 |
def plot_historical_prices(coin_name, from_date, to_date):
|
|
|
|
| 41 |
|
| 42 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 43 |
|
| 44 |
+
if isinstance(prices, str):
|
| 45 |
fig = go.Figure()
|
| 46 |
fig.update_layout(title=prices)
|
| 47 |
return fig
|
|
|
|
| 72 |
# Function to fetch real-time trade data from CoinGecko
|
| 73 |
def get_real_time_trade_data():
|
| 74 |
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1"
|
| 75 |
+
data = fetch_data_with_retries(url)
|
| 76 |
+
if isinstance(data, str):
|
| 77 |
+
return data
|
| 78 |
+
latest_price = data['prices'][-1][1] if data['prices'] else "N/A"
|
| 79 |
+
return f"Latest trading data for Bitcoin is: ${latest_price}"
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Function to analyze Bitcoin based on the prompt
|
| 82 |
def analyze_btc(prompt):
|