Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yfinance as yf
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
from statsmodels.tsa.arima.model import ARIMA
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
def fetch_eth_price(period):
|
9 |
+
eth = yf.Ticker("ETH-USD")
|
10 |
+
if period == '1d':
|
11 |
+
data = eth.history(period="1d", interval="1m")
|
12 |
+
predict_steps = 60 # Next 60 minutes
|
13 |
+
elif period == '5d':
|
14 |
+
data = eth.history(period="5d", interval="15m")
|
15 |
+
predict_steps = 96 # Next 24 hours
|
16 |
+
elif period == '1wk':
|
17 |
+
data = eth.history(period="1wk", interval="30m")
|
18 |
+
predict_steps = 336 # Next 7 days
|
19 |
+
elif period == '1mo':
|
20 |
+
data = eth.history(period="1mo", interval="1h")
|
21 |
+
predict_steps = 720 # Next 30 days
|
22 |
+
else:
|
23 |
+
return None, None
|
24 |
+
|
25 |
+
return data, predict_steps
|
26 |
+
|
27 |
+
def make_predictions(data, predict_steps):
|
28 |
+
model = ARIMA(data['Close'], order=(5, 1, 0))
|
29 |
+
model_fit = model.fit()
|
30 |
+
forecast = model_fit.forecast(steps=predict_steps)
|
31 |
+
|
32 |
+
future_dates = pd.date_range(start=data.index[-1], periods=predict_steps+1, closed='right')
|
33 |
+
forecast_df = pd.DataFrame(forecast, index=future_dates, columns=['Prediction'])
|
34 |
+
|
35 |
+
return forecast_df
|
36 |
+
|
37 |
+
def plot_eth(period):
|
38 |
+
data, predict_steps = fetch_eth_price(period)
|
39 |
+
forecast_df = make_predictions(data, predict_steps)
|
40 |
+
|
41 |
+
fig = go.Figure()
|
42 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='ETH Price'))
|
43 |
+
fig.add_trace(go.Scatter(x=forecast_df.index, y=forecast_df['Prediction'], mode='lines', name='Prediction', line=dict(dash='dash')))
|
44 |
+
fig.update_layout(title=f"ETH Price and Predictions ({period})", xaxis_title="Date", yaxis_title="Price (USD)")
|
45 |
+
|
46 |
+
return fig
|
47 |
+
|
48 |
+
def refresh_predictions(period):
|
49 |
+
return plot_eth(period)
|
50 |
+
|
51 |
+
with gr.Blocks() as iface:
|
52 |
+
period = gr.Radio(["1d", "5d", "1wk", "1mo"], label="Select Period")
|
53 |
+
plot = gr.Plot()
|
54 |
+
refresh_button = gr.Button("Refresh Predictions and Prices")
|
55 |
+
|
56 |
+
period.change(fn=plot_eth, inputs=period, outputs=plot)
|
57 |
+
refresh_button.click(fn=refresh_predictions, inputs=period, outputs=plot)
|
58 |
+
|
59 |
+
iface.launch()
|