Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
@@ -5,11 +16,17 @@ import yfinance as yf
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Load the image classification pipeline
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Set up the Streamlit app title
|
| 15 |
st.title("Stock Trend Analyzer: Bullish or Bearish? 📈📉")
|
|
@@ -24,22 +41,23 @@ end_date = col2.date_input("End date")
|
|
| 24 |
|
| 25 |
if st.button("Analyze"):
|
| 26 |
# Fetch stock data
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
if stock_data.empty:
|
| 30 |
st.error("No data available for the selected stock and date range.")
|
| 31 |
else:
|
| 32 |
# Create a plot of the stock data
|
| 33 |
-
plt.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
# Save the plot to a buffer
|
| 41 |
buf = io.BytesIO()
|
| 42 |
-
|
| 43 |
buf.seek(0)
|
| 44 |
|
| 45 |
# Convert the buffer to an image
|
|
@@ -54,8 +72,8 @@ if st.button("Analyze"):
|
|
| 54 |
|
| 55 |
# Display the predictions
|
| 56 |
st.header("Trend Analysis")
|
| 57 |
-
for p in predictions:
|
| 58 |
-
sentiment = p['label'].
|
| 59 |
confidence = round(p['score'] * 100, 1)
|
| 60 |
st.subheader(f"{sentiment}: {confidence}%")
|
| 61 |
|
|
@@ -79,4 +97,7 @@ if st.button("Analyze"):
|
|
| 79 |
st.info("The stock price remained unchanged over the selected period.")
|
| 80 |
|
| 81 |
else:
|
| 82 |
-
st.info("Enter a stock ticker and select a date range to analyze the trend.")
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
# Check and install required libraries
|
| 5 |
+
required_libraries = ["streamlit", "transformers", "yfinance", "matplotlib", "Pillow"]
|
| 6 |
+
for lib in required_libraries:
|
| 7 |
+
try:
|
| 8 |
+
__import__(lib)
|
| 9 |
+
except ImportError:
|
| 10 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
|
| 11 |
+
|
| 12 |
import streamlit as st
|
| 13 |
from transformers import pipeline
|
| 14 |
from PIL import Image
|
|
|
|
| 16 |
import matplotlib.pyplot as plt
|
| 17 |
import io
|
| 18 |
|
| 19 |
+
# Cache the stock data retrieval
|
| 20 |
+
@st.cache_data
|
| 21 |
+
def get_stock_data(ticker, start_date, end_date):
|
| 22 |
+
return yf.download(ticker, start=start_date, end=end_date)
|
| 23 |
|
| 24 |
# Load the image classification pipeline
|
| 25 |
+
try:
|
| 26 |
+
classifier = pipeline(task="image-classification", model="microsoft/resnet-50")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
st.error(f"Error loading the model: {e}")
|
| 29 |
+
st.stop()
|
| 30 |
|
| 31 |
# Set up the Streamlit app title
|
| 32 |
st.title("Stock Trend Analyzer: Bullish or Bearish? 📈📉")
|
|
|
|
| 41 |
|
| 42 |
if st.button("Analyze"):
|
| 43 |
# Fetch stock data
|
| 44 |
+
with st.spinner("Fetching stock data..."):
|
| 45 |
+
stock_data = get_stock_data(ticker, start_date, end_date)
|
| 46 |
|
| 47 |
if stock_data.empty:
|
| 48 |
st.error("No data available for the selected stock and date range.")
|
| 49 |
else:
|
| 50 |
# Create a plot of the stock data
|
| 51 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 52 |
+
ax.plot(stock_data.index, stock_data['Close'])
|
| 53 |
+
ax.set_title(f"{ticker} Stock Price")
|
| 54 |
+
ax.set_xlabel("Date")
|
| 55 |
+
ax.set_ylabel("Price")
|
| 56 |
+
ax.grid(True)
|
| 57 |
|
| 58 |
# Save the plot to a buffer
|
| 59 |
buf = io.BytesIO()
|
| 60 |
+
fig.savefig(buf, format='png')
|
| 61 |
buf.seek(0)
|
| 62 |
|
| 63 |
# Convert the buffer to an image
|
|
|
|
| 72 |
|
| 73 |
# Display the predictions
|
| 74 |
st.header("Trend Analysis")
|
| 75 |
+
for p in predictions[:2]: # Display top 2 predictions
|
| 76 |
+
sentiment = "Bullish" if "up" in p['label'].lower() else "Bearish"
|
| 77 |
confidence = round(p['score'] * 100, 1)
|
| 78 |
st.subheader(f"{sentiment}: {confidence}%")
|
| 79 |
|
|
|
|
| 97 |
st.info("The stock price remained unchanged over the selected period.")
|
| 98 |
|
| 99 |
else:
|
| 100 |
+
st.info("Enter a stock ticker and select a date range to analyze the trend.")
|
| 101 |
+
|
| 102 |
+
# Clean up matplotlib figures
|
| 103 |
+
plt.close('all')
|