Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import yfinance as yf | |
import requests | |
from bs4 import BeautifulSoup | |
import xml.etree.ElementTree as ET | |
import datetime | |
# Set up the models for Named Entity Recognition, Sentiment Analysis, and Text Generation | |
ner_model = "Cassie-0121/fin-bert-finetuned-ner" | |
sentiment_model = "yiyanghkust/finbert-tone" | |
text_gen_model = "gpt2" | |
ner = pipeline("ner", model=ner_model) | |
sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_model) | |
text_generator = pipeline("text-generation", model=text_gen_model) | |
# App title | |
st.title("AI-Powered Financial Analysis App") | |
# Sidebar for selecting stock | |
st.sidebar.header("Select Stock for Analysis") | |
top_25_stocks = [ | |
"AAPL", "MSFT", "GOOGL", "AMZN", "TSLA", "META", "NVDA", "BRK-B", "JNJ", "JPM", | |
"V", "PG", "UNH", "DIS", "MA", "HD", "BAC", "XOM", "VZ", "PFE", | |
"KO", "PEP", "MRK", "WMT", "CSCO" | |
] | |
selected_stock = st.sidebar.selectbox("Choose a stock for analysis:", top_25_stocks) | |
# Function to fetch the latest stock data using yfinance with dynamic date handling | |
def get_stock_data(ticker): | |
stock = yf.Ticker(ticker) | |
today = datetime.datetime.now().date() | |
# Try fetching the data for today, but fall back to the most recent available date if markets are closed | |
stock_info = stock.history(period="5d") # Fetch data for the last 5 days to account for weekends or holidays | |
if not stock_info.empty: | |
# Get the most recent available data | |
latest_data = stock_info.iloc[-1] | |
return latest_data | |
else: | |
st.warning("No recent stock data available. Please try again later.") | |
return None | |
# Display stock data | |
st.header(f"Latest Stock Data for {selected_stock}") | |
stock_data = get_stock_data(selected_stock) | |
if stock_data is not None: | |
st.write(stock_data) | |
else: | |
st.write("Could not retrieve stock data at this time.") | |
# Other components like fetching news, NER, and sentiment analysis would remain the same | |