Spaces:
Build error
Build error
Update app.py
Browse files1231 unmodified line567891011121314158 unmodified lines242526272829303132333435363738394041424344458 unmodified lines545556575859606117 unmodified lines79808182import 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"])# Load the image classification pipelineclassifier = pipeline(task="image-classification", model="julien-c/bullish-or-bearish")# Set up the Streamlit app titlest.title("Stock Trend Analyzer: Bullish or Bearish? ππ")8 unmodified linesif st.button("Analyze"):# Fetch stock datastock_data = yf.download(ticker, start=start_date, end=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 dataplt.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)# Save the plot to a bufferbuf = io.BytesIO()plt.savefig(buf, format='png')buf.seek(0)# Convert the buffer to an image8 unmodified lines# 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}%")17 unmodified linesst.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.")12345678910111213141 unmodified line16171819202122232425262728293031328 unmodified lines41424344454647484950515253545556575859606162638 unmodified lines727374757677787917 unmodified lines979899100101102103import 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# 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="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 linesif st.button("Analyze"):# Fetch stock datawith 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 datafig, 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 bufferbuf = io.BytesIO()fig.savefig(buf, format='png')buf.seek(0)# Convert the buffer to an image8 unmodified lines# Display the predictionsst.header("Trend Analysis")for p in predictions[:2]: # Display top 2 predictionssentiment = "Bullish" if "up" in p['label'].lower() else "Bearish"confidence = round(p['score'] * 100, 1)st.subheader(f"{sentiment}: {confidence}%")17 unmodified linesst.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.")# Clean up matplotlib figuresplt.close('all')
app.py+35-14