File size: 2,166 Bytes
874f471
 
934ea44
874f471
 
04d0fca
874f471
 
 
 
934ea44
874f471
11f5625
874f471
 
 
934ea44
874f471
 
11f5625
874f471
11f5625
d5dd2e2
 
874f471
11f5625
d5dd2e2
934ea44
d5dd2e2
934ea44
 
d5dd2e2
 
934ea44
d5dd2e2
934ea44
 
d5dd2e2
934ea44
874f471
11f5625
874f471
 
11f5625
874f471
 
 
11f5625
874f471
 
 
 
 
 
 
 
 
 
 
 
 
11f5625
874f471
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()