import streamlit as st import requests from bs4 import BeautifulSoup from transformers import pipeline import yfinance as yf # Set up models 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) # Function to fetch stock news using web scraping from Yahoo Finance def get_stock_news(stock_symbol): url = f'https://finance.yahoo.com/quote/{stock_symbol}?p={stock_symbol}' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'} # Send request and parse the page response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') # Extracting news headlines (For Yahoo Finance, it's under the 'Recent News' section) headlines = [] news_section = soup.find_all('li', {'class': 'js-stream-content'}) for news_item in news_section: headline = news_item.find('h3') if headline: headlines.append(headline.get_text()) return headlines # Function to fetch stock data using Yahoo Finance (yfinance library) def get_stock_data(stock_symbol): stock = yf.Ticker(stock_symbol) stock_info = stock.history(period="1d") # Fetching the latest stock data (1-day) return stock_info # App title st.title("AI-Powered Financial Analysis App") # Sidebar for stock symbol input st.sidebar.header("Stock Data & Analysis") stock_symbol = st.sidebar.text_input("Enter Stock Symbol (e.g., AAPL for Apple):", "AAPL") # Fetch stock data and news if stock_symbol: st.sidebar.write(f"Fetching data for {stock_symbol}...") # Fetch stock news stock_news = get_stock_news(stock_symbol) # Fetch stock data (latest price, volume, etc.) stock_data = get_stock_data(stock_symbol) # Display stock data st.subheader(f"Latest Stock Data for {stock_symbol}") st.write(stock_data) # Display stock news st.subheader(f"Latest News for {stock_symbol}") for news in stock_news: st.write(news) # Text for analysis input_text = ' '.join(stock_news) # Combine news for analysis # Display extracted entities st.subheader("Extracted Key Financial Entities") if input_text: entities = ner(input_text) for entity in entities: st.write(f"Entity: {entity['word']}, Label: {entity.get('entity', 'N/A')}, Score: {entity.get('score', 0.0):.2f}") # Sentiment Analysis st.subheader("Sentiment Analysis") if input_text: sentiment = sentiment_analyzer(input_text) for result in sentiment: st.write(f"Sentiment: {result['label']}, Score: {result['score']:.2f}") # Investment Advice or Strategy st.subheader("Investment Advice or Strategy") if input_text: prompt = f"Provide a focused and complete investment strategy for {stock_symbol} based on the latest news and trends." advice = text_generator(prompt + input_text, max_length=100, num_return_sequences=1, do_sample=True, temperature=0.7) st.write(advice[0]['generated_text']) else: st.write("Please enter a valid stock symbol to get analysis.") # Optional: Footer for extra branding st.sidebar.write("Powered by Hugging Face, Streamlit, and Web Scraping")