Kaylah072001 commited on
Commit
566176f
·
verified ·
1 Parent(s): 98f25f8

Upload stock_predictor copy.py

Browse files
Files changed (1) hide show
  1. stock_predictor copy.py +67 -0
stock_predictor copy.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
+ from torchvision import transforms
5
+ from torchvision.models import resnet50
6
+
7
+ # Streamlit app
8
+ st.title("Stock Trend Predictor: Bullish or Bearish?")
9
+
10
+ # Load pre-trained ResNet50 model
11
+ @st.cache_resource
12
+ def load_model():
13
+ model = resnet50(pretrained=True)
14
+ model.fc = torch.nn.Linear(model.fc.in_features, 2) # 2 classes: bullish and bearish
15
+ try:
16
+ state_dict = torch.load('/Users/kaylahoffman/Desktop/stock_prediction_model_new.h5', map_location=torch.device('cpu'))
17
+ model.load_state_dict(state_dict)
18
+ st.success("Image classification model loaded successfully!")
19
+ except Exception as e:
20
+ st.error(f"Error loading image classification model: {e}")
21
+ st.warning("Using untrained model. Predictions may not be accurate.")
22
+ model.eval()
23
+ return model
24
+
25
+ model = load_model()
26
+
27
+ # Define image transformation
28
+ transform = transforms.Compose([
29
+ transforms.Resize((224, 224)),
30
+ transforms.ToTensor(),
31
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32
+ ])
33
+
34
+ # Image upload and analysis
35
+ uploaded_file = st.file_uploader("Upload a stock graph image", type=["jpg", "jpeg", "png"])
36
+
37
+ if uploaded_file is not None:
38
+ try:
39
+ image = Image.open(uploaded_file).convert('RGB')
40
+ st.image(image, caption="Uploaded Stock Graph", use_column_width=True)
41
+
42
+ # Preprocess the image
43
+ input_tensor = transform(image).unsqueeze(0)
44
+
45
+ # Make prediction
46
+ with torch.no_grad():
47
+ output = model(input_tensor)
48
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
49
+ predicted_class = torch.argmax(probabilities).item()
50
+
51
+ # Display prediction
52
+ st.header("Current Trend Prediction")
53
+ if predicted_class == 0:
54
+ sentiment = "Bearish"
55
+ color = "red"
56
+ else:
57
+ sentiment = "Bullish"
58
+ color = "green"
59
+
60
+ confidence = probabilities[predicted_class].item() * 100
61
+ st.subheader(f"{sentiment}: {confidence:.2f}%")
62
+ st.progress(confidence / 100, text=f"{sentiment} Confidence")
63
+
64
+ except Exception as e:
65
+ st.error(f"Error processing image: {e}")
66
+
67
+ st.write("Note: This is a simplified model and should not be used for actual trading decisions.")