Spaces:
Runtime error
Runtime error
import streamlit as st | |
import yfinance as yf | |
import pandas as pd | |
import cufflinks as cf | |
import datetime | |
import plotly.graph_objects as go | |
from bs4 import BeautifulSoup | |
import requests | |
import os | |
from datetime import date, timedelta | |
from transformers import pipeline | |
import torch | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
from contextlib import redirect_stdout | |
from io import StringIO | |
def scrapper (start_date): | |
d0 = start_date | |
d1 = datetime.date(2008, 1, 1) | |
delta = d0 - d1 | |
#st.write(delta) | |
Begindatestring = start_date | |
val = 39448 + int(delta.days) | |
url = 'https://economictimes.indiatimes.com/archivelist/year-'+str(Begindatestring.year)+',month-'+str(Begindatestring.month)+',starttime-'+str(val)+'.cms' # Replace with your URL | |
response = requests.get(url) | |
if response.status_code == 200: | |
html_text = response.text | |
soup = BeautifulSoup(html_text, "lxml") | |
else: | |
gg=0 | |
jobs = soup.find_all("li") | |
headlines = [] | |
for job in jobs: | |
try: | |
target_element = job.find("a") | |
target_element.text | |
headlines.append(target_element.text) | |
except: | |
continue | |
return headlines | |
# App title | |
st.markdown(''' | |
# Sovrenn Market Sentiment Indicator App | |
Shown are the stock price data for the selected company! | |
**Credits** | |
- App built by SRL | |
''') | |
st.write('---') | |
# Sidebar | |
st.sidebar.subheader('Query parameters') | |
start_date = st.sidebar.date_input("Start date", datetime.date(2023,9, 20)) | |
#start_date = start_date - datetime.timedelta(days=1) | |
end_date = start_date + datetime.timedelta(days=14) | |
# User input for the stock ticker symbol | |
tickerSymbol = st.sidebar.text_input('Enter Stock Ticker Symbol') | |
if tickerSymbol: | |
try: | |
tickerData = yf.Ticker(tickerSymbol) # Get ticker data | |
tickerDf = tickerData.history(period='1d', start=start_date, end=end_date) # Get the historical prices for this ticker | |
string_name = tickerData.info.get('longName', 'Company Name Not Available') | |
st.header('**%s**' % string_name) | |
# Try to get the business summary, handle KeyError if not available | |
try: | |
string_summary = tickerData.info['longBusinessSummary'] | |
st.info(string_summary) | |
except KeyError: | |
st.warning("Business summary not available for this company.") | |
# Ticker data | |
st.header('**Ticker data**') | |
st.write(tickerDf) | |
# Create a candlestick chart and volume bar chart | |
fig_candlestick = go.Figure(data=[go.Candlestick(x=tickerDf.index, | |
open=tickerDf['Open'], | |
high=tickerDf['High'], | |
low=tickerDf['Low'], | |
close=tickerDf['Close'])]) | |
fig_volume = go.Figure(data=[go.Bar(x=tickerDf.index, y=tickerDf['Volume'])]) | |
st.header('**Candlestick Chart**') | |
st.plotly_chart(fig_candlestick) | |
st.header('**Volume Bar Chart**') | |
st.plotly_chart(fig_volume) | |
st.write(start_date) | |
tickerDf = pd.DataFrame(tickerDf).reset_index() | |
# st.write(tickerDf) | |
#date = datetime.date(start_date) | |
date_str = start_date.strftime("%Y-%m-%d") | |
#st.write(date_str) | |
df = tickerDf[tickerDf["Date"]==date_str] | |
#st.write(df) | |
if (df["Close"][0] > df["Open"][0] ): | |
st.write("NSE has uptrend on " +date_str ) | |
if (df["Close"][0] < df["Open"][0] ): | |
st.write(" NSE has downdtrend on " +date_str ) | |
except: | |
st.write("Stock Data of this Index on the selected Date is not available \n") | |
Begindatestring = start_date | |
# Create a dummy stream to suppress output | |
dummy_stream = StringIO() | |
with redirect_stdout(dummy_stream): | |
headlines = scrapper(start_date) | |
index = [idx for idx, s in enumerate(headlines) if s=='Most Read' ][0] | |
del headlines[index:] | |
news = pd.DataFrame({"News": headlines}) | |
news.insert(0, 'Date', Begindatestring) | |
news = news.drop_duplicates() | |
news = news.dropna(how='any') | |
news = news.reset_index(drop=True) | |
tokenizer = AutoTokenizer.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification") | |
model = AutoModelForSequenceClassification.from_pretrained("nickmuchi/sec-bert-finetuned-finance-classification") | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
nlp = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device) | |
length = len(news[ 'News'].to_list()) | |
news_list = news[ 'News'].to_list() | |
df = pd.DataFrame() | |
for i in range (0, length): | |
results = nlp(news_list[i]) | |
df.loc[i, "News"] = news_list[i] | |
df.loc[i , 'label'] = results[0]["label"] | |
df.loc[i , 'score'] = results[0]["score"] | |
if(i%100 ==0): st.write("Articles Processed Number "+ str(i)) | |
bullish_rows = df[df['label'] == 'bullish'] | |
bullish_score_sum = bullish_rows['score'].sum() | |
num_bullish_rows = len(bullish_rows) | |
average_score_for_bullish = bullish_score_sum / num_bullish_rows | |
bearish_rows = df[df['label'] == 'bearish'] | |
bearish_score_sum = bearish_rows['score'].sum() | |
num_bearish_rows = len(bearish_rows) | |
average_score_for_bearish = bearish_score_sum / num_bearish_rows | |
if(average_score_for_bearish > average_score_for_bullish): | |
st.write("Stock will go down") | |
if(average_score_for_bearish < average_score_for_bullish): | |
st.write("Stock will go up") | |
else: | |
st.warning("Please enter a valid Stock Ticker Symbol.") |