Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940import streamlit as stfrom transformers import pipelinefrom PIL import Imageimport io# Load the image classification pipelinetry:classifier = pipeline(task="image-classification", model="microsoft/resnet-50")except Exception as e:st.error(f"Error loading the model: {e}")st.stop()# Set up the Streamlit app titlest.title("Stock Chart Analyzer: Bullish or Bearish? 📈📉")# File uploader for image inputuploaded_file = st.file_uploader("Upload a stock chart image", type=["jpg", "jpeg", "png"])if uploaded_file is not None:# Display the uploaded imageimage = Image.open(uploaded_file)st.image(image, caption="Uploaded Stock Chart", use_column_width=True)# Classify the imagewith st.spinner("Analyzing the trend..."):predictions = classifier(image)# Display the predictionsst.header("Trend Analysis")for p in predictions:sentiment = p['label'].split('_')[0].capitalize()confidence = round(p['score'] * 100, 1)st.subheader(f"{sentiment}: {confidence}%")# Add color-coded bars for visual representationcolor = "green" if sentiment == "Bullish" else "red"st.progress(confidence / 100, text=f"{sentiment} Confidence")else:st.info("Please upload a stock chart image to analyze the trend.")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import streamlit as stfrom PIL import Imageimport torchfrom torchvision import transformsfrom torchvision.models import resnet50import io# Load pre-trained ResNet50 modelmodel = resnet50(pretrained=True)model.fc = torch.nn.Linear(model.fc.in_features, 2) # 2 classes: bullish and bearishmodel.load_state_dict(torch.load('path_to_your_trained_model.pth'))model.eval()# Define image transformationtransform = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])# Streamlit appst.title("Stock Trend Predictor: Bullish or Bearish?")uploaded_file = st.file_uploader("Upload a stock graph image", type=["jpg", "jpeg", "png"])if uploaded_file is not None:image = Image.open(uploaded_file).convert('RGB')st.image(image, caption="Uploaded Stock Graph", use_column_width=True)# Preprocess the imageinput_tensor = transform(image).unsqueeze(0)# Make predictionwith torch.no_grad():output = model(input_tensor)probabilities = torch.nn.functional.softmax(output[0], dim=0)predicted_class = torch.argmax(probabilities).item()# Display predictionst.header("Prediction")if predicted_class == 0:sentiment = "Bearish"color = "red"else:sentiment = "Bullish"color = "green"confidence = probabilities[predicted_class].item() * 100st.subheader(f"{sentiment}: {confidence:.2f}%")st.progress(confidence / 100, text=f"{sentiment} Confidence")else:st.info("Please upload a stock graph image to get a prediction.")
app.py+43-30