File size: 2,380 Bytes
c2af349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pickle

# Function to load the model from pickle
def load_model(path):
    with open(path, 'rb') as f:
        model = pickle.load(f)
    return model

# Define the main function for predicting stock price
def predict_stock_price(open_val, high_val, low_val, close_val, adj_close_val, volume, model):
    try:
        # Prepare the input data as a numpy array
        X = np.array([[open_val, high_val, low_val, close_val, adj_close_val]])

        # Use the loaded model for prediction
        predicted_price = model.predict(X)

        return predicted_price[0]
    except (ValueError, AttributeError) as e:
        st.error(f'Error occurred: {str(e)}')
        return None

# Define the Streamlit app
def main():
    st.title('Stock Prediction')

    # Load your trained model from pickle
    model_path = 'model.pkl'  # Replace with your actual path
    model = load_model(model_path)

    # Input fields for user to enter stock data
    open_val = st.number_input('Open (Range: 0 to 100000)', min_value=0.0, max_value=100000.0, value=0.0)
    high_val = st.number_input('High (Range: 0 to 100000)', min_value=0.0, max_value=100000.0, value=0.0)
    low_val = st.number_input('Low (Range: 0 to 100000)', min_value=0.0, max_value=100000.0, value=0.0)
    close_val = st.number_input('Close* (Range: 0 to 100000)', min_value=0.0, max_value=100000.0, value=0.0)
    adj_close_val = st.number_input('Adj Close** (Range: 0 to 100000)', min_value=0.0, max_value=100000.0, value=0.0)
    volume = st.number_input('Volume (Range: 0 to 1,000,000,000)', min_value=0.0, max_value=1000000000.0, value=0.0)

    # Predict button
    if st.button('Predict'):
        if any([open_val == 0.0, high_val == 0.0, low_val == 0.0, close_val == 0.0, adj_close_val == 0.0, volume == 0.0]):
            st.error('Please enter valid numeric values for all fields.')
        else:
            # Call prediction function
            predicted_price = predict_stock_price(open_val, high_val, low_val, close_val, adj_close_val, volume, model)
            
            if predicted_price is not None:
                st.success(f'The predicted stock price is: {predicted_price:.2f}')
            else:
                st.error('Failed to predict stock price.')

if __name__ == '__main__':
    main()