Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -190,7 +190,12 @@ class StockPredictorApp:
|
|
| 190 |
)
|
| 191 |
|
| 192 |
# Add a vertical line to separate historical and predicted
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
line_dash="solid",
|
| 195 |
line_color="gray",
|
| 196 |
annotation_text="Forecast Start")
|
|
@@ -320,14 +325,18 @@ def predict_stock_price_safe(ticker, num_days):
|
|
| 320 |
Place these files in the same directory as the app.
|
| 321 |
"""
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
# Normal prediction flow
|
| 333 |
return predict_stock_price(ticker, num_days)
|
|
@@ -384,9 +393,6 @@ with gr.Blocks(title="Stock Price Forecaster", theme=gr.themes.Soft()) as app:
|
|
| 384 |
|
| 385 |
# Add examples (use safe function)
|
| 386 |
gr.Examples(
|
| 387 |
-
examples=[
|
| 388 |
-
["AAPL", 7],
|
| 389 |
-
],
|
| 390 |
inputs=[ticker_input, days_input],
|
| 391 |
outputs=[plot_output, summary_output, predictions_table],
|
| 392 |
fn=predict_stock_price_safe,
|
|
@@ -408,9 +414,6 @@ with gr.Blocks(title="Stock Price Forecaster", theme=gr.themes.Soft()) as app:
|
|
| 408 |
- **LSTM**: Long Short-Term Memory neural network with 3 layers and dropout regularization
|
| 409 |
- **Training Data**: Historical stock prices from Yahoo Finance
|
| 410 |
|
| 411 |
-
### ⚠️ Disclaimer
|
| 412 |
-
This is for educational purposes only. Stock predictions are inherently uncertain and should not be used as the sole basis for investment decisions.
|
| 413 |
-
"""
|
| 414 |
)
|
| 415 |
|
| 416 |
# Launch the app
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
# Add a vertical line to separate historical and predicted
|
| 193 |
+
# Convert timestamp to string to avoid Plotly issues
|
| 194 |
+
last_date = historical_data.index[-1]
|
| 195 |
+
if hasattr(last_date, 'strftime'):
|
| 196 |
+
last_date = last_date.strftime('%Y-%m-%d')
|
| 197 |
+
|
| 198 |
+
fig.add_vline(x=last_date,
|
| 199 |
line_dash="solid",
|
| 200 |
line_color="gray",
|
| 201 |
annotation_text="Forecast Start")
|
|
|
|
| 325 |
Place these files in the same directory as the app.
|
| 326 |
"""
|
| 327 |
|
| 328 |
+
try:
|
| 329 |
+
historical_data, predictions_df = create_demo_predictions(ticker, num_days)
|
| 330 |
+
fig = predictor.create_plot(historical_data, predictions_df, f"{ticker} (DEMO)")
|
| 331 |
+
|
| 332 |
+
predictions_display = predictions_df.copy()
|
| 333 |
+
predictions_display['Date'] = predictions_display['Date'].dt.strftime('%Y-%m-%d')
|
| 334 |
+
predictions_display = predictions_display.round(2)
|
| 335 |
+
|
| 336 |
+
return fig, demo_msg, predictions_display
|
| 337 |
+
except Exception as e:
|
| 338 |
+
error_msg = f"Error creating demo predictions: {str(e)}"
|
| 339 |
+
return None, error_msg, empty_df
|
| 340 |
|
| 341 |
# Normal prediction flow
|
| 342 |
return predict_stock_price(ticker, num_days)
|
|
|
|
| 393 |
|
| 394 |
# Add examples (use safe function)
|
| 395 |
gr.Examples(
|
|
|
|
|
|
|
|
|
|
| 396 |
inputs=[ticker_input, days_input],
|
| 397 |
outputs=[plot_output, summary_output, predictions_table],
|
| 398 |
fn=predict_stock_price_safe,
|
|
|
|
| 414 |
- **LSTM**: Long Short-Term Memory neural network with 3 layers and dropout regularization
|
| 415 |
- **Training Data**: Historical stock prices from Yahoo Finance
|
| 416 |
|
|
|
|
|
|
|
|
|
|
| 417 |
)
|
| 418 |
|
| 419 |
# Launch the app
|