Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,79 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import yfinance as yf
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
+
# Load the image classification pipeline
|
| 9 |
+
classifier = pipeline(task="image-classification", model="julien-c/bullish-or-bearish")
|
| 10 |
|
| 11 |
+
# Set up the Streamlit app title
|
| 12 |
+
st.title("Stock Trend Analyzer: Bullish or Bearish? 📈📉")
|
| 13 |
|
| 14 |
+
# Input for stock ticker
|
| 15 |
+
ticker = st.text_input("Enter a stock ticker (e.g., AAPL, GOOGL):", "AAPL")
|
| 16 |
|
| 17 |
+
# Date range selection
|
| 18 |
+
col1, col2 = st.columns(2)
|
| 19 |
+
start_date = col1.date_input("Start date")
|
| 20 |
+
end_date = col2.date_input("End date")
|
| 21 |
|
| 22 |
+
if st.button("Analyze"):
|
| 23 |
+
# Fetch stock data
|
| 24 |
+
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
| 25 |
+
|
| 26 |
+
if stock_data.empty:
|
| 27 |
+
st.error("No data available for the selected stock and date range.")
|
| 28 |
+
else:
|
| 29 |
+
# Create a plot of the stock data
|
| 30 |
+
plt.figure(figsize=(10, 6))
|
| 31 |
+
plt.plot(stock_data.index, stock_data['Close'])
|
| 32 |
+
plt.title(f"{ticker} Stock Price")
|
| 33 |
+
plt.xlabel("Date")
|
| 34 |
+
plt.ylabel("Price")
|
| 35 |
+
plt.grid(True)
|
| 36 |
+
|
| 37 |
+
# Save the plot to a buffer
|
| 38 |
+
buf = io.BytesIO()
|
| 39 |
+
plt.savefig(buf, format='png')
|
| 40 |
+
buf.seek(0)
|
| 41 |
+
|
| 42 |
+
# Convert the buffer to an image
|
| 43 |
+
image = Image.open(buf)
|
| 44 |
+
|
| 45 |
+
# Display the stock graph
|
| 46 |
+
st.image(image, caption=f"{ticker} Stock Price", use_column_width=True)
|
| 47 |
+
|
| 48 |
+
# Classify the image
|
| 49 |
+
with st.spinner("Analyzing the trend..."):
|
| 50 |
+
predictions = classifier(image)
|
| 51 |
+
|
| 52 |
+
# Display the predictions
|
| 53 |
+
st.header("Trend Analysis")
|
| 54 |
+
for p in predictions:
|
| 55 |
+
sentiment = p['label'].split('_')[0].capitalize()
|
| 56 |
+
confidence = round(p['score'] * 100, 1)
|
| 57 |
+
st.subheader(f"{sentiment}: {confidence}%")
|
| 58 |
+
|
| 59 |
+
# Add color-coded bars for visual representation
|
| 60 |
+
color = "green" if sentiment == "Bullish" else "red"
|
| 61 |
+
st.progress(confidence / 100, text=f"{sentiment} Confidence")
|
| 62 |
+
|
| 63 |
+
# Additional analysis based on the stock data
|
| 64 |
+
price_change = stock_data['Close'].iloc[-1] - stock_data['Close'].iloc[0]
|
| 65 |
+
percent_change = (price_change / stock_data['Close'].iloc[0]) * 100
|
| 66 |
+
|
| 67 |
+
st.subheader("Price Analysis")
|
| 68 |
+
st.write(f"Price change: ${price_change:.2f}")
|
| 69 |
+
st.write(f"Percent change: {percent_change:.2f}%")
|
| 70 |
+
|
| 71 |
+
if percent_change > 0:
|
| 72 |
+
st.success(f"The stock price increased by {percent_change:.2f}% over the selected period.")
|
| 73 |
+
elif percent_change < 0:
|
| 74 |
+
st.error(f"The stock price decreased by {abs(percent_change):.2f}% over the selected period.")
|
| 75 |
+
else:
|
| 76 |
+
st.info("The stock price remained unchanged over the selected period.")
|
| 77 |
|
| 78 |
+
else:
|
| 79 |
+
st.info("Enter a stock ticker and select a date range to analyze the trend.")
|
|
|