Update func.py
Browse files
func.py
CHANGED
@@ -1,26 +1,29 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import requests
|
3 |
-
from bs4 import BeautifulSoup
|
4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
model_id = "LinkLinkWu/Stock_Analysis_Test_Ahamed"
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
11 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
12 |
-
return pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
def fetch_news(ticker):
|
25 |
try:
|
26 |
url = f"https://finviz.com/quote.ashx?t={ticker}"
|
@@ -33,18 +36,15 @@ def fetch_news(ticker):
|
|
33 |
}
|
34 |
response = requests.get(url, headers=headers)
|
35 |
if response.status_code != 200:
|
36 |
-
st.error(f"Failed to fetch page for {ticker}: Status code {response.status_code}")
|
37 |
return []
|
38 |
|
39 |
soup = BeautifulSoup(response.text, 'html.parser')
|
40 |
title = soup.title.text if soup.title else ""
|
41 |
if ticker not in title:
|
42 |
-
st.error(f"Page for {ticker} not found or access denied.")
|
43 |
return []
|
44 |
|
45 |
news_table = soup.find(id='news-table')
|
46 |
if news_table is None:
|
47 |
-
st.error(f"News table not found for {ticker}. The website structure might have changed.")
|
48 |
return []
|
49 |
|
50 |
news = []
|
@@ -55,19 +55,17 @@ def fetch_news(ticker):
|
|
55 |
link = a_tag['href']
|
56 |
news.append({'title': title, 'link': link})
|
57 |
return news
|
58 |
-
except Exception
|
59 |
-
st.error(f"Failed to fetch news for {ticker}: {e}")
|
60 |
return []
|
61 |
|
62 |
-
def analyze_sentiment(text):
|
63 |
try:
|
64 |
result = sentiment_pipeline(text)[0]
|
65 |
return "Positive" if result['label'] == 'POSITIVE' else "Negative"
|
66 |
-
except Exception
|
67 |
-
st.error(f"Sentiment analysis failed: {e}")
|
68 |
return "Unknown"
|
69 |
|
70 |
-
def extract_org_entities(text):
|
71 |
try:
|
72 |
entities = ner_pipeline(text)
|
73 |
org_entities = []
|
@@ -79,6 +77,5 @@ def extract_org_entities(text):
|
|
79 |
if len(org_entities) >= 5:
|
80 |
break
|
81 |
return org_entities
|
82 |
-
except Exception
|
83 |
-
st.error(f"NER entity extraction failed: {e}")
|
84 |
return []
|
|
|
|
|
|
|
|
|
1 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
|
5 |
+
# ----------- Lazy Initialization of Pipelines -----------
|
6 |
+
_sentiment_pipeline = None
|
7 |
+
_ner_pipeline = None
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
def get_sentiment_pipeline():
|
10 |
+
global _sentiment_pipeline
|
11 |
+
if _sentiment_pipeline is None:
|
12 |
+
model_id = "LinkLinkWu/Stock_Analysis_Test_Ahamed"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
14 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
15 |
+
_sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
16 |
+
return _sentiment_pipeline
|
17 |
|
18 |
+
def get_ner_pipeline():
|
19 |
+
global _ner_pipeline
|
20 |
+
if _ner_pipeline is None:
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
|
22 |
+
model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
|
23 |
+
_ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, grouped_entities=True)
|
24 |
+
return _ner_pipeline
|
25 |
|
26 |
+
# ----------- Core Functions -----------
|
27 |
def fetch_news(ticker):
|
28 |
try:
|
29 |
url = f"https://finviz.com/quote.ashx?t={ticker}"
|
|
|
36 |
}
|
37 |
response = requests.get(url, headers=headers)
|
38 |
if response.status_code != 200:
|
|
|
39 |
return []
|
40 |
|
41 |
soup = BeautifulSoup(response.text, 'html.parser')
|
42 |
title = soup.title.text if soup.title else ""
|
43 |
if ticker not in title:
|
|
|
44 |
return []
|
45 |
|
46 |
news_table = soup.find(id='news-table')
|
47 |
if news_table is None:
|
|
|
48 |
return []
|
49 |
|
50 |
news = []
|
|
|
55 |
link = a_tag['href']
|
56 |
news.append({'title': title, 'link': link})
|
57 |
return news
|
58 |
+
except Exception:
|
|
|
59 |
return []
|
60 |
|
61 |
+
def analyze_sentiment(text, sentiment_pipeline):
|
62 |
try:
|
63 |
result = sentiment_pipeline(text)[0]
|
64 |
return "Positive" if result['label'] == 'POSITIVE' else "Negative"
|
65 |
+
except Exception:
|
|
|
66 |
return "Unknown"
|
67 |
|
68 |
+
def extract_org_entities(text, ner_pipeline):
|
69 |
try:
|
70 |
entities = ner_pipeline(text)
|
71 |
org_entities = []
|
|
|
77 |
if len(org_entities) >= 5:
|
78 |
break
|
79 |
return org_entities
|
80 |
+
except Exception:
|
|
|
81 |
return []
|