Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141 unmodified line161718891920212223242512262728293031328 unmodified lines414243274445464748495033343536373851525354555657585942606162638 unmodified lines7273745758757677787917 unmodified lines97989982100101102103import sysimport subprocess
# Check and install required librariesrequired_libraries = ["streamlit", "transformers", "yfinance", "matplotlib", "Pillow"]for lib in required_libraries: try: __import__(lib) except ImportError: subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
import streamlit as stfrom transformers import pipelinefrom PIL import Image1 unmodified lineimport matplotlib.pyplot as pltimport io
# Install yfinance if not already installedsubprocess.check_call([sys.executable, "-m", "pip", "install", "yfinance"])# Cache the stock data retrieval@st.cache_datadef get_stock_data(ticker, start_date, end_date): return yf.download(ticker, start=start_date, end=end_date)
# Load the image classification pipelinetry:classifier = pipeline(task="image-classification", model="julien-c/bullish-or-bearish") 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 Trend Analyzer: Bullish or Bearish? 📈📉")8 unmodified lines
if st.button("Analyze"): # Fetch stock data stock_data = yf.download(ticker, start=start_date, end=end_date) with st.spinner("Fetching stock data..."): stock_data = get_stock_data(ticker, start_date, end_date) if stock_data.empty: st.error("No data available for the selected stock and date range.") else: # Create a plot of the stock data plt.figure(figsize=(10, 6)) plt.plot(stock_data.index, stock_data['Close']) plt.title(f"{ticker} Stock Price") plt.xlabel("Date") plt.ylabel("Price") plt.grid(True) fig, ax = plt.subplots(figsize=(10, 6)) ax.plot(stock_data.index, stock_data['Close']) ax.set_title(f"{ticker} Stock Price") ax.set_xlabel("Date") ax.set_ylabel("Price") ax.grid(True) # Save the plot to a buffer buf = io.BytesIO() plt.savefig(buf, format='png') fig.savefig(buf, format='png') buf.seek(0) # Convert the buffer to an image8 unmodified lines # Display the predictions st.header("Trend Analysis") for p in predictions: sentiment = p['label'].split('_')[0].capitalize() for p in predictions[:2]: # Display top 2 predictions sentiment = "Bullish" if "up" in p['label'].lower() else "Bearish" confidence = round(p['score'] * 100, 1) st.subheader(f"{sentiment}: {confidence}%") 17 unmodified lines st.info("The stock price remained unchanged over the selected period.")
else: st.info("Enter a stock ticker and select a date range to analyze the trend.") st.info("Enter a stock ticker and select a date range to analyze the trend.")
# Clean up matplotlib figuresplt.close('all')app.py+35-14