Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import yfinance as yf
|
3 |
from prophet import Prophet
|
4 |
-
import
|
5 |
from datetime import datetime
|
6 |
import plotly.graph_objects as go
|
7 |
|
8 |
-
|
9 |
def download_data(ticker, start_date='2010-01-01'):
|
10 |
""" 데이터를 다운로드하고 적절히 포맷팅하는 함수 """
|
11 |
data = yf.download(ticker, start=start_date)
|
@@ -20,38 +19,33 @@ def download_data(ticker, start_date='2010-01-01'):
|
|
20 |
return data
|
21 |
|
22 |
def predict_future_prices(ticker, periods=1825): # 5년간의 데이터 예측
|
23 |
-
data =
|
24 |
-
data.rename(columns={'Date': 'ds', 'Adj Close': 'y'}, inplace=True)
|
25 |
|
|
|
26 |
model = Prophet(daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=True)
|
27 |
model.fit(data)
|
28 |
|
|
|
29 |
future = model.make_future_dataframe(periods=periods, freq='D')
|
30 |
forecast = model.predict(future)
|
31 |
|
32 |
-
#
|
33 |
-
forecast['ds'] = forecast['ds'].dt.strftime('%Y-%m-%d')
|
34 |
-
|
35 |
fig = go.Figure(data=[go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Forecast')])
|
36 |
-
fig
|
37 |
-
|
38 |
-
return fig, forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].to_dict(orient='records')
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
# Gradio 인터페이스 설정
|
44 |
with gr.Blocks() as app:
|
45 |
with gr.Row():
|
46 |
-
ticker_input = gr.Textbox(value="AAPL", label="Enter Stock Ticker")
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
forecast_button.click(
|
53 |
fn=predict_future_prices,
|
54 |
-
inputs=ticker_input,
|
55 |
outputs=[forecast_chart, forecast_data]
|
56 |
)
|
57 |
|
|
|
1 |
import gradio as gr
|
2 |
import yfinance as yf
|
3 |
from prophet import Prophet
|
4 |
+
import pandas as pd
|
5 |
from datetime import datetime
|
6 |
import plotly.graph_objects as go
|
7 |
|
|
|
8 |
def download_data(ticker, start_date='2010-01-01'):
|
9 |
""" 데이터를 다운로드하고 적절히 포맷팅하는 함수 """
|
10 |
data = yf.download(ticker, start=start_date)
|
|
|
19 |
return data
|
20 |
|
21 |
def predict_future_prices(ticker, periods=1825): # 5년간의 데이터 예측
|
22 |
+
data = download_data(ticker)
|
|
|
23 |
|
24 |
+
# Prophet 모델 생성 및 훈련
|
25 |
model = Prophet(daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=True)
|
26 |
model.fit(data)
|
27 |
|
28 |
+
# 미래 데이터프레임 생성 및 예측
|
29 |
future = model.make_future_dataframe(periods=periods, freq='D')
|
30 |
forecast = model.predict(future)
|
31 |
|
32 |
+
# 예측 결과를 그래프로 표현
|
|
|
|
|
33 |
fig = go.Figure(data=[go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='Forecast')])
|
34 |
+
return fig, forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']] # Pydantic과 같은 엄격한 형식 검사 없이 DataFrame으로 직접 전달
|
|
|
|
|
35 |
|
36 |
+
# Gradio 인터페이스 설정 및 실행
|
|
|
|
|
|
|
37 |
with gr.Blocks() as app:
|
38 |
with gr.Row():
|
39 |
+
ticker_input = gr.Textbox(value="AAPL", label="Enter Stock Ticker for Forecast")
|
40 |
+
periods_input = gr.Number(value=1825, label="Forecast Period (days)")
|
41 |
+
forecast_button = gr.Button("Generate Forecast")
|
42 |
+
|
43 |
+
forecast_chart = gr.Plot(label="Forecast Chart")
|
44 |
+
forecast_data = gr.Dataframe(label="Forecast Data")
|
45 |
|
46 |
forecast_button.click(
|
47 |
fn=predict_future_prices,
|
48 |
+
inputs=[ticker_input, periods_input],
|
49 |
outputs=[forecast_chart, forecast_data]
|
50 |
)
|
51 |
|