-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (61 loc) · 3.05 KB
/
Copy pathapp.py
File metadata and controls
71 lines (61 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from functions import url_to_sentiment_analysis, preprocess_text, ticker_sentiment_analysis
# Load model and tokenizer once
@st.cache_resource
def load_model():
model_path = "./finbert_individual2_sentiment_model"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
return model, tokenizer
model, tokenizer = load_model()
# Streamlit UI
st.title("StockPulse Sentiment Analysis")
st.markdown("Analyze text or financial news URLs for **positive**, **neutral**, or **negative** sentiment.")
# Sidebar mode
mode = st.sidebar.radio("Choose mode", ["URL Analysis", "Text Input"])
if mode == "URL Analysis":
st.subheader("URL Sentiment Analysis")
url = st.text_input("Enter a financial news article URL:")
if st.button("Analyze URL"):
if url.strip():
try:
results = url_to_sentiment_analysis(url, model, tokenizer)
if results:
st.markdown(f"### Sentiment Results from Article at:\n[{url}]({url})")
for i, res in enumerate(results, 1):
st.markdown(f"**{i}. Ticker:** `{res.get('ticker', 'N/A')}`")
st.markdown(f" **Sentiment:** {res.get('sentiment', 'N/A')}")
st.markdown(f" **Confidence:** {res.get('confidence', 0):.2%}")
else:
st.warning("No sentiment results found. Check if the article is accessible and contains ticker mentions.")
except Exception as e:
st.error(f"Error analyzing URL: {str(e)}")
else:
st.warning("Please enter a valid URL.")
elif mode == "Text Input":
st.subheader("Text Sentiment Prediction")
user_text = st.text_area("Enter your text here:", height=150)
if st.button("Analyze"):
if user_text.strip():
cleaned_text = preprocess_text(user_text)
inputs = tokenizer(
cleaned_text,
return_tensors="pt",
max_length=512,
truncation=True,
padding=True
)
results = ticker_sentiment_analysis(cleaned_text, model, tokenizer)
if results:
st.markdown(f"### Sentiment Results:")
for i, res in enumerate(results, 1):
st.markdown(f"**{i}. Ticker:** `{res.get('ticker', 'N/A')}`")
st.markdown(f" **Sentiment:** {res.get('sentiment', 'N/A')}")
st.markdown(f" **Confidence:** {res.get('confidence', 0):.2%}")
else:
st.warning("No sentiment results found. Check if the article is accessible and contains ticker mentions.")
else:
st.warning("Please enter some text.")
st.markdown("---")
st.caption("📊 Powered by FinBERT - Fine-tuned for sentiment classification")