Spaces:
Sleeping
Sleeping
Upload app(1).py
Browse files
app(1).py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from newspaper import Article
|
4 |
+
|
5 |
+
# Model and tokenizer
|
6 |
+
model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Setting the page title
|
11 |
+
st.title("Financial Sentiment Analysis")
|
12 |
+
|
13 |
+
# Input option: Text or URL
|
14 |
+
input_option = st.radio("Choose input type:", ["Text Input", "URL Input"])
|
15 |
+
|
16 |
+
if input_option == "Text Input":
|
17 |
+
text_input = st.text_area("Enter Financial News:", "DEMO : Tesla stock is soaring after record-breaking earnings.")
|
18 |
+
else:
|
19 |
+
url_input = st.text_input("Enter URL to scrape headline:")
|
20 |
+
if url_input:
|
21 |
+
try:
|
22 |
+
# Scrape the headline from the URL
|
23 |
+
article = Article(url_input)
|
24 |
+
article.download()
|
25 |
+
article.parse()
|
26 |
+
text_input = article.title # Use the article's title as the headline
|
27 |
+
st.success(f"Scraped Headline: {text_input}")
|
28 |
+
except Exception as e:
|
29 |
+
st.error(f"Failed to extract headline: {e}")
|
30 |
+
text_input = ""
|
31 |
+
|
32 |
+
# Function to perform sentiment analysis
|
33 |
+
def predict_sentiment(text):
|
34 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
35 |
+
outputs = model(**inputs)
|
36 |
+
sentiment_class = outputs.logits.argmax(dim=1).item()
|
37 |
+
sentiment_mapping = {0: 'Negative', 1: 'Neutral', 2: 'Positive'}
|
38 |
+
predicted_sentiment = sentiment_mapping.get(sentiment_class, 'Unknown')
|
39 |
+
return predicted_sentiment, outputs.logits.softmax(dim=1)[0].tolist()
|
40 |
+
|
41 |
+
# Button to trigger sentiment analysis
|
42 |
+
if st.button("Analyze Sentiment"):
|
43 |
+
# Checking if the input text is not empty
|
44 |
+
if text_input and text_input.strip():
|
45 |
+
# Showing loading spinner while processing
|
46 |
+
with st.spinner("Analyzing sentiment..."):
|
47 |
+
sentiment, confidence_scores = predict_sentiment(text_input)
|
48 |
+
|
49 |
+
# Considering a threshold for sentiment prediction
|
50 |
+
threshold = 0.5
|
51 |
+
|
52 |
+
# Changing the success message background color based on sentiment and threshold
|
53 |
+
if sentiment == 'Positive' and confidence_scores[2] > threshold:
|
54 |
+
st.success(f"Sentiment: {sentiment} (Confidence: {confidence_scores[2]:.3f})")
|
55 |
+
elif sentiment == 'Negative' and confidence_scores[0] > threshold:
|
56 |
+
st.error(f"Sentiment: {sentiment} (Confidence: {confidence_scores[0]:.3f})")
|
57 |
+
elif sentiment == 'Neutral' and confidence_scores[1] > threshold:
|
58 |
+
st.info(f"Sentiment: {sentiment} (Confidence: {confidence_scores[1]:.3f})")
|
59 |
+
else:
|
60 |
+
st.warning("Low confidence, or sentiment not above threshold. Please try again.")
|
61 |
+
else:
|
62 |
+
st.warning("Please enter some valid text for sentiment analysis.")
|
63 |
+
|
64 |
+
# Optional: Displaying the raw sentiment scores
|
65 |
+
if st.checkbox("Show Raw Sentiment Scores"):
|
66 |
+
if text_input and text_input.strip():
|
67 |
+
_, raw_scores = predict_sentiment(text_input)
|
68 |
+
st.info(f"Raw Sentiment Scores: \n Negative : {raw_scores[0]} \n Positive : {raw_scores[2]} \n Neutral : {raw_scores[1]}")
|
69 |
+
|
70 |
+
# footer
|
71 |
+
st.markdown(
|
72 |
+
"""
|
73 |
+
** Built and maintained by Swayam Mohanty **
|
74 |
+
"""
|
75 |
+
)
|