khandelwalkishna15 commited on
Commit
4f76b9d
1 Parent(s): bfa5bb3

new changes

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -5,15 +5,15 @@ model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
5
  tokenizer = AutoTokenizer.from_pretrained(model_name)
6
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
 
8
- # Set the page title
9
- st.title("Financial Sentiment Analysis App")
10
 
11
- # Add a text input for the user to input financial news
12
  text_input = st.text_area("Enter Financial News:", "Tesla stock is soaring after record-breaking earnings.")
13
 
14
  # Function to perform sentiment analysis
15
  def predict_sentiment(text):
16
- inputs = tokenizer(text, return_tensors="pt")
17
  outputs = model(**inputs)
18
  sentiment_class = outputs.logits.argmax(dim=1).item()
19
  sentiment_mapping = {0: 'Negative', 1: 'Neutral', 2: 'Positive'}
@@ -23,28 +23,43 @@ def predict_sentiment(text):
23
  # Button to trigger sentiment analysis
24
  if st.button("Analyze Sentiment"):
25
  # Check if the input text is not empty
26
- if text_input:
27
- # Show loading spinner while processing
28
  with st.spinner("Analyzing sentiment..."):
29
  sentiment = predict_sentiment(text_input)
30
- # Change the view based on the predicted sentiment
31
- st.success(f"Sentiment: {sentiment}")
32
- if sentiment == 'Positive':
33
- st.balloons() # Celebratory animation for positive sentiment
34
- # Add additional views for other sentiments as needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  else:
36
- st.warning("Please enter some text for sentiment analysis.")
37
 
38
- # Optional: Display the raw sentiment scores
39
  if st.checkbox("Show Raw Sentiment Scores"):
40
- if text_input:
41
  inputs = tokenizer(text_input, return_tensors="pt")
42
  outputs = model(**inputs)
43
  raw_scores = outputs.logits[0].tolist()
44
  st.info(f"Raw Sentiment Scores: {raw_scores}")
45
 
46
- # Optional: Display additional information or analysis
47
- # Add more components as needed for your specific use case
48
-
49
- # Add a footer
50
- st.text("Built with Streamlit and Transformers")
 
 
5
  tokenizer = AutoTokenizer.from_pretrained(model_name)
6
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
 
8
+ # Seting the page title
9
+ st.title("Financial Sentiment Analysis")
10
 
11
+ # Adding a text input for the user to input financial news
12
  text_input = st.text_area("Enter Financial News:", "Tesla stock is soaring after record-breaking earnings.")
13
 
14
  # Function to perform sentiment analysis
15
  def predict_sentiment(text):
16
+ inputs = tokenizer(text, return_tensors="pt", max_length=1022, truncation=True)
17
  outputs = model(**inputs)
18
  sentiment_class = outputs.logits.argmax(dim=1).item()
19
  sentiment_mapping = {0: 'Negative', 1: 'Neutral', 2: 'Positive'}
 
23
  # Button to trigger sentiment analysis
24
  if st.button("Analyze Sentiment"):
25
  # Check if the input text is not empty
26
+ if text_input and text_input.strip(): # Check if input is not empty or contains only whitespaces
27
+ # Showing loading spinner while processing
28
  with st.spinner("Analyzing sentiment..."):
29
  sentiment = predict_sentiment(text_input)
30
+
31
+ # Extracting confidence scores
32
+ inputs = tokenizer(text_input, return_tensors="pt")
33
+ outputs = model(**inputs)
34
+ confidence_scores = outputs.logits.softmax(dim=1)[0].tolist()
35
+
36
+ # Considering a threshold for sentiment prediction
37
+ threshold = 0.5
38
+
39
+ # Change the success message background color based on sentiment and threshold
40
+ if sentiment == 'Positive' and confidence_scores[2] > threshold:
41
+ st.success(f"Sentiment: {sentiment} (Confidence: {confidence_scores[2]:.3f})")
42
+ elif sentiment == 'Negative' and confidence_scores[0] > threshold:
43
+ st.error(f"Sentiment: {sentiment} (Confidence: {confidence_scores[0]:.3f})")
44
+ elif sentiment == 'Neutral' and confidence_scores[1] > threshold:
45
+ st.info(f"Sentiment: {sentiment} (Confidence: {confidence_scores[1]:.3f})")
46
+ else:
47
+ st.warning("Low confidence, or sentiment not above threshold. Please try again.")
48
+
49
  else:
50
+ st.warning("Please enter some valid text for sentiment analysis.")
51
 
52
+ # Optional: Displaying the raw sentiment scores
53
  if st.checkbox("Show Raw Sentiment Scores"):
54
+ if text_input and text_input.strip():
55
  inputs = tokenizer(text_input, return_tensors="pt")
56
  outputs = model(**inputs)
57
  raw_scores = outputs.logits[0].tolist()
58
  st.info(f"Raw Sentiment Scores: {raw_scores}")
59
 
60
+ # footer
61
+ st.markdown(
62
+ """
63
+ **Built with [Streamlit](https://streamlit.io/) and [Transformers](https://huggingface.co/models).**
64
+ """
65
+ )