Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,68 @@ import matplotlib.pyplot as plt
|
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
url_stocks = "https://financialmodelingprep.com/api/v3/stock/list?apikey="
|
| 10 |
url_sentiment = "https://yaakovy-fin-proj-docker.hf.space/ticker/"
|
| 11 |
url_timeGpt = "https://ofirmatzlawi-fin-proj-docker-1.hf.space/ticker/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def get_max_sentiment(row):
|
| 15 |
if row["sentiment_score"] > 0.05: # Threshold for positive sentiment
|
|
@@ -18,18 +76,7 @@ def get_max_sentiment(row):
|
|
| 18 |
return "negative"
|
| 19 |
else:
|
| 20 |
return "neutral"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#max_value = max(row['neg'], row['neu'], row['pos'])
|
| 27 |
-
#if max_value == row['neg']:
|
| 28 |
-
# return 'neg'
|
| 29 |
-
#elif max_value == row['neu']:
|
| 30 |
-
# return 'neu'
|
| 31 |
-
#else:
|
| 32 |
-
# return 'pos'
|
| 33 |
|
| 34 |
|
| 35 |
def get_sentiment_data(stock_info):
|
|
|
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
|
| 9 |
+
from urllib.request import urlopen, Request
|
| 10 |
+
from bs4 import BeautifulSoup
|
| 11 |
+
import pandas as pd
|
| 12 |
+
|
| 13 |
+
import nltk
|
| 14 |
+
nltk.downloader.download("vader_lexicon")
|
| 15 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
url_stocks = "https://financialmodelingprep.com/api/v3/stock/list?apikey="
|
| 21 |
url_sentiment = "https://yaakovy-fin-proj-docker.hf.space/ticker/"
|
| 22 |
url_timeGpt = "https://ofirmatzlawi-fin-proj-docker-1.hf.space/ticker/"
|
| 23 |
+
finviz_url = "https://finviz.com/quote.ashx?t="
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_news(ticker):
|
| 27 |
+
url = finviz_url + ticker
|
| 28 |
+
req = Request(
|
| 29 |
+
url=url,
|
| 30 |
+
headers={
|
| 31 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"
|
| 32 |
+
},
|
| 33 |
+
)
|
| 34 |
+
response = urlopen(req)
|
| 35 |
+
if response.status != 200: # Check the response status code
|
| 36 |
+
raise Exception("Failed to fetch news table")
|
| 37 |
+
html = BeautifulSoup(response) # Read the contents of the file into 'html'
|
| 38 |
+
news_table = html.find(id="news-table") # Find 'news-table' in the Soup and load it into 'news_table'
|
| 39 |
+
return news_table
|
| 40 |
+
|
| 41 |
|
| 42 |
+
def parse_news(news_table):
|
| 43 |
+
parsed_news = []
|
| 44 |
+
today_string = datetime.datetime.today().strftime("%Y-%m-%d")
|
| 45 |
+
|
| 46 |
+
for x in news_table.findAll("tr"):
|
| 47 |
+
try:
|
| 48 |
+
# read the text from each tr tag into text
|
| 49 |
+
text = x.a.get_text() # get text from a only
|
| 50 |
+
date_scrape = x.td.text.split() # splite text in the td tag into a list
|
| 51 |
+
if len(date_scrape) == 1: # if the length of 'date_scrape' is 1, load 'time' as the only element
|
| 52 |
+
time = date_scrape[0]
|
| 53 |
+
# else load 'date' as the 1st element and 'time' as the second
|
| 54 |
+
else:
|
| 55 |
+
date = date_scrape[0]
|
| 56 |
+
time = date_scrape[1]
|
| 57 |
+
parsed_news.append([date, time, text]) # Append ticker, date, time and headline as a list to the 'parsed_news' list
|
| 58 |
+
except:
|
| 59 |
+
pass
|
| 60 |
+
columns = ["date", "time", "headline"]
|
| 61 |
+
parsed_news_df = pd.DataFrame(parsed_news, columns=columns) # Convert the parsed_news list into a DataFrame called 'parsed_and_scored_news'
|
| 62 |
+
return parsed_news_df
|
| 63 |
+
|
| 64 |
+
def score_news(parsed_news_df):
|
| 65 |
+
vader = SentimentIntensityAnalyzer() # Instantiate the sentiment intensity analyzer
|
| 66 |
+
scores = parsed_news_df["headline"].apply(vader.polarity_scores).tolist() # Iterate through the headlines and get the polarity scores using vader
|
| 67 |
+
scores_df = pd.DataFrame(scores) # Convert the 'scores' list of dicts into a DataFrame
|
| 68 |
+
parsed_and_scored_news = parsed_news_df.join(scores_df, rsuffix="_right") # Join the DataFrames of the news and the list of dicts
|
| 69 |
+
parsed_and_scored_news = parsed_and_scored_news.rename(columns={"compound": "sentiment"})
|
| 70 |
+
return parsed_and_scored_news
|
| 71 |
|
| 72 |
def get_max_sentiment(row):
|
| 73 |
if row["sentiment_score"] > 0.05: # Threshold for positive sentiment
|
|
|
|
| 76 |
return "negative"
|
| 77 |
else:
|
| 78 |
return "neutral"
|
| 79 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
def get_sentiment_data(stock_info):
|