Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import gradio as gr | |
| import numpy as np | |
| # Load dataset | |
| DATA_PATH = "AAPL_stock_data_finalversion (1).csv" | |
| data = pd.read_csv(DATA_PATH) | |
| # Ensure the dataset has the required columns | |
| data['Date'] = pd.to_datetime(data['Date']) | |
| data['DateNumeric'] = data['Date'].map(pd.Timestamp.toordinal) | |
| # Define prediction function | |
| def stock_analysis(start_date, end_date): | |
| try: | |
| # Filter data by date range | |
| filtered_data = data[(data['Date'] >= pd.to_datetime(start_date)) & (data['Date'] <= pd.to_datetime(end_date))] | |
| if filtered_data.empty: | |
| return "No data available for the given date range.", None | |
| # Prepare data for forecasting | |
| dates_numeric = filtered_data['DateNumeric'].values | |
| prices = filtered_data['Close'].values | |
| # Simple linear trend for predictions | |
| slope, intercept = np.polyfit(dates_numeric, prices, 1) | |
| # Predict next 30 business days | |
| last_date = filtered_data['Date'].iloc[-1] | |
| future_dates = pd.date_range(last_date, periods=30, freq='B') | |
| future_dates_numeric = future_dates.map(pd.Timestamp.toordinal) | |
| future_predictions = slope * future_dates_numeric + intercept | |
| # Create DataFrame for predictions | |
| future_df = pd.DataFrame({ | |
| 'Date': future_dates, | |
| 'Predicted Close Price': future_predictions | |
| }) | |
| return "Analysis completed!", future_df | |
| except Exception as e: | |
| return f"An error occurred: {str(e)}", None | |
| # Define Gradio app | |
| with gr.Blocks() as app: | |
| gr.Markdown("# Stock Price Prediction App") | |
| with gr.Row(): | |
| start_date_input = gr.Textbox(label="Start Date", placeholder="YYYY-MM-DD") | |
| end_date_input = gr.Textbox(label="End Date", placeholder="YYYY-MM-DD") | |
| analyze_button = gr.Button("Analyze") | |
| output_message = gr.Textbox(label="Message", interactive=False) | |
| prediction_table = gr.Dataframe(label="Future Predictions") | |
| analyze_button.click( | |
| stock_analysis, | |
| inputs=[start_date_input, end_date_input], | |
| outputs=[output_message, prediction_table] | |
| ) | |
| # Launch the app | |
| app.launch() | |