File size: 1,890 Bytes
757d747
 
 
c7853dc
757d747
 
 
 
c7853dc
757d747
 
18cc71d
757d747
c7853dc
 
757d747
c7853dc
757d747
 
c7853dc
757d747
c7853dc
757d747
 
 
c7853dc
757d747
 
 
 
 
c7853dc
757d747
 
 
c7853dc
 
 
757d747
 
 
 
c7853dc
757d747
 
 
 
 
c7853dc
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
import streamlit as st
from PIL import Image
import torch
from transformers import AutoModelForImageClassification, AutoFeatureExtractor

# Streamlit app
st.title("Stock Trend Predictor: Bullish or Bearish?")

# Load pre-trained model from Hugging Face
@st.cache_resource
def load_model():
    model_name = "Kaylah072001/stock_prediction_model.h5"  # Replace with your actual model name on Hugging Face
    try:
        model = AutoModelForImageClassification.from_pretrained(model_name)
        feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
        st.success("Model loaded successfully!")
        return model, feature_extractor
    except Exception as e:
        st.error(f"Error loading model: {e}")
        return None, None

model, feature_extractor = load_model()

uploaded_file = st.file_uploader("Upload a stock graph image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None and model is not None and feature_extractor is not None:
    try:
        image = Image.open(uploaded_file).convert('RGB')
        st.image(image, caption="Uploaded Stock Graph", use_column_width=True)

        # Preprocess the image
        inputs = feature_extractor(images=image, return_tensors="pt")

        # Make prediction
        with torch.no_grad():
            outputs = model(**inputs)
            logits = outputs.logits
            probabilities = torch.nn.functional.softmax(logits[0], dim=0)
            predicted_class = torch.argmax(probabilities).item()

        # Display prediction
        st.header("Prediction")
        sentiment = "Bullish" if predicted_class == 1 else "Bearish"
        confidence = probabilities[predicted_class].item() * 100
        st.subheader(f"{sentiment}: {confidence:.2f}%")
        st.progress(confidence / 100, text=f"{sentiment} Confidence")

    except Exception as e:
        st.error(f"Error processing image: {e}")