Kaylah072001 commited on
Commit
b8a59e8
·
verified ·
1 Parent(s): 79ab59b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -30
app.py CHANGED
@@ -1,40 +1,53 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
  from PIL import Image
 
 
 
4
  import io
5
 
6
- # Load the image classification pipeline
7
- try:
8
- classifier = pipeline(task="image-classification", model="microsoft/resnet-50")
9
- except Exception as e:
10
- st.error(f"Error loading the model: {e}")
11
- st.stop()
12
 
13
- # Set up the Streamlit app title
14
- st.title("Stock Chart Analyzer: Bullish or Bearish? 📈📉")
 
 
 
 
15
 
16
- # File uploader for image input
17
- uploaded_file = st.file_uploader("Upload a stock chart image", type=["jpg", "jpeg", "png"])
 
 
18
 
19
  if uploaded_file is not None:
20
- # Display the uploaded image
21
- image = Image.open(uploaded_file)
22
- st.image(image, caption="Uploaded Stock Chart", use_column_width=True)
23
-
24
- # Classify the image
25
- with st.spinner("Analyzing the trend..."):
26
- predictions = classifier(image)
27
-
28
- # Display the predictions
29
- st.header("Trend Analysis")
30
- for p in predictions:
31
- sentiment = p['label'].split('_')[0].capitalize()
32
- confidence = round(p['score'] * 100, 1)
33
- st.subheader(f"{sentiment}: {confidence}%")
34
-
35
- # Add color-coded bars for visual representation
36
- color = "green" if sentiment == "Bullish" else "red"
37
- st.progress(confidence / 100, text=f"{sentiment} Confidence")
 
 
 
 
 
 
38
 
39
  else:
40
- st.info("Please upload a stock chart image to analyze the trend.")
 
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
  import io
7
 
8
+ # Load pre-trained ResNet50 model
9
+ model = resnet50(pretrained=True)
10
+ model.fc = torch.nn.Linear(model.fc.in_features, 2) # 2 classes: bullish and bearish
11
+ model.load_state_dict(torch.load('path_to_your_trained_model.pth'))
12
+ model.eval()
 
13
 
14
+ # Define image transformation
15
+ transform = transforms.Compose([
16
+ transforms.Resize((224, 224)),
17
+ transforms.ToTensor(),
18
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
19
+ ])
20
 
21
+ # Streamlit app
22
+ st.title("Stock Trend Predictor: Bullish or Bearish?")
23
+
24
+ uploaded_file = st.file_uploader("Upload a stock graph image", type=["jpg", "jpeg", "png"])
25
 
26
  if uploaded_file is not None:
27
+ image = Image.open(uploaded_file).convert('RGB')
28
+ st.image(image, caption="Uploaded Stock Graph", use_column_width=True)
29
+
30
+ # Preprocess the image
31
+ input_tensor = transform(image).unsqueeze(0)
32
+
33
+ # Make prediction
34
+ with torch.no_grad():
35
+ output = model(input_tensor)
36
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
37
+ predicted_class = torch.argmax(probabilities).item()
38
+
39
+ # Display prediction
40
+ st.header("Prediction")
41
+ if predicted_class == 0:
42
+ sentiment = "Bearish"
43
+ color = "red"
44
+ else:
45
+ sentiment = "Bullish"
46
+ color = "green"
47
+
48
+ confidence = probabilities[predicted_class].item() * 100
49
+ st.subheader(f"{sentiment}: {confidence:.2f}%")
50
+ st.progress(confidence / 100, text=f"{sentiment} Confidence")
51
 
52
  else:
53
+ st.info("Please upload a stock graph image to get a prediction.")