Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import yfinance as yf
|
2 |
import pandas as pd
|
3 |
-
import plotly.express as px
|
4 |
import plotly.graph_objects as go
|
5 |
import gradio as gr
|
6 |
import timesfm
|
@@ -55,84 +54,36 @@ def stock_forecast(ticker, start_date, end_date):
|
|
55 |
num_jobs=-1,
|
56 |
)
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
# Filter the last 90 days of data
|
62 |
-
last_90_days = combined_df['ds'].max() - pd.Timedelta(days=90)
|
63 |
-
filtered_df = combined_df[combined_df['ds'] >= last_90_days]
|
64 |
|
65 |
# Create an interactive plot with Plotly
|
66 |
-
fig =
|
67 |
-
title=f'{ticker} Stock Price Forecast (Last 90 Days)')
|
68 |
-
|
69 |
-
# Customize the plot for a dark theme
|
70 |
-
fig.update_layout(
|
71 |
-
template="plotly_dark", # Use a dark theme
|
72 |
-
plot_bgcolor='rgba(0, 0, 0, 0)', # Transparent plot background
|
73 |
-
paper_bgcolor='rgba(0, 0, 0, 0)', # Transparent paper background
|
74 |
-
font=dict(color='white'), # White font color
|
75 |
-
legend_title_text='Type',
|
76 |
-
hovermode='x unified', # Show hover info for all series at the same x-value
|
77 |
-
xaxis=dict(
|
78 |
-
rangeselector=dict(
|
79 |
-
buttons=list([
|
80 |
-
dict(count=1, label="1m", step="month", stepmode="backward"),
|
81 |
-
dict(count=6, label="6m", step="month", stepmode="backward"),
|
82 |
-
dict(count=1, label="YTD", step="year", stepmode="todate"),
|
83 |
-
dict(count=1, label="1y", step="year", stepmode="backward"),
|
84 |
-
dict(step="all")
|
85 |
-
]),
|
86 |
-
bgcolor='black', # Dark background for range selector
|
87 |
-
font=dict(color='white') # White font for range selector
|
88 |
-
),
|
89 |
-
rangeslider=dict(visible=True), # Add a range slider
|
90 |
-
type="date",
|
91 |
-
gridcolor='gray', # Grid lines color
|
92 |
-
linecolor='gray', # Axis line color
|
93 |
-
tickfont=dict(color='white') # White tick labels
|
94 |
-
),
|
95 |
-
yaxis=dict(
|
96 |
-
title="Price (USD)",
|
97 |
-
gridcolor='gray', # Grid lines color
|
98 |
-
linecolor='gray', # Axis line color
|
99 |
-
tickfont=dict(color='white') # White tick labels
|
100 |
-
)
|
101 |
-
)
|
102 |
|
103 |
-
#
|
104 |
-
fig.
|
105 |
-
|
106 |
-
|
107 |
-
line_color='blue' # Blue for actual prices
|
108 |
-
)
|
109 |
-
fig.update_traces(
|
110 |
-
line=dict(width=4), # Thicker lines for better visibility
|
111 |
-
selector=dict(name='timesfm'), # Forecasted prices
|
112 |
-
line_color='red' # Purple for forecasted prices
|
113 |
-
)
|
114 |
|
115 |
-
# Add
|
116 |
-
fig.
|
117 |
-
|
118 |
-
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
# Return an empty Plotly figure with an error message
|
124 |
-
error_fig = go.Figure()
|
125 |
-
error_fig.update_layout(
|
126 |
-
template="plotly_dark", # Use a dark theme
|
127 |
-
title=f"Error: {str(e)}",
|
128 |
xaxis_title="Date",
|
129 |
yaxis_title="Price",
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
font=dict(color='white') # White font color
|
134 |
)
|
135 |
-
|
|
|
|
|
|
|
|
|
136 |
|
137 |
# Create Gradio interface with an "Enter" button
|
138 |
with gr.Blocks() as demo:
|
@@ -155,4 +106,4 @@ with gr.Blocks() as demo:
|
|
155 |
)
|
156 |
|
157 |
# Launch the Gradio app
|
158 |
-
demo.launch()
|
|
|
1 |
import yfinance as yf
|
2 |
import pandas as pd
|
|
|
3 |
import plotly.graph_objects as go
|
4 |
import gradio as gr
|
5 |
import timesfm
|
|
|
54 |
num_jobs=-1,
|
55 |
)
|
56 |
|
57 |
+
# Ensure forecast_df has the correct columns
|
58 |
+
forecast_df.rename(columns={"timesfm": "forecast"}, inplace=True)
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Create an interactive plot with Plotly
|
61 |
+
fig = go.Figure()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Add Actual Prices Line
|
64 |
+
fig.add_trace(go.Scatter(x=df["ds"], y=df["y"],
|
65 |
+
mode="lines", name="Actual Prices",
|
66 |
+
line=dict(color="cyan", width=2)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Add Forecasted Prices Line
|
69 |
+
fig.add_trace(go.Scatter(x=forecast_df["ds"], y=forecast_df["forecast"],
|
70 |
+
mode="lines", name="Forecasted Prices",
|
71 |
+
line=dict(color="magenta", width=2, dash="dash")))
|
72 |
|
73 |
+
# Layout Customization
|
74 |
+
fig.update_layout(
|
75 |
+
title=f"{ticker} Stock Price Forecast (Interactive)",
|
|
|
|
|
|
|
|
|
|
|
76 |
xaxis_title="Date",
|
77 |
yaxis_title="Price",
|
78 |
+
template="plotly_dark", # Dark Theme
|
79 |
+
hovermode="x unified", # Show all values on hover
|
80 |
+
legend=dict(bgcolor="black", bordercolor="white"),
|
|
|
81 |
)
|
82 |
+
|
83 |
+
return fig # Return the Plotly figure for Gradio
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
return f"Error: {str(e)}"
|
87 |
|
88 |
# Create Gradio interface with an "Enter" button
|
89 |
with gr.Blocks() as demo:
|
|
|
106 |
)
|
107 |
|
108 |
# Launch the Gradio app
|
109 |
+
demo.launch()
|