Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime, date, timedelta
|
|
|
|
|
|
|
| 3 |
from config import index_options, time_intervals, START_DATE, END_DATE, FORECAST_PERIOD
|
| 4 |
from data_fetcher import get_stocks_from_index
|
| 5 |
from stock_analysis import get_stock_graph_and_info
|
|
@@ -10,6 +12,12 @@ def validate_date(date_string):
|
|
| 10 |
except ValueError:
|
| 11 |
return None
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
demo = gr.Blocks()
|
| 14 |
|
| 15 |
with demo:
|
|
@@ -19,7 +27,6 @@ with demo:
|
|
| 19 |
d4 = gr.Radio(['Line Graph', 'Candlestick Graph'], label='Select Graph Type', value='Line Graph', interactive=True)
|
| 20 |
d5 = gr.Dropdown(['ARIMA', 'Prophet', 'LSTM'], label='Select Forecasting Method', value='ARIMA', interactive=True)
|
| 21 |
|
| 22 |
-
# New date inputs using Textbox
|
| 23 |
date_start = gr.Textbox(label="Start Date (YYYY-MM-DD)", value=START_DATE.strftime("%Y-%m-%d"))
|
| 24 |
date_end = gr.Textbox(label="End Date (YYYY-MM-DD)", value=END_DATE.strftime("%Y-%m-%d"))
|
| 25 |
|
|
@@ -40,17 +47,21 @@ with demo:
|
|
| 40 |
end = validate_date(end_date)
|
| 41 |
|
| 42 |
if start is None or end is None:
|
| 43 |
-
return "Invalid date format. Please use YYYY-MM-DD.", None
|
| 44 |
|
| 45 |
if start > end:
|
| 46 |
-
return "Start date must be before end date.", None
|
| 47 |
|
| 48 |
try:
|
| 49 |
fig, fundamentals = get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start, end)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return fig, fundamentals
|
| 51 |
except Exception as e:
|
| 52 |
error_message = f"An error occurred: {str(e)}"
|
| 53 |
-
return error_message,
|
| 54 |
|
| 55 |
d1.change(update_stock_options, d1, d2)
|
| 56 |
d2.change(process_inputs, inputs, outputs)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datetime import datetime, date, timedelta
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
import pandas as pd
|
| 5 |
from config import index_options, time_intervals, START_DATE, END_DATE, FORECAST_PERIOD
|
| 6 |
from data_fetcher import get_stocks_from_index
|
| 7 |
from stock_analysis import get_stock_graph_and_info
|
|
|
|
| 12 |
except ValueError:
|
| 13 |
return None
|
| 14 |
|
| 15 |
+
def create_error_plot(error_message):
|
| 16 |
+
fig = go.Figure()
|
| 17 |
+
fig.add_annotation(x=0.5, y=0.5, text=error_message, showarrow=False, font_size=20)
|
| 18 |
+
fig.update_layout(title="Error", xaxis_title="", yaxis_title="")
|
| 19 |
+
return fig
|
| 20 |
+
|
| 21 |
demo = gr.Blocks()
|
| 22 |
|
| 23 |
with demo:
|
|
|
|
| 27 |
d4 = gr.Radio(['Line Graph', 'Candlestick Graph'], label='Select Graph Type', value='Line Graph', interactive=True)
|
| 28 |
d5 = gr.Dropdown(['ARIMA', 'Prophet', 'LSTM'], label='Select Forecasting Method', value='ARIMA', interactive=True)
|
| 29 |
|
|
|
|
| 30 |
date_start = gr.Textbox(label="Start Date (YYYY-MM-DD)", value=START_DATE.strftime("%Y-%m-%d"))
|
| 31 |
date_end = gr.Textbox(label="End Date (YYYY-MM-DD)", value=END_DATE.strftime("%Y-%m-%d"))
|
| 32 |
|
|
|
|
| 47 |
end = validate_date(end_date)
|
| 48 |
|
| 49 |
if start is None or end is None:
|
| 50 |
+
return create_error_plot("Invalid date format. Please use YYYY-MM-DD."), None
|
| 51 |
|
| 52 |
if start > end:
|
| 53 |
+
return create_error_plot("Start date must be before end date."), None
|
| 54 |
|
| 55 |
try:
|
| 56 |
fig, fundamentals = get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start, end)
|
| 57 |
+
if not isinstance(fig, go.Figure):
|
| 58 |
+
raise ValueError("Expected a plotly Figure object")
|
| 59 |
+
if not isinstance(fundamentals, pd.DataFrame):
|
| 60 |
+
raise ValueError("Expected a pandas DataFrame for fundamentals")
|
| 61 |
return fig, fundamentals
|
| 62 |
except Exception as e:
|
| 63 |
error_message = f"An error occurred: {str(e)}"
|
| 64 |
+
return create_error_plot(error_message), pd.DataFrame()
|
| 65 |
|
| 66 |
d1.change(update_stock_options, d1, d2)
|
| 67 |
d2.change(process_inputs, inputs, outputs)
|