Spaces:
Sleeping
Sleeping
app.py
#1
by
missbaj
- opened
app.py
CHANGED
@@ -2,9 +2,8 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
5 |
-
import time
|
6 |
|
7 |
-
# Fallback data for demonstration purposes
|
8 |
fallback_historical_data = {
|
9 |
'bitcoin': [
|
10 |
[1633046400000, 42000], [1633132800000, 42250], [1633219200000, 41500],
|
@@ -12,6 +11,7 @@ fallback_historical_data = {
|
|
12 |
]
|
13 |
}
|
14 |
|
|
|
15 |
fallback_current_prices = {
|
16 |
'bitcoin': 42000,
|
17 |
'ethereum': 3100,
|
@@ -30,7 +30,7 @@ def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
30 |
print(f"Error fetching historical data: {e}")
|
31 |
return fallback_historical_data.get(coin_id, f"Error fetching historical data for {coin_id}")
|
32 |
|
33 |
-
# Function to fetch current
|
34 |
def fetch_current_price(coin_id):
|
35 |
gecko_url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
36 |
compare_url = f"https://min-api.cryptocompare.com/data/price?fsym={coin_id.upper()}&tsyms=USD"
|
@@ -78,14 +78,20 @@ def plot_historical_prices(coin_name, from_date, to_date):
|
|
78 |
except Exception as e:
|
79 |
return f"Error in plotting chart: {e}"
|
80 |
|
81 |
-
# Function to
|
82 |
def btc_analysis():
|
83 |
try:
|
84 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
except Exception as e:
|
86 |
return f"Error in BTC analysis: {e}"
|
87 |
|
88 |
-
# Function to fetch real-time trade data
|
89 |
def fetch_real_time_trade_data(coin_id):
|
90 |
fallback_data = {'bitcoin': [1633039200000, 42000]}
|
91 |
|
@@ -97,7 +103,7 @@ def fetch_real_time_trade_data(coin_id):
|
|
97 |
data = response.json()
|
98 |
return data['prices'][-1] # Latest price data
|
99 |
except requests.exceptions.RequestException as e:
|
100 |
-
print(f"Error fetching trade data: {e}")
|
101 |
return fallback_data.get(coin_id, "Trade data not available")
|
102 |
|
103 |
# Function to display real-time trade data
|
@@ -109,42 +115,49 @@ def real_time_trade_dashboard(coin):
|
|
109 |
return f"Error in real-time trade dashboard: {e}"
|
110 |
|
111 |
# Gradio Interfaces
|
|
|
|
|
112 |
btc_analysis_interface = gr.Interface(
|
113 |
fn=btc_analysis,
|
114 |
inputs=[],
|
115 |
outputs=gr.Textbox(label="BTC Analysis"),
|
116 |
-
title="BTC
|
117 |
)
|
118 |
|
|
|
119 |
real_time_trade_interface = gr.Interface(
|
120 |
fn=real_time_trade_dashboard,
|
121 |
-
inputs=gr.Dropdown(choices=['bitcoin', 'ethereum'], label="Cryptocurrency"),
|
122 |
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
123 |
-
title="Real-Time Trade Data"
|
124 |
)
|
125 |
|
|
|
126 |
crypto_price_chart_interface = gr.Interface(
|
127 |
fn=plot_historical_prices,
|
128 |
inputs=[
|
129 |
-
gr.Dropdown(choices=['bitcoin', 'ethereum'], label="Cryptocurrency"),
|
130 |
-
gr.Textbox(value="
|
131 |
-
gr.Textbox(value="
|
132 |
],
|
133 |
-
outputs=gr.Plot(label="Price Chart"),
|
134 |
title="Cryptocurrency Price Chart"
|
135 |
)
|
136 |
|
|
|
137 |
current_price_interface = gr.Interface(
|
138 |
fn=fetch_current_price,
|
139 |
-
inputs=gr.Dropdown(choices=['bitcoin', 'ethereum'], label="Cryptocurrency"),
|
140 |
outputs=gr.Textbox(label="Current Price"),
|
141 |
-
title="Current Price"
|
142 |
)
|
143 |
|
144 |
-
#
|
145 |
-
|
146 |
-
[
|
147 |
-
["
|
148 |
)
|
149 |
|
150 |
-
app
|
|
|
|
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objs as go
|
|
|
5 |
|
6 |
+
# Fallback historical data for demonstration purposes
|
7 |
fallback_historical_data = {
|
8 |
'bitcoin': [
|
9 |
[1633046400000, 42000], [1633132800000, 42250], [1633219200000, 41500],
|
|
|
11 |
]
|
12 |
}
|
13 |
|
14 |
+
# Fallback current price data
|
15 |
fallback_current_prices = {
|
16 |
'bitcoin': 42000,
|
17 |
'ethereum': 3100,
|
|
|
30 |
print(f"Error fetching historical data: {e}")
|
31 |
return fallback_historical_data.get(coin_id, f"Error fetching historical data for {coin_id}")
|
32 |
|
33 |
+
# Function to fetch current price from CoinGecko with fallback to CryptoCompare
|
34 |
def fetch_current_price(coin_id):
|
35 |
gecko_url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
36 |
compare_url = f"https://min-api.cryptocompare.com/data/price?fsym={coin_id.upper()}&tsyms=USD"
|
|
|
78 |
except Exception as e:
|
79 |
return f"Error in plotting chart: {e}"
|
80 |
|
81 |
+
# Function to provide AI BTC analysis (mock data for now)
|
82 |
def btc_analysis():
|
83 |
try:
|
84 |
+
return """
|
85 |
+
BTC Analysis:
|
86 |
+
- Order Book: Buying pressure above, selling pressure below.
|
87 |
+
- Technical Indicators: Divergence detected, RSI showing overbought.
|
88 |
+
- Funding Rate: Higher on longs, suggesting bullish sentiment.
|
89 |
+
- On-chain: Strong accumulation signal detected.
|
90 |
+
"""
|
91 |
except Exception as e:
|
92 |
return f"Error in BTC analysis: {e}"
|
93 |
|
94 |
+
# Function to fetch real-time trade data from CoinGecko
|
95 |
def fetch_real_time_trade_data(coin_id):
|
96 |
fallback_data = {'bitcoin': [1633039200000, 42000]}
|
97 |
|
|
|
103 |
data = response.json()
|
104 |
return data['prices'][-1] # Latest price data
|
105 |
except requests.exceptions.RequestException as e:
|
106 |
+
print(f"Error fetching real-time trade data: {e}")
|
107 |
return fallback_data.get(coin_id, "Trade data not available")
|
108 |
|
109 |
# Function to display real-time trade data
|
|
|
115 |
return f"Error in real-time trade dashboard: {e}"
|
116 |
|
117 |
# Gradio Interfaces
|
118 |
+
|
119 |
+
# AI BTC Analysis Interface
|
120 |
btc_analysis_interface = gr.Interface(
|
121 |
fn=btc_analysis,
|
122 |
inputs=[],
|
123 |
outputs=gr.Textbox(label="BTC Analysis"),
|
124 |
+
title="AI-Powered BTC Analysis"
|
125 |
)
|
126 |
|
127 |
+
# Real-time Trade Data Interface
|
128 |
real_time_trade_interface = gr.Interface(
|
129 |
fn=real_time_trade_dashboard,
|
130 |
+
inputs=gr.Dropdown(choices=['bitcoin', 'ethereum', 'binancecoin'], label="Cryptocurrency"),
|
131 |
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
132 |
+
title="Real-Time Cryptocurrency Trade Data"
|
133 |
)
|
134 |
|
135 |
+
# Price Chart Interface
|
136 |
crypto_price_chart_interface = gr.Interface(
|
137 |
fn=plot_historical_prices,
|
138 |
inputs=[
|
139 |
+
gr.Dropdown(choices=['bitcoin', 'ethereum', 'binancecoin'], label="Cryptocurrency"),
|
140 |
+
gr.Textbox(value="2023-01-01", label="From Date (YYYY-MM-DD)"),
|
141 |
+
gr.Textbox(value="2024-01-01", label="To Date (YYYY-MM-DD)")
|
142 |
],
|
143 |
+
outputs=gr.Plot(label="Cryptocurrency Price Chart"),
|
144 |
title="Cryptocurrency Price Chart"
|
145 |
)
|
146 |
|
147 |
+
# Current Price Interface
|
148 |
current_price_interface = gr.Interface(
|
149 |
fn=fetch_current_price,
|
150 |
+
inputs=gr.Dropdown(choices=['bitcoin', 'ethereum', 'binancecoin'], label="Cryptocurrency"),
|
151 |
outputs=gr.Textbox(label="Current Price"),
|
152 |
+
title="Current Cryptocurrency Price"
|
153 |
)
|
154 |
|
155 |
+
# Combining all interfaces into a single tabbed app
|
156 |
+
block = gr.TabbedInterface(
|
157 |
+
[crypto_price_chart_interface, current_price_interface, real_time_trade_interface, btc_analysis_interface],
|
158 |
+
tab_names=["Price Chart", "Current Price", "Real-Time Trade Data", "AI BTC Analysis"]
|
159 |
)
|
160 |
|
161 |
+
# Launch the Gradio app for Hugging Face Spaces
|
162 |
+
if __name__ == "__main__":
|
163 |
+
block.launch()
|