Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
@@ -3,56 +3,45 @@ 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,
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
data = response.json()
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
# Function to fetch current prices from CryptoCompare
|
22 |
def fetch_current_price(coin_id):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
data = response.json()
|
31 |
-
return data['USD']
|
32 |
-
|
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))
|
54 |
-
fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
|
55 |
-
return fig
|
56 |
|
57 |
# Function to provide TradingView widget HTML
|
58 |
def get_tradingview_chart():
|
@@ -71,27 +60,32 @@ def get_tradingview_chart():
|
|
71 |
|
72 |
# Function to fetch real-time trade data from CryptoCompare
|
73 |
def get_real_time_trade_data():
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Function to analyze Bitcoin based on the prompt
|
87 |
def analyze_btc(prompt):
|
88 |
"""Analyze the prompt and provide information."""
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
92 |
return get_real_time_trade_data()
|
93 |
-
elif "chart" in prompt
|
94 |
-
return
|
95 |
return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
|
96 |
|
97 |
# Gradio Interface Setup
|
@@ -125,7 +119,7 @@ real_time_trade_interface = gr.Interface(
|
|
125 |
crypto_price_chart_interface = gr.Interface(
|
126 |
fn=plot_historical_prices,
|
127 |
inputs=[
|
128 |
-
gr.Dropdown(choices=['
|
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 |
],
|
@@ -136,7 +130,7 @@ crypto_price_chart_interface = gr.Interface(
|
|
136 |
# Current Price Interface
|
137 |
current_price_interface = gr.Interface(
|
138 |
fn=fetch_current_price,
|
139 |
-
inputs=gr.Dropdown(choices=['
|
140 |
outputs=gr.Textbox(label="Current Price"),
|
141 |
title="Current Cryptocurrency Price"
|
142 |
)
|
|
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objects as go
|
5 |
|
6 |
+
# Define your CryptoCompare API key
|
7 |
+
API_KEY = 'YOUR_CRYPTOCOMPARE_API_KEY'
|
8 |
+
|
9 |
# Function to fetch historical price data from CryptoCompare
|
10 |
+
def fetch_historical_data(coin_id, from_date, to_date):
|
11 |
+
try:
|
12 |
+
url = "https://min-api.cryptocompare.com/data/v2/histoday"
|
13 |
+
params = {
|
14 |
+
'fsym': coin_id,
|
15 |
+
'tsym': 'USD',
|
16 |
+
'limit': 2000, # Maximum number of data points
|
17 |
+
'toTs': pd.Timestamp(to_date).timestamp(),
|
18 |
+
'api_key': API_KEY
|
19 |
+
}
|
20 |
+
response = requests.get(url, params=params)
|
21 |
+
response.raise_for_status()
|
22 |
data = response.json()
|
23 |
+
df = pd.DataFrame(data['Data']['Data'])
|
24 |
+
df['time'] = pd.to_datetime(df['time'], unit='s')
|
25 |
+
df = df[['time', 'close']].rename(columns={'time': 'timestamp', 'close': 'price'})
|
26 |
+
return df
|
27 |
+
except Exception as e:
|
28 |
+
return f"Error fetching historical data for {coin_id}: {e}"
|
29 |
|
30 |
# Function to fetch current prices from CryptoCompare
|
31 |
def fetch_current_price(coin_id):
|
32 |
+
try:
|
33 |
+
url = "https://min-api.cryptocompare.com/data/price"
|
34 |
+
params = {
|
35 |
+
'fsym': coin_id,
|
36 |
+
'tsyms': 'USD',
|
37 |
+
'api_key': API_KEY
|
38 |
+
}
|
39 |
+
response = requests.get(url, params=params)
|
40 |
+
response.raise_for_status()
|
41 |
data = response.json()
|
42 |
+
return f"${data['USD']}"
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error fetching current price for {coin_id}: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Function to provide TradingView widget HTML
|
47 |
def get_tradingview_chart():
|
|
|
60 |
|
61 |
# Function to fetch real-time trade data from CryptoCompare
|
62 |
def get_real_time_trade_data():
|
63 |
+
try:
|
64 |
+
return fetch_current_price('BTC')
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error fetching real-time trade data: {e}"
|
67 |
+
|
68 |
+
# Function to plot historical prices using Plotly
|
69 |
+
def plot_historical_prices(coin_name, from_date, to_date):
|
70 |
+
df = fetch_historical_data(coin_name, from_date, to_date)
|
71 |
+
if isinstance(df, str): # In case of error
|
72 |
+
return df
|
73 |
+
|
74 |
+
fig = go.Figure()
|
75 |
+
fig.add_trace(go.Scatter(x=df['timestamp'], y=df['price'], mode='lines', name=coin_name))
|
76 |
+
fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
|
77 |
+
return fig
|
78 |
|
79 |
# Function to analyze Bitcoin based on the prompt
|
80 |
def analyze_btc(prompt):
|
81 |
"""Analyze the prompt and provide information."""
|
82 |
+
prompt = prompt.lower()
|
83 |
+
if "price" in prompt:
|
84 |
+
return f"Current Bitcoin price: {fetch_current_price('BTC')}"
|
85 |
+
elif "trade data" in prompt:
|
86 |
return get_real_time_trade_data()
|
87 |
+
elif "chart" in prompt:
|
88 |
+
return get_tradingview_chart() # Use HTML component for TradingView chart
|
89 |
return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
|
90 |
|
91 |
# Gradio Interface Setup
|
|
|
119 |
crypto_price_chart_interface = gr.Interface(
|
120 |
fn=plot_historical_prices,
|
121 |
inputs=[
|
122 |
+
gr.Dropdown(choices=['BTC'], label="Cryptocurrency"),
|
123 |
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
124 |
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
125 |
],
|
|
|
130 |
# Current Price Interface
|
131 |
current_price_interface = gr.Interface(
|
132 |
fn=fetch_current_price,
|
133 |
+
inputs=gr.Dropdown(choices=['BTC'], label="Cryptocurrency"),
|
134 |
outputs=gr.Textbox(label="Current Price"),
|
135 |
title="Current Cryptocurrency Price"
|
136 |
)
|