Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
st.stop()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if uploaded_file is not None:
|
| 20 |
-
|
| 21 |
-
image =
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
color = "
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
else:
|
| 40 |
-
st.info("Please upload a stock
|
|
|
|
| 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.")
|